Skip to content

Commit

Permalink
genai: add debug log for _function_utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu007 committed Oct 12, 2024
1 parent c53fc81 commit 920341c
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions libs/genai/langchain_google_genai/_function_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import logging
import os
import traceback
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -191,6 +192,7 @@ def _format_json_schema_to_gapic(schema: Dict[str, Any]) -> Dict[str, Any]:

def _dict_to_gapic_schema(schema: Dict[str, Any]) -> Optional[gapic.Schema]:
logger.debug("_dict_to_gapic_schema\n schema=%s", json.dumps(schema, indent=2))
logger.debug("Stack trace:\n%s", "".join(traceback.format_stack()))
if schema:
dereferenced_schema = dereference_refs(schema)
formatted_schema = _format_json_schema_to_gapic(dereferenced_schema)
Expand Down Expand Up @@ -269,19 +271,26 @@ def _format_to_gapic_function_declaration(
tool: _FunctionDeclarationLike,
) -> gapic.FunctionDeclaration:
if isinstance(tool, BaseTool):
logger.debug("_format_to_gapic_function_declaration BaseTool")
return _format_base_tool_to_function_declaration(tool)
elif isinstance(tool, type) and is_basemodel_subclass_safe(tool):
logger.debug("_format_to_gapic_function_declaration BaseModel")
return _convert_pydantic_to_genai_function(tool) # type: ignore[arg-type]
elif isinstance(tool, dict):
if all(k in tool for k in ("name", "description")) and "parameters" not in tool:
logger.debug("_format_to_gapic_function_declaration dict(no parameters1)")
function = cast(dict, tool)
function["parameters"] = {}
else:
if (
"parameters" in tool and tool["parameters"].get("properties") # type: ignore[index]
):
logger.debug("_format_to_gapic_function_declaration dict(via openai)")
function = convert_to_openai_tool(cast(dict, tool))["function"]
else:
logger.debug(
"_format_to_gapic_function_declaration dict(no parameters2)"
)
function = cast(dict, tool)
function["parameters"] = {}
return _format_dict_to_function_declaration(cast(FunctionDescription, function))
Expand Down Expand Up @@ -353,12 +362,16 @@ def _convert_pydantic_to_genai_function(
def _create_function_declaration(
name: str, description: str, schema: Dict[str, Any]
) -> gapic.FunctionDeclaration:
logger.debug("_convert_pydantic_to_genai_function\n schema=%s", schema)
parameters = _create_function_declaration_parameters(schema)
gapic_parameters = _dict_to_gapic_schema(parameters)
logger.debug(
"_convert_pydantic_to_genai_function\n gapic_parameters=%s", gapic_parameters
)
function_declaration = gapic.FunctionDeclaration(
name=name,
description=description,
parameters=gapic_parameters,
name=name if name else schema.get("title"),
description=description if description else schema.get("description"),
parameters=parameters,
)
return function_declaration

Expand Down

0 comments on commit 920341c

Please sign in to comment.