support alibaba and bytedance's reasoning_content

This commit is contained in:
suruiqiang 2025-02-11 12:46:46 +08:00
parent a029b4330b
commit 98a11e56d2
2 changed files with 200 additions and 225 deletions

View File

@ -5,8 +5,14 @@ import {
ALIBABA_BASE_URL, ALIBABA_BASE_URL,
REQUEST_TIMEOUT_MS, REQUEST_TIMEOUT_MS,
} from "@/app/constant"; } from "@/app/constant";
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store"; import {
useAccessStore,
useAppConfig,
useChatStore,
ChatMessageTool,
usePluginStore,
} from "@/app/store";
import { streamWithThink } from "@/app/utils/chat";
import { import {
ChatOptions, ChatOptions,
getHeaders, getHeaders,
@ -15,14 +21,11 @@ import {
SpeechOptions, SpeechOptions,
MultimodalContent, MultimodalContent,
} from "../api"; } from "../api";
import Locale from "../../locales";
import {
EventStreamContentType,
fetchEventSource,
} from "@fortaine/fetch-event-source";
import { prettyObject } from "@/app/utils/format";
import { getClientConfig } from "@/app/config/client"; import { getClientConfig } from "@/app/config/client";
import { getMessageTextContent } from "@/app/utils"; import {
getMessageTextContent,
getMessageTextContentWithoutThinking,
} from "@/app/utils";
import { fetch } from "@/app/utils/stream"; import { fetch } from "@/app/utils/stream";
export interface OpenAIListModelResponse { export interface OpenAIListModelResponse {
@ -92,7 +95,10 @@ export class QwenApi implements LLMApi {
async chat(options: ChatOptions) { async chat(options: ChatOptions) {
const messages = options.messages.map((v) => ({ const messages = options.messages.map((v) => ({
role: v.role, role: v.role,
content: getMessageTextContent(v), content:
v.role === "assistant"
? getMessageTextContentWithoutThinking(v)
: getMessageTextContent(v),
})); }));
const modelConfig = { const modelConfig = {
@ -122,15 +128,17 @@ export class QwenApi implements LLMApi {
options.onController?.(controller); options.onController?.(controller);
try { try {
const headers = {
...getHeaders(),
"X-DashScope-SSE": shouldStream ? "enable" : "disable",
};
const chatPath = this.path(Alibaba.ChatPath); const chatPath = this.path(Alibaba.ChatPath);
const chatPayload = { const chatPayload = {
method: "POST", method: "POST",
body: JSON.stringify(requestPayload), body: JSON.stringify(requestPayload),
signal: controller.signal, signal: controller.signal,
headers: { headers: headers,
...getHeaders(),
"X-DashScope-SSE": shouldStream ? "enable" : "disable",
},
}; };
// make a fetch request // make a fetch request
@ -140,116 +148,96 @@ export class QwenApi implements LLMApi {
); );
if (shouldStream) { if (shouldStream) {
let responseText = ""; const [tools, funcs] = usePluginStore
let remainText = ""; .getState()
let finished = false; .getAsTools(
let responseRes: Response; useChatStore.getState().currentSession().mask?.plugin || [],
);
// animate response to make it looks smooth return streamWithThink(
function animateResponseText() { chatPath,
if (finished || controller.signal.aborted) { requestPayload,
responseText += remainText; headers,
console.log("[Response Animation] finished"); tools as any,
if (responseText?.length === 0) { funcs,
options.onError?.(new Error("empty response from server")); controller,
} // parseSSE
return; (text: string, runTools: ChatMessageTool[]) => {
} // console.log("parseSSE", text, runTools);
const json = JSON.parse(text);
if (remainText.length > 0) { const choices = json.output.choices as Array<{
const fetchCount = Math.max(1, Math.round(remainText.length / 60)); message: {
const fetchText = remainText.slice(0, fetchCount); content: string | null;
responseText += fetchText; tool_calls: ChatMessageTool[];
remainText = remainText.slice(fetchCount); reasoning_content: string | null;
options.onUpdate?.(responseText, fetchText); };
} }>;
const tool_calls = choices[0]?.message?.tool_calls;
requestAnimationFrame(animateResponseText); if (tool_calls?.length > 0) {
} const index = tool_calls[0]?.index;
const id = tool_calls[0]?.id;
// start animaion const args = tool_calls[0]?.function?.arguments;
animateResponseText(); if (id) {
runTools.push({
const finish = () => { id,
if (!finished) { type: tool_calls[0]?.type,
finished = true; function: {
options.onFinish(responseText + remainText, responseRes); name: tool_calls[0]?.function?.name as string,
} arguments: args,
}; },
});
controller.signal.onabort = finish; } else {
// @ts-ignore
fetchEventSource(chatPath, { runTools[index]["function"]["arguments"] += args;
fetch: fetch as any, }
...chatPayload,
async onopen(res) {
clearTimeout(requestTimeoutId);
const contentType = res.headers.get("content-type");
console.log(
"[Alibaba] request response content type: ",
contentType,
);
responseRes = res;
if (contentType?.startsWith("text/plain")) {
responseText = await res.clone().text();
return finish();
} }
const reasoning = choices[0]?.message?.reasoning_content;
const content = choices[0]?.message?.content;
// Skip if both content and reasoning_content are empty or null
if ( if (
!res.ok || (!reasoning || reasoning.trim().length === 0) &&
!res.headers (!content || content.trim().length === 0)
.get("content-type")
?.startsWith(EventStreamContentType) ||
res.status !== 200
) { ) {
const responseTexts = [responseText]; return {
let extraInfo = await res.clone().text(); isThinking: false,
try { content: "",
const resJson = await res.clone().json(); };
extraInfo = prettyObject(resJson);
} catch {}
if (res.status === 401) {
responseTexts.push(Locale.Error.Unauthorized);
}
if (extraInfo) {
responseTexts.push(extraInfo);
}
responseText = responseTexts.join("\n\n");
return finish();
} }
},
onmessage(msg) { if (reasoning && reasoning.trim().length > 0) {
if (msg.data === "[DONE]" || finished) { return {
return finish(); isThinking: true,
} content: reasoning,
const text = msg.data; };
try { } else if (content && content.trim().length > 0) {
const json = JSON.parse(text); return {
const choices = json.output.choices as Array<{ isThinking: false,
message: { content: string }; content: content,
}>; };
const delta = choices[0]?.message?.content;
if (delta) {
remainText += delta;
}
} catch (e) {
console.error("[Request] parse error", text, msg);
} }
return {
isThinking: false,
content: "",
};
}, },
onclose() { // processToolMessage, include tool_calls message and tool call results
finish(); (
requestPayload: RequestPayload,
toolCallMessage: any,
toolCallResult: any[],
) => {
// @ts-ignore
requestPayload?.messages?.splice(
// @ts-ignore
requestPayload?.messages?.length,
0,
toolCallMessage,
...toolCallResult,
);
}, },
onerror(e) { options,
options.onError?.(e); );
throw e;
},
openWhenHidden: true,
});
} else { } else {
const res = await fetch(chatPath, chatPayload); const res = await fetch(chatPath, chatPayload);
clearTimeout(requestTimeoutId); clearTimeout(requestTimeoutId);

View File

@ -5,7 +5,13 @@ import {
BYTEDANCE_BASE_URL, BYTEDANCE_BASE_URL,
REQUEST_TIMEOUT_MS, REQUEST_TIMEOUT_MS,
} from "@/app/constant"; } from "@/app/constant";
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store"; import {
useAccessStore,
useAppConfig,
useChatStore,
ChatMessageTool,
usePluginStore,
} from "@/app/store";
import { import {
ChatOptions, ChatOptions,
@ -15,14 +21,11 @@ import {
MultimodalContent, MultimodalContent,
SpeechOptions, SpeechOptions,
} from "../api"; } from "../api";
import Locale from "../../locales";
import { import { streamWithThink } from "@/app/utils/chat";
EventStreamContentType,
fetchEventSource,
} from "@fortaine/fetch-event-source";
import { prettyObject } from "@/app/utils/format";
import { getClientConfig } from "@/app/config/client"; import { getClientConfig } from "@/app/config/client";
import { preProcessImageContent } from "@/app/utils/chat"; import { preProcessImageContent } from "@/app/utils/chat";
import { getMessageTextContentWithoutThinking } from "@/app/utils";
import { fetch } from "@/app/utils/stream"; import { fetch } from "@/app/utils/stream";
export interface OpenAIListModelResponse { export interface OpenAIListModelResponse {
@ -86,7 +89,10 @@ export class DoubaoApi implements LLMApi {
async chat(options: ChatOptions) { async chat(options: ChatOptions) {
const messages: ChatOptions["messages"] = []; const messages: ChatOptions["messages"] = [];
for (const v of options.messages) { for (const v of options.messages) {
const content = await preProcessImageContent(v.content); const content =
v.role === "assistant"
? getMessageTextContentWithoutThinking(v)
: await preProcessImageContent(v.content);
messages.push({ role: v.role, content }); messages.push({ role: v.role, content });
} }
@ -128,115 +134,96 @@ export class DoubaoApi implements LLMApi {
); );
if (shouldStream) { if (shouldStream) {
let responseText = ""; const [tools, funcs] = usePluginStore
let remainText = ""; .getState()
let finished = false; .getAsTools(
let responseRes: Response; useChatStore.getState().currentSession().mask?.plugin || [],
);
// animate response to make it looks smooth return streamWithThink(
function animateResponseText() { chatPath,
if (finished || controller.signal.aborted) { requestPayload,
responseText += remainText; getHeaders(),
console.log("[Response Animation] finished"); tools as any,
if (responseText?.length === 0) { funcs,
options.onError?.(new Error("empty response from server")); controller,
} // parseSSE
return; (text: string, runTools: ChatMessageTool[]) => {
} // console.log("parseSSE", text, runTools);
const json = JSON.parse(text);
if (remainText.length > 0) { const choices = json.choices as Array<{
const fetchCount = Math.max(1, Math.round(remainText.length / 60)); delta: {
const fetchText = remainText.slice(0, fetchCount); content: string | null;
responseText += fetchText; tool_calls: ChatMessageTool[];
remainText = remainText.slice(fetchCount); reasoning_content: string | null;
options.onUpdate?.(responseText, fetchText); };
} }>;
const tool_calls = choices[0]?.delta?.tool_calls;
requestAnimationFrame(animateResponseText); if (tool_calls?.length > 0) {
} const index = tool_calls[0]?.index;
const id = tool_calls[0]?.id;
// start animaion const args = tool_calls[0]?.function?.arguments;
animateResponseText(); if (id) {
runTools.push({
const finish = () => { id,
if (!finished) { type: tool_calls[0]?.type,
finished = true; function: {
options.onFinish(responseText + remainText, responseRes); name: tool_calls[0]?.function?.name as string,
} arguments: args,
}; },
});
controller.signal.onabort = finish; } else {
// @ts-ignore
fetchEventSource(chatPath, { runTools[index]["function"]["arguments"] += args;
fetch: fetch as any, }
...chatPayload,
async onopen(res) {
clearTimeout(requestTimeoutId);
const contentType = res.headers.get("content-type");
console.log(
"[ByteDance] request response content type: ",
contentType,
);
responseRes = res;
if (contentType?.startsWith("text/plain")) {
responseText = await res.clone().text();
return finish();
} }
const reasoning = choices[0]?.delta?.reasoning_content;
const content = choices[0]?.delta?.content;
// Skip if both content and reasoning_content are empty or null
if ( if (
!res.ok || (!reasoning || reasoning.trim().length === 0) &&
!res.headers (!content || content.trim().length === 0)
.get("content-type")
?.startsWith(EventStreamContentType) ||
res.status !== 200
) { ) {
const responseTexts = [responseText]; return {
let extraInfo = await res.clone().text(); isThinking: false,
try { content: "",
const resJson = await res.clone().json(); };
extraInfo = prettyObject(resJson);
} catch {}
if (res.status === 401) {
responseTexts.push(Locale.Error.Unauthorized);
}
if (extraInfo) {
responseTexts.push(extraInfo);
}
responseText = responseTexts.join("\n\n");
return finish();
} }
},
onmessage(msg) { if (reasoning && reasoning.trim().length > 0) {
if (msg.data === "[DONE]" || finished) { return {
return finish(); isThinking: true,
} content: reasoning,
const text = msg.data; };
try { } else if (content && content.trim().length > 0) {
const json = JSON.parse(text); return {
const choices = json.choices as Array<{ isThinking: false,
delta: { content: string }; content: content,
}>; };
const delta = choices[0]?.delta?.content;
if (delta) {
remainText += delta;
}
} catch (e) {
console.error("[Request] parse error", text, msg);
} }
return {
isThinking: false,
content: "",
};
}, },
onclose() { // processToolMessage, include tool_calls message and tool call results
finish(); (
requestPayload: RequestPayload,
toolCallMessage: any,
toolCallResult: any[],
) => {
// @ts-ignore
requestPayload?.messages?.splice(
// @ts-ignore
requestPayload?.messages?.length,
0,
toolCallMessage,
...toolCallResult,
);
}, },
onerror(e) { options,
options.onError?.(e); );
throw e;
},
openWhenHidden: true,
});
} else { } else {
const res = await fetch(chatPath, chatPayload); const res = await fetch(chatPath, chatPayload);
clearTimeout(requestTimeoutId); clearTimeout(requestTimeoutId);