docs for new agents -- reflexion + gpk

pull/800/head
Kye Gomez 4 weeks ago
parent f487b28fef
commit 216956e5a7

@ -174,6 +174,8 @@ nav:
- IRE Agent: "swarms/agents/iterative_agent.md"
- Reasoning Duo: "swarms/agents/reasoning_duo.md"
- Reasoning Agent Router: "swarms/agents/reasoning_agent_router.md"
- Reflexion Agent: "swarms/agents/reflexion_agent.md"
- GKP Agent: "swarms/agents/gkp_agent.md"
- Swarm Architectures:
- Why MultiAgent Collaboration is Necessary: "swarms/concept/why.md"
- Swarm Architectures: "swarms/concept/swarm_architectures.md"
@ -197,6 +199,7 @@ nav:
- MatrixSwarm: "swarms/structs/matrix_swarm.md"
- ModelRouter: "swarms/structs/model_router.md"
- MALT: "swarms/structs/malt.md"
- Auto Agent Builder: "swarms/structs/auto_agent_builder.md"
- Various Execution Methods: "swarms/structs/various_execution_methods.md"
- Workflows:
- ConcurrentWorkflow: "swarms/structs/concurrentworkflow.md"

@ -0,0 +1,173 @@
# Generated Knowledge Prompting (GKP) Agent
The GKP Agent is a sophisticated reasoning system that enhances its capabilities by generating relevant knowledge before answering queries. This approach, inspired by Liu et al. 2022, is particularly effective for tasks requiring commonsense reasoning and factual information.
## Overview
The GKP Agent consists of three main components:
1. Knowledge Generator - Creates relevant factual information
2. Reasoner - Uses generated knowledge to form answers
3. Coordinator - Synthesizes multiple reasoning paths into a final answer
## Architecture
```mermaid
graph TD
A[Input Query] --> B[Knowledge Generator]
B --> C[Generate Knowledge Items]
C --> D[Reasoner]
D --> E[Multiple Reasoning Paths]
E --> F[Coordinator]
F --> G[Final Answer]
subgraph "Knowledge Generation"
B
C
end
subgraph "Reasoning"
D
E
end
subgraph "Coordination"
F
G
end
```
## Use Cases
```mermaid
graph LR
A[GKP Agent] --> B[Commonsense Reasoning]
A --> C[Factual Question Answering]
A --> D[Complex Problem Solving]
A --> E[Multi-step Reasoning]
B --> B1[Everyday Logic]
B --> B2[Social Situations]
C --> C1[Historical Facts]
C --> C2[Scientific Information]
D --> D1[Technical Analysis]
D --> D2[Decision Making]
E --> E1[Chain of Thought]
E --> E2[Multi-perspective Analysis]
```
## API Reference
### GKPAgent
The main agent class that orchestrates the knowledge generation and reasoning process.
#### Initialization Parameters
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| agent_name | str | "gkp-agent" | Name identifier for the agent |
| model_name | str | "openai/o1" | LLM model to use for all components |
| num_knowledge_items | int | 6 | Number of knowledge snippets to generate per query |
#### Methods
| Method | Description | Parameters | Returns |
|--------|-------------|------------|---------|
| process(query: str) | Process a single query through the GKP pipeline | query: str | Dict[str, Any] containing full processing results |
| run(queries: List[str], detailed_output: bool = False) | Process multiple queries | queries: List[str], detailed_output: bool | Union[List[str], List[Dict[str, Any]]] |
### KnowledgeGenerator
Component responsible for generating relevant knowledge for queries.
#### Initialization Parameters
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| agent_name | str | "knowledge-generator" | Name identifier for the knowledge generator agent |
| model_name | str | "openai/o1" | Model to use for knowledge generation |
| num_knowledge_items | int | 2 | Number of knowledge items to generate per query |
#### Methods
| Method | Description | Parameters | Returns |
|--------|-------------|------------|---------|
| generate_knowledge(query: str) | Generate relevant knowledge for a query | query: str | List[str] of generated knowledge statements |
### Reasoner
Component that uses generated knowledge to reason about and answer queries.
#### Initialization Parameters
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| agent_name | str | "knowledge-reasoner" | Name identifier for the reasoner agent |
| model_name | str | "openai/o1" | Model to use for reasoning |
#### Methods
| Method | Description | Parameters | Returns |
|--------|-------------|------------|---------|
| reason_and_answer(query: str, knowledge: str) | Reason about a query using provided knowledge | query: str, knowledge: str | Dict[str, str] containing explanation, confidence, and answer |
## Example Usage
```python
from swarms.agents.gkp_agent import GKPAgent
# Initialize the GKP Agent
agent = GKPAgent(
agent_name="gkp-agent",
model_name="gpt-4", # Using OpenAI's model
num_knowledge_items=6, # Generate 6 knowledge items per query
)
# Example queries
queries = [
"What are the implications of quantum entanglement on information theory?",
]
# Run the agent
results = agent.run(queries)
# Print results
for i, result in enumerate(results):
print(f"\nQuery {i+1}: {queries[i]}")
print(f"Answer: {result}")
```
## Best Practices
1. **Knowledge Generation**
- Set appropriate number of knowledge items based on query complexity
- Monitor knowledge quality and relevance
- Adjust model parameters for optimal performance
2. **Reasoning Process**
- Ensure diverse reasoning paths for complex queries
- Validate confidence levels
- Consider multiple perspectives
3. **Coordination**
- Review coordination logic for complex scenarios
- Validate final answers against source knowledge
- Monitor processing time and optimize if needed
## Performance Considerations
- Processing time increases with number of knowledge items
- Complex queries may require more knowledge items
- Consider caching frequently used knowledge
- Monitor token usage for cost optimization
## Error Handling
The agent includes robust error handling for:
- Invalid queries
- Failed knowledge generation
- Reasoning errors
- Coordination failures

@ -0,0 +1,192 @@
# ReflexionAgent
The ReflexionAgent is an advanced AI agent that implements the Reflexion framework to improve through self-reflection. It follows a process of acting on tasks, evaluating its performance, generating self-reflections, and using these reflections to improve future responses.
## Overview
The ReflexionAgent consists of three specialized sub-agents:
- **Actor**: Generates initial responses to tasks
- **Evaluator**: Critically assesses responses against quality criteria
- **Reflector**: Generates self-reflections to improve future responses
## Initialization
```python
from swarms.agents import ReflexionAgent
agent = ReflexionAgent(
agent_name="reflexion-agent",
system_prompt="...", # Optional custom system prompt
model_name="openai/o1",
max_loops=3,
memory_capacity=100
)
```
### Parameters
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `agent_name` | `str` | `"reflexion-agent"` | Name of the agent |
| `system_prompt` | `str` | `REFLEXION_PROMPT` | System prompt for the agent |
| `model_name` | `str` | `"openai/o1"` | Model name for generating responses |
| `max_loops` | `int` | `3` | Maximum number of reflection iterations per task |
| `memory_capacity` | `int` | `100` | Maximum capacity of long-term memory |
## Methods
### act
Generates a response to the given task using the actor agent.
```python
response = agent.act(task: str, relevant_memories: List[Dict[str, Any]] = None) -> str
```
| Parameter | Type | Description |
|-----------|------|-------------|
| `task` | `str` | The task to respond to |
| `relevant_memories` | `List[Dict[str, Any]]` | Optional relevant past memories to consider |
### evaluate
Evaluates the quality of a response to a task.
```python
evaluation, score = agent.evaluate(task: str, response: str) -> Tuple[str, float]
```
| Parameter | Type | Description |
|-----------|------|-------------|
| `task` | `str` | The original task |
| `response` | `str` | The response to evaluate |
Returns:
- `evaluation`: Detailed feedback on the response
- `score`: Numerical score between 0 and 1
### reflect
Generates a self-reflection based on the task, response, and evaluation.
```python
reflection = agent.reflect(task: str, response: str, evaluation: str) -> str
```
| Parameter | Type | Description |
|-----------|------|-------------|
| `task` | `str` | The original task |
| `response` | `str` | The generated response |
| `evaluation` | `str` | The evaluation feedback |
### refine
Refines the original response based on evaluation and reflection.
```python
refined_response = agent.refine(
task: str,
original_response: str,
evaluation: str,
reflection: str
) -> str
```
| Parameter | Type | Description |
|-----------|------|-------------|
| `task` | `str` | The original task |
| `original_response` | `str` | The original response |
| `evaluation` | `str` | The evaluation feedback |
| `reflection` | `str` | The self-reflection |
### step
Processes a single task through one iteration of the Reflexion process.
```python
result = agent.step(
task: str,
iteration: int = 0,
previous_response: str = None
) -> Dict[str, Any]
```
| Parameter | Type | Description |
|-----------|------|-------------|
| `task` | `str` | The task to process |
| `iteration` | `int` | Current iteration number |
| `previous_response` | `str` | Response from previous iteration |
Returns a dictionary containing:
- `task`: The original task
- `response`: The generated response
- `evaluation`: The evaluation feedback
- `reflection`: The self-reflection
- `score`: Numerical score
- `iteration`: Current iteration number
### run
Executes the Reflexion process for a list of tasks.
```python
results = agent.run(
tasks: List[str],
include_intermediates: bool = False
) -> List[Any]
```
| Parameter | Type | Description |
|-----------|------|-------------|
| `tasks` | `List[str]` | List of tasks to process |
| `include_intermediates` | `bool` | Whether to include intermediate iterations in results |
Returns:
- If `include_intermediates=False`: List of final responses
- If `include_intermediates=True`: List of complete iteration histories
## Example Usage
```python
from swarms.agents import ReflexionAgent
# Initialize the Reflexion Agent
agent = ReflexionAgent(
agent_name="reflexion-agent",
model_name="openai/o1",
max_loops=3
)
# Example tasks
tasks = [
"Explain quantum computing to a beginner.",
"Write a Python function to sort a list of dictionaries by a specific key."
]
# Run the agent
results = agent.run(tasks)
# Print results
for i, result in enumerate(results):
print(f"\nTask {i+1}: {tasks[i]}")
print(f"Response: {result}")
```
## Memory System
The ReflexionAgent includes a sophisticated memory system (`ReflexionMemory`) that maintains both short-term and long-term memories of past experiences, reflections, and feedback. This system helps the agent learn from past interactions and improve its responses over time.
### Memory Features
- Short-term memory for recent interactions
- Long-term memory for important reflections and patterns
- Automatic memory management with capacity limits
- Relevance-based memory retrieval
- Similarity-based deduplication
## Best Practices
1. **Task Clarity**: Provide clear, specific tasks to get the best results
2. **Iteration Count**: Adjust `max_loops` based on task complexity (more complex tasks may benefit from more iterations)
3. **Memory Management**: Monitor memory usage and adjust `memory_capacity` as needed
4. **Model Selection**: Choose an appropriate model based on your specific use case and requirements
5. **Error Handling**: Implement proper error handling when using the agent in production

@ -0,0 +1,200 @@
# Agent Builder
The Agent Builder is a powerful class that automatically builds and manages swarms of AI agents. It provides a flexible and extensible framework for creating, coordinating, and executing multiple AI agents working together to accomplish complex tasks.
## Overview
The Agent Builder uses a boss agent to delegate work and create new specialized agents as needed. It's designed to be production-ready with robust error handling, logging, and configuration options.
## Architecture
```mermaid
graph TD
A[Agent Builder] --> B[Configuration]
A --> C[Agent Creation]
A --> D[Task Execution]
B --> B1[Name]
B --> B2[Description]
B --> B3[Model Settings]
C --> C1[Agent Pool]
C --> C2[Agent Registry]
C --> C3[Agent Configuration]
D --> D1[Task Distribution]
D --> D2[Result Collection]
D --> D3[Error Handling]
C1 --> E[Specialized Agents]
C2 --> E
C3 --> E
E --> F[Task Execution]
F --> G[Results]
```
## Class Structure
### AgentsBuilder Class
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| name | str | "swarm-creator-01" | The name of the swarm |
| description | str | "This is a swarm that creates swarms" | A description of the swarm's purpose |
| verbose | bool | True | Whether to output detailed logs |
| max_loops | int | 1 | Maximum number of execution loops |
| model_name | str | "gpt-4o" | The model to use for agent creation |
| return_dictionary | bool | True | Whether to return results as a dictionary |
| system_prompt | str | BOSS_SYSTEM_PROMPT | The system prompt for the boss agent |
### Methods
| Method | Description | Parameters | Returns |
|--------|-------------|------------|---------|
| run | Run the swarm on a given task | task: str, image_url: str = None, *args, **kwargs | Tuple[List[Agent], int] |
| _create_agents | Create necessary agents for a task | task: str, *args, **kwargs | List[Agent] |
| build_agent | Build a single agent with specifications | agent_name: str, agent_description: str, agent_system_prompt: str, max_loops: int = 1, model_name: str = "gpt-4o", dynamic_temperature_enabled: bool = True, auto_generate_prompt: bool = False, role: str = "worker", max_tokens: int = 8192, temperature: float = 0.5 | Agent |
## Enterprise Use Cases
### 1. Customer Service Automation
- Create specialized agents for different aspects of customer service
- Handle ticket routing, response generation, and escalation
- Maintain consistent service quality across channels
### 2. Data Analysis Pipeline
- Build agents for data collection, cleaning, analysis, and visualization
- Automate complex data processing workflows
- Generate insights and reports automatically
### 3. Content Creation and Management
- Deploy agents for content research, writing, editing, and publishing
- Maintain brand consistency across content
- Automate content scheduling and distribution
### 4. Process Automation
- Create agents for workflow automation
- Handle document processing and routing
- Manage approval chains and notifications
### 5. Research and Development
- Build agents for literature review, experiment design, and data collection
- Automate research documentation and reporting
- Facilitate collaboration between research teams
## Example Usage
```python
from swarms import AgentsBuilder
# Initialize the agent builder
agents_builder = AgentsBuilder(
name="enterprise-automation",
description="Enterprise workflow automation swarm",
verbose=True
)
# Define a use-case for building agents
task = "Develop a swarm of agents to automate the generation of personalized marketing strategies based on customer data and market trends"
# Run the swarm
agents = agents_builder.run(task)
# Access results
print(agents)
```
## Best Practices
1. **Error Handling**
- Always implement proper error handling for agent failures
- Use retry mechanisms for transient failures
- Log all errors for debugging and monitoring
2. **Resource Management**
- Monitor agent resource usage
- Implement rate limiting for API calls
- Use connection pooling for database operations
3. **Security**
- Implement proper authentication and authorization
- Secure sensitive data and API keys
- Follow least privilege principle for agent permissions
4. **Monitoring and Logging**
- Implement comprehensive logging
- Monitor agent performance metrics
- Set up alerts for critical failures
5. **Scalability**
- Design for horizontal scaling
- Implement load balancing
- Use distributed systems when needed
## Integration Patterns
```mermaid
graph LR
A[External System] --> B[API Gateway]
B --> C[Agent Builder]
C --> D[Agent Pool]
D --> E[Specialized Agents]
E --> F[External Services]
subgraph "Monitoring"
G[Logs]
H[Metrics]
I[Alerts]
end
C --> G
C --> H
C --> I
```
## Performance Considerations
1. **Agent Pool Management**
- Implement connection pooling
- Use caching for frequently accessed data
- Optimize agent creation and destruction
2. **Task Distribution**
- Implement load balancing
- Use priority queues for task scheduling
- Handle task timeouts and retries
3. **Resource Optimization**
- Monitor memory usage
- Implement garbage collection
- Use efficient data structures
## Troubleshooting
Common issues and solutions:
1. **Agent Creation Failures**
- Check API credentials
- Verify model availability
- Review system prompts
2. **Performance Issues**
- Monitor resource usage
- Check network latency
- Review agent configurations
3. **Integration Problems**
- Verify API endpoints
- Check authentication
- Review data formats

@ -23,6 +23,9 @@ from swarms.structs.stopping_conditions import (
check_success,
)
from swarms.agents.flexion_agent import ReflexionAgent
from swarms.agents.gkp_agent import GKPAgent
__all__ = [
# "ToolAgent",
"check_done",
@ -41,4 +44,6 @@ __all__ = [
"ReasoningDuo",
"ReasoningAgentRouter",
"agent_types",
"ReflexionAgent",
"GKPAgent",
]

@ -1,6 +1,8 @@
from typing import List, Literal
from swarms.agents.consistency_agent import SelfConsistencyAgent
from swarms.agents.flexion_agent import ReflexionAgent
from swarms.agents.gkp_agent import GKPAgent
from swarms.agents.i_agent import (
IterativeReflectiveExpansion as IREAgent,
)
@ -14,6 +16,8 @@ agent_types = Literal[
"reasoning-agent",
"consistency-agent",
"ire-agent",
"ReflexionAgent",
"GKPAgent",
]
@ -42,6 +46,8 @@ class ReasoningAgentRouter:
swarm_type: agent_types = "reasoning_duo",
num_samples: int = 1,
output_type: OutputType = "dict",
num_knowledge_items: int = 6,
memory_capacity: int = 6,
):
self.agent_name = agent_name
self.description = description
@ -51,6 +57,8 @@ class ReasoningAgentRouter:
self.swarm_type = swarm_type
self.num_samples = num_samples
self.output_type = output_type
self.num_knowledge_items = num_knowledge_items
self.memory_capacity = memory_capacity
def select_swarm(self):
"""
@ -98,6 +106,20 @@ class ReasoningAgentRouter:
output_type=self.output_type,
)
elif self.swarm_type == "ReflexionAgent":
return ReflexionAgent(
agent_name=self.agent_name,
system_prompt=self.system_prompt,
model_name=self.model_name,
max_loops=self.max_loops,
)
elif self.swarm_type == "GKPAgent":
return GKPAgent(
agent_name=self.agent_name,
model_name=self.model_name,
num_knowledge_items=self.num_knowledge_items,
)
else:
raise ValueError(f"Invalid swarm type: {self.swarm_type}")

Loading…
Cancel
Save