|
| 1 | +# Agents |
| 2 | + |
| 3 | +## 1. Concept |
| 4 | + |
| 5 | +Agents in CAMEL are autonomous entities capable of performing specific tasks through interaction with language models and other components. Each agent is designed with a particular role and capability, allowing them to work independently or collaboratively to achieve complex goals. |
| 6 | + |
| 7 | +### 1.1. Base Agent Architecture |
| 8 | +All CAMEL agents inherit from the `BaseAgent` abstract class, which defines two core methods: |
| 9 | +- `reset()`: Resets the agent to its initial state |
| 10 | +- `step()`: Performs a single step of the agent's operation |
| 11 | + |
| 12 | +### 1.2. Chat Agent |
| 13 | +The `ChatAgent` is the primary implementation that handles conversations with language models. It supports: |
| 14 | +- System message configuration for role definition |
| 15 | +- Memory management for conversation history |
| 16 | +- Tool/function calling capabilities |
| 17 | +- Response formatting and structured outputs |
| 18 | +- Multiple model backend support with scheduling strategies |
| 19 | +- Async operation support |
| 20 | + |
| 21 | +## 2. Types |
| 22 | + |
| 23 | +### 2.1. `ChatAgent` |
| 24 | +The main agent implementation for handling conversations with language models. Features include: |
| 25 | +- Tool integration and management |
| 26 | +- Memory management with customizable window sizes |
| 27 | +- Output language control |
| 28 | +- Response termination handling |
| 29 | +- Structured output support via Pydantic models |
| 30 | + |
| 31 | +### 2.2. `CriticAgent` |
| 32 | +Specialized agent for evaluating and critiquing responses or solutions. Used in scenarios requiring quality assessment or validation. |
| 33 | + |
| 34 | +### 2.3. `DeductiveReasonerAgent` |
| 35 | +Agent focused on logical reasoning and deduction. Breaks down complex problems into smaller, manageable steps. |
| 36 | + |
| 37 | +### 2.4. `EmbodiedAgent` |
| 38 | +Agent designed for embodied AI scenarios, capable of understanding and responding to physical world contexts. |
| 39 | + |
| 40 | +### 2.5. `KnowledgeGraphAgent` |
| 41 | +Specialized in building and utilizing knowledge graphs for enhanced reasoning and information management. |
| 42 | + |
| 43 | +### 2.6. `MultiHopGeneratorAgent` |
| 44 | +Agent designed for handling multi-hop reasoning tasks, generating intermediate steps to reach conclusions. |
| 45 | + |
| 46 | +### 2.7. `SearchAgent` |
| 47 | +Focused on information retrieval and search tasks across various data sources. |
| 48 | + |
| 49 | +### 2.8. `TaskAgent` |
| 50 | +Handles task decomposition and management, breaking down complex tasks into manageable subtasks. |
| 51 | + |
| 52 | +## 3. Usage |
| 53 | + |
| 54 | +### 3.1. Basic Chat Agent Usage |
| 55 | +```python |
| 56 | +from camel.agents import ChatAgent |
| 57 | + |
| 58 | +# Create a chat agent with a system message |
| 59 | +agent = ChatAgent(system_message="You are a helpful assistant.") |
| 60 | + |
| 61 | +# Step through a conversation |
| 62 | +response = agent.step("Hello, can you help me?") |
| 63 | +``` |
| 64 | + |
| 65 | +### 3.2. Using Tools with Chat Agent |
| 66 | +```python |
| 67 | +from camel.agents import ChatAgent |
| 68 | +from camel.functions import FunctionTool |
| 69 | + |
| 70 | +# Define a tool |
| 71 | +def calculator(a: int, b: int) -> int: |
| 72 | + return a + b |
| 73 | + |
| 74 | +# Create agent with tool |
| 75 | +agent = ChatAgent(tools=[calculator]) |
| 76 | + |
| 77 | +# The agent can now use the calculator tool in conversations |
| 78 | +response = agent.step("What is 5 + 3?") |
| 79 | +``` |
| 80 | + |
| 81 | +### 3.3. Structured Output |
| 82 | +```python |
| 83 | +from pydantic import BaseModel |
| 84 | +from typing import List |
| 85 | + |
| 86 | +class ResponseFormat(BaseModel): |
| 87 | + points: List[str] |
| 88 | + summary: str |
| 89 | + |
| 90 | +# Create agent with structured output |
| 91 | +agent = ChatAgent() |
| 92 | +response = agent.step("List benefits of exercise", response_format=ResponseFormat) |
| 93 | +``` |
| 94 | + |
| 95 | +## 4. Best Practices |
| 96 | + |
| 97 | +### 4.1. Memory Management |
| 98 | +- Use appropriate window sizes to manage conversation history |
| 99 | +- Consider token limits when dealing with long conversations |
| 100 | +- Utilize the memory system for maintaining context |
| 101 | + |
| 102 | +### 4.2. Tool Integration |
| 103 | +- Keep tool functions focused and well-documented |
| 104 | +- Handle tool errors gracefully |
| 105 | +- Use external tools for operations that should be handled by the user |
| 106 | + |
| 107 | +### 4.3. Response Handling |
| 108 | +- Implement appropriate response terminators for conversation control |
| 109 | +- Use structured outputs when specific response formats are needed |
| 110 | +- Handle async operations properly when dealing with long-running tasks |
| 111 | + |
| 112 | +## 5. Advanced Features |
| 113 | + |
| 114 | +### 5.1. Model Scheduling |
| 115 | +The agent supports multiple model backends with customizable scheduling strategies: |
| 116 | +```python |
| 117 | +def custom_strategy(models): |
| 118 | + # Custom model selection logic |
| 119 | + return models[0] |
| 120 | + |
| 121 | +agent.add_model_scheduling_strategy("custom", custom_strategy) |
| 122 | +``` |
| 123 | + |
| 124 | +### 5.2. Output Language Control |
| 125 | +Control the language of agent responses: |
| 126 | +```python |
| 127 | +agent.set_output_language("Spanish") |
| 128 | +``` |
0 commit comments