Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -44,12 +44,25 @@ class ZephyrAPI:
|
|
| 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 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
payload = {
|
| 54 |
"inputs": prompt,
|
| 55 |
"parameters": {
|
|
@@ -86,25 +99,27 @@ class LangGraphAgent:
|
|
| 86 |
raw_response = self.model(content)
|
| 87 |
|
| 88 |
# Check if model issued a tool call
|
| 89 |
-
match = re.search(r"Action:\s*(\w+)\s*Action Input:\s
|
| 90 |
if match:
|
| 91 |
tool_name, tool_input = match.groups()
|
| 92 |
tool_fn = self.tools.get(tool_name)
|
| 93 |
if tool_fn:
|
| 94 |
try:
|
| 95 |
-
tool_output = tool_fn(tool_input
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
return {"messages": messages + [
|
| 98 |
-
AIMessage(content=
|
| 99 |
-
AIMessage(content=tool_output),
|
| 100 |
AIMessage(content=follow_up),
|
| 101 |
]}
|
| 102 |
except Exception as e:
|
| 103 |
return {"messages": messages + [AIMessage(content=f"⚠️ Tool error: {e}")]}
|
| 104 |
else:
|
| 105 |
return {"messages": messages + [AIMessage(content=f"⚠️ Unknown tool: {tool_name}")]}
|
| 106 |
-
|
| 107 |
-
return {"messages": messages + [AIMessage(content=
|
| 108 |
|
| 109 |
builder.add_node("chat", call_model)
|
| 110 |
builder.set_entry_point("chat")
|
|
|
|
| 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 |
+
|
| 50 |
+
Available tools:
|
| 51 |
+
{self.tool_descriptions}
|
| 52 |
+
|
| 53 |
+
Respond with this format:
|
| 54 |
+
Thought: do I need to use a tool?
|
| 55 |
+
Action: tool_name
|
| 56 |
+
Action Input: "input"
|
| 57 |
+
Observation: tool result
|
| 58 |
+
... (repeat if needed)
|
| 59 |
+
Final Answer: your final answer to the user
|
| 60 |
+
|
| 61 |
+
<|user|>
|
| 62 |
+
{question}
|
| 63 |
+
|
| 64 |
+
<|assistant|>
|
| 65 |
+
{scratchpad}"""
|
| 66 |
payload = {
|
| 67 |
"inputs": prompt,
|
| 68 |
"parameters": {
|
|
|
|
| 99 |
raw_response = self.model(content)
|
| 100 |
|
| 101 |
# Check if model issued a tool call
|
| 102 |
+
match = re.search(r"Action:\s*(\w+)\s*Action Input:\s*\"(.+?)\"", 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 |
+
# Append tool call and result to scratchpad
|
| 110 |
+
scratchpad = f"{response}\nObservation: {tool_output}"
|
| 111 |
+
follow_up = self.model(content, scratchpad)
|
| 112 |
return {"messages": messages + [
|
| 113 |
+
AIMessage(content=response),
|
| 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=response)]}
|
| 123 |
|
| 124 |
builder.add_node("chat", call_model)
|
| 125 |
builder.set_entry_point("chat")
|