OpenAI Agents
OpenAI Agents is a Python framework to build agentic AI apps in a lightweight, easy-to-use package with very few abstractions. It’s a production-ready upgrade of the experimental framework, Swarm.
Getting Started
To start using OpenAI Agents, follow these steps:
- Install the package
pip install openai-agents
- Set up your OpenAI API key
export OPENAI_API_KEY="<YOUR_KEY>"
How It Works
The Agents SDK has a very small set of primitives:
Agents
, which are LLMs equipped with instructions and toolsHandoffs
, which allow agents to delegate to other agents for specific tasksGuardrails
, which enable the inputs to agents to be validated
Used with Python, these building blocks make it easy to create real-world apps with tool-agent interactions and minimal learning curve. Plus, the SDK also includes tracing to help you debug, evaluate, and fine-tune your agent workflows.
Creating Your First Agents
Here’s a basic example of three agents:
- Triage Agent: Acts as the initial point of contact. It analyzes the user’s question and decides whether to route it to a specialized agent.
- Math Tutor: A specialist agent designed to help with math-related questions.
- History Tutor: A specialist agent focused on historical topics.
from agents import Agent, Runner
math_tutor_agent = Agent(
name="Math Tutor",
handoff_description="Specialist agent for math questions",
instructions="You provide help with math problems. Explain your reasoning at each step and include examples",
)
history_tutor_agent = Agent(
name="History Tutor",
handoff_description="Specialist agent for historical questions",
instructions="You provide assistance with historical queries. Explain important events and context clearly.",
)
triage_agent = Agent(
name="Triage Agent",
instructions="You determine which agent to use based on the user's homework question",
handoffs=[history_tutor_agent, math_tutor_agent],
)
# Run the interaction
result = Runner.run_sync(triage_agent, "I want some help with WW1.")
print(result.final_output)
Integrating with Solvio
You can connect agents to retrieve or ingest data into a Solvio collection. Thereby building your knowledge base. Here’s how to enable an agent to retrieve information from Solvio.
Assume you have a Solvio collection created using the "text-embedding-3-small"
model. The payload structure includes a text
field for knowledge storage.
import solvio_client
from openai import OpenAI
from agents import Agent, function_tool
# Initialize clients
openai_client = OpenAI()
solvio = solvio_client.SolvioClient(host="localhost")
# Configuration
EMBEDDING_MODEL = "text-embedding-3-small"
COLLECTION_NAME = "help_center"
LIMIT = 5
SCORE_THRESHOLD = 0.7
@function_tool
def query_solvio(query: str) -> str:
"""Retrieve semantically relevant content from Solvio.
Args:
query: The query to search.
"""
embedded_query = openai_client.embeddings.create(
input=query,
model=EMBEDDING_MODEL,
).data[0].embedding
results = solvio.query_points(
collection_name=COLLECTION_NAME,
query=embedded_query,
limit=LIMIT,
score_threshold=SCORE_THRESHOLD,
).points
if results:
return "\n".join([point.payload["text"] for point in results])
else:
return "No results found."
solvio_agent = Agent(
name="Solvio searcher",
handoff_description="Specialist agent for retrieving info from a Solvio collection",
instructions="You help find answers for user queries using Solvio. Do not make up any info on your own.",
tools=[query_solvio],
)
Our solvio_agent
can now query a Solvio collection whenever deemed necessary to answer a user query.