thay doi alibaba module mac dinh - chebichat

This commit is contained in:
quangdn-ght
2025-06-24 09:18:27 +07:00
parent 673f907ea4
commit 861d854e16
13 changed files with 204 additions and 214 deletions

View File

@@ -18,7 +18,6 @@ import {
LLMModel,
SpeechOptions,
MultimodalContent,
MultimodalContentForAlibaba,
} from "../api";
import { getClientConfig } from "@/app/config/client";
import {
@@ -156,11 +155,13 @@ export class QwenApi implements LLMApi {
);
if (shouldStream) {
// Lấy danh sách các công cụ (tools) và hàm (funcs) từ plugin hiện tại của phiên chat
const [tools, funcs] = usePluginStore
.getState()
.getAsTools(
useChatStore.getState().currentSession().mask?.plugin || [],
);
// Gọi hàm streamWithThink để xử lý chat dạng stream (dòng sự kiện server-sent events)
return streamWithThink(
chatPath,
requestPayload,
@@ -168,44 +169,19 @@ export class QwenApi implements LLMApi {
tools as any,
funcs,
controller,
// parseSSE
// Updated SSE parse callback for new JSON structure
(text: string, runTools: ChatMessageTool[]) => {
// console.log("parseSSE", text, runTools);
// Parse the JSON response
const json = JSON.parse(text);
const choices = json.output.choices as Array<{
message: {
content: string | null | MultimodalContentForAlibaba[];
tool_calls: ChatMessageTool[];
reasoning_content: string | null;
};
}>;
if (!choices?.length) return { isThinking: false, content: "" };
// console.log("[Alibaba] SSE response", json);
const tool_calls = choices[0]?.message?.tool_calls;
if (tool_calls?.length > 0) {
const index = tool_calls[0]?.index;
const id = tool_calls[0]?.id;
const args = tool_calls[0]?.function?.arguments;
if (id) {
runTools.push({
id,
type: tool_calls[0]?.type,
function: {
name: tool_calls[0]?.function?.name as string,
arguments: args,
},
});
} else {
// @ts-ignore
runTools[index]["function"]["arguments"] += args;
}
}
// Extract content from the new structure
const output = json.output;
const content = output?.text ?? "";
const reasoning = output?.reasoning_content ?? ""; // If exists in your new structure
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 both are empty, return default
if (
(!reasoning || reasoning.length === 0) &&
(!content || content.length === 0)
@@ -216,31 +192,34 @@ export class QwenApi implements LLMApi {
};
}
// If reasoning_content exists, treat as "thinking"
if (reasoning && reasoning.length > 0) {
return {
isThinking: true,
content: reasoning,
};
} else if (content && content.length > 0) {
}
// Otherwise, return the main content
else if (content && content.length > 0) {
return {
isThinking: false,
content: Array.isArray(content)
? content.map((item) => item.text).join(",")
: content,
content: content,
};
}
// Fallback
return {
isThinking: false,
content: "",
};
},
// processToolMessage, include tool_calls message and tool call results
// Hàm xử lý message liên quan đến tool_call và kết quả trả về từ tool_call
(
requestPayload: RequestPayload,
toolCallMessage: any,
toolCallResult: any[],
) => {
// Thêm message gọi tool và kết quả trả về vào cuối mảng messages trong payload gửi lên API
requestPayload?.input?.messages?.splice(
requestPayload?.input?.messages?.length,
0,
@@ -248,7 +227,7 @@ export class QwenApi implements LLMApi {
...toolCallResult,
);
},
options,
options, // Các tuỳ chọn khác cho hàm streamWithThink
);
} else {
const res = await fetch(chatPath, chatPayload);

View File

@@ -151,7 +151,8 @@ export class DeepSeekApi implements LLMApi {
controller,
// parseSSE
(text: string, runTools: ChatMessageTool[]) => {
// console.log("parseSSE", text, runTools);
console.log("parseSSE", text, runTools);
const json = JSON.parse(text);
const choices = json.choices as Array<{
delta: {

View File

@@ -153,81 +153,35 @@ export class SiliconflowApi implements LLMApi {
tools as any,
funcs,
controller,
// parseSSE
// parseSSE mới cho SiliconFlow response
(text: string, runTools: ChatMessageTool[]) => {
// console.log("parseSSE", text, runTools);
// Parse chuỗi JSON trả về thành đối tượng
const json = JSON.parse(text);
const choices = json.choices as Array<{
delta: {
content: string | null;
tool_calls: ChatMessageTool[];
reasoning_content: string | null;
};
}>;
const tool_calls = choices[0]?.delta?.tool_calls;
if (tool_calls?.length > 0) {
const index = tool_calls[0]?.index;
const id = tool_calls[0]?.id;
const args = tool_calls[0]?.function?.arguments;
if (id) {
runTools.push({
id,
type: tool_calls[0]?.type,
function: {
name: tool_calls[0]?.function?.name as string,
arguments: args,
},
});
} else {
// @ts-ignore
runTools[index]["function"]["arguments"] += args;
}
}
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 (
(!reasoning || reasoning.length === 0) &&
(!content || content.length === 0)
) {
// Lấy nội dung trả lời từ output.text
const content = json?.output?.text ?? "";
// Nếu không có nội dung trả lời, trả về trạng thái không suy nghĩ và nội dung rỗng
if (!content || content.length === 0) {
return {
isThinking: false,
content: "",
};
}
if (reasoning && reasoning.length > 0) {
return {
isThinking: true,
content: reasoning,
};
} else if (content && content.length > 0) {
return {
isThinking: false,
content: content,
};
}
// Trả về trạng thái không suy nghĩ và nội dung trả lời
return {
isThinking: false,
content: "",
content: content,
};
},
// processToolMessage, include tool_calls message and tool call results
// processToolMessage: SiliconFlow không có tool_call nên giữ nguyên hoặc để rỗng
(
requestPayload: RequestPayload,
toolCallMessage: any,
toolCallResult: any[],
) => {
// @ts-ignore
requestPayload?.messages?.splice(
// @ts-ignore
requestPayload?.messages?.length,
0,
toolCallMessage,
...toolCallResult,
);
// Không cần xử lý tool_call, có thể để trống hoặc giữ nguyên nếu muốn tương thích
},
options,
);