Skip to content

Commit 639b5a4

Browse files
committed
Only enforce json output in supported AI model APIs
Deepseek reasoner does not support json object or schema via deepseek API Azure Ai API does not support json schema Resolves #1126
1 parent d74c3a1 commit 639b5a4

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/khoj/processor/conversation/openai/gpt.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
from khoj.database.models import Agent, ChatModel, KhojUser
99
from khoj.processor.conversation import prompts
1010
from khoj.processor.conversation.openai.utils import (
11+
ai_api_supports_json_enforcement,
1112
chat_completion_with_backoff,
1213
completion_with_backoff,
1314
)
1415
from khoj.processor.conversation.utils import (
16+
JsonSupport,
1517
clean_json,
1618
construct_structured_message,
1719
generate_chatml_messages_with_context,
@@ -126,13 +128,14 @@ def send_message_to_model(
126128
"""
127129

128130
# Get Response from GPT
131+
json_support = ai_api_supports_json_enforcement(model, api_base_url)
129132
return completion_with_backoff(
130133
messages=messages,
131134
model_name=model,
132135
openai_api_key=api_key,
133136
temperature=temperature,
134137
api_base_url=api_base_url,
135-
model_kwargs={"response_format": {"type": response_type}},
138+
model_kwargs={"response_format": {"type": response_type}} if json_support >= JsonSupport.OBJECT else {},
136139
tracer=tracer,
137140
)
138141

src/khoj/processor/conversation/openai/utils.py

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
)
1717

1818
from khoj.processor.conversation.utils import (
19+
JsonSupport,
1920
ThreadedGenerator,
2021
commit_conversation_trace,
2122
)
@@ -245,3 +246,11 @@ def llm_thread(
245246
logger.error(f"Error in llm_thread: {e}", exc_info=True)
246247
finally:
247248
g.close()
249+
250+
251+
def ai_api_supports_json_enforcement(model_name: str, api_base_url: str = None) -> JsonSupport:
252+
if model_name.startswith("deepseek-reasoner"):
253+
return JsonSupport.NONE
254+
if ".ai.azure.com" in api_base_url:
255+
return JsonSupport.OBJECT
256+
return JsonSupport.SCHEMA

src/khoj/processor/conversation/utils.py

+6
Original file line numberDiff line numberDiff line change
@@ -878,3 +878,9 @@ def safe_serialize(content: Any) -> str:
878878
return str(content)
879879

880880
return "\n".join([f"{json.dumps(safe_serialize(message.content))[:max_length]}..." for message in messages])
881+
882+
883+
class JsonSupport(int, Enum):
884+
NONE = 0
885+
OBJECT = 1
886+
SCHEMA = 2

0 commit comments

Comments
 (0)