Skip to content

Commit 4036c75

Browse files
refactor(tests): improve formatting and structure in SERPEX integration tests
1 parent ca39316 commit 4036c75

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

llama-index-integrations/tools/llama-index-tools-serpex/examples/serpex_example.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
"""Example usage of SERPEX tool with LlamaIndex."""
22

33
import os
4+
45
from llama_index.tools.serpex import SerpexToolSpec
56

67
# Set your API key (or use environment variable SERPEX_API_KEY)
78
os.environ["SERPEX_API_KEY"] = "your_api_key_here"
89

10+
911
def 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()

llama-index-integrations/tools/llama-index-tools-serpex/test_local.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""Local test script for SERPEX tool integration."""
33

44
import os
5+
56
from llama_index.tools.serpex import SerpexToolSpec
67

78
# Test 1: Check initialization
@@ -18,27 +19,28 @@
1819
else:
1920
tool = SerpexToolSpec(api_key=api_key)
2021
print("✅ Tool initialized with real API key!")
21-
22+
2223
# Test 2: Basic search
2324
print("\nTest 2: Testing basic search...")
2425
results = tool.search("LlamaIndex tutorial", num_results=3)
2526
print(f"✅ Search returned {len(results)} results (as Document objects):")
2627
for i, result in enumerate(results, 1):
2728
print(f"\nResult {i}:")
2829
print(result.text[:500]) # Print first 500 chars
29-
30+
3031
# Test 3: Check tool list conversion
31-
print("\n" + "="*60)
32+
print("\n" + "=" * 60)
3233
print("Test 4: Testing tool list conversion...")
3334
tool_list = tool.to_tool_list()
3435
print(f"✅ Tool list has {len(tool_list)} tools:")
3536
for t in tool_list:
3637
print(f" - {t.metadata.name}: {t.metadata.description[:60]}...")
37-
38-
print("\n" + "="*60)
38+
39+
print("\n" + "=" * 60)
3940
print("🎉 All tests passed! SERPEX integration is working!")
40-
41+
4142
except Exception as e:
4243
print(f"❌ Test failed: {e}")
4344
import traceback
45+
4446
traceback.print_exc()

0 commit comments

Comments
 (0)