mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 03:55:55 +00:00
Compare commits
4 Commits
v4.10.0-be
...
copilot/fi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1e0275ee08 | ||
|
|
3fa7389ba9 | ||
|
|
84a3b1f465 | ||
|
|
377d011da2 |
@@ -20,6 +20,24 @@ class GeminiChatCompletions(chatcmpl.OpenAIChatCompletions):
|
||||
'timeout': 120,
|
||||
}
|
||||
|
||||
def _add_thought_signature_to_messages(self, messages: list[dict]) -> list[dict]:
|
||||
"""Add thought_signature to tool_calls in messages for Gemini API compatibility
|
||||
|
||||
Gemini API requires a thought_signature field in function call parts.
|
||||
See: https://ai.google.dev/gemini-api/docs/thought-signatures
|
||||
|
||||
Note: This function modifies the dictionaries in the messages list in place.
|
||||
"""
|
||||
for msg in messages:
|
||||
if 'tool_calls' in msg and msg['tool_calls']:
|
||||
# Ensure we're working with a mutable copy of tool_calls
|
||||
if not isinstance(msg['tool_calls'], list):
|
||||
continue
|
||||
for tool_call in msg['tool_calls']:
|
||||
if isinstance(tool_call, dict) and 'thought_signature' not in tool_call:
|
||||
tool_call['thought_signature'] = ''
|
||||
return messages
|
||||
|
||||
async def _closure_stream(
|
||||
self,
|
||||
query: pipeline_query.Query,
|
||||
@@ -42,6 +60,9 @@ class GeminiChatCompletions(chatcmpl.OpenAIChatCompletions):
|
||||
# 设置此次请求中的messages
|
||||
messages = req_messages.copy()
|
||||
|
||||
# Add thought_signature to tool_calls for Gemini compatibility
|
||||
messages = self._add_thought_signature_to_messages(messages)
|
||||
|
||||
# 检查vision
|
||||
for msg in messages:
|
||||
if 'content' in msg and isinstance(msg['content'], list):
|
||||
@@ -140,3 +161,29 @@ class GeminiChatCompletions(chatcmpl.OpenAIChatCompletions):
|
||||
|
||||
yield provider_message.MessageChunk(**chunk_data)
|
||||
chunk_idx += 1
|
||||
|
||||
async def _closure(
|
||||
self,
|
||||
query: pipeline_query.Query,
|
||||
req_messages: list[dict],
|
||||
use_model: requester.RuntimeLLMModel,
|
||||
use_funcs: list[resource_tool.LLMTool] = None,
|
||||
extra_args: dict[str, typing.Any] = {},
|
||||
remove_think: bool = False,
|
||||
) -> tuple[provider_message.Message, dict]:
|
||||
"""Override _closure to add thought_signature to messages"""
|
||||
# Make a shallow copy to avoid mutating the caller's list
|
||||
messages = req_messages.copy()
|
||||
|
||||
# Add thought_signature to tool_calls for Gemini compatibility
|
||||
messages = self._add_thought_signature_to_messages(messages)
|
||||
|
||||
# Call parent implementation
|
||||
return await super()._closure(
|
||||
query=query,
|
||||
req_messages=messages,
|
||||
use_model=use_model,
|
||||
use_funcs=use_funcs,
|
||||
extra_args=extra_args,
|
||||
remove_think=remove_think,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user