parent
95abbf0e84
commit
fe6a5fde2c
@ -0,0 +1,12 @@
|
|||||||
|
from swarms import SelfConsistencyAgent
|
||||||
|
|
||||||
|
agent = SelfConsistencyAgent(
|
||||||
|
max_loops=1,
|
||||||
|
model_name="gpt-4o-mini",
|
||||||
|
system_prompt="You are a helpful assistant that can answer questions and help with tasks.",
|
||||||
|
description="You are a helpful assistant that can answer questions and help with tasks.",
|
||||||
|
)
|
||||||
|
|
||||||
|
agent.run(
|
||||||
|
"Create a comprehensive proof for the The Birch and Swinnerton-Dyer Conjecture"
|
||||||
|
)
|
@ -0,0 +1,158 @@
|
|||||||
|
# ReasoningDuo
|
||||||
|
|
||||||
|
The ReasoningDuo class implements a dual-agent reasoning system that combines a reasoning agent and a main agent to provide well-thought-out responses to complex tasks. This architecture enables more robust and reliable outputs by separating the reasoning process from the final response generation.
|
||||||
|
|
||||||
|
|
||||||
|
## Class Overview
|
||||||
|
|
||||||
|
### Constructor Parameters
|
||||||
|
|
||||||
|
| Parameter | Type | Default | Description |
|
||||||
|
|-----------|------|---------|-------------|
|
||||||
|
| model_name | str | "reasoning-agent-01" | Name identifier for the reasoning agent |
|
||||||
|
| description | str | "A highly intelligent..." | Description of the reasoning agent's capabilities |
|
||||||
|
| model_names | list[str] | ["gpt-4o-mini", "gpt-4o"] | Model names for reasoning and main agents |
|
||||||
|
| system_prompt | str | "You are a helpful..." | System prompt for the main agent |
|
||||||
|
|
||||||
|
### Methods
|
||||||
|
|
||||||
|
| Method | Parameters | Returns | Description |
|
||||||
|
|--------|------------|---------|-------------|
|
||||||
|
| run | task: str | str | Processes a single task through both agents |
|
||||||
|
| batched_run | tasks: List[str] | List[str] | Processes multiple tasks sequentially |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
```python
|
||||||
|
from swarms.agents.reasoning_duo import ReasoningDuo
|
||||||
|
|
||||||
|
# Initialize the ReasoningDuo
|
||||||
|
duo = ReasoningDuo(
|
||||||
|
model_name="reasoning-agent-01",
|
||||||
|
model_names=["gpt-4o-mini", "gpt-4o"]
|
||||||
|
)
|
||||||
|
|
||||||
|
# Run a single task
|
||||||
|
result = duo.run("Explain the concept of gravitational waves")
|
||||||
|
|
||||||
|
# Run multiple tasks
|
||||||
|
tasks = [
|
||||||
|
"Calculate compound interest for $1000 over 5 years",
|
||||||
|
"Explain quantum entanglement"
|
||||||
|
]
|
||||||
|
results = duo.batched_run(tasks)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
### 1. Mathematical Analysis
|
||||||
|
|
||||||
|
```python
|
||||||
|
duo = ReasoningDuo()
|
||||||
|
|
||||||
|
# Complex mathematical problem
|
||||||
|
math_task = """
|
||||||
|
Solve the following differential equation:
|
||||||
|
dy/dx + 2y = x^2, y(0) = 1
|
||||||
|
"""
|
||||||
|
|
||||||
|
solution = duo.run(math_task)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Physics Problem
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Quantum mechanics problem
|
||||||
|
physics_task = """
|
||||||
|
Calculate the wavelength of an electron with kinetic energy of 50 eV
|
||||||
|
using the de Broglie relationship.
|
||||||
|
"""
|
||||||
|
|
||||||
|
result = duo.run(physics_task)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Financial Analysis
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Complex financial analysis
|
||||||
|
finance_task = """
|
||||||
|
Calculate the Net Present Value (NPV) of a project with:
|
||||||
|
- Initial investment: $100,000
|
||||||
|
- Annual cash flows: $25,000 for 5 years
|
||||||
|
- Discount rate: 8%
|
||||||
|
"""
|
||||||
|
|
||||||
|
analysis = duo.run(finance_task)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Advanced Usage
|
||||||
|
|
||||||
|
### Customizing Agent Behavior
|
||||||
|
|
||||||
|
You can customize both agents by modifying their initialization parameters:
|
||||||
|
|
||||||
|
```python
|
||||||
|
duo = ReasoningDuo(
|
||||||
|
model_name="custom-reasoning-agent",
|
||||||
|
description="Specialized financial analysis agent",
|
||||||
|
model_names=["gpt-4o-mini", "gpt-4o"],
|
||||||
|
system_prompt="You are a financial expert AI assistant..."
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Batch Processing with Progress Tracking
|
||||||
|
|
||||||
|
```python
|
||||||
|
tasks = [
|
||||||
|
"Analyze market trends for tech stocks",
|
||||||
|
"Calculate risk metrics for a portfolio",
|
||||||
|
"Forecast revenue growth"
|
||||||
|
]
|
||||||
|
|
||||||
|
# Process multiple tasks with logging
|
||||||
|
results = duo.batched_run(tasks)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Implementation Details
|
||||||
|
|
||||||
|
The ReasoningDuo uses a two-stage process:
|
||||||
|
|
||||||
|
1. **Reasoning Stage**: The reasoning agent analyzes the task and develops a structured approach
|
||||||
|
2. **Execution Stage**: The main agent uses the reasoning output to generate the final response
|
||||||
|
|
||||||
|
### Internal Architecture
|
||||||
|
|
||||||
|
```
|
||||||
|
Task Input → Reasoning Agent → Structured Analysis → Main Agent → Final Output
|
||||||
|
```
|
||||||
|
|
||||||
|
## Best Practices
|
||||||
|
|
||||||
|
1. **Task Formulation**
|
||||||
|
- Be specific and clear in task descriptions
|
||||||
|
- Include relevant context and constraints
|
||||||
|
- Break complex problems into smaller subtasks
|
||||||
|
|
||||||
|
2. **Performance Optimization**
|
||||||
|
- Use batched_run for multiple related tasks
|
||||||
|
- Monitor agent outputs for consistency
|
||||||
|
- Adjust model parameters based on task complexity
|
||||||
|
|
||||||
|
## Error Handling
|
||||||
|
|
||||||
|
The ReasoningDuo includes built-in logging using the `loguru` library:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
# Logs are automatically generated for each task
|
||||||
|
logger.info("Task processing started")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Limitations
|
||||||
|
|
||||||
|
- Processing time may vary based on task complexity
|
||||||
|
- Model response quality depends on input clarity
|
||||||
|
- Resource usage scales with batch size
|
@ -1,7 +1,7 @@
|
|||||||
from swarms.agents.i_agent import IterativeReflectiveExpansion
|
from swarms.agents.i_agent import IterativeReflectiveExpansion
|
||||||
|
|
||||||
agent = IterativeReflectiveExpansion(
|
agent = IterativeReflectiveExpansion(
|
||||||
max_iterations=3,
|
max_iterations=1,
|
||||||
)
|
)
|
||||||
|
|
||||||
agent.run("What is the 40th prime number?")
|
agent.run("What is the 40th prime number?")
|
||||||
|
@ -0,0 +1,84 @@
|
|||||||
|
from typing import List
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
from swarms.prompts.reasoning_prompt import REASONING_PROMPT
|
||||||
|
from swarms.structs.agent import Agent
|
||||||
|
|
||||||
|
|
||||||
|
class ReasoningDuo:
|
||||||
|
"""
|
||||||
|
ReasoningDuo is a class that encapsulates the functionality of two agents: a reasoning agent and a main agent.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
model_name (str): The name of the model used for the reasoning agent.
|
||||||
|
description (str): A description of the reasoning agent.
|
||||||
|
model_names (list[str]): A list of model names for the agents.
|
||||||
|
system_prompt (str): The system prompt for the main agent.
|
||||||
|
reasoning_agent (Agent): An instance of the Agent class for reasoning tasks.
|
||||||
|
main_agent (Agent): An instance of the Agent class for main tasks.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
model_name: str = "reasoning-agent-01",
|
||||||
|
description: str = "A highly intelligent and thoughtful AI designed to provide accurate and well-reasoned answers to the user's questions.",
|
||||||
|
model_names: list[str] = ["gpt-4o-mini", "gpt-4o"],
|
||||||
|
system_prompt: str = "You are a helpful assistant that can answer questions and help with tasks.",
|
||||||
|
):
|
||||||
|
self.model_name = model_name
|
||||||
|
self.description = description
|
||||||
|
|
||||||
|
self.reasoning_agent = Agent(
|
||||||
|
agent_name="Your",
|
||||||
|
description="A highly intelligent and thoughtful AI designed to provide accurate and well-reasoned answers to the user's questions.",
|
||||||
|
system_prompt=REASONING_PROMPT,
|
||||||
|
max_loops=1,
|
||||||
|
model_name=model_names[0],
|
||||||
|
dynamic_temperature_enabled=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.main_agent = Agent(
|
||||||
|
agent_name="Main Agent",
|
||||||
|
description="A highly intelligent and thoughtful AI designed to provide accurate and well-reasoned answers to the user's questions.",
|
||||||
|
system_prompt=system_prompt,
|
||||||
|
max_loops=1,
|
||||||
|
model_name=model_names[1],
|
||||||
|
dynamic_temperature_enabled=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
def run(self, task: str):
|
||||||
|
"""
|
||||||
|
Executes the reasoning and main agents on the provided task.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task (str): The task to be processed by the agents.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The output from the main agent after processing the task.
|
||||||
|
"""
|
||||||
|
logger.info(f"Running task: {task}")
|
||||||
|
output_reasoner = self.reasoning_agent.run(task)
|
||||||
|
|
||||||
|
output_main = self.main_agent.run(
|
||||||
|
f"Task: {task} \n\n Your thoughts: {output_reasoner}"
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.info(f"Output from main agent: {output_main}")
|
||||||
|
return output_main
|
||||||
|
|
||||||
|
def batched_run(self, tasks: List[str]):
|
||||||
|
"""
|
||||||
|
Executes the run method for a list of tasks.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
tasks (list[str]): A list of tasks to be processed.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list: A list of outputs from the main agent for each task.
|
||||||
|
"""
|
||||||
|
outputs = []
|
||||||
|
for task in tasks:
|
||||||
|
logger.info(f"Processing task: {task}")
|
||||||
|
outputs.append(self.run(task))
|
||||||
|
return outputs
|
@ -0,0 +1,9 @@
|
|||||||
|
REASONING_PROMPT = """
|
||||||
|
This is a structured conversation between the User and the Assistant, where the User poses a question, and the Assistant is tasked with providing a comprehensive solution.
|
||||||
|
|
||||||
|
Before delivering the final answer, the Assistant must engage in a thorough reasoning process. This involves critically analyzing the question, considering various perspectives, and evaluating potential solutions. The Assistant should articulate this reasoning process clearly, allowing the User to understand the thought process behind the answer.
|
||||||
|
|
||||||
|
The reasoning process and the final answer should be distinctly enclosed within <think> </think> tags. For example, the format should be: <think> reasoning process here </think> for the reasoning, followed by <think> final answer here </think> for the answer.
|
||||||
|
|
||||||
|
It is essential to output multiple <think> </think> tags to reflect the depth of thought and exploration involved in addressing the task. The Assistant should strive to think deeply and thoroughly about the question, ensuring that all relevant aspects are considered before arriving at a conclusion.
|
||||||
|
"""
|
Loading…
Reference in new issue