-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_main.py
140 lines (116 loc) · 4.3 KB
/
test_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import json
import uuid
from typing import Any, Callable, Dict, List, Optional
from openai import OpenAI
import requests
from bs4 import BeautifulSoup
from datetime import datetime
from tiktoken import get_encoding
import pickle
import requests
from bs4 import BeautifulSoup
import PyPDF2
import os
import hashlib
from src.resources import Resources
import argparse
from datetime import datetime
from src.rag_tools import TextReaderTool, WebScraperTool, SemanticAnalysisTool, NERExtractionTool, SemanticFileSearchTool, WikipediaSearchTool
from src import agents
from src.agents import Agent
from src import tasks
from src.tasks import Task
class Resources:
def __init__(self, resource_type, path, template):
self.resource_type = resource_type
self.path = path
self.template = template
class Agent:
def __init__(self, role, tools):
self.role = role
self.tools = tools
self.interactions = [] # Simulated interactions log
class Task:
def __init__(self, instructions, agent, tool_name=None):
self.instructions = instructions
self.agent = agent
self.tool_name = tool_name
self.id = str(uuid.uuid4())
self.output = None
def execute(self, context):
# Placeholder for task execution logic
return f"Executed {self.instructions} using {self.agent.role}"
class Squad:
def __init__(self, agents: List[Agent], tasks: List[Task], resources: List[Resources], verbose: bool = False, log_file: str = "squad_log.json"):
self.id = str(uuid.uuid4())
self.agents = agents
self.tasks = tasks
self.resources = resources
self.verbose = verbose
self.log_file = log_file
self.log_data = []
self.llama_logs = []
def run(self, inputs: Optional[Dict[str, Any]] = None) -> str:
context = ""
for task in self.tasks:
if self.verbose:
print(f"Starting Task:\n{task.instructions}")
self.log_data.append({
"timestamp": datetime.now().isoformat(),
"type": "input",
"agent_role": task.agent.role,
"task_name": task.instructions,
"task_id": task.id,
"content": task.instructions
})
output = task.execute(context=context)
task.output = output
if self.verbose:
print(f"Task output:\n{output}\n")
self.log_data.append({
"timestamp": datetime.now().isoformat(),
"type": "output",
"agent_role": task.agent.role,
"task_name": task.instructions,
"task_id": task.id,
"content": output
})
context += f"Task:\n{task.instructions}\nOutput:\n{output}\n\n"
self.save_logs()
return context
def save_logs(self):
with open(self.log_file, "w") as file:
json.dump(self.log_data, file, indent=2)
def load_configuration(file_path): # loading a config file alt is full cli?
with open(file_path, 'r') as file:
return json.load(file)
def initialize_resources(config):
resources = []
for res in config["resources"]:
resources.append(Resources(res['type'], res['path'], res['template']))
return resources
def initialize_agents_and_tasks(config):
agents = [Agent(**ag) for ag in config['agents']]
tasks = [Task(**tk) for tk in config['tasks']]
return agents, tasks
def parse_args():
parser = argparse.ArgumentParser(description="Run the squad with dynamic configurations.")
parser.add_argument('-c', '--config', type=str, help="Path to configuration JSON file", required=True)
parser.add_argument('-v', '--verbose', action='store_true', help="Enable verbose output")
return parser.parse_args()
def mainflow():
args = parse_args()
config = load_configuration(args.config)
resources = initialize_resources(config)
agents, tasks = initialize_agents_and_tasks(config)
squad = Squad(
agents=agents,
tasks=tasks,
resources=resources,
verbose=args.verbose,
log_file="squad_goals_" + datetime.now().strftime("%Y%m%d%H%M%S") + ".json"
)
result = squad.run()
print(f"Final output:\n{result}")
if __name__ == "__main__":
mainflow()