From 48870d9170ffb4621b96209315437ba32f7c8e99 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 7 Jul 2023 15:18:55 -0700 Subject: [PATCH] Fix parsing questions generated by extract_questions actor into list The previous json parsing was failing to handle questions with date filters Fix the chat actor tests to run without throwing error with freezegun complaining about importing transformers.local_llama model Remove quote escapes from date filter examples provided to extract_questions actor --- src/khoj/processor/conversation/gpt.py | 8 +++++--- src/khoj/processor/conversation/prompts.py | 2 +- tests/test_chat_actors.py | 3 +++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/khoj/processor/conversation/gpt.py b/src/khoj/processor/conversation/gpt.py index 423a2124..ed8ea455 100644 --- a/src/khoj/processor/conversation/gpt.py +++ b/src/khoj/processor/conversation/gpt.py @@ -110,14 +110,16 @@ def extract_questions( # Extract, Clean Message from GPT's Response try: - questions = json.loads( - # Clean response to increase likelihood of valid JSON. E.g replace ' with " to enclose strings + questions = ( response.strip(empty_escape_sequences) .replace("['", '["') .replace("']", '"]') .replace("', '", '", "') + .replace('["', "") + .replace('"]', "") + .split('", "') ) - except json.decoder.JSONDecodeError: + except: logger.warning(f"GPT returned invalid JSON. Falling back to using user message as search query.\n{response}") questions = [text] logger.debug(f"Extracted Questions by GPT: {questions}") diff --git a/src/khoj/processor/conversation/prompts.py b/src/khoj/processor/conversation/prompts.py index 2cbc9f40..04b5d108 100644 --- a/src/khoj/processor/conversation/prompts.py +++ b/src/khoj/processor/conversation/prompts.py @@ -102,7 +102,7 @@ A: You visited the Angkor Wat Temple in Cambodia with Pablo, Namita and Xi. Q: What national parks did I go to last year? -["National park I visited in {last_new_year} dt>=\\"{last_new_year_date}\\" dt<\\"{current_new_year_date}\\""] +["National park I visited in {last_new_year} dt>="{last_new_year_date}" dt<"{current_new_year_date}""] A: You visited the Grand Canyon and Yellowstone National Park in {last_new_year}. diff --git a/tests/test_chat_actors.py b/tests/test_chat_actors.py index df710ed3..524f0813 100644 --- a/tests/test_chat_actors.py +++ b/tests/test_chat_actors.py @@ -4,6 +4,7 @@ from datetime import datetime # External Packages import pytest +import freezegun from freezegun import freeze_time # Internal Packages @@ -19,6 +20,8 @@ if api_key is None: allow_module_level=True, ) +freezegun.configure(extend_ignore_list=["transformers"]) + # Test # ----------------------------------------------------------------------------------------------------