Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,7 +32,7 @@ class BasicAgent:
|
|
| 32 |
return fixed_answer
|
| 33 |
|
| 34 |
class ZephyrAPI:
|
| 35 |
-
def __init__(self):
|
| 36 |
self.api_url = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
| 37 |
self.headers = {
|
| 38 |
"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"
|
|
@@ -43,7 +43,7 @@ class ZephyrAPI:
|
|
| 43 |
def format_tools(self, tools):
|
| 44 |
return "\n".join([f"- {tool.name}: {tool.description}" for tool in tools])
|
| 45 |
|
| 46 |
-
def __call__(self, question: str) -> str:
|
| 47 |
prompt = f"""<|system|>
|
| 48 |
You are a smart agent that can reason step-by-step and use tools when necessary.
|
| 49 |
|
|
@@ -94,32 +94,32 @@ class LangGraphAgent:
|
|
| 94 |
user_msg = next((m for m in messages if isinstance(m, HumanMessage)), None)
|
| 95 |
if not user_msg:
|
| 96 |
return {"messages": messages + [AIMessage(content="❌ No user input found.")]}
|
| 97 |
-
|
| 98 |
content = user_msg.content.strip()
|
| 99 |
raw_response = self.model(content)
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
match = re.search(r"Action:\s*(\w+)\s*Action Input:\s*\"(.+?)\"",
|
| 103 |
if match:
|
| 104 |
tool_name, tool_input = match.groups()
|
| 105 |
tool_fn = self.tools.get(tool_name)
|
| 106 |
if tool_fn:
|
| 107 |
try:
|
| 108 |
tool_output = tool_fn(tool_input)
|
| 109 |
-
|
| 110 |
-
scratchpad = f"{response}\nObservation: {tool_output}"
|
| 111 |
follow_up = self.model(content, scratchpad)
|
| 112 |
return {"messages": messages + [
|
| 113 |
-
AIMessage(content=
|
| 114 |
AIMessage(content=f"Observation: {tool_output}"),
|
| 115 |
AIMessage(content=follow_up),
|
| 116 |
]}
|
| 117 |
except Exception as e:
|
| 118 |
return {"messages": messages + [AIMessage(content=f"⚠️ Tool error: {e}")]}
|
|
|
|
| 119 |
else:
|
| 120 |
return {"messages": messages + [AIMessage(content=f"⚠️ Unknown tool: {tool_name}")]}
|
| 121 |
-
|
| 122 |
-
return {"messages": messages + [AIMessage(content=
|
| 123 |
|
| 124 |
builder.add_node("chat", call_model)
|
| 125 |
builder.set_entry_point("chat")
|
|
|
|
| 32 |
return fixed_answer
|
| 33 |
|
| 34 |
class ZephyrAPI:
|
| 35 |
+
def __init__(self, tools=None):
|
| 36 |
self.api_url = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
| 37 |
self.headers = {
|
| 38 |
"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"
|
|
|
|
| 43 |
def format_tools(self, tools):
|
| 44 |
return "\n".join([f"- {tool.name}: {tool.description}" for tool in tools])
|
| 45 |
|
| 46 |
+
def __call__(self, question: str, scratchpad: str = "") -> str:
|
| 47 |
prompt = f"""<|system|>
|
| 48 |
You are a smart agent that can reason step-by-step and use tools when necessary.
|
| 49 |
|
|
|
|
| 94 |
user_msg = next((m for m in messages if isinstance(m, HumanMessage)), None)
|
| 95 |
if not user_msg:
|
| 96 |
return {"messages": messages + [AIMessage(content="❌ No user input found.")]}
|
| 97 |
+
|
| 98 |
content = user_msg.content.strip()
|
| 99 |
raw_response = self.model(content)
|
| 100 |
|
| 101 |
+
# Detect tool usage
|
| 102 |
+
match = re.search(r"Action:\s*(\w+)\s*Action Input:\s*\"(.+?)\"", raw_response, re.DOTALL)
|
| 103 |
if match:
|
| 104 |
tool_name, tool_input = match.groups()
|
| 105 |
tool_fn = self.tools.get(tool_name)
|
| 106 |
if tool_fn:
|
| 107 |
try:
|
| 108 |
tool_output = tool_fn(tool_input)
|
| 109 |
+
scratchpad = f"{raw_response}\nObservation: {tool_output}"
|
|
|
|
| 110 |
follow_up = self.model(content, scratchpad)
|
| 111 |
return {"messages": messages + [
|
| 112 |
+
AIMessage(content=raw_response),
|
| 113 |
AIMessage(content=f"Observation: {tool_output}"),
|
| 114 |
AIMessage(content=follow_up),
|
| 115 |
]}
|
| 116 |
except Exception as e:
|
| 117 |
return {"messages": messages + [AIMessage(content=f"⚠️ Tool error: {e}")]}
|
| 118 |
+
|
| 119 |
else:
|
| 120 |
return {"messages": messages + [AIMessage(content=f"⚠️ Unknown tool: {tool_name}")]}
|
| 121 |
+
|
| 122 |
+
return {"messages": messages + [AIMessage(content=raw_response)]}
|
| 123 |
|
| 124 |
builder.add_node("chat", call_model)
|
| 125 |
builder.set_entry_point("chat")
|