Davit6174 commited on
Commit
90fca0d
·
verified ·
1 Parent(s): b8d7142

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -13
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|>\n"\
48
- f"You are a helpful AI agent. You can use the following tools when needed:\n"\
49
- f"{self.tool_descriptions}\n"\
50
- f"\nRespond to the user. If a tool is needed, use this format:\n"\
51
- f"Action: tool_name\nAction Input: input_for_tool\n"\
52
- f"\n<|user|>\n{question}\n<|assistant|>\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
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*(.+)", raw_response, re.IGNORECASE)
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.strip('"'))
96
- follow_up = self.model(f"User asked: {content}\nTool [{tool_name}] returned: {tool_output}")
 
 
97
  return {"messages": messages + [
98
- AIMessage(content=raw_response),
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=raw_response)]}
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")