Skip to content

Commit abf877a

Browse files
committed
♻️ refactor: refactor Minimax citations
1 parent c5fd412 commit abf877a

File tree

1 file changed

+26
-19
lines changed
  • src/libs/model-runtime/utils/streams

1 file changed

+26
-19
lines changed

src/libs/model-runtime/utils/streams/openai.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,31 +100,38 @@ export const transformOpenAIStream = (
100100
// one-api 的流式接口,会出现既有 finish_reason ,也有 content 的情况
101101
// {"id":"demo","model":"deepl-en","choices":[{"index":0,"delta":{"role":"assistant","content":"Introduce yourself."},"finish_reason":"stop"}]}
102102
if (typeof item.delta?.content === 'string' && !!item.delta.content) {
103-
// MiniMax 的内建 web_search 会在第一个流中返回引用源,需要先转为 JSON 数组后解析
103+
// MiniMax 内建搜索功能会在第一个 tools 流中 content 返回引用源,需要忽略
104104
// {"id":"0483748a25071c611e2f48d2982fbe96","choices":[{"finish_reason":"stop","index":0,"delta":{"content":"[{\"no\":1,\"url\":\"https://www.xiaohongshu.com/discovery/item/66d8de3c000000001f01e752\",\"title\":\"郑钦文为国而战,没有理由不坚持🏅\",\"content\":\"·2024年08月03日\\n中国队选手郑钦文夺得巴黎奥运会网球女单比赛金牌(巴黎奥运第16金)\\n#巴黎奥运会[话题]# #郑钦文[话题]# #人物素材积累[话题]# #作文素材积累[话题]# #申论素材[话题]#\",\"web_icon\":\"https://www.xiaohongshu.com/favicon.ico\"}]","role":"tool","tool_call_id":"call_function_6696730535"}}],"created":1748255114,"model":"abab6.5s-chat","object":"chat.completion.chunk","usage":{"total_tokens":0,"total_characters":0},"input_sensitive":false,"output_sensitive":false,"input_sensitive_type":0,"output_sensitive_type":0,"output_sensitive_int":0}
105105
if (typeof item.delta?.role === 'string' && item.delta.role === 'tool') {
106-
const citations = JSON.parse(item.delta.content);
107-
108-
return [
109-
{
110-
data: {
111-
citations: (citations as any[]).map(
112-
(item) =>
113-
({
114-
title: item.title,
115-
url: item.url,
116-
}) as CitationItem,
117-
),
118-
},
119-
id: chunk.id,
120-
type: 'grounding',
121-
},
122-
];
106+
return { data: null, id: chunk.id, type: 'text' };
123107
}
124108

125109
return { data: item.delta.content, id: chunk.id, type: 'text' };
126110
}
127111

112+
// MiniMax 内建搜索功能会在最后一个流中的 message 数组中返回 4 个 Object,其中最后一个为 annotations
113+
// {"id":"0483bf14ba55225a66de2342a21b4003","choices":[{"finish_reason":"tool_calls","index":0,"messages":[{"content":"","role":"user","reasoning_content":""},{"content":"","role":"assistant","tool_calls":[{"id":"call_function_0872338692","type":"web_search","function":{"name":"get_search_result","arguments":"{\"query_tag\":[\"天气\"],\"query_list\":[\"上海 2025年5月26日 天气\"]}"}}],"reasoning_content":""},{"content":"","role":"tool","tool_call_id":"call_function_0872338692","reasoning_content":""},{"content":"","role":"assistant","name":"海螺AI","annotations":[{"text":"【5†source】","url":"https://mtianqi.eastday.com/tianqi/shanghai/20250526.html","quote":"上海天气预报提供上海2025年05月26日天气"}],"audio_content":"","reasoning_content":""}]}],"created":1748274196,"model":"MiniMax-Text-01","object":"chat.completion","usage":{"total_tokens":13110,"total_characters":0,"prompt_tokens":12938,"completion_tokens":172},"base_resp":{"status_code":0,"status_msg":"Invalid parameters detected, json: unknown field \"user\""}}
114+
if ((item as any).messages && (item as any).messages.length > 0) {
115+
const messages = (item as any).messages;
116+
const citations = messages[messages.length - 1].annotations;
117+
118+
return [
119+
{
120+
data: {
121+
citations: citations.map(
122+
(item: any) =>
123+
({
124+
title: item.url,
125+
url: item.url,
126+
}) as CitationItem,
127+
),
128+
},
129+
id: chunk.id,
130+
type: 'grounding',
131+
},
132+
];
133+
}
134+
128135
// xAI Live Search 功能返回引用源
129136
// {"id":"8721eebb-6465-4c47-ba2e-8e2ec0f97055","object":"chat.completion.chunk","created":1747809109,"model":"grok-3","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":"stop"}],"system_fingerprint":"fp_1affcf9872","citations":["https://world.huanqiu.com/"]}
130137
if ((chunk as any).citations) {
@@ -202,7 +209,7 @@ export const transformOpenAIStream = (
202209
citations: (citations as any[]).map((item) => ({
203210
title: typeof item === 'string' ? item : item.title,
204211
url: typeof item === 'string' ? item : item.url || item.link,
205-
})).filter(c => c.title && c.url),
212+
})).filter(c => c.title && c.url), // Zhipu 内建搜索工具有时会返回空 link 引发程序崩溃
206213
},
207214
id: chunk.id,
208215
type: 'grounding',

0 commit comments

Comments
 (0)