broadfield-dev commited on
Commit
3792b4a
·
verified ·
1 Parent(s): 7bd4e6e

Update tools/orchestrator.py

Browse files
Files changed (1) hide show
  1. tools/orchestrator.py +19 -2
tools/orchestrator.py CHANGED
@@ -7,6 +7,7 @@ import time
7
  from model_logic import call_model_stream, MODELS_BY_PROVIDER, get_default_model_display_name_for_provider
8
  from memory_logic import retrieve_memories_semantic, retrieve_rules_semantic
9
  from tools.websearch import search_and_scrape_duckduckgo, scrape_url
 
10
  import prompts
11
  from utils import format_insights_for_prompt
12
 
@@ -27,7 +28,7 @@ def decide_on_tool(user_input: str, chat_history_for_prompt: list, initial_insig
27
  if len(user_input.split()) <= 3 and any(kw in user_input_lower for kw in ["hello", "hi", "thanks", "ok", "bye"]) and not "?" in user_input:
28
  return "quick_respond", {}
29
 
30
- if len(user_input.split()) > 3 or "?" in user_input or any(w in user_input_lower for w in ["what is", "how to", "explain", "search for"]):
31
  history_snippet = "\n".join([f"{msg['role']}: {msg['content'][:100]}" for msg in chat_history_for_prompt[-2:]])
32
  guideline_snippet = initial_insights_ctx_str[:200].replace('\n', ' ')
33
  tool_user_prompt = prompts.get_tool_user_prompt(user_input, history_snippet, guideline_snippet)
@@ -37,7 +38,7 @@ def decide_on_tool(user_input: str, chat_history_for_prompt: list, initial_insig
37
 
38
  if tool_model_display:
39
  try:
40
- tool_resp_raw = "".join(list(call_model_stream(provider=TOOL_DECISION_PROVIDER, model_display_name=tool_model_display, messages=tool_decision_messages, temperature=0.0, max_tokens=150)))
41
  json_match_tool = re.search(r"\{.*\}", tool_resp_raw, re.DOTALL)
42
  if json_match_tool:
43
  action_data = json.loads(json_match_tool.group(0))
@@ -86,6 +87,22 @@ def orchestrate_and_respond(user_input: str, provider_name: str, model_display_n
86
  context_str = "Web Content:\n" + "\n".join([f"Source {i+1}:\nURL:{r.get('url','N/A')}\nTitle:{r.get('title','N/A')}\nContent:\n{(r.get('content') or r.get('error') or 'N/A')[:3500]}\n---" for i,r in enumerate(web_results)]) if web_results else f"No results from {action_type} for '{query_or_url}'."
87
  yield "status", "[Synthesizing web report...]"
88
  final_system_prompt_str += " Generate report/answer from web content, history, & guidelines. Cite URLs as [Source X]."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  else: # quick_respond or fallback
90
  final_system_prompt_str += " Respond directly using guidelines & history."
91
 
 
7
  from model_logic import call_model_stream, MODELS_BY_PROVIDER, get_default_model_display_name_for_provider
8
  from memory_logic import retrieve_memories_semantic, retrieve_rules_semantic
9
  from tools.websearch import search_and_scrape_duckduckgo, scrape_url
10
+ from tools.space_builder import build_huggingface_space
11
  import prompts
12
  from utils import format_insights_for_prompt
13
 
 
28
  if len(user_input.split()) <= 3 and any(kw in user_input_lower for kw in ["hello", "hi", "thanks", "ok", "bye"]) and not "?" in user_input:
29
  return "quick_respond", {}
30
 
31
+ if len(user_input.split()) > 3 or "?" in user_input or any(w in user_input_lower for w in ["what is", "how to", "explain", "search for", "build", "create", "make"]):
32
  history_snippet = "\n".join([f"{msg['role']}: {msg['content'][:100]}" for msg in chat_history_for_prompt[-2:]])
33
  guideline_snippet = initial_insights_ctx_str[:200].replace('\n', ' ')
34
  tool_user_prompt = prompts.get_tool_user_prompt(user_input, history_snippet, guideline_snippet)
 
38
 
39
  if tool_model_display:
40
  try:
41
+ tool_resp_raw = "".join(list(call_model_stream(provider=TOOL_DECISION_PROVIDER, model_display_name=tool_model_display, messages=tool_decision_messages, temperature=0.0, max_tokens=200)))
42
  json_match_tool = re.search(r"\{.*\}", tool_resp_raw, re.DOTALL)
43
  if json_match_tool:
44
  action_data = json.loads(json_match_tool.group(0))
 
87
  context_str = "Web Content:\n" + "\n".join([f"Source {i+1}:\nURL:{r.get('url','N/A')}\nTitle:{r.get('title','N/A')}\nContent:\n{(r.get('content') or r.get('error') or 'N/A')[:3500]}\n---" for i,r in enumerate(web_results)]) if web_results else f"No results from {action_type} for '{query_or_url}'."
88
  yield "status", "[Synthesizing web report...]"
89
  final_system_prompt_str += " Generate report/answer from web content, history, & guidelines. Cite URLs as [Source X]."
90
+ elif action_type == "build_huggingface_space":
91
+ build_prompt = action_input_dict.get("build_prompt")
92
+ if not build_prompt:
93
+ context_str = "Tool Action Failed: The model decided to build a space but did not provide the necessary instructions (build_prompt)."
94
+ final_system_prompt_str += " Report the tool failure to the user."
95
+ else:
96
+ yield "status", f"[Tool: Building Space '{build_prompt[:50]}'...]"
97
+ build_result = build_huggingface_space(build_prompt)
98
+ if "error" in build_result:
99
+ context_str = f"Hugging Face Space Builder Tool Result:\n- Status: FAILED\n- Error: {build_result['error']}"
100
+ final_system_prompt_str += " The space building tool failed. Report the error to the user and ask if they want to try again."
101
+ yield "status", "[Tool: Space building failed.]"
102
+ else:
103
+ context_str = f"Hugging Face Space Builder Tool Result:\n- Status: SUCCESS\n- Details: {build_result['result']}"
104
+ final_system_prompt_str += " The space building tool has completed. Inform the user about the result, providing any links or key information from the tool's output."
105
+ yield "status", "[Tool: Space building complete.]"
106
  else: # quick_respond or fallback
107
  final_system_prompt_str += " Respond directly using guidelines & history."
108