mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-03-15 12:57:35 +03:00
Fix tool calling
This commit is contained in:
parent
0371f55321
commit
730bf3c0b7
@ -398,9 +398,20 @@ async def _execute_tool_internal(
|
|||||||
elif tool_name == "get_live_context":
|
elif tool_name == "get_live_context":
|
||||||
camera = arguments.get("camera")
|
camera = arguments.get("camera")
|
||||||
if not camera:
|
if not camera:
|
||||||
|
logger.error(
|
||||||
|
"Tool get_live_context failed: camera parameter is required. "
|
||||||
|
"Arguments: %s",
|
||||||
|
json.dumps(arguments),
|
||||||
|
)
|
||||||
return {"error": "Camera parameter is required"}
|
return {"error": "Camera parameter is required"}
|
||||||
return await _execute_get_live_context(request, camera, allowed_cameras)
|
return await _execute_get_live_context(request, camera, allowed_cameras)
|
||||||
else:
|
else:
|
||||||
|
logger.error(
|
||||||
|
"Tool call failed: unknown tool %r. Expected one of: search_objects, get_live_context. "
|
||||||
|
"Arguments received: %s",
|
||||||
|
tool_name,
|
||||||
|
json.dumps(arguments),
|
||||||
|
)
|
||||||
return {"error": f"Unknown tool: {tool_name}"}
|
return {"error": f"Unknown tool: {tool_name}"}
|
||||||
|
|
||||||
|
|
||||||
@ -425,6 +436,14 @@ async def _execute_pending_tools(
|
|||||||
tool_result = await _execute_tool_internal(
|
tool_result = await _execute_tool_internal(
|
||||||
tool_name, tool_args, request, allowed_cameras
|
tool_name, tool_args, request, allowed_cameras
|
||||||
)
|
)
|
||||||
|
if isinstance(tool_result, dict) and tool_result.get("error"):
|
||||||
|
logger.error(
|
||||||
|
"Tool call %s (id: %s) returned error: %s. Arguments: %s",
|
||||||
|
tool_name,
|
||||||
|
tool_call_id,
|
||||||
|
tool_result.get("error"),
|
||||||
|
json.dumps(tool_args),
|
||||||
|
)
|
||||||
if tool_name == "search_objects" and isinstance(tool_result, list):
|
if tool_name == "search_objects" and isinstance(tool_result, list):
|
||||||
tool_result = _format_events_with_local_time(tool_result)
|
tool_result = _format_events_with_local_time(tool_result)
|
||||||
_keys = {
|
_keys = {
|
||||||
@ -459,7 +478,11 @@ async def _execute_pending_tools(
|
|||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Error executing tool {tool_name} (id: {tool_call_id}): {e}",
|
"Error executing tool %s (id: %s): %s. Arguments: %s",
|
||||||
|
tool_name,
|
||||||
|
tool_call_id,
|
||||||
|
e,
|
||||||
|
json.dumps(tool_args),
|
||||||
exc_info=True,
|
exc_info=True,
|
||||||
)
|
)
|
||||||
error_content = json.dumps({"error": f"Tool execution failed: {str(e)}"})
|
error_content = json.dumps({"error": f"Tool execution failed: {str(e)}"})
|
||||||
|
|||||||
@ -291,19 +291,24 @@ class LlamaCppClient(GenAIClient):
|
|||||||
yield ("content_delta", delta["content"])
|
yield ("content_delta", delta["content"])
|
||||||
for tc in delta.get("tool_calls") or []:
|
for tc in delta.get("tool_calls") or []:
|
||||||
idx = tc.get("index", 0)
|
idx = tc.get("index", 0)
|
||||||
|
fn = tc.get("function") or {}
|
||||||
if idx not in tool_calls_by_index:
|
if idx not in tool_calls_by_index:
|
||||||
tool_calls_by_index[idx] = {
|
tool_calls_by_index[idx] = {
|
||||||
"id": tc.get("id", ""),
|
"id": tc.get("id", ""),
|
||||||
"name": tc.get("name", ""),
|
"name": tc.get("name") or fn.get("name", ""),
|
||||||
"arguments": "",
|
"arguments": "",
|
||||||
}
|
}
|
||||||
t = tool_calls_by_index[idx]
|
t = tool_calls_by_index[idx]
|
||||||
if tc.get("id"):
|
if tc.get("id"):
|
||||||
t["id"] = tc["id"]
|
t["id"] = tc["id"]
|
||||||
if tc.get("name"):
|
name = tc.get("name") or fn.get("name")
|
||||||
t["name"] = tc["name"]
|
if name:
|
||||||
if tc.get("arguments"):
|
t["name"] = name
|
||||||
t["arguments"] += tc["arguments"]
|
arg = tc.get("arguments") or fn.get("arguments")
|
||||||
|
if arg is not None:
|
||||||
|
t["arguments"] += (
|
||||||
|
arg if isinstance(arg, str) else json.dumps(arg)
|
||||||
|
)
|
||||||
|
|
||||||
full_content = "".join(content_parts).strip() or None
|
full_content = "".join(content_parts).strip() or None
|
||||||
tool_calls_list = self._streamed_tool_calls_to_list(tool_calls_by_index)
|
tool_calls_list = self._streamed_tool_calls_to_list(tool_calls_by_index)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user