Member-only story
Write your own Search Agent with Pydantic AI
UPDATE: check out Part 2 of this post to see how to improve the agent further.
The team behind Pydantic just released a new AI agent framework called Pydantic AI. Though this framework is still quite new, it stands out to me as one of the most sensible frameworks for building AI agents.
In this blog post, I will demonstrate how simple it is to create an agent capable of searching the internet to answer questions. The idea is straightforward: since LLMs have a cutoff date and lack knowledge of recent events, we can extend their capabilities by enabling internet searches to provide up-to-date answers.
First, follow the instruction on the main website to install the library. After that, we can start with the basic setup:
from pydantic_ai import Agent
from pydantic_ai.settings import ModelSettings
research_agent = Agent(
"openai:gpt-4o",
model_settings=ModelSettings(max_tokens=1024, temperature=0),
result_type=str,
system_prompt=(
'Be a helpful research agent and do your best to answer the given question, be precise. '
'Use the provided tools to answer the question if needed. '
'If you don\'t know the answer, say "I don\'t know" instead of making things up.'
),
)
Creating an agent is simle: you define an Agent
object, provide a system prompt, and specify the result type. Pydantic AI also allows you to define structured type for an agent's results, which can be useful for use cases such as…