Skip to main content
You can also check this cookbook in colab here
CAMEL HomepageJoin Discord
Star us on GitHub, join our Discord, or follow us on X
This notebook demonstrates how to use CAMEL agents to generate structured outputs from language models. You’ll learn to create AI agents that produce consistent, well-formatted responses that can be directly used in your applications. In this notebook, you’ll explore:
  • CAMEL: A powerful multi-agent framework that enables Retrieval-Augmented Generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.
  • Structured Outputs: How to define and enforce specific response schemas using Pydantic models, ensuring consistent and reliable model outputs.
  • Tool Integration: Techniques for combining structured responses with CAMEL’s tool system to create powerful, interactive AI applications.
  • Cross-Model Compatibility: Strategies for achieving structured outputs even with models that don’t natively support function calling or structured responses.

📦 Installation

First, install the CAMEL package with all its dependencies:

🔑 Setting Up API Keys

You’ll need to set up your API keys for OpenAI. This ensures that the tools can interact with external services securely.

1. Basic Structured Response

The simplest way to get structured responses is by defining a Pydantic model and using it as the response_format parameter. This ensures the model’s output matches your expected structure。 Lets create a simple agent that returns a Joke with Structured Response
  1. Model Initialization:
    • We create a chat agent with a default model
    • The system message sets the assistant’s behavior
  1. Response Structure:
    • We define a JokeResponse class using Pydantic’s BaseModel
    • Each field has a type hint and a description
    • The model uses these descriptions to generate appropriate content

2. Structured Response with Tools

In this section, we’ll demonstrate how to combine structured responses with CAMEL’s tool system. This allows the model to perform calculations and searches while maintaining a structured output format. Let’s create an example where we ask the model to perform a calculation and return the result in a structured format:
  1. Import required libraries:
  1. Tool Integration:
    • We import and initialize MathToolkit and SearchToolkit to give the model calculation and search capabilities
    • These tools are passed to the ChatAgent during initialization
  1. Structured Response:
    • We define a CalculationResult Pydantic model with three fields
    • The model uses available tools to perform calculations while maintaining the specified output structure
  1. Execution Flow:
    • The model first uses search tools to find the founding year of the University of Oxford
    • It then performs the age calculation using math tools
    • Finally, it formats the response according to our CalculationResult schema

3. Structured Response with Non-Native Models

Some models don’t natively support structured output formats. In this section, we’ll show how to achieve structured responses through prompt engineering using GPT 3.5 TURBO. This approach is particularly useful with open-source or custom models. Let’s create a recipe generator that returns structured data, demonstrating how to work with models that don’t support native structured output:
  1. Import libraries:
  1. Model-Agnostic Approach:
    • We define our data structure using Pydantic models
  1. Define agent and recipe generator function
    • The generate_recipe function manually constructs a prompt that asks for JSON
    • We handle the response parsing and validation manually
4.1. Generate and display a recipe
4.2. Alternative approach
  • Using response_format with the default model
  • This shows how it would work with a model that supports structured output**

🌟 Highlights

Conclusion

This notebook has guided you through the powerful capabilities of structured responses in CAMEL, from basic implementations to advanced use cases. By leveraging Pydantic models and CAMEL’s flexible architecture, you can create robust, type-safe interactions with language models.

Key Highlights

  • Type-Safe Outputs: Ensure consistent data structures with Pydantic models
  • Flexible Integration: Works with various model types, including those without native structured output support
  • Tool Compatibility: Seamlessly combine structured responses with CAMEL’s tool system

Key Tools Utilized

  • CAMEL: A powerful multi-agent framework that enables Retrieval-Augmented Generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.
  • Pydantic: Provides data validation and settings management using Python type annotations.
  • Structured Outputs: Enforce specific response formats for reliable data processing.

Next Steps

This comprehensive setup allows you to adapt and expand the example for various scenarios, including:
  • Building data processing pipelines
  • Creating structured APIs with LLMs
  • Developing complex multi-agent systems
  • Implementing data validation and transformation workflows
That’s everything: Got questions about 🐫 CAMEL-AI? Join us on Discord! Whether you want to share feedback, explore the latest in multi-agent systems, get support, or connect with others on exciting projects, we’d love to have you in the community! 🤝 Check out some of our other work:
  1. 🐫 Creating Your First CAMEL Agent free Colab
  2. Graph RAG Cookbook free Colab
  3. 🧑‍⚖️ Create A Hackathon Judge Committee with Workforce free Colab
  4. 🔥 3 ways to ingest data from websites with Firecrawl & CAMEL free Colab
  5. 🦥 Agentic SFT Data Generation with CAMEL and Mistral Models, Fine-Tuned with Unsloth free Colab
Thanks from everyone at 🐫 CAMEL-AI
CAMEL HomepageJoin Discord
Star us on GitHub, join our Discord, or follow us on X