-
-
Notifications
You must be signed in to change notification settings - Fork 14k
💄 style: support web_search tool for MiniMax & Zhipu
#7980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
61597c5
5b40c37
c5fd412
abf877a
81feaf2
aa7c4ba
0e3e777
879e638
98325eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -99,11 +99,60 @@ export const transformOpenAIStream = ( | |||||||||||||
| if (item.finish_reason) { | ||||||||||||||
| // one-api 的流式接口,会出现既有 finish_reason ,也有 content 的情况 | ||||||||||||||
| // {"id":"demo","model":"deepl-en","choices":[{"index":0,"delta":{"role":"assistant","content":"Introduce yourself."},"finish_reason":"stop"}]} | ||||||||||||||
|
|
||||||||||||||
| if (typeof item.delta?.content === 'string' && !!item.delta.content) { | ||||||||||||||
| // MiniMax 内建搜索功能会在第一个 tools 流中 content 返回引用源,需要忽略 | ||||||||||||||
| // {"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} | ||||||||||||||
| if (typeof item.delta?.role === 'string' && item.delta.role === 'tool') { | ||||||||||||||
| return { data: null, id: chunk.id, type: 'text' }; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return { data: item.delta.content, id: chunk.id, type: 'text' }; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // MiniMax 内建搜索功能会在最后一个流中的 message 数组中返回 4 个 Object,其中最后一个为 annotations | ||||||||||||||
| // {"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\""}} | ||||||||||||||
| if ((item as any).messages && (item as any).messages.length > 0) { | ||||||||||||||
| const citations = (item as any).messages.at(-1).annotations; | ||||||||||||||
|
Comment on lines
+136
to
+137
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Potential null reference error if annotations is undefined in the last message
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| return [ | ||||||||||||||
| { | ||||||||||||||
| data: { | ||||||||||||||
| citations: citations.map( | ||||||||||||||
| (item: any) => | ||||||||||||||
| ({ | ||||||||||||||
| title: item.url, | ||||||||||||||
| url: item.url, | ||||||||||||||
|
Comment on lines
+145
to
+146
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Using URL as both title and URL may be confusing for users - consider using item.text for title |
||||||||||||||
| }) as CitationItem, | ||||||||||||||
| ), | ||||||||||||||
| }, | ||||||||||||||
| id: chunk.id, | ||||||||||||||
| type: 'grounding', | ||||||||||||||
| }, | ||||||||||||||
| ]; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // xAI Live Search 功能返回引用源 | ||||||||||||||
| // {"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/"]} | ||||||||||||||
| if ((chunk as any).citations) { | ||||||||||||||
| const citations = (chunk as any).citations; | ||||||||||||||
|
|
||||||||||||||
| return [ | ||||||||||||||
| { | ||||||||||||||
| data: { | ||||||||||||||
| citations: citations.map( | ||||||||||||||
| (item: any) => | ||||||||||||||
| ({ | ||||||||||||||
| title: item, | ||||||||||||||
| url: item, | ||||||||||||||
| }) as CitationItem, | ||||||||||||||
| ), | ||||||||||||||
| }, | ||||||||||||||
| id: chunk.id, | ||||||||||||||
| type: 'grounding', | ||||||||||||||
| }, | ||||||||||||||
| ]; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (chunk.usage) { | ||||||||||||||
| const usage = chunk.usage; | ||||||||||||||
| return { data: convertUsage(usage), id: chunk.id, type: 'usage' }; | ||||||||||||||
|
|
@@ -146,21 +195,20 @@ export const transformOpenAIStream = ( | |||||||||||||
| // in Hunyuan api, the citation is in every chunk | ||||||||||||||
| ('search_info' in chunk && (chunk.search_info as any)?.search_results) || | ||||||||||||||
| // in Wenxin api, the citation is in the first and last chunk | ||||||||||||||
| ('search_results' in chunk && chunk.search_results); | ||||||||||||||
| ('search_results' in chunk && chunk.search_results) || | ||||||||||||||
| // in Zhipu api, the citation is in the first chunk | ||||||||||||||
| ('web_search' in chunk && chunk.web_search); | ||||||||||||||
|
|
||||||||||||||
| if (citations) { | ||||||||||||||
| streamContext.returnedCitation = true; | ||||||||||||||
|
|
||||||||||||||
| return [ | ||||||||||||||
| { | ||||||||||||||
| data: { | ||||||||||||||
| citations: (citations as any[]).map( | ||||||||||||||
| (item) => | ||||||||||||||
| ({ | ||||||||||||||
| title: typeof item === 'string' ? item : item.title, | ||||||||||||||
| url: typeof item === 'string' ? item : item.url, | ||||||||||||||
| }) as CitationItem, | ||||||||||||||
| ), | ||||||||||||||
| citations: (citations as any[]).map((item) => ({ | ||||||||||||||
| title: typeof item === 'string' ? item : item.title, | ||||||||||||||
| url: typeof item === 'string' ? item : item.url || item.link, | ||||||||||||||
| })).filter(c => c.title && c.url), // Zhipu 内建搜索工具有时会返回空 link 引发程序崩溃 | ||||||||||||||
| }, | ||||||||||||||
| id: chunk.id, | ||||||||||||||
| type: 'grounding', | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.