From 03c4f614dd25546632e85aaad1039e917918e07b Mon Sep 17 00:00:00 2001 From: Debanjum Date: Wed, 23 Jul 2025 20:30:22 -0500 Subject: [PATCH] Handle tool call requests with openai completion in non stream mode --- src/khoj/processor/conversation/openai/utils.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/khoj/processor/conversation/openai/utils.py b/src/khoj/processor/conversation/openai/utils.py index 38b55071..755394e0 100644 --- a/src/khoj/processor/conversation/openai/utils.py +++ b/src/khoj/processor/conversation/openai/utils.py @@ -158,6 +158,13 @@ def completion_with_backoff( **model_kwargs, ) aggregated_response = chunk.choices[0].message.content + raw_tool_calls = chunk.choices[0].message.tool_calls + if raw_tool_calls: + tool_calls = [ + ToolCall(name=tool.function.name, args=tool.function.parsed_arguments, id=tool.id) + for tool in raw_tool_calls + ] + aggregated_response = json.dumps([tool_call.__dict__ for tool_call in tool_calls]) # Calculate cost of chat input_tokens = chunk.usage.prompt_tokens if hasattr(chunk, "usage") and chunk.usage else 0