pydantic-ai icon indicating copy to clipboard operation
pydantic-ai copied to clipboard

Add multi-agent example

Open Luca-Blight opened this issue 1 year ago • 4 comments

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.

Luca-Blight avatar Dec 02 '24 19:12 Luca-Blight

@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

jacobweiss2305 avatar Dec 03 '24 22:12 jacobweiss2305

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

HamzaFarhan avatar Dec 04 '24 02:12 HamzaFarhan

Any progress on this by chance?

Luca-Blight avatar Dec 09 '24 15:12 Luca-Blight

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.

saschacontes avatar Dec 12 '24 06:12 saschacontes

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

sidsaha-ai avatar Dec 19 '24 19:12 sidsaha-ai

Thanks everyone for your patience, I've proposed #541 which I hope documents multi-agent applications pretty well.

Full support for graphs is coming too, see #528 and #539.

I'd love your feedback, particularly on #541 at this time.

samuelcolvin avatar Dec 24 '24 20:12 samuelcolvin