|
24 | 24 | from ..tools.base_tool import BaseTool |
25 | 25 |
|
26 | 26 |
|
| 27 | +def _find_tool_with_function_declarations( |
| 28 | + llm_request: LlmRequest, |
| 29 | +) -> Optional[types.Tool]: |
| 30 | + """Find an existing Tool with function_declarations in the LlmRequest.""" |
| 31 | + # TODO: add individual tool with declaration and merge in google_llm.py |
| 32 | + if not llm_request.config or not llm_request.config.tools: |
| 33 | + return None |
| 34 | + |
| 35 | + return next( |
| 36 | + ( |
| 37 | + tool |
| 38 | + for tool in llm_request.config.tools |
| 39 | + if isinstance(tool, types.Tool) and tool.function_declarations |
| 40 | + ), |
| 41 | + None, |
| 42 | + ) |
| 43 | + |
| 44 | + |
27 | 45 | class LlmRequest(BaseModel): |
28 | 46 | """LLM request class that allows passing in tools, output schema and system |
29 | 47 |
|
@@ -81,15 +99,26 @@ def append_tools(self, tools: list[BaseTool]) -> None: |
81 | 99 | return |
82 | 100 | declarations = [] |
83 | 101 | for tool in tools: |
84 | | - if isinstance(tool, BaseTool): |
85 | | - declaration = tool._get_declaration() |
86 | | - else: |
87 | | - declaration = tool.get_declaration() |
| 102 | + declaration = tool._get_declaration() |
88 | 103 | if declaration: |
89 | 104 | declarations.append(declaration) |
90 | 105 | self.tools_dict[tool.name] = tool |
91 | 106 | if declarations: |
92 | | - self.config.tools.append(types.Tool(function_declarations=declarations)) |
| 107 | + if self.config.tools is None: |
| 108 | + self.config.tools = [] |
| 109 | + |
| 110 | + # Find existing tool with function_declarations and append to it |
| 111 | + if tool_with_function_declarations := _find_tool_with_function_declarations( |
| 112 | + self |
| 113 | + ): |
| 114 | + if tool_with_function_declarations.function_declarations is None: |
| 115 | + tool_with_function_declarations.function_declarations = [] |
| 116 | + tool_with_function_declarations.function_declarations.extend( |
| 117 | + declarations |
| 118 | + ) |
| 119 | + else: |
| 120 | + # No existing tool with function_declarations, create new one |
| 121 | + self.config.tools.append(types.Tool(function_declarations=declarations)) |
93 | 122 |
|
94 | 123 | def set_output_schema(self, base_model: type[BaseModel]) -> None: |
95 | 124 | """Sets the output schema for the request. |
|
0 commit comments