Skip to content
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

Add multi-agent example #120

Open
Luca-Blight opened this issue Dec 2, 2024 · 5 comments
Open

Add multi-agent example #120

Luca-Blight opened this issue Dec 2, 2024 · 5 comments
Labels
documentation Improvements or additions to documentation

Comments

@Luca-Blight
Copy link

Hello,

I'd love to see an example of a multi-agent architecture. Ideally it would have a managing agent communicating with a particular set of specialized agents based on the nature of the question being asked by the user. Each specialized agent would have it's own collection of tools that It could reference, and may have its own model.

Perhaps even the tool itself could be tied to a specific model attached to it. For instance, the specialized agent decides to run a sql tool that generates sql, this tool generates sql using a specialized model, then after the query it goes back to its original model so that it can run a pandas/polars for data transformation.

Flow 1:

Managing agent calls an agent to run a task which is required for another specialized agent to run theirs.
May use "human in the loop"

Example:

User wants to pay for the cheapest flight to Alaska(brrr!), managing agent takes this information and decides to pull in a "data analyst" agent to run the task. The agent analyst queries the cheapest flights and associated information from a database and returns back the info to the managing agent.

The managing agent decides to returns this flight info to the user and ask if the user would like to pay for a ticket("human in the loop"). The user approves, then the prompt goes back to the managing agent who then decides to use the "payment" agent to buy the ticket.

Flow 2:

A managing calls multiple specialized agents to work in parallel.

Example:

A user want to investigate the current market activity of a stock. The managing agent takes that persons question and calls two agents. The first one investigates financial metrics, we'll call this agent the "stock market analyst".

The second agent will be the "sentiment analyst" agent, this agent is responsible for understanding the sentiment derived from social media channels. Once both agents are completed with the task, the information will be synthesized by the managing agent and returned back to the user.

You could even add a "human in the loop" aspect at the end asking the user if they would like to purchase more stock.
Similar to part of Flow 1's example.

Flow 3:

Not quite sure of a good example here, but it would interesting to have a channel of communication between two specialized agents. Perhaps the sentiment analyst or market analyst comes across information that they would like the other to investigate further.

@jacobweiss2305
Copy link

jacobweiss2305 commented Dec 3, 2024

@samuelcolvin is this the right way to do multi-agent orchestration?

# uv pip install pydantic-ai-slim[openai,logfire]'
# uv run main.py
from pydantic_ai import Agent

import logfire
logfire.configure()

agent = Agent(
    "openai:gpt-4o",
    system_prompt="""
    You are a helpful assistant. Use your writer agent to help with a given topic.
    """
)

writer = Agent("openai:gpt-4o", system_prompt="Write about a given topic.")

@agent.tool_plain
def writer_agent(topic: str) -> str:
    """
    Ask writer agent to write a poem about a given topic.
    """
    result = writer.run_sync(f"Write a poem about {topic}.").data
    return result

result = agent.run_sync("Saint bernards")
print(result.data)

It gets stuck here:

(.venv) PS C:\Users\jawei\playground> uv run .\hello.py
Logfire project URL: https://logfire.pydantic.dev/jacobweiss2305/local-test
23:10:18.992 agent run prompt=Saint bernards
23:10:18.994   model request run_step=1
23:10:20.775   handle model response
23:10:20.779     running tools=['writer_agent']
23:10:20.783     agent run prompt=Write a poem about Saint Bernards.
23:10:20.785       model request run_step=1

@HamzaFarhan
Copy link

@jacobweiss2305 I would need to add the agent as a dependency: https://ai.pydantic.dev/dependencies/#agents-as-dependencies-of-other-agents

@sydney-runkle sydney-runkle added the documentation Improvements or additions to documentation label Dec 5, 2024
@Luca-Blight
Copy link
Author

Any progress on this by chance?

@saschacontes
Copy link

Please provide an example. For example a storyteller agent and 3 character agents. The storyteller guides the plot and the character agents contribute/discuss/act dynamically according to the scene of the plot.

@sidsaha-ai
Copy link

sidsaha-ai commented Dec 19, 2024

I wrote this agent that uses two agents as tools. Not sure if this helps.
https://github.com/sidsaha-ai/pydantic_ai_learn/blob/master/code/examples/translation/main.py

happy to document this as an example if the project authors think it’s useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

6 participants