Remove any markdown json codeblock in chat actors expecting json responses

Strip any json md codeblock wrapper if exists before processing
response by output mode, extract questions chat actor. This is similar
to what is already being done by other chat actors

Useful for succesfully interpreting json output in chat actors when
using non (json) schema enforceable models like o1 and gemma-2

Use conversation helper function to centralize the json md codeblock
removal code
This commit is contained in:
Debanjum Singh Solanky
2024-09-12 16:41:40 -07:00
parent 6e660d11c9
commit 0685a79748
3 changed files with 15 additions and 7 deletions

View File

@@ -14,6 +14,7 @@ from khoj.processor.conversation.openai.utils import (
from khoj.processor.conversation.utils import (
construct_structured_message,
generate_chatml_messages_with_context,
remove_json_codeblock,
)
from khoj.utils.helpers import ConversationCommand, is_none_or_empty
from khoj.utils.rawconfig import LocationData
@@ -85,6 +86,7 @@ def extract_questions(
# Extract, Clean Message from GPT's Response
try:
response = response.strip()
response = remove_json_codeblock(response)
response = json.loads(response)
response = [q.strip() for q in response["queries"] if q.strip()]
if not isinstance(response, list) or not response: