This commit is contained in:
lloydzhou 2024-08-29 00:21:26 +08:00
parent f5209fc344
commit 29b5cd9436
3 changed files with 29 additions and 14 deletions

View File

@ -9,7 +9,12 @@ import {
REQUEST_TIMEOUT_MS, REQUEST_TIMEOUT_MS,
ServiceProvider, ServiceProvider,
} from "@/app/constant"; } from "@/app/constant";
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store"; import {
ChatMessageTool,
useAccessStore,
useAppConfig,
useChatStore,
} from "@/app/store";
import { collectModelsWithDefaultModel } from "@/app/utils/model"; import { collectModelsWithDefaultModel } from "@/app/utils/model";
import { import {
preProcessImageContent, preProcessImageContent,
@ -251,7 +256,7 @@ export class ChatGPTApi implements LLMApi {
let remainText = ""; let remainText = "";
let finished = false; let finished = false;
let running = false; let running = false;
let runTools = []; let runTools: ChatMessageTool[] = [];
// animate response to make it looks smooth // animate response to make it looks smooth
function animateResponseText() { function animateResponseText() {
@ -280,7 +285,7 @@ export class ChatGPTApi implements LLMApi {
// TODO 后面这里是从选择的plugins中获取function列表 // TODO 后面这里是从选择的plugins中获取function列表
const funcs = { const funcs = {
get_current_weather: (args) => { get_current_weather: (args: any) => {
console.log("call get_current_weather", args); console.log("call get_current_weather", args);
return "30"; return "30";
}, },
@ -297,14 +302,16 @@ export class ChatGPTApi implements LLMApi {
runTools.splice(0, runTools.length); // empty runTools runTools.splice(0, runTools.length); // empty runTools
return Promise.all( return Promise.all(
toolCallMessage.tool_calls.map((tool) => { toolCallMessage.tool_calls.map((tool) => {
options?.onBeforeTool(tool); options?.onBeforeTool?.(tool);
return Promise.resolve( return Promise.resolve(
// @ts-ignore
funcs[tool.function.name]( funcs[tool.function.name](
// @ts-ignore
JSON.parse(tool.function.arguments), JSON.parse(tool.function.arguments),
), ),
) )
.then((content) => { .then((content) => {
options?.onAfterTool({ options?.onAfterTool?.({
...tool, ...tool,
content, content,
isError: false, isError: false,
@ -312,7 +319,7 @@ export class ChatGPTApi implements LLMApi {
return content; return content;
}) })
.catch((e) => { .catch((e) => {
options?.onAfterTool({ ...tool, isError: true }); options?.onAfterTool?.({ ...tool, isError: true });
return e.toString(); return e.toString();
}) })
.then((content) => ({ .then((content) => ({
@ -323,8 +330,10 @@ export class ChatGPTApi implements LLMApi {
}), }),
).then((toolCallResult) => { ).then((toolCallResult) => {
console.log("end runTools", toolCallMessage, toolCallResult); console.log("end runTools", toolCallMessage, toolCallResult);
requestPayload["messages"].splice( // @ts-ignore
requestPayload["messages"].length, requestPayload?.messages?.splice(
// @ts-ignore
requestPayload?.messages?.length,
0, 0,
toolCallMessage, toolCallMessage,
...toolCallResult, ...toolCallResult,
@ -333,7 +342,7 @@ export class ChatGPTApi implements LLMApi {
// call again // call again
console.log("start again"); console.log("start again");
running = false; running = false;
chatApi(chatPath, requestPayload); // call fetchEventSource chatApi(chatPath, requestPayload as RequestPayload); // call fetchEventSource
}, 0); }, 0);
}); });
console.log("try run tools", runTools.length, finished); console.log("try run tools", runTools.length, finished);
@ -349,7 +358,7 @@ export class ChatGPTApi implements LLMApi {
controller.signal.onabort = finish; controller.signal.onabort = finish;
function chatApi(chatPath, requestPayload) { function chatApi(chatPath: string, requestPayload: RequestPayload) {
const chatPayload = { const chatPayload = {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
@ -434,7 +443,10 @@ export class ChatGPTApi implements LLMApi {
try { try {
const json = JSON.parse(text); const json = JSON.parse(text);
const choices = json.choices as Array<{ const choices = json.choices as Array<{
delta: { content: string }; delta: {
content: string;
tool_calls: ChatMessageTool[];
};
}>; }>;
console.log("choices", choices); console.log("choices", choices);
const delta = choices[0]?.delta?.content; const delta = choices[0]?.delta?.content;
@ -453,11 +465,12 @@ export class ChatGPTApi implements LLMApi {
id, id,
type: tool_calls[0]?.type, type: tool_calls[0]?.type,
function: { function: {
name: tool_calls[0]?.function?.name, name: tool_calls[0]?.function?.name as string,
arguments: args, arguments: args,
}, },
}); });
} else { } else {
// @ts-ignore
runTools[index]["function"]["arguments"] += args; runTools[index]["function"]["arguments"] += args;
} }
} }
@ -490,7 +503,7 @@ export class ChatGPTApi implements LLMApi {
openWhenHidden: true, openWhenHidden: true,
}); });
} }
chatApi(chatPath, requestPayload); // call fetchEventSource chatApi(chatPath, requestPayload as RequestPayload); // call fetchEventSource
} else { } else {
const res = await fetch(chatPath, chatPayload); const res = await fetch(chatPath, chatPayload);
clearTimeout(requestTimeoutId); clearTimeout(requestTimeoutId);

View File

@ -1579,6 +1579,7 @@ function _Chat() {
{Locale.Chat.Typing} {Locale.Chat.Typing}
</div> </div>
)} )}
{/*@ts-ignore*/}
{message?.tools?.length > 0 && ( {message?.tools?.length > 0 && (
<div className={styles["chat-message-tools"]}> <div className={styles["chat-message-tools"]}>
{message?.tools?.map((tool) => ( {message?.tools?.map((tool) => (
@ -1593,7 +1594,7 @@ function _Chat() {
) : ( ) : (
<LoadingButtonIcon /> <LoadingButtonIcon />
)} )}
<span>{tool.function.name}</span> <span>{tool?.function?.name}</span>
</div> </div>
))} ))}
</div> </div>

View File

@ -30,6 +30,7 @@ import { isDalle3 } from "../utils";
export type ChatMessageTool = { export type ChatMessageTool = {
id: string; id: string;
index?: number;
type?: string; type?: string;
function?: { function?: {
name: string; name: string;