diff --git a/subquery.py b/subquery.py index f34b8c2..1bfcdf7 100644 --- a/subquery.py +++ b/subquery.py @@ -5,6 +5,7 @@ from llama_index.core.tools import QueryEngineTool, ToolMetadata from llama_index.llms.openai import OpenAI from llama_index.question_gen.guidance import GuidanceQuestionGenerator +from tc_hivemind_backend.db.utils.preprocess_text import BasePreprocessor from tc_hivemind_backend.embeddings.cohere import CohereEmbedding from utils.qdrant_utils import QDrantUtils from utils.query_engine import ( @@ -19,7 +20,7 @@ WebsiteQueryEngine, prepare_discord_engine_auto_filter, ) - +from utils.globals import INVALID_QUERY_RESPONSE def query_multiple_source( query: str, @@ -198,6 +199,10 @@ def query_multiple_source( metadata=tool_metadata, ) ) + if not BasePreprocessor().extract_main_content(text=query): + response = INVALID_QUERY_RESPONSE + source_nodes = [] + return response, source_nodes embed_model = CohereEmbedding() llm = OpenAI("gpt-4o-mini") diff --git a/utils/globals.py b/utils/globals.py index eb0bedd..3c43463 100644 --- a/utils/globals.py +++ b/utils/globals.py @@ -1,2 +1,4 @@ # the theshold to skip nodes of being included in an answer RETRIEVER_THRESHOLD = 0.4 +INVALID_QUERY_RESPONSE="We're unable to process your query. Please refine it and try again." +QUERY_ERROR_MESSAGE="Sorry, we're unable to process your question at the moment. Please try again later." diff --git a/worker/tasks.py b/worker/tasks.py index 346f542..47d5fd2 100644 --- a/worker/tasks.py +++ b/worker/tasks.py @@ -5,6 +5,7 @@ from llama_index.core.schema import NodeWithScore from subquery import query_multiple_source from utils.data_source_selector import DataSourceSelector +from utils.globals import QUERY_ERROR_MESSAGE from utils.query_engine.prepare_answer_sources import PrepareAnswerSources from utils.traceloop import init_tracing from worker.celery import app @@ -21,7 +22,7 @@ def ask_question_auto_search( ) answer_sources = PrepareAnswerSources().prepare_answer_sources(nodes=references) except Exception: - response = "Sorry, We cannot process your question at the moment." + response = QUERY_ERROR_MESSAGE answer_sources = None logging.error( f"Errors raised while processing the question for community: {community_id}!"