Commit 3c8f3879 by archer

perf: ts type

parent 91b02bbf
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
"axios": "^1.3.3", "axios": "^1.3.3",
"crypto": "^1.0.1", "crypto": "^1.0.1",
"dayjs": "^1.11.7", "dayjs": "^1.11.7",
"delay": "^5.0.0",
"eventsource-parser": "^0.1.0", "eventsource-parser": "^0.1.0",
"formidable": "^2.1.1", "formidable": "^2.1.1",
"framer-motion": "^9.0.6", "framer-motion": "^9.0.6",
......
...@@ -6,20 +6,39 @@ import { OpenAiChatEnum } from '@/constants/model'; ...@@ -6,20 +6,39 @@ import { OpenAiChatEnum } from '@/constants/model';
import { chatResponse, openAiStreamResponse } from './openai'; import { chatResponse, openAiStreamResponse } from './openai';
import type { NextApiResponse } from 'next'; import type { NextApiResponse } from 'next';
import type { PassThrough } from 'stream'; import type { PassThrough } from 'stream';
import delay from 'delay';
export type ChatCompletionType = { export type ChatCompletionType = {
apiKey: string; apiKey: string;
temperature: number; temperature: number;
messages: ChatItemSimpleType[]; messages: ChatItemSimpleType[];
stream: boolean; stream: boolean;
params?: any;
};
export type ChatCompletionResponseType = {
streamResponse: any;
responseMessages: ChatItemSimpleType[];
responseText: string;
totalTokens: number;
}; };
export type StreamResponseType = { export type StreamResponseType = {
stream: PassThrough; stream: PassThrough;
chatResponse: any; chatResponse: any;
prompts: ChatItemSimpleType[]; prompts: ChatItemSimpleType[];
}; };
export type StreamResponseReturnType = {
responseContent: string;
totalTokens: number;
finishMessages: ChatItemSimpleType[];
};
export const modelServiceToolMap = { export const modelServiceToolMap: Record<
ChatModelType,
{
chatCompletion: (data: ChatCompletionType) => Promise<ChatCompletionResponseType>;
streamResponse: (data: StreamResponseType) => Promise<StreamResponseReturnType>;
}
> = {
[OpenAiChatEnum.GPT35]: { [OpenAiChatEnum.GPT35]: {
chatCompletion: (data: ChatCompletionType) => chatCompletion: (data: ChatCompletionType) =>
chatResponse({ model: OpenAiChatEnum.GPT35, ...data }), chatResponse({ model: OpenAiChatEnum.GPT35, ...data }),
...@@ -142,16 +161,16 @@ export const resStreamResponse = async ({ ...@@ -142,16 +161,16 @@ export const resStreamResponse = async ({
prompts prompts
}); });
setTimeout(() => { await delay(100);
// push system prompt
!stream.destroyed && // push system prompt
systemPrompt && !stream.destroyed &&
stream.push(`${SYSTEM_PROMPT_PREFIX}${systemPrompt.replace(/\n/g, '<br/>')}`); systemPrompt &&
stream.push(`${SYSTEM_PROMPT_PREFIX}${systemPrompt.replace(/\n/g, '<br/>')}`);
// close stream // close stream
!stream.destroyed && stream.push(null); !stream.destroyed && stream.push(null);
stream.destroy(); stream.destroy();
}, 100);
return { responseContent, totalTokens, finishMessages }; return { responseContent, totalTokens, finishMessages };
}; };
...@@ -2,10 +2,17 @@ import { OpenAiChatEnum } from '@/constants/model'; ...@@ -2,10 +2,17 @@ import { OpenAiChatEnum } from '@/constants/model';
import type { ChatModelType } from '@/constants/model'; import type { ChatModelType } from '@/constants/model';
import type { ChatItemSimpleType } from '@/types/chat'; import type { ChatItemSimpleType } from '@/types/chat';
import { countOpenAIToken, getOpenAiEncMap, adaptChatItem_openAI } from './openai'; import { countOpenAIToken, getOpenAiEncMap, adaptChatItem_openAI } from './openai';
import { ChatCompletionRequestMessage } from 'openai';
export type CountTokenType = { messages: ChatItemSimpleType[] }; export type CountTokenType = { messages: ChatItemSimpleType[] };
export const modelToolMap = { export const modelToolMap: Record<
ChatModelType,
{
countTokens: (data: CountTokenType) => number;
adaptChatMessages: (data: CountTokenType) => ChatCompletionRequestMessage[];
}
> = {
[OpenAiChatEnum.GPT35]: { [OpenAiChatEnum.GPT35]: {
countTokens: ({ messages }: CountTokenType) => countTokens: ({ messages }: CountTokenType) =>
countOpenAIToken({ model: OpenAiChatEnum.GPT35, messages }), countOpenAIToken({ model: OpenAiChatEnum.GPT35, messages }),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment