-
Notifications
You must be signed in to change notification settings - Fork 17k
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
google-genai[patch]: match function call interface #17213
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bd51736
remove pg_essay.txt
a2d67fc
Merge branch 'master' into erick/genai-functions-tools
7052e73
x
b21f8c1
Merge branch 'master' into erick/genai-functions-tools
5a9bafd
tests
e087d1a
x
3b25a5d
fmt
90176a5
x
4459a81
function call type
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
135 changes: 135 additions & 0 deletions
135
libs/partners/google-genai/langchain_google_genai/_function_utils.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
from __future__ import annotations | ||
|
||
from typing import ( | ||
Dict, | ||
List, | ||
Type, | ||
Union, | ||
) | ||
|
||
from langchain_core.pydantic_v1 import BaseModel | ||
from langchain_core.tools import BaseTool | ||
from langchain_core.utils.json_schema import dereference_refs | ||
|
||
FunctionCallType = Union[BaseTool, Type[BaseModel], Dict] | ||
|
||
TYPE_ENUM = { | ||
"string": 1, | ||
"number": 2, | ||
"integer": 3, | ||
"boolean": 4, | ||
"array": 5, | ||
"object": 6, | ||
} | ||
|
||
|
||
def convert_to_genai_function_declarations( | ||
function_calls: List[FunctionCallType], | ||
) -> Dict: | ||
function_declarations = [] | ||
for fc in function_calls: | ||
function_declarations.append(_convert_to_genai_function(fc)) | ||
return { | ||
"function_declarations": function_declarations, | ||
} | ||
|
||
|
||
def _convert_to_genai_function(fc: FunctionCallType) -> Dict: | ||
""" | ||
Produce | ||
|
||
{ | ||
"name": "get_weather", | ||
"description": "Determine weather in my location", | ||
"parameters": { | ||
"properties": { | ||
"location": { | ||
"description": "The city and state e.g. San Francisco, CA", | ||
"type_": 1 | ||
}, | ||
"unit": { "enum": ["c", "f"], "type_": 1 } | ||
}, | ||
"required": ["location"], | ||
"type_": 6 | ||
} | ||
} | ||
|
||
""" | ||
if isinstance(fc, BaseTool): | ||
return _convert_tool_to_genai_function(fc) | ||
elif isinstance(fc, type) and issubclass(fc, BaseModel): | ||
return _convert_pydantic_to_genai_function(fc) | ||
elif isinstance(fc, dict): | ||
return { | ||
**fc, | ||
"parameters": { | ||
"properties": { | ||
k: { | ||
"type_": TYPE_ENUM[v["type"]], | ||
"description": v.get("description"), | ||
} | ||
for k, v in fc["parameters"]["properties"].items() | ||
}, | ||
"required": fc["parameters"].get("required", []), | ||
"type_": TYPE_ENUM[fc["parameters"]["type"]], | ||
}, | ||
} | ||
else: | ||
raise ValueError(f"Unsupported function call type {fc}") | ||
|
||
|
||
def _convert_tool_to_genai_function(tool: BaseTool) -> Dict: | ||
if tool.args_schema: | ||
schema = dereference_refs(tool.args_schema.schema()) | ||
schema.pop("definitions", None) | ||
|
||
return { | ||
"name": tool.name or schema["title"], | ||
"description": tool.description or schema["description"], | ||
"parameters": { | ||
"properties": { | ||
k: { | ||
"type_": TYPE_ENUM[v["type"]], | ||
"description": v.get("description"), | ||
} | ||
for k, v in schema["properties"].items() | ||
}, | ||
"required": schema["required"], | ||
"type_": TYPE_ENUM[schema["type"]], | ||
}, | ||
} | ||
else: | ||
return { | ||
"name": tool.name, | ||
"description": tool.description, | ||
"parameters": { | ||
"properties": { | ||
"__arg1": {"type": "string"}, | ||
}, | ||
"required": ["__arg1"], | ||
"type_": TYPE_ENUM["object"], | ||
}, | ||
} | ||
|
||
|
||
def _convert_pydantic_to_genai_function( | ||
pydantic_model: Type[BaseModel], | ||
) -> Dict: | ||
schema = dereference_refs(pydantic_model.schema()) | ||
schema.pop("definitions", None) | ||
|
||
return { | ||
"name": schema["title"], | ||
"description": schema.get("description", ""), | ||
"parameters": { | ||
"properties": { | ||
k: { | ||
"type_": TYPE_ENUM[v["type"]], | ||
"description": v.get("description"), | ||
} | ||
for k, v in schema["properties"].items() | ||
}, | ||
"required": schema["required"], | ||
"type_": TYPE_ENUM[schema["type"]], | ||
}, | ||
} |
This file contains 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
This file contains 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.
switched to string to match others