mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-09 05:39:12 +00:00
Separate notes, online context from user message sent to chat models (#950)
Overview --- - Put context into separate user message before sending to chat model. This should improve model response quality and truncation logic in code - Pass online context from chat history to chat model for response. This should improve response speed when previous online context can be reused - Improve format of notes, online context passed to chat models in prompt. This should improve model response quality Details --- The document, online search context are now passed as separate user messages to chat model, instead of being added to the final user message. This will improve - Models ability to differentiate data from user query. That should improve response quality and reduce prompt injection probability - Make truncation logic simpler and more robust When context window hit, can simply pop messages to auto truncate context in order of context, user, assistant message for each conversation turn in history until reach current user query The complex, brittle logic to extract user query from context in last user message isn't required.
This commit is contained in:
@@ -214,7 +214,7 @@ def test_answer_from_chat_history_and_previously_retrieved_content():
|
||||
(
|
||||
"When was I born?",
|
||||
"You were born on 1st April 1984.",
|
||||
["Testatron was born on 1st April 1984 in Testville."],
|
||||
[{"compiled": "Testatron was born on 1st April 1984 in Testville.", "file": "birth.org"}],
|
||||
),
|
||||
]
|
||||
|
||||
@@ -415,15 +415,18 @@ def test_ask_for_clarification_if_not_enough_context_in_question():
|
||||
context = [
|
||||
{
|
||||
"compiled": f"""# Ramya
|
||||
My sister, Ramya, is married to Kali Devi. They have 2 kids, Ravi and Rani."""
|
||||
My sister, Ramya, is married to Kali Devi. They have 2 kids, Ravi and Rani.""",
|
||||
"file": "Family.md",
|
||||
},
|
||||
{
|
||||
"compiled": f"""# Fang
|
||||
My sister, Fang Liu is married to Xi Li. They have 1 kid, Xiao Li."""
|
||||
My sister, Fang Liu is married to Xi Li. They have 1 kid, Xiao Li.""",
|
||||
"file": "Family.md",
|
||||
},
|
||||
{
|
||||
"compiled": f"""# Aiyla
|
||||
My sister, Aiyla is married to Tolga. They have 3 kids, Yildiz, Ali and Ahmet."""
|
||||
My sister, Aiyla is married to Tolga. They have 3 kids, Yildiz, Ali and Ahmet.""",
|
||||
"file": "Family.md",
|
||||
},
|
||||
]
|
||||
|
||||
@@ -608,9 +611,11 @@ async def test_infer_webpage_urls_actor_extracts_correct_links(chat_client, defa
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_infer_task_scheduling_request(chat_client, user_query, expected_crontime, expected_qs, unexpected_qs):
|
||||
async def test_infer_task_scheduling_request(
|
||||
chat_client, user_query, expected_crontime, expected_qs, unexpected_qs, default_user2
|
||||
):
|
||||
# Act
|
||||
crontime, inferred_query, _ = await schedule_query(user_query, {})
|
||||
crontime, inferred_query, _ = await schedule_query(user_query, {}, default_user2)
|
||||
inferred_query = inferred_query.lower()
|
||||
|
||||
# Assert
|
||||
@@ -630,7 +635,7 @@ async def test_infer_task_scheduling_request(chat_client, user_query, expected_c
|
||||
"scheduling_query, executing_query, generated_response, expected_should_notify",
|
||||
[
|
||||
(
|
||||
"Notify me if it is going to rain tomorrow?",
|
||||
"Notify me only if it is going to rain tomorrow?",
|
||||
"What's the weather forecast for tomorrow?",
|
||||
"It is sunny and warm tomorrow.",
|
||||
False,
|
||||
@@ -656,10 +661,10 @@ async def test_infer_task_scheduling_request(chat_client, user_query, expected_c
|
||||
],
|
||||
)
|
||||
def test_decision_on_when_to_notify_scheduled_task_results(
|
||||
chat_client, scheduling_query, executing_query, generated_response, expected_should_notify
|
||||
chat_client, default_user2, scheduling_query, executing_query, generated_response, expected_should_notify
|
||||
):
|
||||
# Act
|
||||
generated_should_notify = should_notify(scheduling_query, executing_query, generated_response)
|
||||
generated_should_notify = should_notify(scheduling_query, executing_query, generated_response, default_user2)
|
||||
|
||||
# Assert
|
||||
assert generated_should_notify == expected_should_notify
|
||||
|
||||
@@ -307,7 +307,7 @@ def test_summarize_one_file(chat_client, default_user2: KhojUser):
|
||||
json={"filename": summarization_file, "conversation_id": str(conversation.id)},
|
||||
)
|
||||
query = "/summarize"
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": str(conversation.id)})
|
||||
response_message = response.json()["response"]
|
||||
# Assert
|
||||
assert response_message != ""
|
||||
@@ -339,7 +339,7 @@ def test_summarize_extra_text(chat_client, default_user2: KhojUser):
|
||||
json={"filename": summarization_file, "conversation_id": str(conversation.id)},
|
||||
)
|
||||
query = "/summarize tell me about Xiu"
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": str(conversation.id)})
|
||||
response_message = response.json()["response"]
|
||||
# Assert
|
||||
assert response_message != ""
|
||||
@@ -367,7 +367,7 @@ def test_summarize_multiple_files(chat_client, default_user2: KhojUser):
|
||||
)
|
||||
|
||||
query = "/summarize"
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": str(conversation.id)})
|
||||
response_message = response.json()["response"]
|
||||
|
||||
# Assert
|
||||
@@ -383,7 +383,7 @@ def test_summarize_no_files(chat_client, default_user2: KhojUser):
|
||||
|
||||
# Act
|
||||
query = "/summarize"
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": str(conversation.id)})
|
||||
response_message = response.json()["response"]
|
||||
|
||||
# Assert
|
||||
@@ -418,11 +418,11 @@ def test_summarize_different_conversation(chat_client, default_user2: KhojUser):
|
||||
|
||||
# Act
|
||||
query = "/summarize"
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation2.id})
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": str(conversation2.id)})
|
||||
response_message_conv2 = response.json()["response"]
|
||||
|
||||
# now make sure that the file filter is still in conversation 1
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation1.id})
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": str(conversation1.id)})
|
||||
response_message_conv1 = response.json()["response"]
|
||||
|
||||
# Assert
|
||||
@@ -449,7 +449,7 @@ def test_summarize_nonexistant_file(chat_client, default_user2: KhojUser):
|
||||
json={"filename": "imaginary.markdown", "conversation_id": str(conversation.id)},
|
||||
)
|
||||
query = urllib.parse.quote("/summarize")
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": str(conversation.id)})
|
||||
response_message = response.json()["response"]
|
||||
# Assert
|
||||
assert response_message == "No files selected for summarization. Please add files using the section on the left."
|
||||
@@ -481,7 +481,7 @@ def test_summarize_diff_user_file(chat_client, default_user: KhojUser, pdf_confi
|
||||
|
||||
# Act
|
||||
query = "/summarize"
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
|
||||
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": str(conversation.id)})
|
||||
response_message = response.json()["response"]
|
||||
|
||||
# Assert
|
||||
|
||||
Reference in New Issue
Block a user