-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: enable tool calling for Gemini models #819
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
Changes from 1 commit
5e8bd01
d469540
473c832
a45618f
e3d4741
bf7f37f
a66d023
45ec376
0f7dfc9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -731,8 +731,21 @@ def _build_messages(self, prompt, temperature=0.2, output_json=None, output_pyda | |
| if self.use_system_prompt: | ||
| system_prompt = f"""{self.backstory}\n | ||
| Your Role: {self.role}\n | ||
| Your Goal: {self.goal} | ||
| """ | ||
| Your Goal: {self.goal}""" | ||
|
|
||
| # Add tool usage instructions if tools are available | ||
| if self.tools: | ||
| tool_names = [] | ||
| for tool in self.tools: | ||
| if callable(tool) and hasattr(tool, '__name__'): | ||
| tool_names.append(tool.__name__) | ||
| elif isinstance(tool, dict) and 'function' in tool and 'name' in tool['function']: | ||
| tool_names.append(tool['function']['name']) | ||
| elif isinstance(tool, str): | ||
| tool_names.append(tool) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic for extracting tool names is not as comprehensive as in |
||
|
|
||
| if tool_names: | ||
| system_prompt += f"\n\nYou have access to the following tools: {', '.join(tool_names)}. Use these tools when appropriate to help complete your tasks. Always use tools when they can help provide accurate information or perform actions." | ||
|
|
||
| # Use openai_client's build_messages method if available | ||
| if self._openai_client is not None: | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,115 @@ | ||||||||||||||||||||
| # Tool Call Fix Documentation | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ## Issue | ||||||||||||||||||||
| Agents using Gemini models (`gemini/gemini-1.5-flash-8b`) were not calling provided tools, instead responding with "I do not have access to the internet" when tasked with searching. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ## Root Cause | ||||||||||||||||||||
| The Gemini model through LiteLLM was not being properly instructed to use the available tools. The system prompt didn't mention the tools, and the tool_choice parameter wasn't being set. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ## Fix Applied | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ### 1. Enhanced System Prompt (agent.py) | ||||||||||||||||||||
| When tools are available, the agent's system prompt now explicitly mentions them: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ```python | ||||||||||||||||||||
| # In _build_messages method | ||||||||||||||||||||
| if self.tools: | ||||||||||||||||||||
| tool_names = [] | ||||||||||||||||||||
| for tool in self.tools: | ||||||||||||||||||||
|
Comment on lines
+26
to
+
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix inconsistent variable usage in tool iteration. The code sets - # Use provided tools or fall back to self.tools
- tools_to_use = tools if tools is not None else self.tools
- if tools_to_use:
- tool_names = []
- for tool in self.tools:
+ # Use provided tools or fall back to self.tools
+ tools_to_use = tools if tools is not None else self.tools
+ if tools_to_use:
+ tool_names = []
+ for tool in tools_to_use:📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| if callable(tool) and hasattr(tool, '__name__'): | ||||||||||||||||||||
| tool_names.append(tool.__name__) | ||||||||||||||||||||
| elif isinstance(tool, dict) and 'function' in tool and 'name' in tool['function']: | ||||||||||||||||||||
| tool_names.append(tool['function']['name']) | ||||||||||||||||||||
| elif isinstance(tool, str): | ||||||||||||||||||||
| tool_names.append(tool) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if tool_names: | ||||||||||||||||||||
| system_prompt += f"\n\nYou have access to the following tools: {', '.join(tool_names)}. Use these tools when appropriate to help complete your tasks. Always use tools when they can help provide accurate information or perform actions." | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ### 2. Tool Choice Parameter (llm.py) | ||||||||||||||||||||
| For Gemini models, we now set `tool_choice='auto'` to encourage tool usage: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ```python | ||||||||||||||||||||
| # In _build_completion_params method | ||||||||||||||||||||
| if 'tools' in params and params['tools'] and 'tool_choice' not in params: | ||||||||||||||||||||
| # For Gemini models, use tool_choice to encourage tool usage | ||||||||||||||||||||
| if self.model.startswith(('gemini', 'gemini/')): | ||||||||||||||||||||
| params['tool_choice'] = 'auto' | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ## Testing the Fix | ||||||||||||||||||||
|
|
||||||||||||||||||||
| To test the fix, use the following code: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ```python | ||||||||||||||||||||
| import asyncio | ||||||||||||||||||||
| from praisonaiagents import Agent, Task, PraisonAIAgents | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # Define a simple tool | ||||||||||||||||||||
| async def search_tool(query: str) -> str: | ||||||||||||||||||||
| """Search for information on the internet""" | ||||||||||||||||||||
| return f"Search results for: {query}" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # Create agent with Gemini model | ||||||||||||||||||||
| agent = Agent( | ||||||||||||||||||||
| name="SearchAgent", | ||||||||||||||||||||
| role="Information Researcher", | ||||||||||||||||||||
| goal="Find accurate information using search tools", | ||||||||||||||||||||
| backstory="Expert at finding and analyzing information", | ||||||||||||||||||||
| tools=[search_tool], | ||||||||||||||||||||
| llm={"model": "gemini/gemini-1.5-flash-8b"} | ||||||||||||||||||||
| ) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # Create task | ||||||||||||||||||||
| task = Task( | ||||||||||||||||||||
| description="Search for information about AI breakthroughs in 2024", | ||||||||||||||||||||
| expected_output="Summary of AI breakthroughs", | ||||||||||||||||||||
| agent=agent | ||||||||||||||||||||
| ) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # Run | ||||||||||||||||||||
| async def test(): | ||||||||||||||||||||
| agents = PraisonAIAgents(agents=[agent], tasks=[task]) | ||||||||||||||||||||
| result = await agents.astart() | ||||||||||||||||||||
| print(result) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| asyncio.run(test()) | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ## Backward Compatibility | ||||||||||||||||||||
| - The fix only adds to existing functionality without modifying core behavior | ||||||||||||||||||||
| - Tools continue to work exactly as before for all other models | ||||||||||||||||||||
| - The system prompt enhancement only occurs when tools are present | ||||||||||||||||||||
| - The tool_choice parameter is only added for Gemini models | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ## Additional Recommendations | ||||||||||||||||||||
|
|
||||||||||||||||||||
| If issues persist with specific models: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| 1. **Explicit Tool Instructions in Task Description**: | ||||||||||||||||||||
| ```python | ||||||||||||||||||||
| task = Task( | ||||||||||||||||||||
| description="Use the tavily_search tool to find information about AI breakthroughs", | ||||||||||||||||||||
| # ... rest of task config | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
|
||||||||||||||||||||
| 2. **Use OpenAI Models for Tool-Heavy Tasks**: | ||||||||||||||||||||
| OpenAI models (gpt-4, gpt-4o) have better native tool calling support. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| 3. **Debug Tool Registration**: | ||||||||||||||||||||
| Enable debug logging to see tool registration: | ||||||||||||||||||||
| ```python | ||||||||||||||||||||
| import logging | ||||||||||||||||||||
| logging.basicConfig(level=logging.DEBUG) | ||||||||||||||||||||
| ``` | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ## Verification Steps | ||||||||||||||||||||
|
|
||||||||||||||||||||
| 1. Check that tools are properly formatted by the agent | ||||||||||||||||||||
| 2. Verify the system prompt includes tool instructions | ||||||||||||||||||||
| 3. Confirm tool_choice is set for Gemini models | ||||||||||||||||||||
| 4. Monitor LLM responses for tool_calls in the response | ||||||||||||||||||||
|
|
||||||||||||||||||||
| The fix ensures that Gemini models are properly instructed to use available tools, resolving the issue where agents would claim they don't have internet access despite having search tools available. | ||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """ | ||
| Test example demonstrating the tool call fix for Gemini models. | ||
|
|
||
| This example shows how agents now properly use tools instead of saying | ||
| "I do not have access to the internet". | ||
| """ | ||
| import logging | ||
| from praisonaiagents import Agent, Task, PraisonAIAgents | ||
|
|
||
| # Enable debug logging to see tool processing | ||
| logging.basicConfig(level=logging.DEBUG) | ||
|
|
||
| # Define a simple search tool (synchronous version) | ||
| def mock_search(query: str) -> str: | ||
| """Search for information on the internet. | ||
|
|
||
| Args: | ||
| query: The search query string | ||
|
|
||
| Returns: | ||
| Mock search results for the query | ||
| """ | ||
| return f"Mock search results for '{query}': Found 10 relevant articles about {query}. Top result: Latest developments and breakthroughs in this field..." | ||
|
|
||
| # Create agent with Gemini model and the search tool | ||
| search_agent = Agent( | ||
| name="SearchAgent", | ||
| role="Information Researcher", | ||
| goal="Find accurate information using the mock_search tool", | ||
| backstory="Expert researcher skilled at finding and analyzing information from various sources", | ||
| tools=[mock_search], | ||
| llm={"model": "gemini/gemini-1.5-flash-8b"}, | ||
| verbose=True | ||
| ) | ||
|
|
||
| # Create a task that should trigger tool usage | ||
| search_task = Task( | ||
| name="search_ai_breakthroughs", | ||
| description="Search for information about latest AI breakthroughs in 2024", | ||
| expected_output="A comprehensive summary of AI breakthroughs found through search", | ||
| agent=search_agent | ||
| ) | ||
|
|
||
| def test_tool_usage(): | ||
| """Test that the agent uses tools instead of saying it has no internet access.""" | ||
| print("=" * 60) | ||
| print("Testing Tool Usage with Gemini Model") | ||
| print("=" * 60) | ||
|
|
||
| # Create workflow | ||
| workflow = PraisonAIAgents( | ||
| agents=[search_agent], | ||
| tasks=[search_task], | ||
| verbose=True | ||
| ) | ||
|
|
||
| # Execute the workflow | ||
| print("\nExecuting task...") | ||
| result = workflow.start() | ||
|
|
||
| # Check the result | ||
| print("\n" + "=" * 60) | ||
| print("RESULT:") | ||
| print("=" * 60) | ||
|
|
||
| if isinstance(result, dict) and 'task_results' in result: | ||
| task_result = result['task_results'][0] | ||
| print(f"Task Output: {task_result}") | ||
|
|
||
| # Check if the agent used the tool or claimed no access | ||
| if "do not have access" in str(task_result).lower(): | ||
| print("\n❌ FAILED: Agent still claims no internet access") | ||
| elif "mock search results" in str(task_result).lower(): | ||
| print("\n✅ SUCCESS: Agent used the search tool!") | ||
| else: | ||
| print("\n⚠️ UNCLEAR: Check if agent used the tool properly") | ||
| else: | ||
| print(f"Result: {result}") | ||
|
|
||
| print("\n" + "=" * 60) | ||
| print("Test Complete") | ||
| print("=" * 60) | ||
|
|
||
| if __name__ == "__main__": | ||
| # Run the test | ||
| test_tool_usage() | ||
|
|
||
| print("\n\nNOTE: With the fix applied, the agent should use the mock_search tool") | ||
| print("instead of saying 'I do not have access to the internet'.") |
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.
To prevent a
TypeErroriftool['function']is not a dictionary, add a check to ensure it is a dictionary before attempting to access the'name'key.