Spaces:
Runtime error
Runtime error
Update tools/orchestrator.py
Browse files- tools/orchestrator.py +23 -38
tools/orchestrator.py
CHANGED
@@ -3,6 +3,7 @@ import json
|
|
3 |
import re
|
4 |
import logging
|
5 |
import time
|
|
|
6 |
from huggingface_hub import whoami
|
7 |
|
8 |
from model_logic import call_model_stream, MODELS_BY_PROVIDER, get_default_model_display_name_for_provider
|
@@ -61,7 +62,11 @@ def orchestrate_and_respond(user_input: str, provider_name: str, model_display_n
|
|
61 |
logger.info(f"ORCHESTRATOR [{request_id}]: Tool Decision: Action='{action_type}', Input='{action_input}'")
|
62 |
|
63 |
yield "status", f"[Path: {action_type}]"
|
64 |
-
|
|
|
|
|
|
|
|
|
65 |
context_str = None
|
66 |
|
67 |
if action_type == "create_huggingface_space":
|
@@ -70,7 +75,6 @@ def orchestrate_and_respond(user_input: str, provider_name: str, model_display_n
|
|
70 |
try:
|
71 |
hf_token = os.getenv("HF_TOKEN")
|
72 |
if not hf_token: raise ValueError("Hugging Face token (HF_TOKEN) not found.")
|
73 |
-
|
74 |
yield "status", "[Tool: Verifying user identity...]"
|
75 |
user_info = whoami(token=hf_token)
|
76 |
owner = user_info.get("name")
|
@@ -85,65 +89,46 @@ def orchestrate_and_respond(user_input: str, provider_name: str, model_display_n
|
|
85 |
]
|
86 |
markdown_content = "".join(list(call_model_stream(provider_name, model_display_name, space_gen_messages, ui_api_key_override, 0.1, 4096)))
|
87 |
yield "status", "[Tool: Creating Space...]"
|
88 |
-
result = create_huggingface_space(
|
89 |
-
owner=owner,
|
90 |
-
space_name=action_input["space_name"],
|
91 |
-
sdk=action_input["sdk"],
|
92 |
-
markdown_content=markdown_content.strip()
|
93 |
-
)
|
94 |
context_str = f"Tool Result (Create Space): {result.get('result') or result.get('error', 'Unknown outcome')}"
|
|
|
95 |
except Exception as e:
|
96 |
context_str = f"Tool Failed: An error occurred during space creation process - {e}"
|
97 |
else:
|
98 |
context_str = "Tool Failed: Missing parameters for create_huggingface_space. Required: " + ", ".join(params)
|
99 |
-
elif action_type
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
else:
|
107 |
-
files_list = result.get("files", [])
|
108 |
-
files_str = "\n- ".join(files_list) if files_list else "No files found."
|
109 |
-
context_str = f"Tool Result (List Files):\nStatus: {result.get('status', 'OK')}\nFiles:\n- {files_str}"
|
110 |
-
else:
|
111 |
-
context_str = "Tool Failed: Missing parameters for list_huggingface_space_files. Required: " + ", ".join(params)
|
112 |
-
elif action_type == "get_huggingface_space_file_content":
|
113 |
-
params = ["owner", "space_name", "file_path"]
|
114 |
-
if all(p in action_input for p in params):
|
115 |
-
yield "status", "[Tool: Reading file content...]"
|
116 |
-
result = get_huggingface_space_file_content(**action_input)
|
117 |
-
if "error" in result:
|
118 |
-
context_str = f"Tool Result (Get Content): Error - {result['error']}"
|
119 |
-
else:
|
120 |
-
context_str = f"Tool Result (Get Content for '{action_input['file_path']}'):\nStatus: {result.get('status', 'OK')}\n```\n{result.get('content', '')}\n```"
|
121 |
-
else:
|
122 |
-
context_str = "Tool Failed: Missing parameters for get_huggingface_space_file_content. Required: " + ", ".join(params)
|
123 |
-
elif action_type == "update_huggingface_space_file":
|
124 |
-
params = ["owner", "space_name", "file_path", "new_content", "commit_message"]
|
125 |
if all(p in action_input for p in params):
|
126 |
-
yield "status", "[Tool:
|
127 |
-
result =
|
128 |
-
context_str = f"Tool Result (
|
|
|
129 |
else:
|
130 |
-
context_str = "Tool Failed: Missing parameters for
|
131 |
elif action_type == "search_duckduckgo_and_report" and WEB_SEARCH_ENABLED:
|
132 |
query = action_input.get("search_engine_query")
|
133 |
if query:
|
134 |
yield "status", f"[Web: '{query[:60]}'...]"
|
135 |
results = search_and_scrape_duckduckgo(query, num_results=2)
|
136 |
context_str = "Web Content:\n" + "\n".join([f"Source {i+1} ({r.get('url','N/A')}):\n{r.get('content', r.get('error', 'N/A'))[:3000]}\n---" for i, r in enumerate(results)])
|
|
|
137 |
elif action_type == "scrape_url_and_report" and WEB_SEARCH_ENABLED:
|
138 |
url = action_input.get("url")
|
139 |
if url:
|
140 |
yield "status", f"[Web: '{url[:60]}'...]"
|
141 |
result = scrape_url(url)
|
142 |
context_str = f"Web Content for {url}:\n{result.get('content', result.get('error', 'No content scraped.'))}"
|
|
|
143 |
elif action_type == "answer_using_conversation_memory":
|
144 |
yield "status", "[Searching conversation memory...]"
|
145 |
mems = retrieve_memories_semantic(f"User query: {user_input}\nContext:\n{history_str_for_prompt[-1000:]}", k=2)
|
146 |
context_str = "Relevant Past Interactions:\n" + "\n".join([f"- User:{m.get('user_input','')}->AI:{m.get('bot_response','')} (Takeaway:{m.get('metrics',{}).get('takeaway','N/A')})" for m in mems]) if mems else "No relevant past interactions found."
|
|
|
147 |
|
148 |
final_user_prompt = prompts.get_final_response_prompt(history_str_for_prompt, initial_insights_ctx_str, user_input, context_str)
|
149 |
final_llm_messages = [{"role": "system", "content": final_system_prompt}, {"role": "user", "content": final_user_prompt}]
|
|
|
3 |
import re
|
4 |
import logging
|
5 |
import time
|
6 |
+
from datetime import datetime
|
7 |
from huggingface_hub import whoami
|
8 |
|
9 |
from model_logic import call_model_stream, MODELS_BY_PROVIDER, get_default_model_display_name_for_provider
|
|
|
62 |
logger.info(f"ORCHESTRATOR [{request_id}]: Tool Decision: Action='{action_type}', Input='{action_input}'")
|
63 |
|
64 |
yield "status", f"[Path: {action_type}]"
|
65 |
+
|
66 |
+
current_time_str = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
67 |
+
base_system_prompt = custom_system_prompt or prompts.DEFAULT_SYSTEM_PROMPT
|
68 |
+
final_system_prompt = base_system_prompt.format(current_date_time=current_time_str)
|
69 |
+
|
70 |
context_str = None
|
71 |
|
72 |
if action_type == "create_huggingface_space":
|
|
|
75 |
try:
|
76 |
hf_token = os.getenv("HF_TOKEN")
|
77 |
if not hf_token: raise ValueError("Hugging Face token (HF_TOKEN) not found.")
|
|
|
78 |
yield "status", "[Tool: Verifying user identity...]"
|
79 |
user_info = whoami(token=hf_token)
|
80 |
owner = user_info.get("name")
|
|
|
89 |
]
|
90 |
markdown_content = "".join(list(call_model_stream(provider_name, model_display_name, space_gen_messages, ui_api_key_override, 0.1, 4096)))
|
91 |
yield "status", "[Tool: Creating Space...]"
|
92 |
+
result = create_huggingface_space(owner=owner, space_name=action_input["space_name"], sdk=action_input["sdk"], markdown_content=markdown_content.strip())
|
|
|
|
|
|
|
|
|
|
|
93 |
context_str = f"Tool Result (Create Space): {result.get('result') or result.get('error', 'Unknown outcome')}"
|
94 |
+
final_system_prompt += " The space building tool has completed. Inform the user about the result, providing any links or key information from the tool's output."
|
95 |
except Exception as e:
|
96 |
context_str = f"Tool Failed: An error occurred during space creation process - {e}"
|
97 |
else:
|
98 |
context_str = "Tool Failed: Missing parameters for create_huggingface_space. Required: " + ", ".join(params)
|
99 |
+
elif action_type in ["list_huggingface_space_files", "get_huggingface_space_file_content", "update_huggingface_space_file"]:
|
100 |
+
tool_map = {
|
101 |
+
"list_huggingface_space_files": (list_huggingface_space_files, ["owner", "space_name"], "Listing files"),
|
102 |
+
"get_huggingface_space_file_content": (get_huggingface_space_file_content, ["owner", "space_name", "file_path"], "Reading file content"),
|
103 |
+
"update_huggingface_space_file": (update_huggingface_space_file, ["owner", "space_name", "file_path", "new_content", "commit_message"], "Updating file")
|
104 |
+
}
|
105 |
+
tool_func, params, status_msg = tool_map[action_type]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
if all(p in action_input for p in params):
|
107 |
+
yield "status", f"[Tool: {status_msg}...]"
|
108 |
+
result = tool_func(**action_input)
|
109 |
+
context_str = f"Tool Result ({action_type}): {result}"
|
110 |
+
final_system_prompt += " A file operation tool has completed. Inform the user about the result, presenting the data clearly."
|
111 |
else:
|
112 |
+
context_str = f"Tool Failed: Missing parameters for {action_type}. Required: " + ", ".join(params)
|
113 |
elif action_type == "search_duckduckgo_and_report" and WEB_SEARCH_ENABLED:
|
114 |
query = action_input.get("search_engine_query")
|
115 |
if query:
|
116 |
yield "status", f"[Web: '{query[:60]}'...]"
|
117 |
results = search_and_scrape_duckduckgo(query, num_results=2)
|
118 |
context_str = "Web Content:\n" + "\n".join([f"Source {i+1} ({r.get('url','N/A')}):\n{r.get('content', r.get('error', 'N/A'))[:3000]}\n---" for i, r in enumerate(results)])
|
119 |
+
final_system_prompt += " Generate a report/answer from the provided web content, history, & guidelines. Cite URLs as [Source X]."
|
120 |
elif action_type == "scrape_url_and_report" and WEB_SEARCH_ENABLED:
|
121 |
url = action_input.get("url")
|
122 |
if url:
|
123 |
yield "status", f"[Web: '{url[:60]}'...]"
|
124 |
result = scrape_url(url)
|
125 |
context_str = f"Web Content for {url}:\n{result.get('content', result.get('error', 'No content scraped.'))}"
|
126 |
+
final_system_prompt += " Summarize or answer questions based on the scraped web page content."
|
127 |
elif action_type == "answer_using_conversation_memory":
|
128 |
yield "status", "[Searching conversation memory...]"
|
129 |
mems = retrieve_memories_semantic(f"User query: {user_input}\nContext:\n{history_str_for_prompt[-1000:]}", k=2)
|
130 |
context_str = "Relevant Past Interactions:\n" + "\n".join([f"- User:{m.get('user_input','')}->AI:{m.get('bot_response','')} (Takeaway:{m.get('metrics',{}).get('takeaway','N/A')})" for m in mems]) if mems else "No relevant past interactions found."
|
131 |
+
final_system_prompt += " Respond using the provided Memory Context, your general knowledge, and the conversation history."
|
132 |
|
133 |
final_user_prompt = prompts.get_final_response_prompt(history_str_for_prompt, initial_insights_ctx_str, user_input, context_str)
|
134 |
final_llm_messages = [{"role": "system", "content": final_system_prompt}, {"role": "user", "content": final_user_prompt}]
|