Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,19 @@
|
|
1 |
import os
|
2 |
import subprocess
|
3 |
import random
|
4 |
-
from
|
|
|
|
|
|
|
|
|
5 |
from datetime import datetime
|
6 |
import logging
|
7 |
-
|
8 |
-
import gradio as gr
|
9 |
-
from huggingface_hub import InferenceClient
|
10 |
|
11 |
# --- Configuration ---
|
12 |
-
MODEL_NAME = "mistralai/Mixtral-8x7B-Instruct-v0.1" #
|
13 |
-
MAX_HISTORY_TURNS = 5 # Number of
|
14 |
-
|
15 |
-
VERBOSE_LOGGING = True # Enable verbose logging for debugging
|
16 |
-
DEFAULT_AGENT = "WEB_DEV" # Default agent to use
|
17 |
|
18 |
# --- Logging Setup ---
|
19 |
logging.basicConfig(
|
@@ -23,323 +23,220 @@ logging.basicConfig(
|
|
23 |
)
|
24 |
|
25 |
# --- Agent Definitions ---
|
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 |
-
task: The current task.
|
54 |
|
55 |
-
|
56 |
-
The prompt string.
|
57 |
-
"""
|
58 |
-
now = datetime.now()
|
59 |
-
date_time_str = now.strftime("%Y-%m-%d %H:%M:%S")
|
60 |
-
prompt = f"""
|
61 |
-
{date_time_str}
|
62 |
-
Agent: {self.name}
|
63 |
-
Task: {task}
|
64 |
-
History:
|
65 |
-
{self.format_history(history)}
|
66 |
-
Message: {message}
|
67 |
-
"""
|
68 |
-
return prompt
|
69 |
-
|
70 |
-
def format_history(self, history: List[Tuple[str, str]]) -> str:
|
71 |
-
"""Formats the conversation history for the prompt."""
|
72 |
-
formatted_history = ""
|
73 |
-
for user_message, agent_response in history[-MAX_HISTORY_TURNS:]:
|
74 |
-
formatted_history += f"[INST] {user_message} [/INST]\n{agent_response}\n"
|
75 |
-
return formatted_history
|
76 |
-
|
77 |
-
class WebDevAgent(Agent):
|
78 |
-
"""Agent for web development tasks."""
|
79 |
-
|
80 |
-
def __init__(self):
|
81 |
-
super().__init__(name="WEB_DEV", description="Agent specialized in web development tasks.")
|
82 |
-
|
83 |
-
def handle_action(self, action: str, action_input: str, history: List[Tuple[str, str]], task: str) -> Tuple[str, str, List[Tuple[str, str]], str]:
|
84 |
-
if action == "SEARCH":
|
85 |
-
return self._handle_search_action(action_input, history, task)
|
86 |
-
elif action == "GENERATE_HTML":
|
87 |
-
return self._handle_generate_html_action(action_input, history, task)
|
88 |
-
elif action == "GENERATE_CSS":
|
89 |
-
return self._handle_generate_css_action(action_input, history, task)
|
90 |
-
elif action == "GENERATE_JS":
|
91 |
-
return self._handle_generate_js_action(action_input, history, task)
|
92 |
-
elif action == "COMPLETE":
|
93 |
-
return "COMPLETE", "COMPLETE", history, task
|
94 |
-
else:
|
95 |
-
return "MAIN", None, history, task
|
96 |
-
|
97 |
-
def _handle_search_action(self, action_input: str, history: List[Tuple[str, str]], task: str) -> Tuple[str, str, List[Tuple[str, str]], str]:
|
98 |
-
"""Handles the SEARCH action."""
|
99 |
-
if VERBOSE_LOGGING:
|
100 |
-
logging.info(f"Calling SEARCH action with input: {action_input}")
|
101 |
try:
|
102 |
if "http" in action_input:
|
103 |
if "<" in action_input:
|
104 |
action_input = action_input.strip("<")
|
105 |
if ">" in action_input:
|
106 |
action_input = action_input.strip(">")
|
107 |
-
response = i_s(action_input)
|
108 |
-
|
|
|
109 |
else:
|
110 |
-
history
|
111 |
except Exception as e:
|
112 |
-
history
|
113 |
return "MAIN", None, history, task
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
# Simulate OpenAI
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
logging.info(f"Calling GENERATE_JS action with input: {action_input}")
|
139 |
-
# Simulate OpenAI's code generation capabilities using Hugging Face
|
140 |
-
prompt = self.get_prompt(f"Generate JavaScript code for a web page that {action_input}", history, task)
|
141 |
-
response = run_gpt(prompt, stop_tokens=["```", "```js"], max_tokens=MAX_TOKENS_PER_TURN)
|
142 |
-
history.append(("observation: generated JavaScript code:", response))
|
143 |
-
return "MAIN", None, history, task
|
144 |
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
return self._handle_generate_prompt_action(action_input, history, task)
|
154 |
-
elif action == "COMPLETE":
|
155 |
-
return "COMPLETE", "COMPLETE", history, task
|
156 |
-
else:
|
157 |
-
return "MAIN", None, history, task
|
158 |
-
|
159 |
-
def _handle_generate_prompt_action(self, action_input: str, history: List[Tuple[str, str]], task: str) -> Tuple[str, str, List[Tuple[str, str]], str]:
|
160 |
-
"""Handles the GENERATE_PROMPT action."""
|
161 |
-
if VERBOSE_LOGGING:
|
162 |
-
logging.info(f"Calling GENERATE_PROMPT action with input: {action_input}")
|
163 |
-
# Simulate OpenAI's prompt generation capabilities using Hugging Face
|
164 |
-
prompt = self.get_prompt(f"Generate a system prompt for a language model that {action_input}", history, task)
|
165 |
-
response = run_gpt(prompt, stop_tokens=["```", "```json"], max_tokens=MAX_TOKENS_PER_TURN)
|
166 |
-
history.append(("observation: generated system prompt:", response))
|
167 |
-
return "MAIN", None, history, task
|
168 |
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
def __init__(self):
|
173 |
-
super().__init__(name="PYTHON_CODE_DEV", description="Agent specialized in Python code development tasks.")
|
174 |
-
|
175 |
-
def handle_action(self, action: str, action_input: str, history: List[Tuple[str, str]], task: str) -> Tuple[str, str, List[Tuple[str, str]], str]:
|
176 |
-
if action == "GENERATE_CODE":
|
177 |
-
return self._handle_generate_code_action(action_input, history, task)
|
178 |
-
elif action == "RUN_CODE":
|
179 |
-
return self._handle_run_code_action(action_input, history, task)
|
180 |
-
elif action == "COMPLETE":
|
181 |
-
return "COMPLETE", "COMPLETE", history, task
|
182 |
-
else:
|
183 |
-
return "MAIN", None, history, task
|
184 |
-
|
185 |
-
def _handle_generate_code_action(self, action_input: str, history: List[Tuple[str, str]], task: str) -> Tuple[str, str, List[Tuple[str, str]], str]:
|
186 |
-
"""Handles the GENERATE_CODE action."""
|
187 |
-
if VERBOSE_LOGGING:
|
188 |
-
logging.info(f"Calling GENERATE_CODE action with input: {action_input}")
|
189 |
-
# Simulate OpenAI's code generation capabilities using Hugging Face
|
190 |
-
prompt = self.get_prompt(f"Generate Python code that {action_input}", history, task)
|
191 |
-
response = run_gpt(prompt, stop_tokens=["```", "```python"], max_tokens=MAX_TOKENS_PER_TURN)
|
192 |
-
history.append(("observation: generated Python code:", response))
|
193 |
-
return "MAIN", None, history, task
|
194 |
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
for line in lines:
|
217 |
-
if line == "":
|
218 |
-
continue
|
219 |
-
if line.startswith("thought: "):
|
220 |
-
history.append((line, ""))
|
221 |
-
if VERBOSE_LOGGING:
|
222 |
logging.info(f"Thought: {line}")
|
223 |
-
|
224 |
-
|
225 |
-
history.append((line, ""))
|
226 |
-
if VERBOSE_LOGGING:
|
227 |
logging.info(f"Action: {action_name} - {action_input}")
|
228 |
-
|
229 |
-
|
230 |
-
return action_name, action_input, history, task
|
231 |
-
else:
|
232 |
-
return action_name, action_input, history, task
|
233 |
-
else:
|
234 |
-
history.append((line, ""))
|
235 |
-
if VERBOSE_LOGGING:
|
236 |
-
logging.info(f"Other Output: {line}")
|
237 |
-
return "MAIN", None, history, task
|
238 |
-
|
239 |
-
def handle_update_task_action(action: str, action_input: str, history: List[Tuple[str, str]], task: str, agent: Agent) -> Tuple[str, str, List[Tuple[str, str]], str]:
|
240 |
-
"""Handles the UPDATE-TASK action, which updates the current task."""
|
241 |
-
if VERBOSE_LOGGING:
|
242 |
-
logging.info(f"Calling UPDATE-TASK action with input: {action_input}")
|
243 |
-
prompt = agent.get_prompt(action_input, history, task)
|
244 |
-
task = run_gpt(prompt, stop_tokens=[], max_tokens=64).strip("\n")
|
245 |
-
history.append(("observation: task has been updated to:", task))
|
246 |
-
return "MAIN", None, history, task
|
247 |
-
|
248 |
-
def handle_search_action(action: str, action_input: str, history: List[Tuple[str, str]], task: str, agent: Agent) -> Tuple[str, str, List[Tuple[str, str]], str]:
|
249 |
-
"""Handles the SEARCH action, which performs a web search."""
|
250 |
-
if VERBOSE_LOGGING:
|
251 |
-
logging.info(f"Calling SEARCH action with input: {action_input}")
|
252 |
-
try:
|
253 |
-
if "http" in action_input:
|
254 |
-
if "<" in action_input:
|
255 |
-
action_input = action_input.strip("<")
|
256 |
-
if ">" in action_input:
|
257 |
-
action_input = action_input.strip(">")
|
258 |
-
response = i_s(action_input) # Use i_search for web search
|
259 |
-
history.append(("observation: search result is:", response))
|
260 |
-
else:
|
261 |
-
history.append(("observation: I need a valid URL for the SEARCH action.", ""))
|
262 |
-
except Exception as e:
|
263 |
-
history.append(("observation:", str(e)))
|
264 |
-
return "MAIN", None, history, task
|
265 |
-
|
266 |
-
def handle_complete_action(action: str, action_input: str, history: List[Tuple[str, str]], task: str, agent: Agent) -> Tuple[str, str, List[Tuple[str, str]], str]:
|
267 |
-
"""Handles the COMPLETE action, which ends the current task."""
|
268 |
-
if VERBOSE_LOGGING:
|
269 |
-
logging.info(f"Calling COMPLETE action.")
|
270 |
-
task = "END"
|
271 |
-
return "COMPLETE", "COMPLETE", history, task
|
272 |
-
|
273 |
-
# --- Action Mapping ---
|
274 |
-
ACTION_HANDLERS: Dict[str, callable] = {
|
275 |
-
"MAIN": handle_main_action,
|
276 |
-
"UPDATE-TASK": handle_update_task_action,
|
277 |
-
"SEARCH": handle_search_action,
|
278 |
-
"COMPLETE": handle_complete_action,
|
279 |
-
}
|
280 |
|
281 |
-
#
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
logging.info(f"Prompt: {prompt}")
|
286 |
-
client = InferenceClient(MODEL_NAME)
|
287 |
-
resp = client.text_generation(prompt, max_new_tokens=max_tokens, stop_sequences=stop_tokens, temperature=0.7, top_p=0.8, repetition_penalty=1.5)
|
288 |
-
if VERBOSE_LOGGING:
|
289 |
-
logging.info(f"Response: {resp}")
|
290 |
-
return resp
|
291 |
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
if len(parts) == 2:
|
296 |
-
action_name = parts[0].replace("action", "").strip()
|
297 |
-
action_input = parts[1].strip()
|
298 |
-
else:
|
299 |
-
action_name = parts[0].replace("action", "").strip()
|
300 |
-
action_input = ""
|
301 |
-
return action_name, action_input
|
302 |
-
|
303 |
-
def run_agent(purpose: str, history: List[Tuple[str, str]], agent: Agent) -> List[Tuple[str, str]]:
|
304 |
-
"""Runs the agent and returns the updated conversation history."""
|
305 |
-
task = None
|
306 |
-
directory = "./"
|
307 |
-
action_name = "UPDATE-TASK" if task is None else "MAIN"
|
308 |
-
action_input = None
|
309 |
-
while True:
|
310 |
-
if VERBOSE_LOGGING:
|
311 |
-
logging.info(f"---")
|
312 |
-
logging.info(f"Purpose: {purpose}")
|
313 |
-
logging.info(f"Task: {task}")
|
314 |
-
logging.info(f"---")
|
315 |
-
logging.info(f"History: {history}")
|
316 |
-
logging.info(f"---")
|
317 |
-
if VERBOSE_LOGGING:
|
318 |
-
logging.info(f"Running action: {action_name} - {action_input}")
|
319 |
-
try:
|
320 |
-
if "RESPONSE" in action_name or "COMPLETE" in action_name:
|
321 |
-
action_name = "COMPLETE"
|
322 |
-
task = "END"
|
323 |
-
return history
|
324 |
-
if action_name not in ACTION_HANDLERS:
|
325 |
-
action_name = "MAIN"
|
326 |
-
if action_name == "" or action_name is None:
|
327 |
-
action_name = "MAIN"
|
328 |
-
action_handler = ACTION_HANDLERS[action_name]
|
329 |
-
action_name, action_input, history, task = action_handler(action_name, action_input, history, task, agent)
|
330 |
-
yield history
|
331 |
-
if task == "END":
|
332 |
-
return history
|
333 |
-
except Exception as e:
|
334 |
-
history.append(("observation: the previous command did not produce any useful output, I need to check the commands syntax, or use a different command", ""))
|
335 |
-
logging.error(f"Error in run_agent: {e}")
|
336 |
-
return history
|
337 |
|
338 |
# --- Gradio Interface ---
|
339 |
def main():
|
340 |
with gr.Blocks() as demo:
|
341 |
-
gr.Markdown("## FragMixt
|
342 |
-
gr.Markdown("###
|
343 |
|
344 |
# Chat Interface
|
345 |
chatbot = gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel")
|
@@ -347,17 +244,17 @@ def main():
|
|
347 |
# Input Components
|
348 |
message = gr.Textbox(label="Enter your message", placeholder="Ask me anything!")
|
349 |
purpose = gr.Textbox(label="Purpose", placeholder="What is the purpose of this interaction?")
|
350 |
-
agent_name = gr.Dropdown(label="Agents", choices=
|
351 |
-
|
352 |
temperature = gr.Slider(label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more diverse outputs")
|
353 |
-
max_new_tokens = gr.Slider(label="Max new tokens", value=1048*10, minimum=0, maximum=1048*10, step=64, interactive=True, info="The maximum numbers of new tokens")
|
354 |
top_p = gr.Slider(label="Top-p (nucleus sampling)", value=0.90, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens")
|
355 |
repetition_penalty = gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens")
|
356 |
|
357 |
# Button to submit the message
|
358 |
submit_button = gr.Button(value="Send")
|
359 |
|
360 |
-
# Project Explorer Tab
|
361 |
with gr.Tab("Project Explorer"):
|
362 |
project_path = gr.Textbox(label="Project Path", placeholder="/home/user/app/current_project")
|
363 |
explore_button = gr.Button(value="Explore")
|
@@ -369,22 +266,41 @@ def main():
|
|
369 |
examples = [
|
370 |
["What is the purpose of this AI agent?", "I am designed to assist with no-code development tasks."],
|
371 |
["Can you help me generate a Python function to calculate the factorial of a number?", "Sure! Here is a Python function to calculate the factorial of a number:"],
|
372 |
-
["Generate a web page with a heading that says 'Welcome to My Website!'", "action: GENERATE_HTML action_input=a heading that says 'Welcome to My Website!'"],
|
373 |
]
|
374 |
|
375 |
-
def chat(purpose, message, agent_name,
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
388 |
|
389 |
demo.launch()
|
390 |
|
|
|
1 |
import os
|
2 |
import subprocess
|
3 |
import random
|
4 |
+
from huggingface_hub import InferenceClient
|
5 |
+
import gradio as gr
|
6 |
+
from safe_search import safe_search
|
7 |
+
from i_search import google
|
8 |
+
from i_search import i_search as i_s
|
9 |
from datetime import datetime
|
10 |
import logging
|
11 |
+
import json
|
|
|
|
|
12 |
|
13 |
# --- Configuration ---
|
14 |
+
MODEL_NAME = "mistralai/Mixtral-8x7B-Instruct-v0.1" # Model to use
|
15 |
+
MAX_HISTORY_TURNS = 5 # Number of history turns to keep
|
16 |
+
VERBOSE = True # Enable verbose logging
|
|
|
|
|
17 |
|
18 |
# --- Logging Setup ---
|
19 |
logging.basicConfig(
|
|
|
23 |
)
|
24 |
|
25 |
# --- Agent Definitions ---
|
26 |
+
agents = {
|
27 |
+
"WEB_DEV": {
|
28 |
+
"description": "Specialized in web development tasks.",
|
29 |
+
"system_prompt": "You are a helpful AI assistant specializing in web development. You can generate code, answer questions, and provide guidance on web technologies.",
|
30 |
+
},
|
31 |
+
"AI_SYSTEM_PROMPT": {
|
32 |
+
"description": "Focuses on generating system prompts for AI agents.",
|
33 |
+
"system_prompt": "You are a helpful AI assistant that generates effective system prompts for AI agents. Your prompts should be clear, concise, and provide specific instructions.",
|
34 |
+
},
|
35 |
+
"PYTHON_CODE_DEV": {
|
36 |
+
"description": "Expert in Python code development.",
|
37 |
+
"system_prompt": "You are a helpful AI assistant specializing in Python code development. You can generate Python code, debug code, and answer questions about Python.",
|
38 |
+
},
|
39 |
+
"DATA_SCIENCE": {
|
40 |
+
"description": "Expert in data science tasks.",
|
41 |
+
"system_prompt": "You are a helpful AI assistant specializing in data science. You can analyze data, build models, and provide insights.",
|
42 |
+
},
|
43 |
+
"GAME_DEV": {
|
44 |
+
"description": "Expert in game development tasks.",
|
45 |
+
"system_prompt": "You are a helpful AI assistant specializing in game development. You can generate game logic, design levels, and provide guidance on game engines.",
|
46 |
+
},
|
47 |
+
# Add more agents as needed
|
48 |
+
}
|
49 |
|
50 |
+
# --- Function to format prompt with history ---
|
51 |
+
def format_prompt(message, history, agent_name, system_prompt):
|
52 |
+
prompt = " "
|
53 |
+
for user_prompt, bot_response in history[-MAX_HISTORY_TURNS:]:
|
54 |
+
prompt += f"[INST] {user_prompt} [/ "
|
55 |
+
prompt += f" {bot_response}"
|
56 |
+
prompt += f"[INST] {message} [/ "
|
57 |
+
|
58 |
+
# Add system prompt if provided
|
59 |
+
if system_prompt:
|
60 |
+
prompt = f"{system_prompt}\n\n{prompt}"
|
61 |
+
|
62 |
+
return prompt
|
63 |
+
|
64 |
+
# --- Function to run the LLM with specified parameters ---
|
65 |
+
def run_llm(
|
66 |
+
prompt,
|
67 |
+
stop_sequences,
|
68 |
+
max_tokens,
|
69 |
+
temperature=0.7,
|
70 |
+
top_p=0.8,
|
71 |
+
repetition_penalty=1.5,
|
72 |
+
):
|
73 |
+
seed = random.randint(1, 1111111111111111)
|
74 |
+
logging.info(f"Seed: {seed}") # Log the seed
|
75 |
|
76 |
+
client = InferenceClient(MODEL_NAME)
|
77 |
+
resp = client.text_generation(
|
78 |
+
prompt,
|
79 |
+
max_new_tokens=max_tokens,
|
80 |
+
stop_sequences=stop_sequences,
|
81 |
+
temperature=temperature,
|
82 |
+
top_p=top_p,
|
83 |
+
repetition_penalty=repetition_penalty,
|
84 |
+
)
|
85 |
+
if VERBOSE:
|
86 |
+
logging.info(f"Prompt: {prompt}")
|
87 |
+
logging.info(f"Response: {resp}")
|
88 |
+
return resp
|
89 |
|
90 |
+
# --- Function to handle agent interactions ---
|
91 |
+
def agent_interaction(
|
92 |
+
purpose,
|
93 |
+
message,
|
94 |
+
agent_name,
|
95 |
+
system_prompt,
|
96 |
+
history,
|
97 |
+
temperature,
|
98 |
+
max_new_tokens,
|
99 |
+
top_p,
|
100 |
+
repetition_penalty,
|
101 |
+
):
|
102 |
+
# Format the prompt with history
|
103 |
+
prompt = format_prompt(message, history, agent_name, system_prompt)
|
104 |
+
|
105 |
+
# Run the LLM
|
106 |
+
response = run_llm(
|
107 |
+
prompt,
|
108 |
+
stop_sequences=["observation:", "task:", "action:", "thought:"],
|
109 |
+
max_tokens=max_new_tokens,
|
110 |
+
temperature=temperature,
|
111 |
+
top_p=top_p,
|
112 |
+
repetition_penalty=repetition_penalty,
|
113 |
+
)
|
114 |
+
|
115 |
+
# Update history
|
116 |
+
history.append((message, response))
|
117 |
+
return history, history
|
118 |
+
|
119 |
+
# --- Function to parse actions from LLM response ---
|
120 |
+
def parse_action(line):
|
121 |
+
"""Parse the action line to get the action name and input."""
|
122 |
+
parts = line.split(":", 1)
|
123 |
+
if len(parts) == 2:
|
124 |
+
action_name = parts[0].replace("action", "").strip()
|
125 |
+
action_input = parts[1].strip()
|
126 |
+
else:
|
127 |
+
action_name = parts[0].replace("action", "").strip()
|
128 |
+
action_input = ""
|
129 |
+
return action_name, action_input
|
130 |
|
131 |
+
# --- Function to execute actions based on agent's response ---
|
132 |
+
def execute_action(purpose, task, history, action_name, action_input):
|
133 |
+
logging.info(f"Executing Action: {action_name} - {action_input}")
|
|
|
134 |
|
135 |
+
if action_name == "SEARCH":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
try:
|
137 |
if "http" in action_input:
|
138 |
if "<" in action_input:
|
139 |
action_input = action_input.strip("<")
|
140 |
if ">" in action_input:
|
141 |
action_input = action_input.strip(">")
|
142 |
+
response = i_s(action_input)
|
143 |
+
logging.info(f"Search Result: {response}")
|
144 |
+
history += "observation: search result is: {}\n".format(response)
|
145 |
else:
|
146 |
+
history += "observation: I need to provide a valid URL to 'action: SEARCH action_input=https://URL'\n"
|
147 |
except Exception as e:
|
148 |
+
history += "observation: {}\n".format(e)
|
149 |
return "MAIN", None, history, task
|
150 |
|
151 |
+
elif action_name == "COMPLETE":
|
152 |
+
task = "END"
|
153 |
+
return "COMPLETE", "COMPLETE", history, task
|
154 |
+
|
155 |
+
elif action_name == "GENERATE_CODE":
|
156 |
+
# Simulate OpenAI API response for code generation (using Hugging Face model)
|
157 |
+
# ... (Implement code generation logic using a suitable Hugging Face model)
|
158 |
+
# Example:
|
159 |
+
# code = generate_code_from_huggingface_model(action_input) # Replace with actual code generation function
|
160 |
+
# history += f"observation: Here's the code: {code}\n"
|
161 |
+
# return "MAIN", None, history, task
|
162 |
+
pass # Placeholder for code generation logic
|
163 |
+
|
164 |
+
elif action_name == "RUN_CODE":
|
165 |
+
# Simulate OpenAI API response for code execution (using Hugging Face model)
|
166 |
+
# ... (Implement code execution logic using a suitable Hugging Face model)
|
167 |
+
# Example:
|
168 |
+
# output = execute_code_from_huggingface_model(action_input) # Replace with actual code execution function
|
169 |
+
# history += f"observation: Code output: {output}\n"
|
170 |
+
# return "MAIN", None, history, task
|
171 |
+
pass # Placeholder for code execution logic
|
172 |
|
173 |
+
else:
|
174 |
+
# Default action: "MAIN"
|
175 |
+
return "MAIN", action_input, history, task
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
+
# --- Function to handle the main loop of agent interaction ---
|
178 |
+
def run_agent(purpose, history):
|
179 |
+
task = None
|
180 |
+
directory = "./"
|
181 |
+
if history:
|
182 |
+
history = str(history).strip("[]")
|
183 |
+
if not history:
|
184 |
+
history = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
+
action_name = "UPDATE-TASK" if task is None else "MAIN"
|
187 |
+
action_input = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
|
189 |
+
while True:
|
190 |
+
logging.info(f"---")
|
191 |
+
logging.info(f"Purpose: {purpose}")
|
192 |
+
logging.info(f"Task: {task}")
|
193 |
+
logging.info(f"---")
|
194 |
+
logging.info(f"History: {history}")
|
195 |
+
logging.info(f"---")
|
196 |
+
|
197 |
+
# Get the agent's next action
|
198 |
+
prompt = f"""
|
199 |
+
You are a helpful AI assistant. You are working on the task: {task}
|
200 |
+
Your current history is:
|
201 |
+
{history}
|
202 |
+
What is your next thought?
|
203 |
+
thought:
|
204 |
+
What is your next action?
|
205 |
+
action:
|
206 |
+
"""
|
207 |
|
208 |
+
response = run_llm(
|
209 |
+
prompt,
|
210 |
+
stop_sequences=["observation:", "task:", "action:", "thought:"],
|
211 |
+
max_tokens=32000,
|
212 |
+
)
|
213 |
+
|
214 |
+
# Parse the action
|
215 |
+
lines = response.strip().strip("\n").split("\n")
|
216 |
+
for line in lines:
|
217 |
+
if line.startswith("thought: "):
|
218 |
+
history += "{}\n".format(line)
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
logging.info(f"Thought: {line}")
|
220 |
+
elif line.startswith("action: "):
|
221 |
+
action_name, action_input = parse_action(line)
|
|
|
|
|
222 |
logging.info(f"Action: {action_name} - {action_input}")
|
223 |
+
history += "{}\n".format(line)
|
224 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
|
226 |
+
# Execute the action
|
227 |
+
action_name, action_input, history, task = execute_action(
|
228 |
+
purpose, task, history, action_name, action_input
|
229 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
|
231 |
+
yield (history)
|
232 |
+
if task == "END":
|
233 |
+
return (history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
234 |
|
235 |
# --- Gradio Interface ---
|
236 |
def main():
|
237 |
with gr.Blocks() as demo:
|
238 |
+
gr.Markdown("## FragMixt - No-Code Development Powerhouse")
|
239 |
+
gr.Markdown("### Your AI-Powered Development Companion")
|
240 |
|
241 |
# Chat Interface
|
242 |
chatbot = gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel")
|
|
|
244 |
# Input Components
|
245 |
message = gr.Textbox(label="Enter your message", placeholder="Ask me anything!")
|
246 |
purpose = gr.Textbox(label="Purpose", placeholder="What is the purpose of this interaction?")
|
247 |
+
agent_name = gr.Dropdown(label="Agents", choices=list(agents.keys()), value=list(agents.keys())[0], interactive=True)
|
248 |
+
system_prompt = gr.Textbox(label="System Prompt", max_lines=1, interactive=True)
|
249 |
temperature = gr.Slider(label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more diverse outputs")
|
250 |
+
max_new_tokens = gr.Slider(label="Max new tokens", value=1048 * 10, minimum=0, maximum=1048 * 10, step=64, interactive=True, info="The maximum numbers of new tokens")
|
251 |
top_p = gr.Slider(label="Top-p (nucleus sampling)", value=0.90, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens")
|
252 |
repetition_penalty = gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens")
|
253 |
|
254 |
# Button to submit the message
|
255 |
submit_button = gr.Button(value="Send")
|
256 |
|
257 |
+
# Project Explorer Tab (Placeholder)
|
258 |
with gr.Tab("Project Explorer"):
|
259 |
project_path = gr.Textbox(label="Project Path", placeholder="/home/user/app/current_project")
|
260 |
explore_button = gr.Button(value="Explore")
|
|
|
266 |
examples = [
|
267 |
["What is the purpose of this AI agent?", "I am designed to assist with no-code development tasks."],
|
268 |
["Can you help me generate a Python function to calculate the factorial of a number?", "Sure! Here is a Python function to calculate the factorial of a number:"],
|
|
|
269 |
]
|
270 |
|
271 |
+
def chat(purpose, message, agent_name, system_prompt, temperature, max_new_tokens, top_p, repetition_penalty, history):
|
272 |
+
# Get the system prompt for the selected agent
|
273 |
+
system_prompt = agents.get(agent_name, {}).get("system_prompt", "")
|
274 |
+
|
275 |
+
# Run the agent interaction
|
276 |
+
history, history_output = agent_interaction(
|
277 |
+
purpose,
|
278 |
+
message,
|
279 |
+
agent_name,
|
280 |
+
system_prompt,
|
281 |
+
history,
|
282 |
+
temperature,
|
283 |
+
max_new_tokens,
|
284 |
+
top_p,
|
285 |
+
repetition_penalty,
|
286 |
+
)
|
287 |
+
return history, history_output
|
288 |
+
|
289 |
+
submit_button.click(
|
290 |
+
chat,
|
291 |
+
inputs=[
|
292 |
+
purpose,
|
293 |
+
message,
|
294 |
+
agent_name,
|
295 |
+
system_prompt,
|
296 |
+
temperature,
|
297 |
+
max_new_tokens,
|
298 |
+
top_p,
|
299 |
+
repetition_penalty,
|
300 |
+
history,
|
301 |
+
],
|
302 |
+
outputs=[chatbot, history],
|
303 |
+
)
|
304 |
|
305 |
demo.launch()
|
306 |
|