Skip to content

Commit 40187b8

Browse files
committed
Add query optimization back in, but don't run translation for chat queries
1 parent a81a381 commit 40187b8

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

core_backend/app/llm_call/llm_prompts.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ class ChatHistory:
268268
269269
{{
270270
"message_type": "The type of the user's LATEST MESSAGE. List of valid
271-
options are: {valid_message_types}"
271+
options are: {valid_message_types}",
272+
"query": "The vector database query that you have constructed based on
273+
the user's LATEST MESSAGE and the conversation history."
272274
}}
273275
274276
Do NOT attempt to answer the user's question/concern. Only output the JSON
@@ -283,6 +285,7 @@ class ChatHistoryConstructSearchQuery(BaseModel):
283285
"""Pydantic model for the output of the construct search query chat history."""
284286

285287
message_type: Literal["FOLLOW-UP", "NEW"]
288+
query: str
286289

287290
@staticmethod
288291
def parse_json(*, chat_type: Literal["search"], json_str: str) -> dict[str, str]:

core_backend/app/llm_call/process_input.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ async def _identify_language(
114114
litellm_model=LITELLM_MODEL_LANGUAGE_DETECT,
115115
metadata=metadata,
116116
system_message=LANGUAGE_ID_PROMPT,
117-
user_message=query_refined.query_text,
117+
# Always use the original query text for language and script detection
118+
user_message=query_refined.query_text_original,
118119
)
119120

120121
cleaned_json_str = remove_json_markdown(text=json_str)
@@ -256,9 +257,10 @@ async def wrapper(
256257
The appropriate response object.
257258
"""
258259

259-
query_refined, response = await _translate_question(
260-
query_refined=query_refined, response=response
261-
)
260+
if not query_refined.chat_query_params:
261+
query_refined, response = await _translate_question(
262+
query_refined=query_refined, response=response
263+
)
262264
response = await func(query_refined, response, *args, **kwargs)
263265

264266
return response
@@ -492,9 +494,11 @@ async def wrapper(
492494
The appropriate response object.
493495
"""
494496

495-
query_refined, response = await _paraphrase_question(
496-
query_refined=query_refined, response=response
497-
)
497+
if not query_refined.chat_query_params:
498+
query_refined, response = await _paraphrase_question(
499+
query_refined=query_refined, response=response
500+
)
501+
498502
response = await func(query_refined, response, *args, **kwargs)
499503

500504
return response

core_backend/app/question_answer/routers.py

+8
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,13 @@ async def get_user_query_and_response(
844844
workspace_id=workspace_id,
845845
)
846846

847+
# In case of a chat query, use the optimized query as the base query_text.
848+
# Note that for language identification, we use query_text_original.
849+
if user_query_refined.chat_query_params:
850+
user_query_refined.query_text = user_query_refined.chat_query_params.pop(
851+
"search_query"
852+
)
853+
847854
# Prepare the placeholder response object.
848855
response_template = QueryResponse(
849856
debug_info={},
@@ -1072,6 +1079,7 @@ async def init_user_query_and_chat_histories(
10721079
"chat_history": user_assistant_chat_history,
10731080
"chat_params": chat_params,
10741081
"message_type": search_query_json_response["message_type"],
1082+
"search_query": search_query_json_response["query"],
10751083
"redis_client": redis_client,
10761084
"session_id": session_id,
10771085
}

core_backend/app/question_answer/utils.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def get_context_string_from_search_results(
2323
for key, result in search_results.items():
2424
if not isinstance(result, QuerySearchResult):
2525
result = QuerySearchResult(**result)
26-
context_list.append(f"{key}. {result.title}\n{result.text}")
26+
context_list.append(
27+
f"<document id={key}> \n**{result.title}**\n\n{result.text}\n</document>"
28+
)
2729
context_string = "\n\n".join(context_list)
2830
return context_string

core_backend/tests/api/test_chat.py

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ async def test_init_user_query_and_chat_histories(redis_client: aioredis.Redis)
8585
chat_query_params["chat_cache_key"] == f"chatCache:{user_query.session_id}"
8686
)
8787
assert chat_query_params["message_type"] == "NEW"
88+
assert chat_query_params["search_query"] == "stomachache and possible remedies"
8889

8990

9091
async def test__ask_llm_async() -> None:

0 commit comments

Comments
 (0)