Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add swarm logic for task decomposition and validation #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Ditto is a user-friendly tool that allows you to generate a multi-file Flask app
- **Automated Code Generation**: Generates routes, templates, and static files based on your description.
- **Self-Building Agent**: Automatically plans and constructs the application without the need for manual coding.
- **Modular Structure**: Organizes code into a clean, modular structure with separate directories for templates, static files, and routes.
- **Swarm Logic**: Utilizes swarm logic to decompose tasks into agents, assign tasks, facilitate agent communication, validate completed tasks, and add validated files to the IDE.

## Getting Started

Expand Down Expand Up @@ -39,7 +40,7 @@ Ditto is a user-friendly tool that allows you to generate a multi-file Flask app
3. **Install Dependencies**

```bash
pip install litellm
pip install -r requirements.txt
```

### Setting the `OPENAI_API_KEY`
Expand Down Expand Up @@ -120,6 +121,33 @@ python main.py
python main.py
```

### Using Swarm Logic

Ditto now includes a swarm logic feature to enhance task decomposition and validation. Here's how to use it:

1. **Describe Your Task**

Provide a detailed description of the task you want to accomplish.

2. **Task Decomposition**

The system will decompose the task into smaller tasks and create agents to handle each task.

3. **Task Assignment**

Tasks are assigned to agents, and the agents will work on their respective tasks.

4. **Agent Communication**

Agents will communicate with each other to ensure tasks are completed efficiently.

5. **Task Validation**

Completed tasks are validated, and if validated, the files are added to the IDE. If denied, the task is sent back to the agent for rework.

6. **Monitor Progress**

You can monitor the progress of the tasks and validation status in real-time.

## Contribution

Expand Down
17 changes: 16 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
# Correctly import the completion function from LiteLLM
from litellm import completion, supports_function_calling

# Import the swarm_logic module
from swarm_logic import SwarmLogic

# Configuration
MODEL_NAME = os.environ.get('LITELLM_MODEL', 'gpt-4o') # Default model; can be swapped easily

Expand All @@ -30,7 +33,8 @@
"iteration": 0,
"max_iterations": 50,
"output": "",
"completed": False
"completed": False,
"validation_status": ""
}

# Ensure directories exist and create __init__.py in routes
Expand Down Expand Up @@ -135,6 +139,7 @@ def home():
progress["iteration"] = 0
progress["output"] = ""
progress["completed"] = False
progress["validation_status"] = ""
thread = Thread(target=run_main_loop, args=(user_input,))
thread.start()
return render_template_string('''
Expand Down Expand Up @@ -323,6 +328,9 @@ def run_main_loop(user_input):

output = ""

# Initialize SwarmLogic
swarm_logic = SwarmLogic()

while iteration < max_iterations:
progress["iteration"] = iteration + 1
# Create a new iteration dictionary for each loop
Expand Down Expand Up @@ -454,6 +462,13 @@ def run_main_loop(user_input):
progress["completed"] = True
progress["status"] = "completed"

# Use swarm logic to process user input
validated_tasks = swarm_logic.process_user_input(user_input)
if validated_tasks:
progress["validation_status"] = "Tasks validated and files added to IDE."
else:
progress["validation_status"] = "No tasks validated."

return output

if __name__ == '__main__':
Expand Down
51 changes: 51 additions & 0 deletions swarm_logic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os
import json
from swarms import Swarm, Agent

class SwarmLogic:
def __init__(self):
self.swarm = Swarm()

def decompose_task(self, user_input):
# Decompose the user-defined task into smaller tasks
tasks = self.swarm.decompose(user_input)
return tasks

def create_agents(self, tasks):
# Create agents for each task
agents = [Agent(task) for task in tasks]
return agents

def assign_tasks(self, agents):
# Assign tasks to agents
for agent in agents:
self.swarm.assign(agent)

def facilitate_communication(self, agents):
# Facilitate communication between agents
for agent in agents:
self.swarm.communicate(agent)

def validate_tasks(self, agents):
# Validate completed tasks
validated_tasks = []
for agent in agents:
if self.swarm.validate(agent):
validated_tasks.append(agent.task)
return validated_tasks

def add_validated_files_to_ide(self, validated_tasks):
# Add validated files to the IDE
for task in validated_tasks:
file_path = os.path.join('ide', task['file_name'])
with open(file_path, 'w') as f:
f.write(task['content'])

def process_user_input(self, user_input):
tasks = self.decompose_task(user_input)
agents = self.create_agents(tasks)
self.assign_tasks(agents)
self.facilitate_communication(agents)
validated_tasks = self.validate_tasks(agents)
self.add_validated_files_to_ide(validated_tasks)
return validated_tasks