forked from langchain-ai/langchain
-
Notifications
You must be signed in to change notification settings - Fork 0
handle gemini thought signatures #3
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
Open
DhirenMhatre
wants to merge
1
commit into
master
Choose a base branch
from
cc/openai_gemini_thought_signatures
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -194,6 +194,23 @@ def _get_default_model_profile(model_name: str) -> ModelProfile: | |||||||||||||||||||||||||
| "tool_search", | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Google's OpenAI-compatible endpoint returns Gemini thought signatures on | ||||||||||||||||||||||||||
| # tool calls via `extra_content.google.thought_signature`. The signature must | ||||||||||||||||||||||||||
| # be echoed back on the corresponding tool call in subsequent turns. | ||||||||||||||||||||||||||
| _GEMINI_THOUGHT_SIGNATURES_MAP_KEY = "__gemini_function_call_thought_signatures__" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def _extract_gemini_thought_signature(raw_tool_call: Mapping[str, Any]) -> str | None: | ||||||||||||||||||||||||||
| """Pull a Gemini thought signature off a raw OpenAI-format tool call.""" | ||||||||||||||||||||||||||
| extra_content = raw_tool_call.get("extra_content") | ||||||||||||||||||||||||||
| if not isinstance(extra_content, Mapping): | ||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||
| google = extra_content.get("google") | ||||||||||||||||||||||||||
| if not isinstance(google, Mapping): | ||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||
| signature = google.get("thought_signature") | ||||||||||||||||||||||||||
| return signature if isinstance(signature, str) else None | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage: | ||||||||||||||||||||||||||
| """Convert a dictionary to a LangChain message. | ||||||||||||||||||||||||||
|
|
@@ -218,6 +235,7 @@ def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage: | |||||||||||||||||||||||||
| additional_kwargs["function_call"] = dict(function_call) | ||||||||||||||||||||||||||
| tool_calls = [] | ||||||||||||||||||||||||||
| invalid_tool_calls = [] | ||||||||||||||||||||||||||
| thought_signatures: dict[str, str] = {} | ||||||||||||||||||||||||||
| if raw_tool_calls := _dict.get("tool_calls"): | ||||||||||||||||||||||||||
| for raw_tool_call in raw_tool_calls: | ||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||
|
|
@@ -226,6 +244,12 @@ def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage: | |||||||||||||||||||||||||
| invalid_tool_calls.append( | ||||||||||||||||||||||||||
| make_invalid_tool_call(raw_tool_call, str(e)) | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| if (signature := _extract_gemini_thought_signature(raw_tool_call)) and ( | ||||||||||||||||||||||||||
| tool_call_id := raw_tool_call.get("id") | ||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||
| thought_signatures[tool_call_id] = signature | ||||||||||||||||||||||||||
| if thought_signatures: | ||||||||||||||||||||||||||
| additional_kwargs[_GEMINI_THOUGHT_SIGNATURES_MAP_KEY] = thought_signatures | ||||||||||||||||||||||||||
| if audio := _dict.get("audio"): | ||||||||||||||||||||||||||
| additional_kwargs["audio"] = audio | ||||||||||||||||||||||||||
| return AIMessage( | ||||||||||||||||||||||||||
|
|
@@ -381,6 +405,18 @@ def _convert_message_to_dict( | |||||||||||||||||||||||||
| message_dict["function_call"] = message.additional_kwargs["function_call"] | ||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||
| pass | ||||||||||||||||||||||||||
| if "tool_calls" in message_dict and ( | ||||||||||||||||||||||||||
| thought_signatures := message.additional_kwargs.get( | ||||||||||||||||||||||||||
| _GEMINI_THOUGHT_SIGNATURES_MAP_KEY | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||
| for tool_call in message_dict["tool_calls"]: | ||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||
| signature := thought_signatures.get(tool_call.get("id")) | ||||||||||||||||||||||||||
| ) is not None: | ||||||||||||||||||||||||||
| tool_call["extra_content"] = { | ||||||||||||||||||||||||||
| "google": {"thought_signature": signature} | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| # If tool calls present, content null value should be None not empty string. | ||||||||||||||||||||||||||
| if "function_call" in message_dict or "tool_calls" in message_dict: | ||||||||||||||||||||||||||
| message_dict["content"] = message_dict["content"] or None | ||||||||||||||||||||||||||
|
|
@@ -452,6 +488,14 @@ def _convert_delta_to_message_chunk( | |||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||
| except KeyError: | ||||||||||||||||||||||||||
| pass | ||||||||||||||||||||||||||
| thought_signatures: dict[str, str] = {} | ||||||||||||||||||||||||||
| for rtc in raw_tool_calls: | ||||||||||||||||||||||||||
| if (signature := _extract_gemini_thought_signature(rtc)) and ( | ||||||||||||||||||||||||||
| tool_call_id := rtc.get("id") | ||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||
| thought_signatures[tool_call_id] = signature | ||||||||||||||||||||||||||
| if thought_signatures: | ||||||||||||||||||||||||||
|
Comment on lines
+492
to
+497
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.
Suggested change
|
||||||||||||||||||||||||||
| additional_kwargs[_GEMINI_THOUGHT_SIGNATURES_MAP_KEY] = thought_signatures | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if role == "user" or default_class == HumanMessageChunk: | ||||||||||||||||||||||||||
| return HumanMessageChunk(content=content, id=id_) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought_signature(falsy) would be silently dropped instead of being stored and echoed back. The same pattern appears a second time in_convert_delta_to_message_chunk. Prefer an explicitis not Nonecheck to match the intent of the extractor, which already returnsNonefor absent/wrong-type values.