-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Multi-Agent Collaboration Notebook #1859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2d3192c
multi-agent-cookbook-example
rajpathak-openai 9d46367
Remove main.py and update investment report in Jupyter notebook with …
rajpathak-openai f0f05e1
Refactor multi-agent portfolio collaboration notebook by removing red…
rajpathak-openai 09e0093
Markdown Cell
rajpathak-openai 4263f1d
Merge remote-tracking branch 'origin/main' into agents-sdk-pm-advisor…
rajpathak-openai 0f99a52
Apply suggestions from code review
rajpathak-openai 0d294f5
Update authors.yaml
rajpathak-openai 7f9ebca
Refactor multi-agent portfolio collaboration notebook by removing exe…
rajpathak-openai 9c93271
Remove duplicate function `run_all_specialists_parallel` from investm…
rajpathak-openai 58c9834
Merge branch 'main' into agents-sdk-pm-advisor-cookbook
rajpathak-openai e4f1d95
Refactor file reading functionality by consolidating `read_markdown` …
rajpathak-openai a2c270f
Update multi-agent portfolio collaboration notebook with enhanced int…
rajpathak-openai 8facd7d
Merge branch 'main' into agents-sdk-pm-advisor-cookbook
rajpathak-openai 76b8c98
Update notebook title to reflect focus on financial portfolio analysi…
rajpathak-openai 539bb12
Render Output
rajpathak-openai 0bff720
Merge branch 'main' into agents-sdk-pm-advisor-cookbook
rajpathak-openai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
examples/agents_sdk/multi-agent-portfolio-collaboration/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
env/ | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
|
||
# Installer logs | ||
distutils.log | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
|
||
# Jupyter Notebook checkpoints | ||
.ipynb_checkpoints | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# VS Code | ||
.vscode/ | ||
|
||
# Mac OS | ||
.DS_Store | ||
|
||
# Output and log directories | ||
outputs/ | ||
logs/ | ||
|
||
# Project-specific logs and outputs | ||
*.log | ||
*.jsonl | ||
|
||
# Secret keys and environment variables | ||
.env | ||
.env.* |
1 change: 1 addition & 0 deletions
1
examples/agents_sdk/multi-agent-portfolio-collaboration/investment_agents/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# This file marks the agents directory as a Python package. |
30 changes: 30 additions & 0 deletions
30
examples/agents_sdk/multi-agent-portfolio-collaboration/investment_agents/config.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from dataclasses import dataclass | ||
from investment_agents.fundamental import build_fundamental_agent | ||
from investment_agents.macro import build_macro_agent | ||
from investment_agents.quant import build_quant_agent | ||
from investment_agents.editor import build_editor_agent, build_memo_edit_tool | ||
from investment_agents.pm import build_head_pm_agent, SpecialistRequestInput | ||
import asyncio | ||
|
||
@dataclass | ||
class InvestmentAgentsBundle: | ||
head_pm: object | ||
fundamental: object | ||
macro: object | ||
quant: object | ||
|
||
|
||
def build_investment_agents() -> InvestmentAgentsBundle: | ||
fundamental = build_fundamental_agent() | ||
macro = build_macro_agent() | ||
quant = build_quant_agent() | ||
editor = build_editor_agent() | ||
memo_edit_tool = build_memo_edit_tool(editor) | ||
head_pm = build_head_pm_agent(fundamental, macro, quant, memo_edit_tool) | ||
return InvestmentAgentsBundle( | ||
head_pm=head_pm, | ||
fundamental=fundamental, | ||
macro=macro, | ||
quant=quant, | ||
) | ||
|
40 changes: 40 additions & 0 deletions
40
examples/agents_sdk/multi-agent-portfolio-collaboration/investment_agents/editor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from agents import Agent, ModelSettings, function_tool, Runner, RunContextWrapper | ||
from tools import write_markdown, read_file, list_output_files | ||
from utils import load_prompt, DISCLAIMER | ||
from pydantic import BaseModel | ||
import json | ||
|
||
default_model = "gpt-4.1" | ||
|
||
class MemoEditorInput(BaseModel): | ||
fundamental: str | ||
macro: str | ||
quant: str | ||
pm: str | ||
files: list[str] | ||
|
||
def build_editor_agent(): | ||
tool_retry_instructions = load_prompt("tool_retry_prompt.md") | ||
editor_prompt = load_prompt("editor_base.md") | ||
return Agent( | ||
name="Memo Editor Agent", | ||
instructions=(editor_prompt + DISCLAIMER + tool_retry_instructions), | ||
tools=[write_markdown, read_file, list_output_files], | ||
model=default_model, | ||
model_settings=ModelSettings(temperature=0), | ||
) | ||
|
||
def build_memo_edit_tool(editor): | ||
@function_tool( | ||
name_override="memo_editor", | ||
description_override="Stitch analysis sections into a Markdown memo and save it. This is the ONLY way to generate and save the final investment report. All memos must be finalized through this tool.", | ||
) | ||
async def memo_edit_tool(ctx: RunContextWrapper, input: MemoEditorInput) -> str: | ||
result = await Runner.run( | ||
starting_agent=editor, | ||
input=json.dumps(input.model_dump()), | ||
context=ctx.context, | ||
max_turns=40, | ||
) | ||
return result.final_output | ||
return memo_edit_tool |
28 changes: 28 additions & 0 deletions
28
examples/agents_sdk/multi-agent-portfolio-collaboration/investment_agents/fundamental.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from agents import Agent, WebSearchTool, ModelSettings | ||
from utils import load_prompt, DISCLAIMER, repo_path | ||
from pathlib import Path | ||
|
||
default_model = "gpt-4.1" | ||
default_search_context = "medium" | ||
RECENT_DAYS = 15 | ||
|
||
def build_fundamental_agent(): | ||
tool_retry_instructions = load_prompt("tool_retry_prompt.md") | ||
fundamental_prompt = load_prompt("fundamental_base.md", RECENT_DAYS=RECENT_DAYS) | ||
# Set up the Yahoo Finance MCP server | ||
from agents.mcp import MCPServerStdio | ||
server_path = str(repo_path("mcp/yahoo_finance_server.py")) | ||
yahoo_mcp_server = MCPServerStdio( | ||
params={"command": "python", "args": [server_path]}, | ||
client_session_timeout_seconds=300, | ||
cache_tools_list=True, | ||
) | ||
|
||
return Agent( | ||
name="Fundamental Analysis Agent", | ||
instructions=(fundamental_prompt + DISCLAIMER + tool_retry_instructions), | ||
mcp_servers=[yahoo_mcp_server], | ||
tools=[WebSearchTool(search_context_size=default_search_context)], | ||
model=default_model, | ||
model_settings=ModelSettings(parallel_tool_calls=True, temperature=0), | ||
) |
18 changes: 18 additions & 0 deletions
18
examples/agents_sdk/multi-agent-portfolio-collaboration/investment_agents/macro.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from agents import Agent, WebSearchTool, ModelSettings | ||
from tools import get_fred_series | ||
from utils import load_prompt, DISCLAIMER | ||
|
||
default_model = "gpt-4.1" | ||
default_search_context = "medium" | ||
RECENT_DAYS = 45 | ||
|
||
def build_macro_agent(): | ||
tool_retry_instructions = load_prompt("tool_retry_prompt.md") | ||
macro_prompt = load_prompt("macro_base.md", RECENT_DAYS=RECENT_DAYS) | ||
return Agent( | ||
name="Macro Analysis Agent", | ||
instructions=(macro_prompt + DISCLAIMER + tool_retry_instructions), | ||
tools=[WebSearchTool(search_context_size=default_search_context), get_fred_series], | ||
model=default_model, | ||
model_settings=ModelSettings(parallel_tool_calls=True, temperature=0), | ||
) |
69 changes: 69 additions & 0 deletions
69
examples/agents_sdk/multi-agent-portfolio-collaboration/investment_agents/pm.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from agents import Agent, ModelSettings, function_tool, Runner | ||
from utils import load_prompt, DISCLAIMER | ||
from dataclasses import dataclass | ||
from pydantic import BaseModel | ||
import json | ||
import asyncio | ||
|
||
|
||
class SpecialistRequestInput(BaseModel): | ||
section: str # e.g., 'fundamental', 'macro', 'quant', or 'pm' | ||
user_question: str | ||
guidance: str | ||
|
||
# Core async functions for each specialist | ||
async def specialist_analysis_func(agent, input: SpecialistRequestInput): | ||
result = await Runner.run( | ||
starting_agent=agent, | ||
input=json.dumps(input.model_dump()), | ||
max_turns=75, | ||
) | ||
return result.final_output | ||
|
||
async def run_all_specialists_parallel( | ||
fundamental, macro, quant, | ||
fundamental_input: SpecialistRequestInput, | ||
macro_input: SpecialistRequestInput, | ||
quant_input: SpecialistRequestInput | ||
): | ||
results = await asyncio.gather( | ||
specialist_analysis_func(fundamental, fundamental_input), | ||
specialist_analysis_func(macro, macro_input), | ||
specialist_analysis_func(quant, quant_input) | ||
) | ||
return { | ||
"fundamental": results[0], | ||
"macro": results[1], | ||
"quant": results[2] | ||
} | ||
|
||
def build_head_pm_agent(fundamental, macro, quant, memo_edit_tool): | ||
def make_agent_tool(agent, name, description): | ||
@function_tool(name_override=name, description_override=description) | ||
async def agent_tool(input: SpecialistRequestInput): | ||
return await specialist_analysis_func(agent, input) | ||
return agent_tool | ||
fundamental_tool = make_agent_tool(fundamental, "fundamental_analysis", "Generate the Fundamental Analysis section.") | ||
macro_tool = make_agent_tool(macro, "macro_analysis", "Generate the Macro Environment section.") | ||
quant_tool = make_agent_tool(quant, "quantitative_analysis", "Generate the Quantitative Analysis section.") | ||
|
||
@function_tool(name_override="run_all_specialists_parallel", description_override="Run all three specialist analyses (fundamental, macro, quant) in parallel and return their results as a dict.") | ||
async def run_all_specialists_tool(fundamental_input: SpecialistRequestInput, macro_input: SpecialistRequestInput, quant_input: SpecialistRequestInput): | ||
return await run_all_specialists_parallel( | ||
fundamental, macro, quant, | ||
fundamental_input, macro_input, quant_input | ||
) | ||
|
||
return Agent( | ||
name="Head Portfolio Manager Agent", | ||
instructions=( | ||
load_prompt("pm_base.md") + DISCLAIMER | ||
rajpathak-openai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
model="gpt-4.1", | ||
#Reasoning model | ||
#model="o4-mini", | ||
rajpathak-openai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tools=[fundamental_tool, macro_tool, quant_tool, memo_edit_tool, run_all_specialists_tool], | ||
# Settings for a reasoning model | ||
#model_settings=ModelSettings(parallel_tool_calls=True, reasoning={"summary": "auto", "effort": "high"}, tool_choice="auto") | ||
model_settings=ModelSettings(parallel_tool_calls=True, tool_choice="auto", temperature=0) | ||
) |
27 changes: 27 additions & 0 deletions
27
examples/agents_sdk/multi-agent-portfolio-collaboration/investment_agents/quant.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from agents import Agent, ModelSettings | ||
from tools import run_code_interpreter, get_fred_series, read_file, list_output_files | ||
from utils import load_prompt, DISCLAIMER, repo_path | ||
from pathlib import Path | ||
|
||
default_model = "gpt-4.1" | ||
|
||
def build_quant_agent(): | ||
tool_retry_instructions = load_prompt("tool_retry_prompt.md") | ||
quant_prompt = load_prompt("quant_base.md") | ||
# Set up the Yahoo Finance MCP server | ||
from agents.mcp import MCPServerStdio | ||
server_path = str(repo_path("mcp/yahoo_finance_server.py")) | ||
yahoo_mcp_server = MCPServerStdio( | ||
params={"command": "python", "args": [server_path]}, | ||
client_session_timeout_seconds=300, | ||
cache_tools_list=True, | ||
) | ||
|
||
return Agent( | ||
name="Quantitative Analysis Agent", | ||
instructions=(quant_prompt + DISCLAIMER + tool_retry_instructions), | ||
mcp_servers=[yahoo_mcp_server], | ||
tools=[run_code_interpreter, get_fred_series, read_file, list_output_files], | ||
model=default_model, | ||
model_settings=ModelSettings(parallel_tool_calls=True, temperature=0), | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.