Skip to content

Commit 79f9248

Browse files
feat: Add support for Gemini internal tools (Google Search, Code Execution, URL Context)
- Add parameters to LLM class for Gemini internal tools configuration - Support google_search_retrieval, enable_code_execution, dynamic_retrieval_config - Handle both simplified boolean flags and detailed tool_config format - Add comprehensive documentation and examples - Maintain full backward compatibility Fixes #867 Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
1 parent 6ac3d78 commit 79f9248

8 files changed

Lines changed: 1090 additions & 2 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
Example demonstrating Gemini's internal tools:
3+
- Google Search Grounding for real-time information
4+
- Code Execution for computational tasks
5+
- URL Context for web content analysis
6+
"""
7+
8+
from praisonaiagents import Agent
9+
10+
def main():
11+
# Example 1: Google Search for current information
12+
print("=== Example 1: Google Search Grounding ===")
13+
search_agent = Agent(
14+
instructions="You are a helpful assistant that provides current information.",
15+
llm={
16+
"model": "gemini/gemini-1.5-flash",
17+
"google_search_retrieval": True
18+
}
19+
)
20+
21+
response = search_agent.start("What are the latest developments in quantum computing in 2024?")
22+
print(f"Response: {response}\n")
23+
24+
# Example 2: Code Execution for data analysis
25+
print("=== Example 2: Code Execution ===")
26+
code_agent = Agent(
27+
instructions="You are a data analyst that can write and execute Python code.",
28+
llm={
29+
"model": "gemini/gemini-1.5-flash",
30+
"enable_code_execution": True
31+
}
32+
)
33+
34+
response = code_agent.start("""
35+
Create a Python script that:
36+
1. Generates a list of the first 20 Fibonacci numbers
37+
2. Calculates their sum and average
38+
3. Creates a simple ASCII bar chart showing their growth
39+
""")
40+
print(f"Response: {response}\n")
41+
42+
# Example 3: Comprehensive research with all tools
43+
print("=== Example 3: Combined Internal Tools ===")
44+
research_agent = Agent(
45+
instructions="""You are a comprehensive research assistant that can:
46+
- Search the web for current information
47+
- Analyze web page content
48+
- Write and execute code for data processing""",
49+
llm={
50+
"model": "gemini/gemini-1.5-pro-latest",
51+
"temperature": 0.7,
52+
"tool_config": {
53+
"google_search_retrieval": {
54+
"threshold": 0.8 # Higher threshold for more accurate results
55+
},
56+
"code_execution": {},
57+
"dynamic_retrieval_config": {
58+
"mode": "grounded",
59+
"dynamic_threshold": 0.6
60+
}
61+
}
62+
},
63+
verbose=True # Show detailed execution
64+
)
65+
66+
response = research_agent.start("""
67+
Please help me understand the current state of renewable energy:
68+
1. Search for the latest statistics on global renewable energy adoption
69+
2. If you find any relevant URLs, analyze their content
70+
3. Write Python code to visualize the key statistics you found
71+
""")
72+
print(f"Response: {response}")
73+
74+
if __name__ == "__main__":
75+
main()
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
"""
2+
Different ways to configure Gemini internal tools in PraisonAI
3+
"""
4+
5+
from praisonaiagents import Agent
6+
from praisonaiagents.llm import LLM
7+
8+
# Method 1: Simple boolean flags
9+
print("=== Method 1: Simple Boolean Flags ===")
10+
agent1 = Agent(
11+
instructions="Assistant with search and code capabilities",
12+
llm={
13+
"model": "gemini/gemini-1.5-flash",
14+
"google_search_retrieval": True,
15+
"enable_code_execution": True
16+
}
17+
)
18+
print("✓ Agent created with boolean flags\n")
19+
20+
# Method 2: Detailed configuration with thresholds
21+
print("=== Method 2: Detailed Configuration ===")
22+
agent2 = Agent(
23+
instructions="Research assistant with fine-tuned settings",
24+
llm={
25+
"model": "gemini/gemini-1.5-pro",
26+
"google_search_retrieval": {
27+
"threshold": 0.9 # High confidence threshold
28+
},
29+
"dynamic_retrieval_config": {
30+
"mode": "grounded",
31+
"dynamic_threshold": 0.7
32+
}
33+
}
34+
)
35+
print("✓ Agent created with detailed configuration\n")
36+
37+
# Method 3: Using tool_config parameter
38+
print("=== Method 3: Tool Config Parameter ===")
39+
agent3 = Agent(
40+
instructions="Advanced assistant with all tools",
41+
llm={
42+
"model": "gemini/gemini-1.5-pro-latest",
43+
"tool_config": {
44+
"google_search_retrieval": {
45+
"threshold": 0.8
46+
},
47+
"code_execution": {},
48+
"dynamic_retrieval_config": {
49+
"mode": "grounded",
50+
"dynamic_threshold": 0.6
51+
}
52+
}
53+
}
54+
)
55+
print("✓ Agent created with tool_config\n")
56+
57+
# Method 4: Direct LLM instance with internal tools
58+
print("=== Method 4: Direct LLM Instance ===")
59+
llm = LLM(
60+
model="gemini/gemini-1.5-flash",
61+
google_search_retrieval=True,
62+
enable_code_execution=True,
63+
temperature=0.7,
64+
max_tokens=2000
65+
)
66+
67+
# Use the LLM instance with an agent
68+
agent4 = Agent(
69+
instructions="Assistant using pre-configured LLM",
70+
llm=llm # Pass the LLM instance directly
71+
)
72+
print("✓ Agent created with pre-configured LLM instance\n")
73+
74+
# Method 5: Combining with custom tools
75+
print("=== Method 5: Internal + Custom Tools ===")
76+
77+
def calculate_compound_interest(principal: float, rate: float, time: int) -> float:
78+
"""Calculate compound interest
79+
80+
Args:
81+
principal: Initial amount
82+
rate: Annual interest rate (as decimal)
83+
time: Time period in years
84+
"""
85+
return principal * (1 + rate) ** time
86+
87+
agent5 = Agent(
88+
instructions="Financial assistant with search and custom calculation tools",
89+
llm={
90+
"model": "gemini/gemini-1.5-flash",
91+
"google_search_retrieval": True, # For market research
92+
"enable_code_execution": True # For complex calculations
93+
},
94+
tools=[calculate_compound_interest] # Custom tool
95+
)
96+
print("✓ Agent created with both internal and custom tools\n")
97+
98+
# Method 6: Environment-specific configuration
99+
print("=== Method 6: Environment-Specific Config ===")
100+
import os
101+
102+
# You can set defaults via environment
103+
config = {
104+
"model": os.getenv("GEMINI_MODEL", "gemini/gemini-1.5-flash"),
105+
"temperature": float(os.getenv("GEMINI_TEMPERATURE", "0.7"))
106+
}
107+
108+
# Add internal tools based on environment
109+
if os.getenv("ENABLE_SEARCH", "true").lower() == "true":
110+
config["google_search_retrieval"] = True
111+
112+
if os.getenv("ENABLE_CODE_EXEC", "true").lower() == "true":
113+
config["enable_code_execution"] = True
114+
115+
agent6 = Agent(
116+
instructions="Environment-configured assistant",
117+
llm=config
118+
)
119+
print("✓ Agent created with environment-based configuration\n")
120+
121+
# Example usage demonstrating the tools
122+
print("\n=== Example Usage ===")
123+
print("Testing search capability...")
124+
response = agent1.start("What's the current temperature in Tokyo?")
125+
print(f"Search result preview: {response[:100]}...\n")
126+
127+
print("Testing code execution...")
128+
response = agent1.start("Write and run a Python function to check if 2024 is a leap year")
129+
print(f"Code execution result preview: {response[:100]}...\n")
130+
131+
print("Configuration examples complete!")

0 commit comments

Comments
 (0)