LlamaIndex
Add a SerpApi FunctionTool to a LlamaIndex agent.
Call a search constructor and add the resulting FunctionTool to a LlamaIndex agent.
Install
pip install "serpapi-search-tools[llamaindex]" llama-index-llms-openaiIf LlamaIndex is already installed in your project, use pip install serpapi-search-tools instead.
The SerpApi extra installs LlamaIndex Core for FunctionTool; the second package is the OpenAI model integration chosen by this example. Replace it when your application uses another LlamaIndex LLM integration.
export SERPAPI_API_KEY="your-serpapi-key"
export OPENAI_API_KEY="your-openai-key"Run it
import asyncio
from typing import Any
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.core.llms import LLMMetadata, MessageRole
from llama_index.llms.openai import OpenAI
from serpapi_search_tools import travel_explore_search, web_search
class OpenAICompatible(OpenAI):
@property
def _tokenizer(self) -> Any | None:
return None
@property
def metadata(self) -> LLMMetadata:
return LLMMetadata(
context_window=400_000,
num_output=-1,
is_chat_model=True,
is_function_calling_model=True,
model_name=self.model,
system_role=MessageRole.SYSTEM,
)
async def main():
agent = FunctionAgent(
llm=OpenAICompatible(model="gpt-5.4-mini", temperature=0),
tools=[
web_search(),
travel_explore_search(),
],
)
result = await agent.run(
"Starting from JFK, find a good city for a one-month remote-work stay."
)
print(result)
asyncio.run(main())Try it
Ask: “Compare three cities for a one-month remote-work stay using current web results.”
Customize the search
travel_search = travel_explore_search(
default_params={"hl": "en", "gl": "us"},
include_examples=False,
)Replace the tool in the agent when travel discovery is the main task.