fix tool-call streaming mode
This commit is contained in:
@@ -15,6 +15,7 @@ import ChatGPT, {
|
|||||||
FetchResponse,
|
FetchResponse,
|
||||||
Message as MessageType,
|
Message as MessageType,
|
||||||
MessageDetail,
|
MessageDetail,
|
||||||
|
ToolCall,
|
||||||
} from "./chatgpt";
|
} from "./chatgpt";
|
||||||
import Message from "./message";
|
import Message from "./message";
|
||||||
import models from "./models";
|
import models from "./models";
|
||||||
@@ -71,12 +72,48 @@ export default function ChatBOX(props: {
|
|||||||
let responseTokenCount = 0;
|
let responseTokenCount = 0;
|
||||||
chatStore.streamMode = true;
|
chatStore.streamMode = true;
|
||||||
const allChunkMessage: string[] = [];
|
const allChunkMessage: string[] = [];
|
||||||
|
const allChunkTool: ToolCall[] = [];
|
||||||
setShowGenerating(true);
|
setShowGenerating(true);
|
||||||
for await (const i of client.processStreamResponse(response)) {
|
for await (const i of client.processStreamResponse(response)) {
|
||||||
chatStore.responseModelName = i.model;
|
chatStore.responseModelName = i.model;
|
||||||
responseTokenCount += 1;
|
responseTokenCount += 1;
|
||||||
allChunkMessage.push(i.choices[0].delta.content ?? "");
|
allChunkMessage.push(i.choices[0].delta.content ?? "");
|
||||||
setGeneratingMessage(allChunkMessage.join(""));
|
const tool_calls = i.choices[0].delta.tool_calls;
|
||||||
|
if (tool_calls) {
|
||||||
|
for (const tool_call of tool_calls) {
|
||||||
|
// init
|
||||||
|
if (tool_call.id) {
|
||||||
|
allChunkTool.push({
|
||||||
|
id: tool_call.id,
|
||||||
|
type: tool_call.type,
|
||||||
|
index: tool_call.index,
|
||||||
|
function: {
|
||||||
|
name: tool_call.function.name,
|
||||||
|
arguments: "",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// update tool call arguments
|
||||||
|
const tool = allChunkTool.find(
|
||||||
|
(tool) => tool.index === tool_call.index
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!tool) {
|
||||||
|
console.log("tool (by index) not found", tool_call.index);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
tool.function.arguments += tool_call.function.arguments;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setGeneratingMessage(
|
||||||
|
allChunkMessage.join("") +
|
||||||
|
allChunkTool.map((tool) => {
|
||||||
|
return `Tool Call ID: ${tool.id}\nType: ${tool.type}\nFunction: ${tool.function.name}\nArguments: ${tool.function.arguments}`;
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
setShowGenerating(false);
|
setShowGenerating(false);
|
||||||
const content = allChunkMessage.join("");
|
const content = allChunkMessage.join("");
|
||||||
@@ -103,6 +140,7 @@ export default function ChatBOX(props: {
|
|||||||
chatStore.history.push({
|
chatStore.history.push({
|
||||||
role: "assistant",
|
role: "assistant",
|
||||||
content,
|
content,
|
||||||
|
tool_calls: allChunkTool,
|
||||||
hide: false,
|
hide: false,
|
||||||
token: responseTokenCount,
|
token: responseTokenCount,
|
||||||
example: false,
|
example: false,
|
||||||
|
|||||||
@@ -8,17 +8,43 @@ export interface MessageDetail {
|
|||||||
text?: string;
|
text?: string;
|
||||||
image_url?: ImageURL;
|
image_url?: ImageURL;
|
||||||
}
|
}
|
||||||
|
export interface ToolCall {
|
||||||
|
index: number;
|
||||||
|
id?: string;
|
||||||
|
type: string;
|
||||||
|
function: {
|
||||||
|
name: string;
|
||||||
|
arguments: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
export interface Message {
|
export interface Message {
|
||||||
role: "system" | "user" | "assistant" | "tool";
|
role: "system" | "user" | "assistant" | "tool";
|
||||||
content: string | MessageDetail[];
|
content: string | MessageDetail[];
|
||||||
name?: "example_user" | "example_assistant";
|
name?: "example_user" | "example_assistant";
|
||||||
tool_calls?: {
|
tool_calls?: ToolCall[];
|
||||||
id: string;
|
|
||||||
type: string;
|
|
||||||
function: any;
|
|
||||||
}[];
|
|
||||||
tool_call_id?: string;
|
tool_call_id?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Delta {
|
||||||
|
role?: string;
|
||||||
|
content?: string;
|
||||||
|
tool_calls?: ToolCall[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Choices {
|
||||||
|
index: number;
|
||||||
|
delta: Delta;
|
||||||
|
finish_reason: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface StreamingResponseChunk {
|
||||||
|
id: string;
|
||||||
|
object: string;
|
||||||
|
created: number;
|
||||||
|
model: string;
|
||||||
|
system_fingerprint: string;
|
||||||
|
choices: Choices[];
|
||||||
|
}
|
||||||
export const getMessageText = (message: Message): string => {
|
export const getMessageText = (message: Message): string => {
|
||||||
if (typeof message.content === "string") {
|
if (typeof message.content === "string") {
|
||||||
return message.content;
|
return message.content;
|
||||||
@@ -252,7 +278,7 @@ class Chat {
|
|||||||
console.log("line", line);
|
console.log("line", line);
|
||||||
try {
|
try {
|
||||||
const jsonStr = line.slice("data:".length).trim();
|
const jsonStr = line.slice("data:".length).trim();
|
||||||
const json = JSON.parse(jsonStr);
|
const json = JSON.parse(jsonStr) as StreamingResponseChunk;
|
||||||
yield json;
|
yield json;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(`Chunk parse error at: ${line}`);
|
console.log(`Chunk parse error at: ${line}`);
|
||||||
|
|||||||
@@ -299,7 +299,8 @@ export default function Message(props: Props) {
|
|||||||
<div className="bg-blue-300 dark:bg-blue-800 p-1 rounded">
|
<div className="bg-blue-300 dark:bg-blue-800 p-1 rounded">
|
||||||
<strong>Tool Call ID: {tool_call?.id}</strong>
|
<strong>Tool Call ID: {tool_call?.id}</strong>
|
||||||
<p>Type: {tool_call?.type}</p>
|
<p>Type: {tool_call?.type}</p>
|
||||||
<p>Function: {JSON.stringify(tool_call?.function)}</p>
|
<p>Function: {tool_call.function.name}</p>
|
||||||
|
<p>Arguments: {tool_call.function.arguments}</p>
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
))}
|
))}
|
||||||
|
|||||||
Reference in New Issue
Block a user