# {{name}} - Multi-Agent System

{{description}}

## Overview

This multi-agent system uses three specialized agents working together:

1. **Researcher Agent** - Gathers information and facts
2. **Writer Agent** - Creates content from research
3. **Reviewer Agent** - Evaluates and improves content

## Architecture

```
User Task
    ↓
Researcher Agent → Research Findings
    ↓
Writer Agent → Written Content
    ↓
Reviewer Agent → Review & Feedback
    ↓
Final Output
```

## Setup

### 1. Create Agents

```bash
python create_agents.py
```

This creates three specialized agents:
- `{{name}}_researcher`
- `{{name}}_writer`
- `{{name}}_reviewer`

### 2. Run Multi-Agent Orchestration

```bash
python orchestrate_{{name}}.py
```

The orchestrator:
- Takes your task input
- Passes it through each agent sequentially
- Shows output from each stage
- Provides final reviewed content

## Usage Example

```bash
$ python orchestrate_{{name}}.py

Enter task: Write an article about quantum computing

STAGE 1: Research
Researcher agent working...
[Research findings about quantum computing...]

STAGE 2: Writing
Writer agent working...
[Written article based on research...]

STAGE 3: Review
Reviewer agent working...
[Review and improvement suggestions...]
```

## Customization

### Adding More Agents

Edit `create_agents.py` and add to `agents_config`:

```python
{
    "name": "{{name}}_fact_checker",
    "description": "Fact checker agent",
    "instructions": "Verify facts and accuracy...",
}
```

### Modifying Orchestration

Edit `orchestrate_{{name}}.py` to change the workflow:

```python
# Add parallel processing
import asyncio

# Add conditional logic
if "technical" in task.lower():
    # Use specialized technical agent
    pass

# Add iteration/refinement loops
for iteration in range(3):
    # Iteratively improve content
    pass
```

### Changing Agent Roles

Modify the instructions in `create_agents.py` to change agent behavior:

```python
"instructions": """You are a [specialized role].
Your responsibilities include:
- [Task 1]
- [Task 2]
..."""
```

## Advanced Patterns

### Parallel Processing

```python
import concurrent.futures

def run_agent(agent_name, prompt):
    return client.chat_completion(
        model=f"saia:agent:{agent_name}",
        messages=[{"role": "user", "content": prompt}],
        **LLM_SETTINGS
    )

with concurrent.futures.ThreadPoolExecutor() as executor:
    futures = [
        executor.submit(run_agent, "agent1", "task1"),
        executor.submit(run_agent, "agent2", "task2"),
    ]
    results = [f.result() for f in futures]
```

### Debate/Consensus

```python
# Multiple agents discuss and reach consensus
agents = ["agent1", "agent2", "agent3"]
opinions = []

for agent in agents:
    response = client.chat_completion(
        model=f"saia:agent:{agent}",
        messages=[{"role": "user", "content": question}],
    )
    opinions.append(response)

# Synthesize consensus from opinions
```

### Hierarchical Coordination

```python
# Coordinator agent manages worker agents
coordinator_prompt = f"""
You are coordinating these agents:
- Researcher: {research_result}
- Writer: {writer_result}

Synthesize their outputs into a final response.
"""
```

## Configuration

- **Model**: {{model}}
- **Temperature**: {{temperature}}
- **Max Tokens**: 2000 per agent
- **Timeout**: 120 seconds per agent

Modify these in `create_agents.py` after generation.

## Notes

- Each agent is independent and can be used separately
- Agents maintain no state between calls
- For stateful workflows, use Lab processes (see `lab-process-basic` template)
- Agents use shared LLM settings from `LLM_SETTINGS`

## Cleanup

To delete the agents:

```python
from pygeai.lab.managers import AILabManager

manager = AILabManager()
manager.delete_agent(agent_name="{{name}}_researcher")
manager.delete_agent(agent_name="{{name}}_writer")
manager.delete_agent(agent_name="{{name}}_reviewer")
```

## Next Steps

- Add more specialized agents for your use case
- Implement error handling and retries
- Add logging and monitoring
- Create a web interface (see `web-ui-gradio` template)
- Build an API server (see `api-server` template)
- Integrate with Lab processes for complex workflows
