Building a Multi-Agent System with OpenAI Agents SDK - Part 1

Building a Multi-Agent System with OpenAI Agents SDK - Part 1

OpenAI recently released their Agents SDK, a lightweight yet powerful framework for building multi-agent workflows. Unlike the experimental OpenAI Swarm released last year, this SDK is production-ready and offers one of the most straightforward implementations of multi-agent orchestration currently available.

In this tutorial, we'll build a Restaurant Customer Support system using multiple specialized agents that can handle different types of customer queries automatically.

What is OpenAI Agent SDK?

The OpenAI Agent SDK provides a framework for creating multi-agent systems where specialized agents handle specific tasks. The core concepts include:

  • Agents: The fundamental building blocks powered by language models and configured with specific instructions and tools
  • Handoffs: The ability for agents to delegate tasks to other specialized agents
  • Guardrails: Concurrent checks and validations that ensure interactions adhere to predefined criteria
  • Tools: Functions or methods that agents can use to retrieve or manipulate data

Building a Restaurant Support System

Let's create a customer support system for a restaurant that can:

  • Check order status
  • Answer FAQs
  • Handle complaints
  • Manage table reservations

Setting Up the Environment

First, install the required package - that is all you need:

pip install openai-agents        

Creating Our Agents

The Classifier (Triage) Agent

This agent serves as the entry point, determining the intent of customer queries and routing them to specialized agents.

The handoffs parameter is the key here - it takes a list of specialised agents as arguments.

classifier_agent = Agent(
    name="User Interface Agent",
    model="gpt-4o-mini",
    instructions="Handoff to appropriate agent based on user query",
    handoffs=[order_agent,faq_agent,complaint_agent,reservation_agent]
)        

The rest of agents look like below. Instead of handoffs we pass the tool/method the agent can use to process the task/ user query.

For example - in the case of order_agent, I have oversimplified the check_order_status tool that it uses to check the status of an order.

In a real use case, this function could be making an API call, or fetching the order status from a database.

We use @function_tool decorator to denote a method as a tool for an agent. This function can be used with an agent. It just needs be passed as an argument to the tools parameter in the Agent class.

@function_tool
def check_order_status(order_id: str):
    """Check the status of an order with the given order ID."""
    order_statuses = {
        "12345": "Your order 12345 is being prepared and will be delivered in 20 minutes.",
        "67890": "Your order 67890 has been dispatched and will arrive in 10 minutes.",
        "11121": "Your order 11121 is still being processed. Please wait a little longer."
    }
    return order_statuses.get(order_id, "Order ID not found. Please check and try again.")        
order_agent = Agent(
    name="OrderAgent",
    instructions="Help customers with their order status. If they provide an order ID, fetch the status.",
    tools=[check_order_status]
)        

FAQAgent, ComplaintAgent and ReservationAgent are also setup as above with their own relevant functions.

Running the Multi-Agent System

To create an interactive chat interface, the most important entity is to establish context or conversation history. We can simple add “response.to_input_list()” which has the context from previous query added.

async def chat():
    print("Welcome to the Restaurant Customer Support chat! Type 'exit' to end the chat.")
    response = ""
    while True:
        user_input = input(Fore.GREEN + "You: " + Style.RESET_ALL)
        if user_input.lower() == "exit":
            print(Fore.RED + "Goodbye!" + Style.RESET_ALL)
            break

        if response:
          input_with_context = response.to_input_list() + [
          {"role": "user", "content": user_input}
          ]
        else:
          input_with_context = [{"role": "user", "content": user_input}]
        response = await Runner.run(classifier_agent, input=input_with_context)
        print(Fore.BLUE + f"Support Agent: {response.final_output}" + Style.RESET_ALL)



if __name__ == "__main__":
    asyncio.run(chat())        

This makes it a very efficient chat interface.

Testing the System

Let's test our multi-agent system with various queries:

You will see that the agent can easily identify the intents and route them to specialised agents for processing. All agents are easily able to extract relevant arguments for their tools and execute the functions.

Full code below...


Benefits of This Approach

  1. Modularity: Each agent handles a specific domain, making code organization cleaner
  2. Specialized Knowledge: Agents can be optimized for particular tasks
  3. Maintainability: Add or modify agents without affecting the entire system
  4. Contextual Understanding: The system maintains conversation context between interactions

Conclusion

The OpenAI Agent SDK provides a streamlined way to build powerful multi-agent systems. In this tutorial, we built a restaurant customer support system using specialized agents that can handle different types of queries. This approach provides a clean, modular architecture that can be extended to handle more complex use cases.

In a future tutorial, we'll explore more advanced features like guardrails and tracing to enhance the robustness and debuggability of our multi-agent system.

要查看或添加评论,请登录

Zahiruddin Tavargere的更多文章

社区洞察