mirror of
https://github.com/EvolutionAPI/adk-python.git
synced 2026-02-04 13:56:24 -06:00
refactor: uniform Google LLM variant and parsing logic and make contant value consistent with Google GenAI SDK : 903e0729ce/google/genai/_automatic_function_calling_util.py (L96)
PiperOrigin-RevId: 765639681
This commit is contained in:
committed by
Copybara-Service
parent
62d7bf58bb
commit
036f954a2a
@@ -31,6 +31,7 @@ from pydantic import create_model
|
||||
from pydantic import fields as pydantic_fields
|
||||
|
||||
from . import _function_parameter_parse_util
|
||||
from ..utils.variant_utils import GoogleLLMVariant
|
||||
|
||||
_py_type_2_schema_type = {
|
||||
'str': types.Type.STRING,
|
||||
@@ -194,7 +195,7 @@ def _get_return_type(func: Callable) -> Any:
|
||||
def build_function_declaration(
|
||||
func: Union[Callable, BaseModel],
|
||||
ignore_params: Optional[list[str]] = None,
|
||||
variant: Literal['GOOGLE_AI', 'VERTEX_AI', 'DEFAULT'] = 'GOOGLE_AI',
|
||||
variant: GoogleLLMVariant = GoogleLLMVariant.GEMINI_API,
|
||||
) -> types.FunctionDeclaration:
|
||||
signature = inspect.signature(func)
|
||||
should_update_signature = False
|
||||
@@ -291,16 +292,9 @@ def build_function_declaration_util(
|
||||
|
||||
def from_function_with_options(
|
||||
func: Callable,
|
||||
variant: Literal['GOOGLE_AI', 'VERTEX_AI', 'DEFAULT'] = 'GOOGLE_AI',
|
||||
variant: GoogleLLMVariant = GoogleLLMVariant.GEMINI_API,
|
||||
) -> 'types.FunctionDeclaration':
|
||||
|
||||
supported_variants = ['GOOGLE_AI', 'VERTEX_AI', 'DEFAULT']
|
||||
if variant not in supported_variants:
|
||||
raise ValueError(
|
||||
f'Unsupported variant: {variant}. Supported variants are:'
|
||||
f' {", ".join(supported_variants)}'
|
||||
)
|
||||
|
||||
parameters_properties = {}
|
||||
for name, param in inspect.signature(func).parameters.items():
|
||||
if param.kind in (
|
||||
@@ -330,7 +324,7 @@ def from_function_with_options(
|
||||
declaration.parameters
|
||||
)
|
||||
)
|
||||
if not variant == 'VERTEX_AI':
|
||||
if variant == GoogleLLMVariant.GEMINI_API:
|
||||
return declaration
|
||||
|
||||
return_annotation = inspect.signature(func).return_annotation
|
||||
|
||||
@@ -28,6 +28,8 @@ from typing import Union
|
||||
from google.genai import types
|
||||
import pydantic
|
||||
|
||||
from ..utils.variant_utils import GoogleLLMVariant
|
||||
|
||||
_py_builtin_type_to_schema_type = {
|
||||
str: types.Type.STRING,
|
||||
int: types.Type.INTEGER,
|
||||
@@ -63,8 +65,10 @@ def _update_for_default_if_mldev(schema: types.Schema):
|
||||
)
|
||||
|
||||
|
||||
def _raise_if_schema_unsupported(variant: str, schema: types.Schema):
|
||||
if not variant == 'VERTEX_AI':
|
||||
def _raise_if_schema_unsupported(
|
||||
variant: GoogleLLMVariant, schema: types.Schema
|
||||
):
|
||||
if variant == GoogleLLMVariant.GEMINI_API:
|
||||
_raise_for_any_of_if_mldev(schema)
|
||||
_update_for_default_if_mldev(schema)
|
||||
|
||||
@@ -116,7 +120,7 @@ def _is_default_value_compatible(
|
||||
|
||||
|
||||
def _parse_schema_from_parameter(
|
||||
variant: str, param: inspect.Parameter, func_name: str
|
||||
variant: GoogleLLMVariant, param: inspect.Parameter, func_name: str
|
||||
) -> types.Schema:
|
||||
"""parse schema from parameter.
|
||||
|
||||
|
||||
@@ -15,13 +15,14 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC
|
||||
import os
|
||||
from typing import Any
|
||||
from typing import Optional
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from google.genai import types
|
||||
|
||||
from ..utils.variant_utils import get_google_llm_variant
|
||||
from ..utils.variant_utils import GoogleLLMVariant
|
||||
from .tool_context import ToolContext
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -118,12 +119,8 @@ class BaseTool(ABC):
|
||||
)
|
||||
|
||||
@property
|
||||
def _api_variant(self) -> str:
|
||||
use_vertexai = os.environ.get('GOOGLE_GENAI_USE_VERTEXAI', '0').lower() in [
|
||||
'true',
|
||||
'1',
|
||||
]
|
||||
return 'VERTEX_AI' if use_vertexai else 'GOOGLE_AI'
|
||||
def _api_variant(self) -> GoogleLLMVariant:
|
||||
return get_google_llm_variant()
|
||||
|
||||
|
||||
def _find_tool_with_function_declarations(
|
||||
|
||||
Reference in New Issue
Block a user