Improve error handling

This commit is contained in:
Shuguang Chen 2025-02-04 11:25:30 -08:00
parent 962727f244
commit 0bd0e86972
2 changed files with 21 additions and 15 deletions

View file

@ -355,7 +355,7 @@ class ArchFunctionHandler(ArchBaseHandler):
try:
tool_content = json.loads(fixed_content)
except Exception:
tool_calls, is_valid, error_message = [], False, e
is_valid, error_message = False, e
break
tool_calls.append(
@ -571,23 +571,28 @@ class ArchFunctionHandler(ArchBaseHandler):
# Extract tool calls from model response
extracted = self._extract_tool_calls(model_response)
if len(extracted["result"]) and extracted["status"]:
verified = self._verify_tool_calls(
tools=req.tools, tool_calls=extracted["result"]
)
if verified["status"]:
logger.info(
f"[Tool calls]: {json.dumps([tool_call['function'] for tool_call in extracted['result']])}"
if extracted["status"]:
# Response with tool calls
if len(extracted["result"]):
verified = self._verify_tool_calls(
tools=req.tools, tool_calls=extracted["result"]
)
model_response = Message(content="", tool_calls=extracted["result"])
if verified["status"]:
logger.info(
f"[Tool calls]: {json.dumps([tool_call['function'] for tool_call in extracted['result']])}"
)
model_response = Message(content="", tool_calls=extracted["result"])
else:
# TODO: make a call to default LLM to get responses or retry Arch-Function
logger.error(f"Invalid tool call - {verified['message']}")
# Response without tool calls
else:
logger.error(f"Invalid tool call - {verified['message']}")
# raise ValueError(
# f"[Arch-Function]: Invalid tool call - {verified['message']}"
# )
model_response = Message(content=model_response, tool_calls=[])
# Response with tool calls but contain errors
else:
model_response = Message(content=model_response, tool_calls=[])
# TODO: make a call to default LLM to get responses or retry Arch-Function
logger.error(f"Tool call extraction error - {extracted['message']}")
chat_completion_response = ChatCompletionResponse(
choices=[Choice(message=model_response)], model=self.model_name

View file

@ -102,6 +102,7 @@ async def function_calling(req: ChatMessage, res: Response):
res.status_code = 500
error_messages = f"[Arch-Function] - Error in ChatCompletion: {e}"
else:
# TODO: make a call to default LLM to get responses
intent_response.metadata = {
"intent_latency": str(round(intent_latency * 1000, 3)),
}