-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Update to the latest google-cloud-aiplatform dependency and properly handle function calls in gemini #16793
Draft
stfines-clgx
wants to merge
20
commits into
run-llama:main
Choose a base branch
from
stfines-clgx:dev/1_70_protobuf_only
base: main
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.
Draft
Update to the latest google-cloud-aiplatform dependency and properly handle function calls in gemini #16793
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0a0072b
(chore) Update to 1.71.1
stfines-clgx 1da6066
Merge branch 'run-llama:main' into dev/1_70_protobuf_only
stfines-clgx cf7b348
Merge branch 'main' into dev/1_70_protobuf_only
stfines-clgx 1b77e75
Merge branch 'main' into dev/1_70_protobuf_only
stfines-clgx dd09285
(feat) Wrapped the BaseTool for Gemini
stfines-clgx 2545dc3
Merge branch 'main' into dev/1_70_protobuf_only
stfines-clgx 5190d6f
Merge branch 'dev/1_70_protobuf_only' of github.com:stfines-clgx/llam…
stfines-clgx f92fbd9
Merge branch 'main' into dev/1_70_protobuf_only
stfines-clgx f479a98
(bug) #16625 Address that Plan object is not compatible with Protobuf
stfines-clgx b588cf0
Merge remote-tracking branch 'origin/dev/1_70_protobuf_only' into dev…
stfines-clgx 6214e5f
Merge branch 'main' into dev/1_70_protobuf_only
stfines-clgx b3bda1a
Merge branch 'main' into dev/1_70_protobuf_only
stfines-clgx 8b83300
Merge branch 'main' into dev/1_70_protobuf_only
stfines-clgx 46839d7
Merge branch 'main' into dev/1_70_protobuf_only
stfines-clgx 42cfcfd
Merge branch 'main' into dev/1_70_protobuf_only
stfines-clgx 1b36d96
Merge branch 'main' into dev/1_70_protobuf_only
stfines-clgx 693aff1
(feat) Add feature flags for model garden
stfines-clgx 5d80b1f
Merge Commit
stfines-clgx 2aeec3e
Merge branch 'run-llama:main' into dev/1_70_protobuf_only
stfines-clgx 8034a99
Merge branch 'main' into dev/1_70_protobuf_only
stfines-clgx 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
105 changes: 105 additions & 0 deletions
105
llama-index-integrations/llms/llama-index-llms-vertex/llama_index/llms/vertex/gemini_tool.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,105 @@ | ||
import json | ||
from typing import Optional, Type, Any | ||
|
||
from llama_index.core.tools import ToolMetadata | ||
from llama_index.core.tools.types import DefaultToolFnSchema | ||
from pydantic import BaseModel | ||
|
||
|
||
def remap_schema(schema: dict) -> dict: | ||
""" | ||
Remap schema to match Gemini's internal API. | ||
""" | ||
parameters = {} | ||
|
||
for key, value in schema.items(): | ||
if key in ["title", "type", "properties", "required", "definitions"]: | ||
parameters[key] = value | ||
elif key == "$ref": | ||
parameters["defs"] = value | ||
else: | ||
continue | ||
|
||
return parameters | ||
|
||
|
||
class GeminiToolMetadataWrapper: | ||
""" | ||
The purpose of this dataclass is to represent the metadata in | ||
a manner that is compatible with Gemini's internal APIs. The | ||
default ToolMetadata class generates a json schema using $ref | ||
and $def field types which break google's protocol buffer | ||
serialization. | ||
""" | ||
|
||
def __init__(self, base: ToolMetadata) -> None: | ||
self._base = base | ||
self._name = self._base.name | ||
self._description = self._base.description | ||
self._fn_schema = self._base.fn_schema | ||
self._parameters = self.get_parameters_dict() | ||
|
||
fn_schema: Optional[Type[BaseModel]] = DefaultToolFnSchema | ||
|
||
def get_parameters_dict(self) -> dict: | ||
parameters = {} | ||
|
||
if self.fn_schema is None: | ||
parameters = { | ||
"type": "object", | ||
"properties": { | ||
"input": {"title": "input query string", "type": "string"}, | ||
}, | ||
"required": ["input"], | ||
} | ||
else: | ||
parameters = remap_schema( | ||
{ | ||
k: v | ||
for k, v in self.fn_schema.model_json_schema() | ||
if k in ["type", "properties", "required", "definitions", "$defs"] | ||
} | ||
) | ||
|
||
return parameters | ||
|
||
@property | ||
def fn_schema_str(self) -> str: | ||
"""Get fn schema as string.""" | ||
if self.fn_schema is None: | ||
raise ValueError("fn_schema is None.") | ||
parameters = self.get_parameters_dict() | ||
return json.dumps(parameters) | ||
|
||
def __getattr__(self, item) -> Any: | ||
match item: | ||
case "name": | ||
return self._name | ||
case "description": | ||
return self._description | ||
case "fn_schema": | ||
return self.fn_schema | ||
case "parameters": | ||
return self._parameters | ||
case _: | ||
raise AttributeError( | ||
f"No attribute '{item}' found in GeminiToolMetadataWrapper" | ||
) | ||
|
||
|
||
class GeminiToolWrapper: | ||
""" | ||
Wraps a base tool object to make it compatible with Gemini's | ||
internal APIs. | ||
""" | ||
|
||
def __init__(self, base_obj, *args, **kwargs) -> None: | ||
self.base_obj = base_obj | ||
# some stuff | ||
|
||
@property | ||
def metadata(self) -> GeminiToolMetadataWrapper: | ||
return GeminiToolMetadataWrapper(self.base_obj.metadata) | ||
|
||
def __getattr__(self, name) -> Any: | ||
return getattr(self.base_obj, name) |
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
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.
no way to do this without accessing private attributes hey? 😅 Oh google
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.
Well, in another branch, I did it by just creating an entirely new object by hand. I kept it this way to minimize the change surface area. I can move over to the other change style if it is an issue, but yeah- their MessageToDict function doesn't work without private access. Kind of silly; but then again, I'm not entirely sure that google makes apis for use outside of google.