Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd79abf

Browse files
authored
Merge d248bd8 into sapling-pr-archive-rm-openai
2 parents 6058021 + d248bd8 commit cd79abf

File tree

13 files changed

+462
-3
lines changed

13 files changed

+462
-3
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,4 @@ cython_debug/
141141
.ruff_cache/
142142

143143
# PyPI configuration file
144-
.pypirc
144+
.pypirc

docs/assets/images/graph.png

92.8 KB
Loading

docs/visualization.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Agent Visualization
2+
3+
Agent visualization allows you to generate a structured graphical representation of agents and their relationships using **Graphviz**. This is useful for understanding how agents, tools, and handoffs interact within an application.
4+
5+
## Installation
6+
7+
Install the optional `viz` dependency group:
8+
9+
```bash
10+
pip install "openai-agents[viz]"
11+
```
12+
13+
## Generating a Graph
14+
15+
You can generate an agent visualization using the `draw_graph` function. This function creates a directed graph where:
16+
17+
- **Agents** are represented as yellow boxes.
18+
- **Tools** are represented as green ellipses.
19+
- **Handoffs** are directed edges from one agent to another.
20+
21+
### Example Usage
22+
23+
```python
24+
from agents import Agent, function_tool
25+
from agents.extensions.visualization import draw_graph
26+
27+
@function_tool
28+
def get_weather(city: str) -> str:
29+
return f"The weather in {city} is sunny."
30+
31+
spanish_agent = Agent(
32+
name="Spanish agent",
33+
instructions="You only speak Spanish.",
34+
)
35+
36+
english_agent = Agent(
37+
name="English agent",
38+
instructions="You only speak English",
39+
)
40+
41+
triage_agent = Agent(
42+
name="Triage agent",
43+
instructions="Handoff to the appropriate agent based on the language of the request.",
44+
handoffs=[spanish_agent, english_agent],
45+
tools=[get_weather],
46+
)
47+
48+
draw_graph(triage_agent)
49+
```
50+
51+
![Agent Graph](./assets/images/graph.png)
52+
53+
This generates a graph that visually represents the structure of the **triage agent** and its connections to sub-agents and tools.
54+
55+
56+
## Understanding the Visualization
57+
58+
The generated graph includes:
59+
60+
- A **start node** (`__start__`) indicating the entry point.
61+
- Agents represented as **rectangles** with yellow fill.
62+
- Tools represented as **ellipses** with green fill.
63+
- Directed edges indicating interactions:
64+
- **Solid arrows** for agent-to-agent handoffs.
65+
- **Dotted arrows** for tool invocations.
66+
- An **end node** (`__end__`) indicating where execution terminates.
67+
68+
## Customizing the Graph
69+
70+
### Showing the Graph
71+
By default, `draw_graph` displays the graph inline. To show the graph in a separate window, write the following:
72+
73+
```python
74+
draw_graph(triage_agent).view()
75+
```
76+
77+
### Saving the Graph
78+
By default, `draw_graph` displays the graph inline. To save it as a file, specify a filename:
79+
80+
```python
81+
draw_graph(triage_agent, filename="agent_graph.png")
82+
```
83+
84+
This will generate `agent_graph.png` in the working directory.
85+
86+

examples/mcp/filesystem_example/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MCP Filesystem Example
22

3-
This example uses the [fileystem MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem), running locally via `npx`.
3+
This example uses the [filesystem MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem), running locally via `npx`.
44

55
Run it via:
66

examples/mcp/git_example/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# MCP Git Example
2+
3+
This example uses the [git MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/git), running locally via `uvx`.
4+
5+
Run it via:
6+
7+
```
8+
uv run python examples/mcp/git_example/main.py
9+
```
10+
11+
## Details
12+
13+
The example uses the `MCPServerStdio` class from `agents`, with the command:
14+
15+
```bash
16+
uvx mcp-server-git
17+
```
18+
Prior to running the agent, the user is prompted to provide a local directory path to their git repo. Using that, the Agent can invoke Git MCP tools like `git_log` to inspect the git commit log.
19+
20+
Under the hood:
21+
22+
1. The server is spun up in a subprocess, and exposes a bunch of tools like `git_log()`
23+
2. We add the server instance to the Agent via `mcp_agents`.
24+
3. Each time the agent runs, we call out to the MCP server to fetch the list of tools via `server.list_tools()`. The result is cached.
25+
4. If the LLM chooses to use an MCP tool, we call the MCP server to run the tool via `server.run_tool()`.

examples/mcp/git_example/main.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import asyncio
2+
import shutil
3+
4+
from agents import Agent, Runner, trace
5+
from agents.mcp import MCPServer, MCPServerStdio
6+
7+
8+
async def run(mcp_server: MCPServer, directory_path: str):
9+
agent = Agent(
10+
name="Assistant",
11+
instructions=f"Answer questions about the git repository at {directory_path}, use that for repo_path",
12+
mcp_servers=[mcp_server],
13+
)
14+
15+
message = "Who's the most frequent contributor?"
16+
print("\n" + "-" * 40)
17+
print(f"Running: {message}")
18+
result = await Runner.run(starting_agent=agent, input=message)
19+
print(result.final_output)
20+
21+
message = "Summarize the last change in the repository."
22+
print("\n" + "-" * 40)
23+
print(f"Running: {message}")
24+
result = await Runner.run(starting_agent=agent, input=message)
25+
print(result.final_output)
26+
27+
28+
async def main():
29+
# Ask the user for the directory path
30+
directory_path = input("Please enter the path to the git repository: ")
31+
32+
async with MCPServerStdio(
33+
params={
34+
"command": "uvx",
35+
"args": [
36+
"mcp-server-git"
37+
]
38+
}
39+
) as server:
40+
with trace(workflow_name="MCP Git Example"):
41+
await run(server, directory_path)
42+
43+
44+
if __name__ == "__main__":
45+
if not shutil.which("uvx"):
46+
raise RuntimeError("uvx is not installed. Please install it with `pip install uvx`.")
47+
48+
asyncio.run(main())

examples/voice/static/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import asyncio
22
import random
33

4+
import numpy as np
5+
46
from agents import Agent, function_tool
57
from agents.extensions.handoff_prompt import prompt_with_handoff_instructions
68
from agents.voice import (
@@ -78,6 +80,9 @@ async def main():
7880
elif event.type == "voice_stream_event_lifecycle":
7981
print(f"Received lifecycle event: {event.event}")
8082

83+
# Add 1 second of silence to the end of the stream to avoid cutting off the last audio.
84+
player.add_audio(np.zeros(24000 * 1, dtype=np.int16))
85+
8186

8287
if __name__ == "__main__":
8388
asyncio.run(main())

examples/voice/static/util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __enter__(self):
6262
return self
6363

6464
def __exit__(self, exc_type, exc_value, traceback):
65+
self.stream.stop() # wait for the stream to finish
6566
self.stream.close()
6667

6768
def add_audio(self, audio_data: npt.NDArray[np.int16]):

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ nav:
3636
- multi_agent.md
3737
- models.md
3838
- config.md
39+
- visualization.md
3940
- Voice agents:
4041
- voice/quickstart.md
4142
- voice/pipeline.md

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "openai-agents"
3-
version = "0.0.6"
3+
version = "0.0.7"
44
description = "OpenAI Agents SDK"
55
readme = "README.md"
66
requires-python = ">=3.9"
@@ -35,6 +35,7 @@ Repository = "https://github.com/openai/openai-agents-python"
3535

3636
[project.optional-dependencies]
3737
voice = ["numpy>=2.2.0, <3; python_version>='3.10'", "websockets>=15.0, <16"]
38+
viz = ["graphviz>=0.17"]
3839

3940
[dependency-groups]
4041
dev = [
@@ -56,7 +57,9 @@ dev = [
5657
"pynput",
5758
"textual",
5859
"websockets",
60+
"graphviz",
5961
]
62+
6063
[tool.uv.workspace]
6164
members = ["agents"]
6265

0 commit comments

Comments
 (0)