-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Google Search Grounding static tool (#147)
* add google search static tool * Fix google search tool * fix linter * remove dead code * Fixes due to PR comments * fix tests * fix test helper * replace long comment with issue in GH * update integration test for static tools * minor refactor
- Loading branch information
1 parent
9d3d351
commit 801bedf
Showing
17 changed files
with
339 additions
and
48 deletions.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from abc import ABC, abstractmethod | ||
from enum import Enum | ||
from typing import List, NoReturn, Self | ||
|
||
from aidial_sdk.chat_completion.request import ( | ||
AzureChatCompletionRequest, | ||
StaticFunction, | ||
StaticTool, | ||
) | ||
from pydantic import BaseModel | ||
from vertexai.preview.generative_models import Tool as GeminiTool | ||
from vertexai.preview.generative_models import grounding | ||
|
||
from aidial_adapter_vertexai.chat.errors import ValidationError | ||
|
||
|
||
class ToolName(str, Enum): | ||
# https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/grounding | ||
GOOGLE_SEARCH = "google_search" | ||
|
||
|
||
class StaticToolProcessor(ABC): | ||
@staticmethod | ||
@abstractmethod | ||
def parse_gemini_tools( | ||
static_function: StaticFunction, | ||
) -> List[GeminiTool] | None: ... | ||
|
||
|
||
class GoogleSearchGroundingTool(StaticToolProcessor): | ||
@staticmethod | ||
def parse_gemini_tools( | ||
static_function: StaticFunction, | ||
) -> List[GeminiTool] | None: | ||
if static_function.name == ToolName.GOOGLE_SEARCH: | ||
if static_function.configuration: | ||
raise ValidationError( | ||
"Google search tool doesn't support configuration" | ||
) | ||
return [ | ||
GeminiTool.from_google_search_retrieval( | ||
grounding.GoogleSearchRetrieval() | ||
) | ||
] | ||
return None | ||
|
||
|
||
def unknown_tool_name( | ||
static_function: StaticFunction, | ||
) -> NoReturn: | ||
raise ValidationError( | ||
f"Unsupported static function: {static_function.name}" | ||
) | ||
|
||
|
||
class StaticToolsConfig(BaseModel): | ||
functions: List[StaticFunction] | ||
|
||
@classmethod | ||
def from_request(cls, request: AzureChatCompletionRequest) -> Self: | ||
if request.tools is None: | ||
return cls(functions=[]) | ||
|
||
return cls( | ||
functions=[ | ||
tool.static_function | ||
for tool in request.tools | ||
if isinstance(tool, StaticTool) | ||
] | ||
) | ||
|
||
@classmethod | ||
def noop(cls) -> Self: | ||
return cls(functions=[]) | ||
|
||
def to_gemini_tools(self) -> List[GeminiTool]: | ||
ret: List[GeminiTool] = [] | ||
for tool in self.functions: | ||
ret.extend( | ||
GoogleSearchGroundingTool.parse_gemini_tools(tool) | ||
or unknown_tool_name(tool) | ||
) | ||
return ret | ||
|
||
def not_supported(self) -> None: | ||
if self.functions: | ||
raise ValidationError("Static tools aren't supported") |
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.