-
Notifications
You must be signed in to change notification settings - Fork 15.8k
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
added support for tools on VertexAI #14839
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
41 changes: 41 additions & 0 deletions
41
libs/community/langchain_community/utils/vertex_functions.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,41 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
from langchain_core.tools import Tool | ||
|
||
from langchain_community.utils.openai_functions import ( | ||
FunctionDescription, | ||
convert_pydantic_to_openai_function, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from vertexai.preview.generative_models import Tool as VertexTool | ||
|
||
|
||
def format_tool_to_vertex_function(tool: Tool) -> FunctionDescription: | ||
"Format tool into the Vertex function API." | ||
if tool.args_schema: | ||
return convert_pydantic_to_openai_function( | ||
tool.args_schema, name=tool.name, description=tool.description | ||
) | ||
else: | ||
return { | ||
"name": tool.name, | ||
"description": tool.description, | ||
"parameters": { | ||
"properties": { | ||
"__arg1": {"type": "string"}, | ||
}, | ||
"required": ["__arg1"], | ||
"type": "object", | ||
}, | ||
} | ||
|
||
|
||
def format_tool_to_vertex_tool(tool: Tool) -> "VertexTool": | ||
"Format tool into the Vertex Tool instance." | ||
|
||
from vertexai.preview.generative_models import FunctionDeclaration | ||
from vertexai.preview.generative_models import Tool as VertexTool | ||
|
||
function_declaration = FunctionDeclaration(**format_tool_to_vertex_function(tool)) | ||
return VertexTool(function_declarations=[function_declaration]) |
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
Oops, something went wrong.
Oops, something went wrong.
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.
i tried this out and it errors, i think you need to remove
title
from the json schema that is generatedThere 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.
@hwchase17 which error do you get?
I've just tried this again
and it seems to be working fine.
and there's also a working integration test:
langchain/libs/community/tests/integration_tests/chat_models/test_vertexai.py
Line 299 in 45f2fd5