Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,47 @@
|
|
|
|
|
|
1 |
import os
|
|
|
2 |
import subprocess
|
3 |
-
|
4 |
-
import gradio as gr
|
5 |
-
import random
|
6 |
-
import prompts
|
7 |
import time
|
8 |
-
from
|
9 |
-
from .
|
10 |
-
from
|
11 |
-
|
12 |
-
# Simulated agent and tool libraries
|
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 |
-
|
27 |
-
# Initialize Hugging Face client
|
28 |
-
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
29 |
-
|
30 |
-
VERBOSE = False
|
31 |
-
MAX_HISTORY = 100
|
32 |
-
MODEL = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
33 |
-
|
34 |
-
# Import necessary prompts and functions from the existing code
|
35 |
-
from prompts import (
|
36 |
-
ACTION_PROMPT,
|
37 |
-
ADD_PROMPT,
|
38 |
-
COMPRESS_HISTORY_PROMPT,
|
39 |
-
LOG_PROMPT,
|
40 |
-
LOG_RESPONSE,
|
41 |
-
MODIFY_PROMPT,
|
42 |
-
PREFIX,
|
43 |
-
READ_PROMPT,
|
44 |
-
TASK_PROMPT,
|
45 |
-
UNDERSTAND_TEST_RESULTS_PROMPT,
|
46 |
-
)
|
47 |
-
from .utils import parse_action, parse_file_content, read_python_module_structure
|
48 |
-
from flask import Flask, request, jsonify
|
49 |
|
|
|
|
|
50 |
|
51 |
class Agent:
|
52 |
def __init__(self, name: str, agent_type: str, complexity: int):
|
53 |
-
self.name = name
|
54 |
-
self.
|
55 |
-
self.complexity = complexity
|
56 |
-
self.tools = []
|
57 |
|
58 |
-
def add_tool(self, tool):
|
59 |
self.tools.append(tool)
|
60 |
|
61 |
def __str__(self):
|
62 |
-
return f"{self.name} ({self.
|
63 |
-
|
64 |
|
65 |
class Tool:
|
66 |
def __init__(self, name: str, tool_type: str):
|
67 |
-
self.name = name
|
68 |
-
self.
|
69 |
|
70 |
def __str__(self):
|
71 |
-
return f"{self.name} ({self.
|
72 |
-
|
73 |
|
74 |
class Pypelyne:
|
75 |
def __init__(self):
|
76 |
self.agents: List[Agent] = []
|
77 |
self.tools: List[Tool] = []
|
78 |
-
self.history = ""
|
79 |
-
self.task =
|
80 |
-
self.purpose =
|
81 |
-
self.directory =
|
|
|
82 |
|
83 |
def add_agent(self, agent: Agent):
|
84 |
self.agents.append(agent)
|
@@ -86,28 +49,29 @@ class Pypelyne:
|
|
86 |
def add_tool(self, tool: Tool):
|
87 |
self.tools.append(tool)
|
88 |
|
89 |
-
def generate_chat_app(self):
|
90 |
-
time.sleep(2)
|
91 |
return f"Chat app generated with {len(self.agents)} agents and {len(self.tools)} tools."
|
92 |
|
93 |
-
def run_gpt(
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
98 |
|
99 |
if VERBOSE:
|
100 |
print(LOG_PROMPT.format(content))
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
)
|
109 |
-
|
110 |
-
resp = "".join(token for token in stream)
|
111 |
|
112 |
if VERBOSE:
|
113 |
print(LOG_RESPONSE.format(resp))
|
@@ -123,37 +87,29 @@ class Pypelyne:
|
|
123 |
)
|
124 |
self.history = f"observation: {resp}\n"
|
125 |
|
126 |
-
def run_action(self, action_name, action_input):
|
127 |
if action_name == "COMPLETE":
|
128 |
return "Task completed."
|
129 |
|
130 |
if len(self.history.split("\n")) > MAX_HISTORY:
|
131 |
-
if VERBOSE:
|
132 |
-
print("COMPRESSING HISTORY")
|
133 |
self.compress_history()
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
"UPDATE-TASK": self.call_set_task,
|
138 |
-
"MODIFY-FILE": self.call_modify,
|
139 |
-
"READ-FILE": self.call_read,
|
140 |
-
"ADD-FILE": self.call_add,
|
141 |
-
"TEST": self.call_test,
|
142 |
-
}
|
143 |
|
144 |
-
|
145 |
-
|
|
|
|
|
146 |
|
147 |
-
|
148 |
-
return action_funcs[action_name](action_input)
|
149 |
-
|
150 |
-
def call_main(self, action_input):
|
151 |
resp = self.run_gpt(
|
152 |
-
ACTION_PROMPT,
|
153 |
stop_tokens=["observation:", "task:"],
|
154 |
max_tokens=256,
|
155 |
task=self.task,
|
156 |
history=self.history,
|
|
|
157 |
)
|
158 |
lines = resp.strip().strip("\n").split("\n")
|
159 |
for line in lines:
|
@@ -161,276 +117,134 @@ class Pypelyne:
|
|
161 |
continue
|
162 |
if line.startswith("thought: "):
|
163 |
self.history += f"{line}\n"
|
164 |
-
elif line.startswith("action: "):
|
165 |
action_name, action_input = parse_action(line)
|
166 |
-
self.
|
167 |
-
return self.run_action(action_name, action_input)
|
168 |
return "No valid action found."
|
169 |
|
170 |
-
def call_set_task(self, action_input):
|
171 |
-
self.task =
|
172 |
-
TASK_PROMPT,
|
173 |
-
stop_tokens=[],
|
174 |
-
max_tokens=64,
|
175 |
-
task=self.task,
|
176 |
-
history=self.history,
|
177 |
-
).strip("\n")
|
178 |
-
self.history += f"observation: task has been updated to: {self.task}\n"
|
179 |
return f"Task updated: {self.task}"
|
180 |
|
181 |
-
def call_modify(self, action_input):
|
182 |
-
|
183 |
-
|
184 |
-
return "File does not exist."
|
185 |
-
|
186 |
-
content = read_python_module_structure(self.directory)[1]
|
187 |
-
f_content = (
|
188 |
-
content[action_input] if content[action_input] else "< document is empty >"
|
189 |
-
)
|
190 |
|
191 |
resp = self.run_gpt(
|
192 |
-
MODIFY_PROMPT,
|
193 |
stop_tokens=["action:", "thought:", "observation:"],
|
194 |
max_tokens=2048,
|
195 |
task=self.task,
|
196 |
history=self.history,
|
197 |
file_path=action_input,
|
198 |
-
file_contents=
|
|
|
199 |
)
|
200 |
-
new_contents
|
201 |
-
if new_contents is None:
|
202 |
-
self.history += "observation: failed to modify file\n"
|
203 |
-
return "Failed to modify file."
|
204 |
|
205 |
-
with open(action_input, "w") as
|
206 |
-
|
207 |
|
208 |
self.history += f"observation: file successfully modified\n"
|
209 |
-
self.history += f"observation: {description}\n"
|
210 |
return f"File modified: {action_input}"
|
211 |
|
212 |
-
def call_read(self, action_input):
|
213 |
-
|
214 |
-
|
215 |
-
return "File does not exist."
|
216 |
|
217 |
-
|
218 |
-
|
219 |
-
content[action_input] if content[action_input] else "< document is empty >"
|
220 |
-
)
|
221 |
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
max_tokens=256,
|
226 |
-
task=self.task,
|
227 |
-
history=self.history,
|
228 |
-
file_path=action_input,
|
229 |
-
file_contents=f_content,
|
230 |
-
).strip("\n")
|
231 |
-
self.history += f"observation: {resp}\n"
|
232 |
-
return f"File read: {action_input}"
|
233 |
-
|
234 |
-
def call_add(self, action_input):
|
235 |
-
d = os.path.dirname(action_input)
|
236 |
-
if not d.startswith(self.directory):
|
237 |
-
self.history += (
|
238 |
-
f"observation: files must be under directory {self.directory}\n"
|
239 |
-
)
|
240 |
-
return f"Invalid directory: {d}"
|
241 |
-
elif not action_input.endswith(".py"):
|
242 |
-
self.history += "observation: can only write .py files\n"
|
243 |
-
return "Only .py files are allowed."
|
244 |
-
else:
|
245 |
-
if d and not os.path.exists(d):
|
246 |
-
os.makedirs(d)
|
247 |
-
if not os.path.exists(action_input):
|
248 |
-
resp = self.run_gpt(
|
249 |
-
ADD_PROMPT,
|
250 |
-
stop_tokens=["action:", "thought:", "observation:"],
|
251 |
-
max_tokens=2048,
|
252 |
-
task=self.task,
|
253 |
-
history=self.history,
|
254 |
-
file_path=action_input,
|
255 |
-
)
|
256 |
-
new_contents, description = parse_file_content(resp)
|
257 |
-
if new_contents is None:
|
258 |
-
self.history += "observation: failed to write file\n"
|
259 |
-
return "Failed to write file."
|
260 |
-
|
261 |
-
with open(action_input, "w") as f:
|
262 |
-
f.write(new_contents)
|
263 |
-
|
264 |
-
self.history += "observation: file successfully written\n"
|
265 |
-
self.history += f"observation: {description}\n"
|
266 |
-
return f"File added: {action_input}"
|
267 |
-
else:
|
268 |
-
self.history += "observation: file already exists\n"
|
269 |
-
return "File already exists."
|
270 |
-
|
271 |
-
def call_test(self, action_input):
|
272 |
-
result = subprocess.run(
|
273 |
-
["python", "-m", "pytest", "--collect-only", self.directory],
|
274 |
-
capture_output=True,
|
275 |
-
text=True,
|
276 |
-
)
|
277 |
-
if result.returncode != 0:
|
278 |
-
self.history += f"observation: there are no tests! Test should be written in a test folder under {self.directory}\n"
|
279 |
-
return "No tests found."
|
280 |
-
result = subprocess.run(
|
281 |
-
["python", "-m", "pytest", self.directory], capture_output=True, text=True
|
282 |
-
)
|
283 |
-
if result.returncode == 0:
|
284 |
-
self.history += "observation: tests pass\n"
|
285 |
-
return "All tests passed."
|
286 |
|
287 |
-
|
288 |
-
|
289 |
-
stop_tokens=[],
|
290 |
-
max_tokens=256,
|
291 |
-
task=self.task,
|
292 |
-
history=self.history,
|
293 |
-
stdout=result.stdout[:5000],
|
294 |
-
stderr=result.stderr[:5000],
|
295 |
-
)
|
296 |
-
self.history += f"observation: tests failed: {resp}\n"
|
297 |
-
return f"Tests failed: {resp}"
|
298 |
|
|
|
|
|
299 |
|
300 |
-
|
|
|
|
|
301 |
|
|
|
|
|
302 |
|
303 |
-
|
|
|
|
|
|
|
|
|
304 |
agent = Agent(name, agent_type, complexity)
|
305 |
pypelyne.add_agent(agent)
|
306 |
-
return
|
307 |
|
308 |
-
def create_tool(name: str, tool_type: str) ->
|
309 |
tool = Tool(name, tool_type)
|
310 |
pypelyne.add_tool(tool)
|
311 |
-
return
|
312 |
-
|
313 |
-
def assign_tool(agent_name: str, tool_name: str) -> str:
|
314 |
-
agent = next((a for a in pypelyne.agents if a.name == agent_name), None)
|
315 |
-
tool = next((t for t in pypelyne.tools if t.name == tool_name), None)
|
316 |
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
else:
|
321 |
-
return "Agent or tool not found."
|
322 |
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
return (
|
328 |
-
"\n".join(str(agent) for agent in pypelyne.agents) or "No agents created yet."
|
329 |
)
|
330 |
-
|
331 |
-
def list_tools() -> str:
|
332 |
-
return "\n".join(str(tool) for tool in pypelyne.tools) or "No tools created yet."
|
333 |
-
|
334 |
-
def chat_with_pypelyne(message: str) -> str:
|
335 |
-
return pypelyne.run_action("MAIN", message)
|
336 |
-
|
337 |
-
def set_purpose_and_directory(purpose: str, directory: str) -> str:
|
338 |
-
pypelyne.purpose = purpose
|
339 |
pypelyne.directory = directory
|
340 |
-
return f"Purpose set to: {purpose}\nWorking directory set to: {directory}"
|
341 |
-
|
342 |
-
with gr.Blocks() as app:
|
343 |
-
gr.Markdown("# Welcome to Pypelyne")
|
344 |
-
gr.Markdown("Create your custom pipeline with agents and tools, then chat with it!")
|
345 |
-
|
346 |
-
with gr.Tab("Setup"):
|
347 |
-
purpose_input = gr.Textbox(label="Set Purpose")
|
348 |
-
directory_input = gr.Textbox(label="Set Working Directory")
|
349 |
-
setup_btn = gr.Button("Set Purpose and Directory")
|
350 |
-
setup_output = gr.Textbox(label="Setup Output")
|
351 |
-
setup_btn.click(
|
352 |
-
set_purpose_and_directory,
|
353 |
-
inputs=[purpose_input, directory_input],
|
354 |
-
outputs=setup_output,
|
355 |
-
)
|
356 |
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
create_agent_btn = gr.Button("Create Agent")
|
364 |
-
agent_output = gr.Textbox(label="Output")
|
365 |
-
create_agent_btn.click(
|
366 |
-
create_agent,
|
367 |
-
inputs=[agent_name, agent_type, agent_complexity],
|
368 |
-
outputs=agent_output,
|
369 |
-
)
|
370 |
-
|
371 |
-
with gr.Tab("Create Tools"):
|
372 |
-
tool_name = gr.Textbox(label="Tool Name")
|
373 |
-
tool_type = gr.Dropdown(choices=TOOL_TYPES, label="Tool Type")
|
374 |
-
create_tool_btn = gr.Button("Create Tool")
|
375 |
-
tool_output = gr.Textbox(label="Output")
|
376 |
-
create_tool_btn.click(
|
377 |
-
create_tool, inputs=[tool_name, tool_type], outputs=tool_output
|
378 |
-
)
|
379 |
-
|
380 |
-
with gr.Tab("Assign Tools"):
|
381 |
-
agent_select = gr.Dropdown(choices=[], label="Select Agent")
|
382 |
-
tool_select = gr.Dropdown(choices=[], label="Select Tool")
|
383 |
-
assign_tool_btn = gr.Button("Assign Tool")
|
384 |
-
assign_output = gr.Textbox(label="Output")
|
385 |
-
assign_tool_btn.click(
|
386 |
-
assign_tool, inputs=[agent_select, tool_select], outputs=assign_output
|
387 |
-
)
|
388 |
-
|
389 |
-
with gr.Tab("Generate Chat App"):
|
390 |
-
generate_btn = gr.Button("Generate Chat App")
|
391 |
-
generate_output = gr.Textbox(label="Output")
|
392 |
-
generate_btn.click(generate_chat_app, outputs=generate_output)
|
393 |
-
|
394 |
-
with gr.Tab("Chat with Pypelyne"):
|
395 |
-
chat_input = gr.Textbox(label="Your Message")
|
396 |
-
chat_output = gr.Textbox(label="Pypelyne's Response")
|
397 |
-
chat_btn = gr.Button("Send")
|
398 |
-
chat_btn.click(chat_with_pypelyne, inputs=chat_input, outputs=chat_output)
|
399 |
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
406 |
|
407 |
-
|
408 |
-
return gr.Dropdown.update(
|
409 |
-
choices=[agent.name for agent in pypelyne.agents]
|
410 |
-
), gr.Dropdown.update(choices=[tool.name for tool in pypelyne.tools])
|
411 |
|
412 |
-
|
413 |
-
|
|
|
|
|
414 |
|
415 |
if __name__ == "__main__":
|
416 |
-
|
417 |
-
app = Flask(__name__)
|
418 |
-
|
419 |
-
@app.route("/chat", methods=["POST"])
|
420 |
-
def chat():
|
421 |
-
message = request.json["message"]
|
422 |
-
response = chat_with_pypelyne(message)
|
423 |
-
return jsonify({"response": response})
|
424 |
-
|
425 |
-
@app.route("/agents", methods=["GET"])
|
426 |
-
def get_agents():
|
427 |
-
agents = list_agents()
|
428 |
-
return jsonify({"agents": agents})
|
429 |
-
|
430 |
-
@app.route("/tools", methods=["GET"])
|
431 |
-
def get_tools():
|
432 |
-
tools = list_tools()
|
433 |
-
return jsonify({"tools": tools})
|
434 |
-
|
435 |
-
if __name__ == "__main__":
|
436 |
-
app.run()
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
from typing import List, Dict, Union
|
3 |
import os
|
4 |
+
import re
|
5 |
import subprocess
|
6 |
+
import streamlit as st
|
|
|
|
|
|
|
7 |
import time
|
8 |
+
from langchain_core.prompts import PromptTemplate
|
9 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
10 |
+
from huggingface_hub.inference_api import InferenceApi as InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# Load LLM
|
13 |
+
llm = HuggingFaceHub(repo_id="tiiuae/falcon-7b-instruct", model_kwargs={"temperature": 0.1, "max_new_tokens": 500})
|
14 |
|
15 |
class Agent:
|
16 |
def __init__(self, name: str, agent_type: str, complexity: int):
|
17 |
+
self.name: str = name
|
18 |
+
self.agent_type: str = agent_type
|
19 |
+
self.complexity: int = complexity
|
20 |
+
self.tools: List[Tool] = []
|
21 |
|
22 |
+
def add_tool(self, tool: Tool):
|
23 |
self.tools.append(tool)
|
24 |
|
25 |
def __str__(self):
|
26 |
+
return f"{self.name} ({self.agent_type}) - Complexity: {self.complexity}"
|
|
|
27 |
|
28 |
class Tool:
|
29 |
def __init__(self, name: str, tool_type: str):
|
30 |
+
self.name: str = name
|
31 |
+
self.tool_type: str = tool_type
|
32 |
|
33 |
def __str__(self):
|
34 |
+
return f"{self.name} ({self.tool_type})"
|
|
|
35 |
|
36 |
class Pypelyne:
|
37 |
def __init__(self):
|
38 |
self.agents: List[Agent] = []
|
39 |
self.tools: List[Tool] = []
|
40 |
+
self.history: str = ""
|
41 |
+
self.task: str = ""
|
42 |
+
self.purpose: str = ""
|
43 |
+
self.directory: str = ""
|
44 |
+
self.task_queue: list = []
|
45 |
|
46 |
def add_agent(self, agent: Agent):
|
47 |
self.agents.append(agent)
|
|
|
49 |
def add_tool(self, tool: Tool):
|
50 |
self.tools.append(tool)
|
51 |
|
52 |
+
def generate_chat_app(self) -> str:
|
53 |
+
time.sleep(2)
|
54 |
return f"Chat app generated with {len(self.agents)} agents and {len(self.tools)} tools."
|
55 |
|
56 |
+
def run_gpt(
|
57 |
+
self,
|
58 |
+
prompt_template: PromptTemplate,
|
59 |
+
stop_tokens: List[str],
|
60 |
+
max_tokens: int,
|
61 |
+
**prompt_kwargs,
|
62 |
+
) -> str:
|
63 |
+
content = f"""{PREFIX}
|
64 |
+
{prompt_template.format(**prompt_kwargs)}"""
|
65 |
|
66 |
if VERBOSE:
|
67 |
print(LOG_PROMPT.format(content))
|
68 |
|
69 |
+
try:
|
70 |
+
stream = llm.predict(content)
|
71 |
+
resp = "".join(stream)
|
72 |
+
except Exception as e:
|
73 |
+
print(f"Error in run_gpt: {e}")
|
74 |
+
resp = f"Error: {e}"
|
|
|
|
|
|
|
75 |
|
76 |
if VERBOSE:
|
77 |
print(LOG_RESPONSE.format(resp))
|
|
|
87 |
)
|
88 |
self.history = f"observation: {resp}\n"
|
89 |
|
90 |
+
def run_action(self, action_name: str, action_input: Union[str, List[str]], tools: List[Tool] = None) -> str:
|
91 |
if action_name == "COMPLETE":
|
92 |
return "Task completed."
|
93 |
|
94 |
if len(self.history.split("\n")) > MAX_HISTORY:
|
|
|
|
|
95 |
self.compress_history()
|
96 |
|
97 |
+
if action_name not in self.task_queue:
|
98 |
+
self.task_queue.append(action_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
+
task_function = getattr(self, f"call_{action_name.lower()}")
|
101 |
+
result = task_function(action_input, tools)
|
102 |
+
self.task_queue.pop(0)
|
103 |
+
return result
|
104 |
|
105 |
+
def call_main(self, action_input: List[str]) -> str:
|
|
|
|
|
|
|
106 |
resp = self.run_gpt(
|
107 |
+
f"{ACTION_PROMPT}",
|
108 |
stop_tokens=["observation:", "task:"],
|
109 |
max_tokens=256,
|
110 |
task=self.task,
|
111 |
history=self.history,
|
112 |
+
actions=action_input,
|
113 |
)
|
114 |
lines = resp.strip().strip("\n").split("\n")
|
115 |
for line in lines:
|
|
|
117 |
continue
|
118 |
if line.startswith("thought: "):
|
119 |
self.history += f"{line}\n"
|
|
|
120 |
action_name, action_input = parse_action(line)
|
121 |
+
self.run_action(action_name, action_input)
|
|
|
122 |
return "No valid action found."
|
123 |
|
124 |
+
def call_set_task(self, action_input: str) -> str:
|
125 |
+
self.task = action_input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
return f"Task updated: {self.task}"
|
127 |
|
128 |
+
def call_modify(self, action_input: str, agent: Agent) -> str:
|
129 |
+
with open(action_input, "r") as file:
|
130 |
+
file_content = file.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
resp = self.run_gpt(
|
133 |
+
f"{MODIFY_PROMPT}",
|
134 |
stop_tokens=["action:", "thought:", "observation:"],
|
135 |
max_tokens=2048,
|
136 |
task=self.task,
|
137 |
history=self.history,
|
138 |
file_path=action_input,
|
139 |
+
file_contents=file_content,
|
140 |
+
agent=agent,
|
141 |
)
|
142 |
+
new_contents = resp.strip()
|
|
|
|
|
|
|
143 |
|
144 |
+
with open(action_input, "w") as file:
|
145 |
+
file.write(new_contents)
|
146 |
|
147 |
self.history += f"observation: file successfully modified\n"
|
|
|
148 |
return f"File modified: {action_input}"
|
149 |
|
150 |
+
def call_read(self, action_input: str) -> str:
|
151 |
+
with open(action_input, "r") as file:
|
152 |
+
file_content = file.read()
|
|
|
153 |
|
154 |
+
self.history += f"observation: {file_content}\n"
|
155 |
+
return file_content
|
|
|
|
|
156 |
|
157 |
+
def call_add(self, action_input: str) -> str:
|
158 |
+
if not os.path.exists(self.directory):
|
159 |
+
os.makedirs(self.directory)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
+
with open(os.path.join(self.directory, action_input), "w") as file:
|
162 |
+
file.write("")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
+
self.history += f"observation: file created: {action_input}\n"
|
165 |
+
return f"File created: {action_input}"
|
166 |
|
167 |
+
def call_test(self, action_input: str) -> str:
|
168 |
+
result = subprocess.run(["python", os.path.join(self.directory, action_input)], capture_output=True, text=True)
|
169 |
+
error_message = result.stderr.strip()
|
170 |
|
171 |
+
self.history += f"observation: tests {('passed' if error_message == '' else 'failed')}\n"
|
172 |
+
return f"Tests {'passed' if error_message == '' else 'failed'}: {error_message}"
|
173 |
|
174 |
+
# Global Pypelyne Instance
|
175 |
+
pypelyne = Pypelyne()
|
176 |
+
|
177 |
+
# Helper Functions
|
178 |
+
def create_agent(name: str, agent_type: str, complexity: int) -> Agent:
|
179 |
agent = Agent(name, agent_type, complexity)
|
180 |
pypelyne.add_agent(agent)
|
181 |
+
return agent
|
182 |
|
183 |
+
def create_tool(name: str, tool_type: str) -> Tool:
|
184 |
tool = Tool(name, tool_type)
|
185 |
pypelyne.add_tool(tool)
|
186 |
+
return tool
|
|
|
|
|
|
|
|
|
187 |
|
188 |
+
# Streamlit App Code
|
189 |
+
def main():
|
190 |
+
st.title("🧠 Pypelyne: Your AI-Powered Coding Assistant")
|
|
|
|
|
191 |
|
192 |
+
# Settings
|
193 |
+
st.sidebar.title("⚙️ Settings")
|
194 |
+
directory = st.sidebar.text_input(
|
195 |
+
"Project Directory:", value=pypelyne.directory, help="Path to your coding project"
|
|
|
|
|
196 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
pypelyne.directory = directory
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
+
purpose = st.sidebar.text_area(
|
200 |
+
"Project Purpose:",
|
201 |
+
value=pypelyne.purpose,
|
202 |
+
help="Describe the purpose of your coding project.",
|
203 |
+
)
|
204 |
+
pypelyne.purpose = purpose
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
|
206 |
+
# Agent and Tool Management
|
207 |
+
st.sidebar.header("🤖 Agents")
|
208 |
+
agents = st.sidebar.column(2)
|
209 |
+
tools = st.sidebar.column(1)
|
210 |
+
|
211 |
+
for agent in pypelyne.agents:
|
212 |
+
agents.write(f"- {agent}")
|
213 |
+
|
214 |
+
if st.sidebar.button("Create New Agent"):
|
215 |
+
agent_name = st.sidebar.text_input("Agent Name:")
|
216 |
+
agent_type = st.sidebar.selectbox("Agent Type:", ["Task Executor", "Information Retriever", "Decision Maker", "Data Analyzer"])
|
217 |
+
agent_complexity = st.sidebar.slider("Complexity (1-5):", 1, 5, 3)
|
218 |
+
new_agent = create_agent(agent_name, agent_type, agent_complexity)
|
219 |
+
pypelyne.agents = pypelyne.agents + [new_agent]
|
220 |
+
|
221 |
+
st.sidebar.header("🛠️ Tools")
|
222 |
+
for tool in pypelyne.tools:
|
223 |
+
tools.write(f"- {tool}")
|
224 |
+
|
225 |
+
if st.sidebar.button("Create New Tool"):
|
226 |
+
tool_name = st.sidebar.text_input("Tool Name:")
|
227 |
+
tool_type = st.sidebar.selectbox("Tool Type:", ["Web Scraper", "Database Connector", "API Caller", "File Handler", "Text Processor"])
|
228 |
+
new_tool = create_tool(tool_name, tool_type)
|
229 |
+
pypelyne.tools = pypelyne.tools + [new_tool]
|
230 |
+
|
231 |
+
# Main Content Area
|
232 |
+
st.header("💻 Code Interaction")
|
233 |
+
|
234 |
+
task = st.text_area(
|
235 |
+
"🎯 Task:",
|
236 |
+
value=pypelyne.task,
|
237 |
+
help="Describe the coding task you want to perform.",
|
238 |
+
)
|
239 |
+
if task:
|
240 |
+
pypelyne.task = task
|
241 |
|
242 |
+
user_input = st.text_input("💬 Your Input:")
|
|
|
|
|
|
|
243 |
|
244 |
+
if st.button("Execute"):
|
245 |
+
if user_input:
|
246 |
+
response = pypelyne.run_action("main", [user_input])
|
247 |
+
st.write("Pypelyne Says: ", response)
|
248 |
|
249 |
if __name__ == "__main__":
|
250 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|