mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-29 09:44:47 +00:00
feat: http request
This commit is contained in:
@@ -66,7 +66,7 @@ export async function dispatchContentExtract({
|
||||
// function body
|
||||
const agentFunction = {
|
||||
name: agentFunName,
|
||||
description: `${description}\n如果内容不存在,返回空字符串。当前时间是2023/7/31 18:00`,
|
||||
description: `${description}\n如果内容不存在,返回空字符串。`,
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties,
|
||||
|
@@ -3,4 +3,6 @@ export * from './init/userChatInput';
|
||||
export * from './chat/oneapi';
|
||||
export * from './kb/search';
|
||||
export * from './tools/answer';
|
||||
export * from './tools/http';
|
||||
export * from './agent/classifyQuestion';
|
||||
export * from './agent/extract';
|
||||
|
51
client/src/service/moduleDispatch/tools/http.ts
Normal file
51
client/src/service/moduleDispatch/tools/http.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { HttpPropsEnum } from '@/constants/flow/flowField';
|
||||
import type { NextApiResponse } from 'next';
|
||||
|
||||
export type HttpRequestProps = {
|
||||
res: NextApiResponse;
|
||||
stream: boolean;
|
||||
userOpenaiAccount: any;
|
||||
[HttpPropsEnum.url]: string;
|
||||
[key: string]: any;
|
||||
};
|
||||
export type HttpResponse = {
|
||||
[HttpPropsEnum.finish]: boolean;
|
||||
[HttpPropsEnum.failed]?: boolean;
|
||||
[key: string]: any;
|
||||
};
|
||||
|
||||
export const dispatchHttpRequest = async (props: Record<string, any>): Promise<HttpResponse> => {
|
||||
const { res, stream, userOpenaiAccount, url, ...body } = props as HttpRequestProps;
|
||||
|
||||
try {
|
||||
const response = await fetchData({ url, body });
|
||||
|
||||
return {
|
||||
[HttpPropsEnum.finish]: true,
|
||||
...response
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
[HttpPropsEnum.finish]: true,
|
||||
[HttpPropsEnum.failed]: true
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
async function fetchData({
|
||||
url,
|
||||
body
|
||||
}: {
|
||||
url: string;
|
||||
body: Record<string, any>;
|
||||
}): Promise<Record<string, any>> {
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(body)
|
||||
}).then((res) => res.json());
|
||||
|
||||
return response;
|
||||
}
|
@@ -1,31 +0,0 @@
|
||||
import { sseResponseEventEnum, TaskResponseKeyEnum } from '@/constants/chat';
|
||||
import { sseResponse } from '@/service/utils/tools';
|
||||
import { textAdaptGptResponse } from '@/utils/adapt';
|
||||
import type { NextApiResponse } from 'next';
|
||||
|
||||
export type AnswerProps = {
|
||||
res: NextApiResponse;
|
||||
text: string;
|
||||
stream: boolean;
|
||||
};
|
||||
export type AnswerResponse = {
|
||||
[TaskResponseKeyEnum.answerText]: string;
|
||||
};
|
||||
|
||||
export const dispatchAnswer = (props: Record<string, any>): AnswerResponse => {
|
||||
const { res, text = '', stream } = props as AnswerProps;
|
||||
|
||||
if (stream) {
|
||||
sseResponse({
|
||||
res,
|
||||
event: sseResponseEventEnum.answer,
|
||||
data: textAdaptGptResponse({
|
||||
text: text.replace(/\\n/g, '\n')
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
[TaskResponseKeyEnum.answerText]: text
|
||||
};
|
||||
};
|
Reference in New Issue
Block a user