feat: http request

This commit is contained in:
archer
2023-08-03 15:43:06 +08:00
parent 952da2a06e
commit ffd4e194bf
13 changed files with 138 additions and 139 deletions

View File

@@ -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,

View File

@@ -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';

View 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;
}

View File

@@ -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
};
};