Use Stores with OpenAI Agents SDK
In this quickstart, we will be creating a simple agent that can get the top posts on Hacker News.
While OpenAI models can generate text, they need additional tools to perform actions like fetching data from Hacker News. Using Stores, we will add tools for querying the Hacker News API.
Hacker News agent
from agents import Agent, Runner, function_tool
import stores
# Load the Hacker News tool index
index = stores.Index(["silanthro/hackernews"])
# Set up the tools with Agents SDK's function_tool
formatted_tools = [
# OpenAI only supports ^[a-zA-Z0-9_-]{1,64}$
function_tool(name_override=fn.__name__.replace(".", "_"))(fn)
for fn in index.tools
]
# Initialize OpenAI agent
agent = Agent(
name="Hacker News Agent",
model="gpt-4o-mini-2024-07-18",
tools=formatted_tools,
)
# Get the response from the agent. The OpenAI agent will automatically execute
# tool calls and generate a response.
result = Runner.run_sync(agent, "What are the top 10 posts on Hacker News today?")
print(f"Agent output: {result.final_output}")
Agent script walkthrough
1. Load the tools
First, we will load the Hacker News tools from the silanthro/hackernews
tool index.
index = stores.Index(["silanthro/hackernews"])
You can also load a tool index from a public GitHub repository or load your own custom tools from your repository. Learn more about what a tool index is here.
The Hacker News API doesn't require any API key. If a tool requires an API key, you can pass it via the env_var
parameter.
2. Format tools
To set up the tools as required by the Agents SDK, we will wrap our tools with Agents SDK's function_tool
.
When loading tool indexes, Stores includes the module name in the tool name, such as tools.get_top_stories
. Because OpenAI does not accept .
in the tool names, we will also substitute any .
in the tool names with -
.
formatted_tools = [
function_tool(name_override=fn.__name__.replace(".", "_"))(fn)
for fn in index.tools
]
3. Initialize the agent
Remember to add your OpenAI API key (OPENAI_API_KEY
) to your .env
file.
agent = Agent(
name="Hacker News Agent",
model="gpt-4o-mini-2024-07-18",
tools=formatted_tools,
)
4. Run the agent
The OpenAI agent will automatically execute any functions required by the input task and generate a response with the tool call result.
result = Runner.run_sync(agent, "What are the top 10 posts on Hacker News today?")
Next steps
- Learn more about how the Stores package works
- If you have built an agent with Stores, let us know
- If you are interested in building tools for other developers, get started here