11"""Example usage of SERPEX tool with LlamaIndex."""
22
33import os
4+
45from llama_index .tools .serpex import SerpexToolSpec
56
67# Set your API key (or use environment variable SERPEX_API_KEY)
78os .environ ["SERPEX_API_KEY" ] = "your_api_key_here"
89
10+
911def basic_search_example ():
1012 """Basic search example."""
1113 print ("=" * 60 )
1214 print ("Basic Search Example" )
1315 print ("=" * 60 )
14-
16+
1517 # Initialize tool
1618 tool = SerpexToolSpec ()
17-
19+
1820 # Perform search
19- results = tool .search ("latest developments in artificial intelligence" , num_results = 5 )
20-
21+ results = tool .search (
22+ "latest developments in artificial intelligence" , num_results = 5
23+ )
24+
2125 for doc in results :
2226 print (doc .text )
2327 print ()
@@ -28,17 +32,15 @@ def location_search_example():
2832 print ("=" * 60 )
2933 print ("Location-Based Search Example" )
3034 print ("=" * 60 )
31-
35+
3236 # Initialize tool
3337 tool = SerpexToolSpec ()
34-
38+
3539 # Search with location
3640 results = tool .search_with_location (
37- query = "best Italian restaurants" ,
38- location = "San Francisco, CA" ,
39- num_results = 5
41+ query = "best Italian restaurants" , location = "San Francisco, CA" , num_results = 5
4042 )
41-
43+
4244 for doc in results :
4345 print (doc .text )
4446 print ()
@@ -49,18 +51,18 @@ def international_search_example():
4951 print ("=" * 60 )
5052 print ("International Search Example" )
5153 print ("=" * 60 )
52-
54+
5355 # Initialize tool
5456 tool = SerpexToolSpec ()
55-
57+
5658 # Search with country and language
5759 results = tool .search (
5860 query = "noticias de tecnología" ,
5961 num_results = 5 ,
6062 gl = "es" , # Spain
61- hl = "es" # Spanish
63+ hl = "es" , # Spanish
6264 )
63-
65+
6466 for doc in results :
6567 print (doc .text )
6668 print ()
@@ -71,29 +73,27 @@ def agent_example():
7173 print ("=" * 60 )
7274 print ("Agent Example" )
7375 print ("=" * 60 )
74-
76+
7577 try :
7678 from llama_index .agent .openai import OpenAIAgent
7779 from llama_index .llms .openai import OpenAI
78-
80+
7981 # Initialize SERPEX tool
8082 serpex_tool = SerpexToolSpec ()
81-
83+
8284 # Create agent
8385 llm = OpenAI (model = "gpt-4" )
8486 agent = OpenAIAgent .from_tools (
85- serpex_tool .to_tool_list (),
86- llm = llm ,
87- verbose = True
87+ serpex_tool .to_tool_list (), llm = llm , verbose = True
8888 )
89-
89+
9090 # Ask question that requires web search
9191 response = agent .chat (
9292 "What are the latest features announced for LlamaIndex? "
9393 "Search the web for recent news."
9494 )
9595 print (response )
96-
96+
9797 except ImportError :
9898 print ("OpenAI dependencies not installed. Install with:" )
9999 print ("pip install llama-index-agent-openai llama-index-llms-openai" )
@@ -107,7 +107,7 @@ def main():
107107 print ("Please set SERPEX_API_KEY environment variable" )
108108 print ("Get your API key at: https://serpex.io/dashboard" )
109109 return
110-
110+
111111 # Run examples
112112 basic_search_example ()
113113 location_search_example ()
0 commit comments