fomext commited on
Commit
8f79d0d
·
verified ·
1 Parent(s): 0183322

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -1
app.py CHANGED
@@ -193,10 +193,31 @@ def chat_completions(
193
  "tool_call_id": m.get("tool_call_id", ""),
194
  })
195
  elif role == "assistant" and m.get("tool_calls"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  hf_messages.append({
197
  "role": "assistant",
198
  "content": m.get("content") or "",
199
- "tool_calls": m["tool_calls"],
200
  })
201
  else:
202
  hf_messages.append({"role": role, "content": m.get("content", "")})
 
193
  "tool_call_id": m.get("tool_call_id", ""),
194
  })
195
  elif role == "assistant" and m.get("tool_calls"):
196
+ # Normalise tool_calls: apply_chat_template needs arguments as a dict,
197
+ # but OpenAI format (and our own output) stores them as a JSON string.
198
+ normalised_tool_calls = []
199
+ for tc in m["tool_calls"]:
200
+ fn = tc.get("function", {})
201
+ raw_args = fn.get("arguments", "{}")
202
+ if isinstance(raw_args, str):
203
+ try:
204
+ parsed_args = json.loads(raw_args)
205
+ except json.JSONDecodeError:
206
+ parsed_args = {}
207
+ else:
208
+ parsed_args = raw_args # already a dict
209
+ normalised_tool_calls.append({
210
+ "id": tc.get("id", ""),
211
+ "type": "function",
212
+ "function": {
213
+ "name": fn.get("name", ""),
214
+ "arguments": parsed_args, # dict, not string
215
+ },
216
+ })
217
  hf_messages.append({
218
  "role": "assistant",
219
  "content": m.get("content") or "",
220
+ "tool_calls": normalised_tool_calls,
221
  })
222
  else:
223
  hf_messages.append({"role": role, "content": m.get("content", "")})