Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,303 +1,422 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import time
|
| 4 |
-
from typing import Dict, List, Tuple
|
| 5 |
|
| 6 |
-
|
| 7 |
-
import
|
| 8 |
-
from huggingface_hub import InferenceClient, hf_hub_url, cached_download
|
| 9 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 10 |
-
from rich import print as rprint
|
| 11 |
-
from rich.panel import Panel
|
| 12 |
-
from rich.progress import track
|
| 13 |
-
from rich.table import Table
|
| 14 |
import subprocess
|
| 15 |
-
import
|
| 16 |
-
import
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
from
|
| 20 |
-
from
|
| 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 |
-
def
|
| 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 |
-
def
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
else:
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
responses[agent] = response
|
| 241 |
-
st.session_state.chat_history.append(f"User: {agent_chat_input}")
|
| 242 |
-
for agent, response in responses.items():
|
| 243 |
-
st.session_state.chat_history.append(f"{agent}: {response}")
|
| 244 |
-
st_chat(st.session_state.chat_history) # Display chat history using st_chat
|
| 245 |
-
else:
|
| 246 |
-
st.warning("Please select at least one agent and enter a message.")
|
| 247 |
-
|
| 248 |
-
# --- Agent Control ---
|
| 249 |
-
st.subheader("Agent Control")
|
| 250 |
-
for agent_name in agents:
|
| 251 |
-
agent = agents[agent_name]
|
| 252 |
-
with st.expander(f"{agent_name} ({agent['description']})"):
|
| 253 |
-
if st.button(f"Activate {agent_name}", key=f"activate_{agent_name}"):
|
| 254 |
-
st.session_state.active_agent = agent_name
|
| 255 |
-
st.success(f"{agent_name} activated.")
|
| 256 |
-
if st.button(f"Deactivate {agent_name}", key=f"deactivate_{agent_name}"):
|
| 257 |
-
st.session_state.active_agent = None
|
| 258 |
-
st.success(f"{agent_name} deactivated.")
|
| 259 |
-
|
| 260 |
-
# --- Automate Build Process ---
|
| 261 |
-
st.subheader("Automate Build Process")
|
| 262 |
-
if st.button("Automate"):
|
| 263 |
-
if st.session_state.selected_agents and project_name:
|
| 264 |
-
run_autonomous_build(st.session_state.selected_agents, project_name)
|
| 265 |
-
else:
|
| 266 |
-
st.warning("Please select at least one agent and create a project.")
|
| 267 |
-
|
| 268 |
-
# --- Version Control ---
|
| 269 |
-
st.subheader("Version Control")
|
| 270 |
-
repo_url = st.text_input("Enter repository URL:")
|
| 271 |
-
if st.button("Clone Repository"):
|
| 272 |
-
if repo_url and project_name:
|
| 273 |
-
try:
|
| 274 |
-
git.Repo.clone_from(repo_url, project_name)
|
| 275 |
-
st.success(f"Repository cloned successfully to {project_name}")
|
| 276 |
-
except git.GitCommandError as e:
|
| 277 |
-
st.error(f"Error cloning repository: {e}")
|
| 278 |
-
else:
|
| 279 |
-
st.warning("Please enter a repository URL and create a project.")
|
| 280 |
-
|
| 281 |
-
# --- Collaborative Agent Example ---
|
| 282 |
-
st.subheader("Collaborative Agent Example")
|
| 283 |
-
collab_agents = st.multiselect("Select AI agents for collaboration", list(agents.keys()), key="collab_agent_select")
|
| 284 |
-
collab_project = st.text_input("Enter project name for collaboration:")
|
| 285 |
-
collab_task = st.text_input("Enter collaborative task:")
|
| 286 |
-
if st.button("Run Collaborative Task"):
|
| 287 |
-
if collab_agents and collab_project and collab_task:
|
| 288 |
-
collaborative_agent_example(collab_agents, collab_project, collab_task)
|
| 289 |
-
else:
|
| 290 |
-
st.warning("Please select agents, enter a project name, and specify a task.")
|
| 291 |
-
|
| 292 |
-
# --- Display Information ---
|
| 293 |
-
st.sidebar.subheader("Current State")
|
| 294 |
-
st.sidebar.json(st.session_state)
|
| 295 |
-
if st.session_state.active_agent:
|
| 296 |
-
display_agent_info(st.session_state.active_agent)
|
| 297 |
-
display_workspace_projects()
|
| 298 |
-
display_chat_history()
|
| 299 |
|
| 300 |
if __name__ == "__main__":
|
| 301 |
-
|
| 302 |
-
st.sidebar.info("This is an AI-powered development environment.")
|
| 303 |
-
st.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
|
| 3 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import subprocess
|
| 5 |
+
from huggingface_hub import InferenceClient
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import random
|
| 8 |
+
import time
|
| 9 |
+
from typing import List, Dict
|
| 10 |
+
from flask import Flask, request, jsonify
|
| 11 |
+
|
| 12 |
+
# Constants
|
| 13 |
+
AGENT_TYPES = [
|
| 14 |
+
"Task Executor",
|
| 15 |
+
"Information Retriever",
|
| 16 |
+
"Decision Maker",
|
| 17 |
+
"Data Analyzer",
|
| 18 |
+
]
|
| 19 |
+
TOOL_TYPES = [
|
| 20 |
+
"Web Scraper",
|
| 21 |
+
"Database Connector",
|
| 22 |
+
"API Caller",
|
| 23 |
+
"File Handler",
|
| 24 |
+
"Text Processor",
|
| 25 |
+
]
|
| 26 |
+
VERBOSE = False
|
| 27 |
+
MAX_HISTORY = 100
|
| 28 |
+
MODEL = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 29 |
+
|
| 30 |
+
# Initialize Hugging Face client
|
| 31 |
+
client = InferenceClient(MODEL)
|
| 32 |
+
|
| 33 |
+
# Import necessary prompts and functions from the existing code
|
| 34 |
+
from .prompts import (
|
| 35 |
+
ACTION_PROMPT,
|
| 36 |
+
ADD_PROMPT,
|
| 37 |
+
COMPRESS_HISTORY_PROMPT,
|
| 38 |
+
LOG_PROMPT,
|
| 39 |
+
LOG_RESPONSE,
|
| 40 |
+
MODIFY_PROMPT,
|
| 41 |
+
PREFIX,
|
| 42 |
+
READ_PROMPT,
|
| 43 |
+
TASK_PROMPT,
|
| 44 |
+
UNDERSTAND_TEST_RESULTS_PROMPT,
|
| 45 |
+
)
|
| 46 |
+
from .utils import parse_action, parse_file_content, read_python_module_structure
|
| 47 |
+
|
| 48 |
+
class Agent:
|
| 49 |
+
def __init__(self, name: str, agent_type: str, complexity: int):
|
| 50 |
+
self.name = name
|
| 51 |
+
self.type = agent_type
|
| 52 |
+
self.complexity = complexity
|
| 53 |
+
self.tools = []
|
| 54 |
+
|
| 55 |
+
def add_tool(self, tool):
|
| 56 |
+
self.tools.append(tool)
|
| 57 |
+
|
| 58 |
+
def __str__(self):
|
| 59 |
+
return f"{self.name} ({self.type}) - Complexity: {self.complexity}"
|
| 60 |
+
|
| 61 |
+
class Tool:
|
| 62 |
+
def __init__(self, name: str, tool_type: str):
|
| 63 |
+
self.name = name
|
| 64 |
+
self.type = tool_type
|
| 65 |
+
|
| 66 |
+
def __str__(self):
|
| 67 |
+
return f"{self.name} ({self.type})"
|
| 68 |
+
|
| 69 |
+
class Pypelyne:
|
| 70 |
+
def __init__(self):
|
| 71 |
+
self.agents: List[Agent] = []
|
| 72 |
+
self.tools: List[Tool] = []
|
| 73 |
+
self.history = ""
|
| 74 |
+
self.task = None
|
| 75 |
+
self.purpose = None
|
| 76 |
+
self.directory = None
|
| 77 |
+
|
| 78 |
+
def add_agent(self, agent: Agent):
|
| 79 |
+
self.agents.append(agent)
|
| 80 |
+
|
| 81 |
+
def add_tool(self, tool: Tool):
|
| 82 |
+
self.tools.append(tool)
|
| 83 |
+
|
| 84 |
+
def generate_chat_app(self):
|
| 85 |
+
time.sleep(2) # Simulate processing time
|
| 86 |
+
return f"Chat app generated with {len(self.agents)} agents and {len(self.tools)} tools."
|
| 87 |
+
|
| 88 |
+
def run_gpt(self, prompt_template, stop_tokens, max_tokens, **prompt_kwargs):
|
| 89 |
+
content = PREFIX.format(
|
| 90 |
+
module_summary=read_python_module_structure(self.directory)[0],
|
| 91 |
+
purpose=self.purpose,
|
| 92 |
+
) + prompt_template.format(**prompt_kwargs)
|
| 93 |
+
|
| 94 |
+
if VERBOSE:
|
| 95 |
+
print(LOG_PROMPT.format(content))
|
| 96 |
+
|
| 97 |
+
stream = client.text_generation(
|
| 98 |
+
prompt=content,
|
| 99 |
+
max_new_tokens=max_tokens,
|
| 100 |
+
stop_sequences=stop_tokens if stop_tokens else None,
|
| 101 |
+
do_sample=True,
|
| 102 |
+
temperature=0.7,
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
resp = "".join(token for token in stream)
|
| 106 |
+
|
| 107 |
+
if VERBOSE:
|
| 108 |
+
print(LOG_RESPONSE.format(resp))
|
| 109 |
+
return resp
|
| 110 |
+
|
| 111 |
+
def compress_history(self):
|
| 112 |
+
resp = self.run_gpt(
|
| 113 |
+
COMPRESS_HISTORY_PROMPT,
|
| 114 |
+
stop_tokens=["observation:", "task:", "action:", "thought:"],
|
| 115 |
+
max_tokens=512,
|
| 116 |
+
task=self.task,
|
| 117 |
+
history=self.history,
|
| 118 |
+
)
|
| 119 |
+
self.history = f"observation: {resp}\n"
|
| 120 |
+
|
| 121 |
+
def run_action(self, action_name, action_input):
|
| 122 |
+
if action_name == "COMPLETE":
|
| 123 |
+
return "Task completed."
|
| 124 |
+
|
| 125 |
+
if len(self.history.split("\n")) > MAX_HISTORY:
|
| 126 |
+
if VERBOSE:
|
| 127 |
+
print("COMPRESSING HISTORY")
|
| 128 |
+
self.compress_history()
|
| 129 |
+
|
| 130 |
+
action_funcs = {
|
| 131 |
+
"MAIN": self.call_main,
|
| 132 |
+
"UPDATE-TASK": self.call_set_task,
|
| 133 |
+
"MODIFY-FILE": self.call_modify,
|
| 134 |
+
"READ-FILE": self.call_read,
|
| 135 |
+
"ADD-FILE": self.call_add,
|
| 136 |
+
"TEST": self.call_test,
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
if action_name not in action_funcs:
|
| 140 |
+
return f"Unknown action: {action_name}"
|
| 141 |
+
|
| 142 |
+
print(f"RUN: {action_name} {action_input}")
|
| 143 |
+
return action_funcs[action_name](action_input)
|
| 144 |
+
|
| 145 |
+
def call_main(self, action_input):
|
| 146 |
+
resp = self.run_gpt(
|
| 147 |
+
ACTION_PROMPT,
|
| 148 |
+
stop_tokens=["observation:", "task:"],
|
| 149 |
+
max_tokens=256,
|
| 150 |
+
task=self.task,
|
| 151 |
+
history=self.history,
|
| 152 |
+
)
|
| 153 |
+
lines = resp.strip().strip("\n").split("\n")
|
| 154 |
+
for line in lines:
|
| 155 |
+
if line == "":
|
| 156 |
+
continue
|
| 157 |
+
if line.startswith("thought: "):
|
| 158 |
+
self.history += f"{line}\n"
|
| 159 |
+
elif line.startswith("action: "):
|
| 160 |
+
action_name, action_input = parse_action(line)
|
| 161 |
+
self.history += f"{line}\n"
|
| 162 |
+
return self.run_action(action_name, action_input)
|
| 163 |
+
return "No valid action found."
|
| 164 |
+
|
| 165 |
+
def call_set_task(self, action_input):
|
| 166 |
+
self.task = self.run_gpt(
|
| 167 |
+
TASK_PROMPT,
|
| 168 |
+
stop_tokens=[],
|
| 169 |
+
max_tokens=64,
|
| 170 |
+
task=self.task,
|
| 171 |
+
history=self.history,
|
| 172 |
+
).strip("\n")
|
| 173 |
+
self.history += f"observation: task has been updated to: {self.task}\n"
|
| 174 |
+
return f"Task updated: {self.task}"
|
| 175 |
+
|
| 176 |
+
def call_modify(self, action_input):
|
| 177 |
+
if not os.path.exists(action_input):
|
| 178 |
+
self.history += "observation: file does not exist\n"
|
| 179 |
+
return "File does not exist."
|
| 180 |
+
|
| 181 |
+
content = read_python_module_structure(self.directory)[1]
|
| 182 |
+
f_content = (
|
| 183 |
+
content[action_input] if content[action_input] else "< document is empty >"
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
resp = self.run_gpt(
|
| 187 |
+
MODIFY_PROMPT,
|
| 188 |
+
stop_tokens=["action:", "thought:", "observation:"],
|
| 189 |
+
max_tokens=2048,
|
| 190 |
+
task=self.task,
|
| 191 |
+
history=self.history,
|
| 192 |
+
file_path=action_input,
|
| 193 |
+
file_contents=f_content,
|
| 194 |
+
)
|
| 195 |
+
new_contents, description = parse_file_content(resp)
|
| 196 |
+
if new_contents is None:
|
| 197 |
+
self.history += "observation: failed to modify file\n"
|
| 198 |
+
return "Failed to modify file."
|
| 199 |
+
|
| 200 |
+
with open(action_input, "w") as f:
|
| 201 |
+
f.write(new_contents)
|
| 202 |
+
|
| 203 |
+
self.history += f"observation: file successfully modified\n"
|
| 204 |
+
self.history += f"observation: {description}\n"
|
| 205 |
+
return f"File modified: {action_input}"
|
| 206 |
+
|
| 207 |
+
def call_read(self, action_input):
|
| 208 |
+
if not os.path.exists(action_input):
|
| 209 |
+
self.history += "observation: file does not exist\n"
|
| 210 |
+
return "File does not exist."
|
| 211 |
+
|
| 212 |
+
content = read_python_module_structure(self.directory)[1]
|
| 213 |
+
f_content = (
|
| 214 |
+
content[action_input] if content[action_input] else "< document is empty >"
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
resp = self.run_gpt(
|
| 218 |
+
READ_PROMPT,
|
| 219 |
+
stop_tokens=[],
|
| 220 |
+
max_tokens=256,
|
| 221 |
+
task=self.task,
|
| 222 |
+
history=self.history,
|
| 223 |
+
file_path=action_input,
|
| 224 |
+
file_contents=f_content,
|
| 225 |
+
).strip("\n")
|
| 226 |
+
self.history += f"observation: {resp}\n"
|
| 227 |
+
return f"File read: {action_input}"
|
| 228 |
+
|
| 229 |
+
def call_add(self, action_input):
|
| 230 |
+
d = os.path.dirname(action_input)
|
| 231 |
+
if not d.startswith(self.directory):
|
| 232 |
+
self.history += (
|
| 233 |
+
f"observation: files must be under directory {self.directory}\n"
|
| 234 |
+
)
|
| 235 |
+
return f"Invalid directory: {d}"
|
| 236 |
+
elif not action_input.endswith(".py"):
|
| 237 |
+
self.history += "observation: can only write .py files\n"
|
| 238 |
+
return "Only .py files are allowed."
|
| 239 |
+
else:
|
| 240 |
+
if d and not os.path.exists(d):
|
| 241 |
+
os.makedirs(d)
|
| 242 |
+
if not os.path.exists(action_input):
|
| 243 |
+
resp = self.run_gpt(
|
| 244 |
+
ADD_PROMPT,
|
| 245 |
+
stop_tokens=["action:", "thought:", "observation:"],
|
| 246 |
+
max_tokens=2048,
|
| 247 |
+
task=self.task,
|
| 248 |
+
history=self.history,
|
| 249 |
+
file_path=action_input,
|
| 250 |
+
)
|
| 251 |
+
new_contents, description = parse_file_content(resp)
|
| 252 |
+
if new_contents is None:
|
| 253 |
+
self.history += "observation: failed to write file\n"
|
| 254 |
+
return "Failed to write file."
|
| 255 |
+
|
| 256 |
+
with open(action_input, "w") as f:
|
| 257 |
+
f.write(new_contents)
|
| 258 |
+
|
| 259 |
+
self.history += "observation: file successfully written\n"
|
| 260 |
+
self.history += f"observation: {description}\n"
|
| 261 |
+
return f"File added: {action_input}"
|
| 262 |
+
else:
|
| 263 |
+
self.history += "observation: file already exists\n"
|
| 264 |
+
return "File already exists."
|
| 265 |
+
|
| 266 |
+
def call_test(self, action_input):
|
| 267 |
+
result = subprocess.run(
|
| 268 |
+
["python", "-m", "pytest", "--collect-only", self.directory],
|
| 269 |
+
capture_output=True,
|
| 270 |
+
text=True,
|
| 271 |
+
)
|
| 272 |
+
if result.returncode != 0:
|
| 273 |
+
self.history += f"observation: there are no tests! Test should be written in a test folder under {self.directory}\n"
|
| 274 |
+
return "No tests found."
|
| 275 |
+
result = subprocess.run(
|
| 276 |
+
["python", "-m", "pytest", self.directory], capture_output=True, text=True
|
| 277 |
+
)
|
| 278 |
+
if result.returncode == 0:
|
| 279 |
+
self.history += "observation: tests pass\n"
|
| 280 |
+
return "All tests passed."
|
| 281 |
+
|
| 282 |
+
resp = self.run_gpt(
|
| 283 |
+
UNDERSTAND_TEST_RESULTS_PROMPT,
|
| 284 |
+
stop_tokens=[],
|
| 285 |
+
max_tokens=256,
|
| 286 |
+
task=self.task,
|
| 287 |
+
history=self.history,
|
| 288 |
+
stdout=result.stdout[:5000],
|
| 289 |
+
stderr=result.stderr[:5000],
|
| 290 |
+
)
|
| 291 |
+
self.history += f"observation: tests failed: {resp}\n"
|
| 292 |
+
return f"Tests failed: {resp}"
|
| 293 |
+
|
| 294 |
+
pypelyne = Pypelyne()
|
| 295 |
+
|
| 296 |
+
def create_agent(name: str, agent_type: str, complexity: int) -> Agent:
|
| 297 |
+
agent = Agent(name, agent_type, complexity)
|
| 298 |
+
pypelyne.add_agent(agent)
|
| 299 |
+
return agent
|
| 300 |
+
|
| 301 |
+
def create_tool(name: str, tool_type: str) -> Tool:
|
| 302 |
+
tool = Tool(name, tool_type)
|
| 303 |
+
pypelyne.add_tool(tool)
|
| 304 |
+
return tool
|
| 305 |
+
|
| 306 |
+
def main():
|
| 307 |
+
# Create a Flask app
|
| 308 |
+
app = Flask(__name__)
|
| 309 |
+
|
| 310 |
+
# Define a route for the chat interface
|
| 311 |
+
@app.route("/chat", methods=["GET", "POST"])
|
| 312 |
+
def chat():
|
| 313 |
+
if request.method == "POST":
|
| 314 |
+
# Get the user's input
|
| 315 |
+
user_input = request.form["input"]
|
| 316 |
+
|
| 317 |
+
# Run the input through the Pypelyne
|
| 318 |
+
response = pypelyne.run_action("MAIN", user_input)
|
| 319 |
+
|
| 320 |
+
# Return the response
|
| 321 |
+
return jsonify({"response": response})
|
| 322 |
+
else:
|
| 323 |
+
# Return the chat interface
|
| 324 |
+
return """
|
| 325 |
+
<html>
|
| 326 |
+
<body>
|
| 327 |
+
<h1>Pypelyne Chat Interface</h1>
|
| 328 |
+
<form action="/chat" method="post">
|
| 329 |
+
<input type="text" name="input" placeholder="Enter your input">
|
| 330 |
+
<input type="submit" value="Submit">
|
| 331 |
+
</form>
|
| 332 |
+
<div id="response"></div>
|
| 333 |
+
<script>
|
| 334 |
+
// Update the response div with the response from the server
|
| 335 |
+
function updateResponse(response) {
|
| 336 |
+
document.getElementById("response").innerHTML = response;
|
| 337 |
+
}
|
| 338 |
+
</script>
|
| 339 |
+
</body>
|
| 340 |
+
</html>
|
| 341 |
+
"""
|
| 342 |
+
|
| 343 |
+
# Define a route for the agent creation interface
|
| 344 |
+
@app.route("/create_agent", methods=["GET", "POST"])
|
| 345 |
+
def create_agent_interface():
|
| 346 |
+
if request.method == "POST":
|
| 347 |
+
# Get the agent's name, type, and complexity
|
| 348 |
+
name = request.form["name"]
|
| 349 |
+
agent_type = request.form["type"]
|
| 350 |
+
complexity = int(request.form["complexity"])
|
| 351 |
+
|
| 352 |
+
# Create the agent
|
| 353 |
+
agent = create_agent(name, agent_type, complexity)
|
| 354 |
+
|
| 355 |
+
# Return a success message
|
| 356 |
+
return jsonify({"message": f"Agent {name} created successfully"})
|
| 357 |
+
else:
|
| 358 |
+
# Return the agent creation interface
|
| 359 |
+
return """
|
| 360 |
+
<html>
|
| 361 |
+
<body>
|
| 362 |
+
<h1>Create Agent</h1>
|
| 363 |
+
<form action="/create_agent" method="post">
|
| 364 |
+
<label for="name">Name:</label>
|
| 365 |
+
<input type="text" id="name" name="name"><br><br>
|
| 366 |
+
<label for="type">Type:</label>
|
| 367 |
+
<select id="type" name="type">
|
| 368 |
+
<option value="Task Executor">Task Executor</option>
|
| 369 |
+
<option value="Information Retriever">Information Retriever</option>
|
| 370 |
+
<option value="Decision Maker">Decision Maker</option>
|
| 371 |
+
<option value="Data Analyzer">Data Analyzer</option>
|
| 372 |
+
</select><br><br>
|
| 373 |
+
<label for="complexity">Complexity:</label>
|
| 374 |
+
<input type="number" id="complexity" name="complexity"><br><br>
|
| 375 |
+
<input type="submit" value="Create Agent">
|
| 376 |
+
</form>
|
| 377 |
+
</body>
|
| 378 |
+
</html>
|
| 379 |
+
"""
|
| 380 |
+
|
| 381 |
+
# Define a route for the tool creation interface
|
| 382 |
+
@app.route("/create_tool", methods=["GET", "POST"])
|
| 383 |
+
def create_tool_interface():
|
| 384 |
+
if request.method == "POST":
|
| 385 |
+
# Get the tool's name and type
|
| 386 |
+
name = request.form["name"]
|
| 387 |
+
tool_type = request.form["type"]
|
| 388 |
+
|
| 389 |
+
# Create the tool
|
| 390 |
+
tool = create_tool(name, tool_type)
|
| 391 |
+
|
| 392 |
+
# Return a success message
|
| 393 |
+
return jsonify({"message": f"Tool {name} created successfully"})
|
| 394 |
else:
|
| 395 |
+
# Return the tool creation interface
|
| 396 |
+
return """
|
| 397 |
+
<html>
|
| 398 |
+
<body>
|
| 399 |
+
<h1>Create Tool</h1>
|
| 400 |
+
<form action="/create_tool" method="post">
|
| 401 |
+
<label for="name">Name:</label>
|
| 402 |
+
<input type="text" id="name" name="name"><br><br>
|
| 403 |
+
<label for="type">Type:</label>
|
| 404 |
+
<select id="type" name="type">
|
| 405 |
+
<option value="Web Scraper">Web Scraper</option>
|
| 406 |
+
<option value="Database Connector">Database Connector</option>
|
| 407 |
+
<option value="API Caller">API Caller</option>
|
| 408 |
+
<option value="File Handler">File Handler</option>
|
| 409 |
+
<option value="Text Processor">Text Processor</option>
|
| 410 |
+
</select><br><br>
|
| 411 |
+
<input type="submit" value="Create Tool">
|
| 412 |
+
</form>
|
| 413 |
+
</body>
|
| 414 |
+
</html>
|
| 415 |
+
"""
|
| 416 |
+
|
| 417 |
+
# Run the app
|
| 418 |
+
if __name__ == "__main__":
|
| 419 |
+
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
|
| 421 |
if __name__ == "__main__":
|
| 422 |
+
main()
|
|
|
|
|
|