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 theresponse_format
parameter. This ensures the modelβs output matches your expected structureγ
Lets create a simple agent that returns a Joke with Structured Response
- Model Initialization:
- We create a chat agent with a default model
- The system message sets the assistantβs behavior
- 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
- We define a JokeResponse class using Pydanticβs
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:- Import required libraries:
- Tool Integration:
- We import and initialize
MathToolkit
andSearchToolkit
to give the model calculation and search capabilities - These tools are passed to the
ChatAgent
during initialization
- We import and initialize
- 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
- We define a
- 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:- Import libraries:
- Model-Agnostic Approach:
- We define our data structure using Pydantic models
- 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
- The
- 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
- π« Creating Your First CAMEL Agent free Colab
- Graph RAG Cookbook free Colab
- π§ββοΈ Create A Hackathon Judge Committee with Workforce free Colab
- π₯ 3 ways to ingest data from websites with Firecrawl & CAMEL free Colab
- π¦₯ Agentic SFT Data Generation with CAMEL and Mistral Models, Fine-Tuned with Unsloth free Colab