Update chat director, client tests to call chat API using new POST method

This commit is contained in:
Debanjum Singh Solanky 2024-09-11 16:45:44 -07:00
parent 3f51af9a96
commit bc2e889d72
3 changed files with 160 additions and 111 deletions

View file

@ -459,11 +459,12 @@ def test_user_no_data_returns_empty(client, sample_org_data, api_user3: KhojApiU
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
async def test_chat_with_unauthenticated_user(chat_client_with_auth, api_user2: KhojApiUser): async def test_chat_with_unauthenticated_user(chat_client_with_auth, api_user2: KhojApiUser):
# Arrange # Arrange
query = "Hello!"
headers = {"Authorization": f"Bearer {api_user2.token}"} headers = {"Authorization": f"Bearer {api_user2.token}"}
# Act # Act
auth_response = chat_client_with_auth.post(f'/api/chat?q="Hello!"', headers=headers) auth_response = chat_client_with_auth.post(f"/api/chat", json={"q": query}, headers=headers)
no_auth_response = chat_client_with_auth.post(f'/api/chat?q="Hello!"') no_auth_response = chat_client_with_auth.post(f"/api/chat", json={"q": query})
# Assert # Assert
assert auth_response.status_code == 200 assert auth_response.status_code == 200

View file

@ -1,6 +1,4 @@
import os
import urllib.parse import urllib.parse
from urllib.parse import quote
import pytest import pytest
from faker import Faker from faker import Faker
@ -49,7 +47,8 @@ def create_conversation(message_list, user, agent=None):
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
def test_offline_chat_with_no_chat_history_or_retrieved_content(client_offline_chat): def test_offline_chat_with_no_chat_history_or_retrieved_content(client_offline_chat):
# Act # Act
response = client_offline_chat.post(f'/api/chat?q="Hello, my name is Testatron. Who are you?"&stream=true') query = "Hello, my name is Testatron. Who are you?"
response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -66,8 +65,7 @@ def test_offline_chat_with_no_chat_history_or_retrieved_content(client_offline_c
def test_chat_with_online_content(client_offline_chat): def test_chat_with_online_content(client_offline_chat):
# Act # Act
q = "/online give me the link to paul graham's essay how to do great work" q = "/online give me the link to paul graham's essay how to do great work"
encoded_q = quote(q, safe="") response = client_offline_chat.post(f"/api/chat", json={"q": q})
response = client_offline_chat.post(f"/api/chat?q={encoded_q}")
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -88,8 +86,7 @@ def test_chat_with_online_content(client_offline_chat):
def test_chat_with_online_webpage_content(client_offline_chat): def test_chat_with_online_webpage_content(client_offline_chat):
# Act # Act
q = "/online how many firefighters were involved in the great chicago fire and which year did it take place?" q = "/online how many firefighters were involved in the great chicago fire and which year did it take place?"
encoded_q = quote(q, safe="") response = client_offline_chat.post(f"/api/chat", json={"q": q})
response = client_offline_chat.post(f"/api/chat?q={encoded_q}")
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -112,7 +109,8 @@ def test_answer_from_chat_history(client_offline_chat, default_user2):
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = client_offline_chat.post(f'/api/chat?q="What is my name?"&stream=true') q = "What is my name?"
response = client_offline_chat.post(f"/api/chat", json={"q": q, "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -139,7 +137,8 @@ def test_answer_from_currently_retrieved_content(client_offline_chat, default_us
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = client_offline_chat.post(f'/api/chat?q="Where was Xi Li born?"') q = "Where was Xi Li born?"
response = client_offline_chat.post(f"/api/chat", json={"q": q})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -163,7 +162,8 @@ def test_answer_from_chat_history_and_previously_retrieved_content(client_offlin
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = client_offline_chat.post(f'/api/chat?q="Where was I born?"') q = "Where was I born?"
response = client_offline_chat.post(f"/api/chat", json={"q": q})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -185,7 +185,8 @@ def test_answer_from_chat_history_and_currently_retrieved_content(client_offline
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = client_offline_chat.post(f'/api/chat?q="Where was I born?"') q = "Where was I born?"
response = client_offline_chat.post(f"/api/chat", json={"q": q})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -211,7 +212,8 @@ def test_no_answer_in_chat_history_or_retrieved_content(client_offline_chat, def
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = client_offline_chat.post(f'/api/chat?q="Where was I born?"&stream=true') q = "Where was I born?"
response = client_offline_chat.post(f"/api/chat", json={"q": q, "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -227,12 +229,12 @@ def test_no_answer_in_chat_history_or_retrieved_content(client_offline_chat, def
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
def test_answer_using_general_command(client_offline_chat, default_user2): def test_answer_using_general_command(client_offline_chat, default_user2):
# Arrange # Arrange
query = urllib.parse.quote("/general Where was Xi Li born?")
message_list = [] message_list = []
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = client_offline_chat.post(f"/api/chat?q={query}&stream=true") query = "/general Where was Xi Li born?"
response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -245,12 +247,12 @@ def test_answer_using_general_command(client_offline_chat, default_user2):
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
def test_answer_from_retrieved_content_using_notes_command(client_offline_chat, default_user2): def test_answer_from_retrieved_content_using_notes_command(client_offline_chat, default_user2):
# Arrange # Arrange
query = urllib.parse.quote("/notes Where was Xi Li born?")
message_list = [] message_list = []
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = client_offline_chat.post(f"/api/chat?q={query}&stream=true") query = "/notes Where was Xi Li born?"
response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -269,8 +271,12 @@ def test_answer_using_file_filter(client_offline_chat, default_user2):
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
no_answer_response = client_offline_chat.post(f"/api/chat?q={no_answer_query}&stream=true").content.decode("utf-8") no_answer_response = client_offline_chat.post(
answer_response = client_offline_chat.post(f"/api/chat?q={answer_query}&stream=true").content.decode("utf-8") f"/api/chat", json={"q": no_answer_query, "stream": True}
).content.decode("utf-8")
answer_response = client_offline_chat.post(f"/api/chat", json={"q": answer_query, "stream": True}).content.decode(
"utf-8"
)
# Assert # Assert
assert "Fujiang" not in no_answer_response assert "Fujiang" not in no_answer_response
@ -282,12 +288,12 @@ def test_answer_using_file_filter(client_offline_chat, default_user2):
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
def test_answer_not_known_using_notes_command(client_offline_chat, default_user2): def test_answer_not_known_using_notes_command(client_offline_chat, default_user2):
# Arrange # Arrange
query = urllib.parse.quote("/notes Where was Testatron born?")
message_list = [] message_list = []
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = client_offline_chat.post(f"/api/chat?q={query}&stream=true") query = urllib.parse.quote("/notes Where was Testatron born?")
response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -299,6 +305,7 @@ def test_answer_not_known_using_notes_command(client_offline_chat, default_user2
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
@pytest.mark.chatquality @pytest.mark.chatquality
def test_summarize_one_file(client_offline_chat, default_user2: KhojUser): def test_summarize_one_file(client_offline_chat, default_user2: KhojUser):
# Arrange
message_list = [] message_list = []
conversation = create_conversation(message_list, default_user2) conversation = create_conversation(message_list, default_user2)
# post "Xi Li.markdown" file to the file filters # post "Xi Li.markdown" file to the file filters
@ -319,9 +326,14 @@ def test_summarize_one_file(client_offline_chat, default_user2: KhojUser):
"api/chat/conversation/file-filters", "api/chat/conversation/file-filters",
json={"filename": summarization_file, "conversation_id": str(conversation.id)}, json={"filename": summarization_file, "conversation_id": str(conversation.id)},
) )
query = urllib.parse.quote("/summarize")
response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") # Act
query = "/summarize"
response = client_offline_chat.post(
f"/api/chat", json={"q": query, "conversation_id": conversation.id, "stream": True}
)
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
assert response_message != "" assert response_message != ""
assert response_message != "No files selected for summarization. Please add files using the section on the left." assert response_message != "No files selected for summarization. Please add files using the section on the left."
@ -331,6 +343,7 @@ def test_summarize_one_file(client_offline_chat, default_user2: KhojUser):
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
@pytest.mark.chatquality @pytest.mark.chatquality
def test_summarize_extra_text(client_offline_chat, default_user2: KhojUser): def test_summarize_extra_text(client_offline_chat, default_user2: KhojUser):
# Arrange
message_list = [] message_list = []
conversation = create_conversation(message_list, default_user2) conversation = create_conversation(message_list, default_user2)
# post "Xi Li.markdown" file to the file filters # post "Xi Li.markdown" file to the file filters
@ -351,9 +364,14 @@ def test_summarize_extra_text(client_offline_chat, default_user2: KhojUser):
"api/chat/conversation/file-filters", "api/chat/conversation/file-filters",
json={"filename": summarization_file, "conversation_id": str(conversation.id)}, json={"filename": summarization_file, "conversation_id": str(conversation.id)},
) )
query = urllib.parse.quote("/summarize tell me about Xiu")
response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") # Act
query = "/summarize tell me about Xiu"
response = client_offline_chat.post(
f"/api/chat", json={"q": query, "conversation_id": conversation.id, "stream": True}
)
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
assert response_message != "" assert response_message != ""
assert response_message != "No files selected for summarization. Please add files using the section on the left." assert response_message != "No files selected for summarization. Please add files using the section on the left."
@ -363,6 +381,7 @@ def test_summarize_extra_text(client_offline_chat, default_user2: KhojUser):
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
@pytest.mark.chatquality @pytest.mark.chatquality
def test_summarize_multiple_files(client_offline_chat, default_user2: KhojUser): def test_summarize_multiple_files(client_offline_chat, default_user2: KhojUser):
# Arrange
message_list = [] message_list = []
conversation = create_conversation(message_list, default_user2) conversation = create_conversation(message_list, default_user2)
# post "Xi Li.markdown" file to the file filters # post "Xi Li.markdown" file to the file filters
@ -379,9 +398,10 @@ def test_summarize_multiple_files(client_offline_chat, default_user2: KhojUser):
"api/chat/conversation/file-filters", json={"filename": file_list[1], "conversation_id": str(conversation.id)} "api/chat/conversation/file-filters", json={"filename": file_list[1], "conversation_id": str(conversation.id)}
) )
query = urllib.parse.quote("/summarize") # Act
response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") query = "/summarize"
response_message = response.content.decode("utf-8") response = client_offline_chat.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
response_message = response.json()["response"]
# Assert # Assert
assert response_message == "Only one file can be selected for summarization." assert response_message == "Only one file can be selected for summarization."
@ -390,11 +410,13 @@ def test_summarize_multiple_files(client_offline_chat, default_user2: KhojUser):
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
@pytest.mark.chatquality @pytest.mark.chatquality
def test_summarize_no_files(client_offline_chat, default_user2: KhojUser): def test_summarize_no_files(client_offline_chat, default_user2: KhojUser):
# Arrange
message_list = [] message_list = []
conversation = create_conversation(message_list, default_user2) conversation = create_conversation(message_list, default_user2)
query = urllib.parse.quote("/summarize") # Act
response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") query = "/summarize"
response_message = response.content.decode("utf-8") response = client_offline_chat.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
response_message = response.json()["response"]
# Assert # Assert
assert response_message == "No files selected for summarization. Please add files using the section on the left." assert response_message == "No files selected for summarization. Please add files using the section on the left."
@ -424,16 +446,16 @@ def test_summarize_different_conversation(client_offline_chat, default_user2: Kh
json={"filename": summarization_file, "conversation_id": str(conversation1.id)}, json={"filename": summarization_file, "conversation_id": str(conversation1.id)},
) )
query = urllib.parse.quote("/summarize") query = "/summarize"
response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation2.id}&stream=true") response = client_offline_chat.post(f"/api/chat", json={"q": query, "conversation_id": conversation2.id})
response_message = response.content.decode("utf-8") response_message = response.json()["response"]
# Assert # Assert
assert response_message == "No files selected for summarization. Please add files using the section on the left." assert response_message == "No files selected for summarization. Please add files using the section on the left."
# now make sure that the file filter is still in conversation 1 # now make sure that the file filter is still in conversation 1
response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation1.id}&stream=true") response = client_offline_chat.post(f"/api/chat", json={"q": query, "conversation_id": conversation1.id})
response_message = response.content.decode("utf-8") response_message = response.json()["response"]
# Assert # Assert
assert response_message != "" assert response_message != ""
@ -444,6 +466,7 @@ def test_summarize_different_conversation(client_offline_chat, default_user2: Kh
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
@pytest.mark.chatquality @pytest.mark.chatquality
def test_summarize_nonexistant_file(client_offline_chat, default_user2: KhojUser): def test_summarize_nonexistant_file(client_offline_chat, default_user2: KhojUser):
# Arrange
message_list = [] message_list = []
conversation = create_conversation(message_list, default_user2) conversation = create_conversation(message_list, default_user2)
# post "imaginary.markdown" file to the file filters # post "imaginary.markdown" file to the file filters
@ -451,9 +474,10 @@ def test_summarize_nonexistant_file(client_offline_chat, default_user2: KhojUser
"api/chat/conversation/file-filters", "api/chat/conversation/file-filters",
json={"filename": "imaginary.markdown", "conversation_id": str(conversation.id)}, json={"filename": "imaginary.markdown", "conversation_id": str(conversation.id)},
) )
query = urllib.parse.quote("/summarize") # Act
response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") query = "/summarize"
response_message = response.content.decode("utf-8") response = client_offline_chat.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
response_message = response.json()["response"]
# Assert # Assert
assert response_message == "No files selected for summarization. Please add files using the section on the left." assert response_message == "No files selected for summarization. Please add files using the section on the left."
@ -463,6 +487,7 @@ def test_summarize_nonexistant_file(client_offline_chat, default_user2: KhojUser
def test_summarize_diff_user_file( def test_summarize_diff_user_file(
client_offline_chat, default_user: KhojUser, pdf_configured_user1, default_user2: KhojUser client_offline_chat, default_user: KhojUser, pdf_configured_user1, default_user2: KhojUser
): ):
# Arrange
message_list = [] message_list = []
conversation = create_conversation(message_list, default_user2) conversation = create_conversation(message_list, default_user2)
# Get the pdf file called singlepage.pdf # Get the pdf file called singlepage.pdf
@ -482,9 +507,12 @@ def test_summarize_diff_user_file(
"api/chat/conversation/file-filters", "api/chat/conversation/file-filters",
json={"filename": summarization_file, "conversation_id": str(conversation.id)}, json={"filename": summarization_file, "conversation_id": str(conversation.id)},
) )
query = urllib.parse.quote("/summarize")
response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") # Act
response_message = response.content.decode("utf-8") query = "/summarize"
response = client_offline_chat.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
response_message = response.json()["response"]
# Assert # Assert
assert response_message == "No files selected for summarization. Please add files using the section on the left." assert response_message == "No files selected for summarization. Please add files using the section on the left."
@ -495,11 +523,9 @@ def test_summarize_diff_user_file(
@freeze_time("2023-04-01", ignore=["transformers"]) @freeze_time("2023-04-01", ignore=["transformers"])
def test_answer_requires_current_date_awareness(client_offline_chat): def test_answer_requires_current_date_awareness(client_offline_chat):
"Chat actor should be able to answer questions relative to current date using provided notes" "Chat actor should be able to answer questions relative to current date using provided notes"
# Arrange
query = urllib.parse.quote("Where did I have lunch today?")
# Act # Act
response = client_offline_chat.post(f"/api/chat?q={query}&stream=true") query = "Where did I have lunch today?"
response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -518,7 +544,8 @@ def test_answer_requires_current_date_awareness(client_offline_chat):
def test_answer_requires_date_aware_aggregation_across_provided_notes(client_offline_chat): def test_answer_requires_date_aware_aggregation_across_provided_notes(client_offline_chat):
"Chat director should be able to answer questions that require date aware aggregation across multiple notes" "Chat director should be able to answer questions that require date aware aggregation across multiple notes"
# Act # Act
response = client_offline_chat.post(f'/api/chat?q="How much did I spend on dining this year?"&stream=true') query = "How much did I spend on dining this year?"
response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -539,9 +566,8 @@ def test_answer_general_question_not_in_chat_history_or_retrieved_content(client
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = client_offline_chat.get( query = "Write a haiku about unit testing. Do not say anything else."
f'/api/chat?q=""Write a haiku about unit testing. Do not say anything else."&stream=true' response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True})
)
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -559,7 +585,8 @@ def test_answer_general_question_not_in_chat_history_or_retrieved_content(client
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
def test_ask_for_clarification_if_not_enough_context_in_question(client_offline_chat, default_user2): def test_ask_for_clarification_if_not_enough_context_in_question(client_offline_chat, default_user2):
# Act # Act
response = client_offline_chat.post(f'/api/chat?q="What is the name of Namitas older son"&stream=true') query = "What is the name of Namitas older son"
response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -589,7 +616,8 @@ def test_answer_in_chat_history_beyond_lookback_window(client_offline_chat, defa
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = client_offline_chat.post(f'/api/chat?q="What is my name?"&stream=true') query = "What is my name?"
response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -621,8 +649,9 @@ def test_answer_in_chat_history_by_conversation_id(client_offline_chat, default_
create_conversation(message_list2, default_user2) create_conversation(message_list2, default_user2)
# Act # Act
response = client_offline_chat.get( query = "What is my favorite color?"
f'/api/chat?q="What is my favorite color?"&conversation_id={conversation.id}&stream=true' response = client_offline_chat.post(
f"/api/chat", json={"q": query, "conversation_id": conversation.id, "stream": True}
) )
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
@ -652,8 +681,10 @@ def test_answer_in_chat_history_by_conversation_id_with_agent(
conversation = create_conversation(message_list, default_user2, offline_agent) conversation = create_conversation(message_list, default_user2, offline_agent)
# Act # Act
query = urllib.parse.quote("/general What did I eat for breakfast?") query = "/general What did I eat for breakfast?"
response = client_offline_chat.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") response = client_offline_chat.post(
f"/api/chat", json={"q": query, "conversation_id": conversation.id, "stream": True}
)
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert that agent only responds with the summary of spending # Assert that agent only responds with the summary of spending
@ -669,11 +700,11 @@ def test_answer_in_chat_history_by_conversation_id_with_agent(
def test_answer_chat_history_very_long(client_offline_chat, default_user2): def test_answer_chat_history_very_long(client_offline_chat, default_user2):
# Arrange # Arrange
message_list = [(" ".join([fake.paragraph() for _ in range(50)]), fake.sentence(), []) for _ in range(10)] message_list = [(" ".join([fake.paragraph() for _ in range(50)]), fake.sentence(), []) for _ in range(10)]
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = client_offline_chat.post(f'/api/chat?q="What is my name?"&stream=true') query = "What is my name?"
response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -687,7 +718,8 @@ def test_answer_chat_history_very_long(client_offline_chat, default_user2):
def test_answer_requires_multiple_independent_searches(client_offline_chat): def test_answer_requires_multiple_independent_searches(client_offline_chat):
"Chat director should be able to answer by doing multiple independent searches for required information" "Chat director should be able to answer by doing multiple independent searches for required information"
# Act # Act
response = client_offline_chat.post(f'/api/chat?q="Is Xi older than Namita?"&stream=true') query = "Is Xi older than Namita?"
response = client_offline_chat.post(f"/api/chat", json={"q": query, "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert

View file

@ -49,7 +49,7 @@ def create_conversation(message_list, user, agent=None):
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
def test_chat_with_no_chat_history_or_retrieved_content(chat_client): def test_chat_with_no_chat_history_or_retrieved_content(chat_client):
# Act # Act
response = chat_client.post(f'/api/chat?q="Hello, my name is Testatron. Who are you?"') response = chat_client.post(f"/api/chat", json={"q": "Hello, my name is Testatron. Who are you?"})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -66,8 +66,7 @@ def test_chat_with_no_chat_history_or_retrieved_content(chat_client):
def test_chat_with_online_content(chat_client): def test_chat_with_online_content(chat_client):
# Act # Act
q = "/online give me the link to paul graham's essay how to do great work" q = "/online give me the link to paul graham's essay how to do great work"
encoded_q = quote(q, safe="") response = chat_client.post(f"/api/chat?", json={"q": q})
response = chat_client.post(f"/api/chat?q={encoded_q}")
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -87,8 +86,7 @@ def test_chat_with_online_content(chat_client):
def test_chat_with_online_webpage_content(chat_client): def test_chat_with_online_webpage_content(chat_client):
# Act # Act
q = "/online how many firefighters were involved in the great chicago fire and which year did it take place?" q = "/online how many firefighters were involved in the great chicago fire and which year did it take place?"
encoded_q = quote(q, safe="") response = chat_client.post(f"/api/chat", json={"q": q})
response = chat_client.post(f"/api/chat?q={encoded_q}")
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -111,7 +109,7 @@ def test_answer_from_chat_history(chat_client, default_user2: KhojUser):
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = chat_client.post(f'/api/chat?q="What is my name?"') response = chat_client.post(f"/api/chat", json={"q": "What is my name?"})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -138,7 +136,7 @@ def test_answer_from_currently_retrieved_content(chat_client, default_user2: Kho
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = chat_client.post(f'/api/chat?q="Where was Xi Li born?"') response = chat_client.post(f"/api/chat", json={"q": "Where was Xi Li born?"})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -162,7 +160,7 @@ def test_answer_from_chat_history_and_previously_retrieved_content(chat_client_n
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = chat_client_no_background.post(f'/api/chat?q="Where was I born?"') response = chat_client_no_background.post(f"/api/chat", json={"q": "Where was I born?"})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -185,7 +183,7 @@ def test_answer_from_chat_history_and_currently_retrieved_content(chat_client, d
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = chat_client.post(f'/api/chat?q="Where was I born?"') response = chat_client.post(f"/api/chat", json={"q": "Where was I born?"})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -210,7 +208,7 @@ def test_no_answer_in_chat_history_or_retrieved_content(chat_client, default_use
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = chat_client.post(f'/api/chat?q="Where was I born?"') response = chat_client.post(f"/api/chat", json={"q": "Where was I born?"})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -235,12 +233,12 @@ def test_no_answer_in_chat_history_or_retrieved_content(chat_client, default_use
@pytest.mark.chatquality @pytest.mark.chatquality
def test_answer_using_general_command(chat_client, default_user2: KhojUser): def test_answer_using_general_command(chat_client, default_user2: KhojUser):
# Arrange # Arrange
query = urllib.parse.quote("/general Where was Xi Li born?") query = "/general Where was Xi Li born?"
message_list = [] message_list = []
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = chat_client.post(f"/api/chat?q={query}&stream=true") response = chat_client.post(f"/api/chat", json={"q": query, "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -253,12 +251,12 @@ def test_answer_using_general_command(chat_client, default_user2: KhojUser):
@pytest.mark.chatquality @pytest.mark.chatquality
def test_answer_from_retrieved_content_using_notes_command(chat_client, default_user2: KhojUser): def test_answer_from_retrieved_content_using_notes_command(chat_client, default_user2: KhojUser):
# Arrange # Arrange
query = urllib.parse.quote("/notes Where was Xi Li born?") query = "/notes Where was Xi Li born?"
message_list = [] message_list = []
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = chat_client.post(f"/api/chat?q={query}") response = chat_client.post(f"/api/chat", json={"q": query})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -271,12 +269,12 @@ def test_answer_from_retrieved_content_using_notes_command(chat_client, default_
@pytest.mark.chatquality @pytest.mark.chatquality
def test_answer_not_known_using_notes_command(chat_client_no_background, default_user2: KhojUser): def test_answer_not_known_using_notes_command(chat_client_no_background, default_user2: KhojUser):
# Arrange # Arrange
query = urllib.parse.quote("/notes Where was Testatron born?") query = "/notes Where was Testatron born?"
message_list = [] message_list = []
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = chat_client_no_background.post(f"/api/chat?q={query}") response = chat_client_no_background.post(f"/api/chat", json={"q": query})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -308,8 +306,8 @@ def test_summarize_one_file(chat_client, default_user2: KhojUser):
"api/chat/conversation/file-filters", "api/chat/conversation/file-filters",
json={"filename": summarization_file, "conversation_id": str(conversation.id)}, json={"filename": summarization_file, "conversation_id": str(conversation.id)},
) )
query = urllib.parse.quote("/summarize") query = "/summarize"
response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}") response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
assert response_message != "" assert response_message != ""
@ -340,8 +338,8 @@ def test_summarize_extra_text(chat_client, default_user2: KhojUser):
"api/chat/conversation/file-filters", "api/chat/conversation/file-filters",
json={"filename": summarization_file, "conversation_id": str(conversation.id)}, json={"filename": summarization_file, "conversation_id": str(conversation.id)},
) )
query = urllib.parse.quote("/summarize tell me about Xiu") query = "/summarize tell me about Xiu"
response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}") response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
assert response_message != "" assert response_message != ""
@ -368,8 +366,8 @@ def test_summarize_multiple_files(chat_client, default_user2: KhojUser):
"api/chat/conversation/file-filters", json={"filename": file_list[1], "conversation_id": str(conversation.id)} "api/chat/conversation/file-filters", json={"filename": file_list[1], "conversation_id": str(conversation.id)}
) )
query = urllib.parse.quote("/summarize") query = "/summarize"
response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}") response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -379,11 +377,15 @@ def test_summarize_multiple_files(chat_client, default_user2: KhojUser):
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
@pytest.mark.chatquality @pytest.mark.chatquality
def test_summarize_no_files(chat_client, default_user2: KhojUser): def test_summarize_no_files(chat_client, default_user2: KhojUser):
# Arrange
message_list = [] message_list = []
conversation = create_conversation(message_list, default_user2) conversation = create_conversation(message_list, default_user2)
query = urllib.parse.quote("/summarize")
response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}") # Act
query = "/summarize"
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
assert response_message == "No files selected for summarization. Please add files using the section on the left." assert response_message == "No files selected for summarization. Please add files using the section on the left."
@ -391,6 +393,7 @@ def test_summarize_no_files(chat_client, default_user2: KhojUser):
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
@pytest.mark.chatquality @pytest.mark.chatquality
def test_summarize_different_conversation(chat_client, default_user2: KhojUser): def test_summarize_different_conversation(chat_client, default_user2: KhojUser):
# Arrange
message_list = [] message_list = []
conversation1 = create_conversation(message_list, default_user2) conversation1 = create_conversation(message_list, default_user2)
conversation2 = create_conversation(message_list, default_user2) conversation2 = create_conversation(message_list, default_user2)
@ -413,21 +416,26 @@ def test_summarize_different_conversation(chat_client, default_user2: KhojUser):
json={"filename": summarization_file, "conversation_id": str(conversation1.id)}, json={"filename": summarization_file, "conversation_id": str(conversation1.id)},
) )
query = urllib.parse.quote("/summarize") # Act
response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation2.id}") query = "/summarize"
response_message = response.json()["response"] response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation2.id})
response_message_conv2 = response.json()["response"]
# Assert
assert response_message == "No files selected for summarization. Please add files using the section on the left."
# now make sure that the file filter is still in conversation 1 # now make sure that the file filter is still in conversation 1
response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation1.id}") response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation1.id})
response_message = response.json()["response"] response_message_conv1 = response.json()["response"]
# Assert # Assert
assert response_message != "" assert (
assert response_message != "No files selected for summarization. Please add files using the section on the left." response_message_conv2 == "No files selected for summarization. Please add files using the section on the left."
assert response_message != "Only one file can be selected for summarization." )
# now check the file filter is still in conversation 1
assert response_message_conv1 != ""
assert (
response_message_conv1 != "No files selected for summarization. Please add files using the section on the left."
)
assert response_message_conv1 != "Only one file can be selected for summarization."
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
@ -441,7 +449,7 @@ def test_summarize_nonexistant_file(chat_client, default_user2: KhojUser):
json={"filename": "imaginary.markdown", "conversation_id": str(conversation.id)}, json={"filename": "imaginary.markdown", "conversation_id": str(conversation.id)},
) )
query = urllib.parse.quote("/summarize") query = urllib.parse.quote("/summarize")
response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}") response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
assert response_message == "No files selected for summarization. Please add files using the section on the left." assert response_message == "No files selected for summarization. Please add files using the section on the left."
@ -450,6 +458,7 @@ def test_summarize_nonexistant_file(chat_client, default_user2: KhojUser):
@pytest.mark.django_db(transaction=True) @pytest.mark.django_db(transaction=True)
@pytest.mark.chatquality @pytest.mark.chatquality
def test_summarize_diff_user_file(chat_client, default_user: KhojUser, pdf_configured_user1, default_user2: KhojUser): def test_summarize_diff_user_file(chat_client, default_user: KhojUser, pdf_configured_user1, default_user2: KhojUser):
# Arrange
message_list = [] message_list = []
conversation = create_conversation(message_list, default_user2) conversation = create_conversation(message_list, default_user2)
# Get the pdf file called singlepage.pdf # Get the pdf file called singlepage.pdf
@ -469,9 +478,12 @@ def test_summarize_diff_user_file(chat_client, default_user: KhojUser, pdf_confi
"api/chat/conversation/file-filters", "api/chat/conversation/file-filters",
json={"filename": summarization_file, "conversation_id": str(conversation.id)}, json={"filename": summarization_file, "conversation_id": str(conversation.id)},
) )
query = urllib.parse.quote("/summarize")
response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}") # Act
query = "/summarize"
response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
assert response_message == "No files selected for summarization. Please add files using the section on the left." assert response_message == "No files selected for summarization. Please add files using the section on the left."
@ -484,7 +496,7 @@ def test_summarize_diff_user_file(chat_client, default_user: KhojUser, pdf_confi
def test_answer_requires_current_date_awareness(chat_client): def test_answer_requires_current_date_awareness(chat_client):
"Chat actor should be able to answer questions relative to current date using provided notes" "Chat actor should be able to answer questions relative to current date using provided notes"
# Act # Act
response = chat_client.post(f'/api/chat?q="Where did I have lunch today?"&stream=true') response = chat_client.post(f"/api/chat", json={"q": "Where did I have lunch today?", "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -502,7 +514,8 @@ def test_answer_requires_current_date_awareness(chat_client):
def test_answer_requires_date_aware_aggregation_across_provided_notes(chat_client): def test_answer_requires_date_aware_aggregation_across_provided_notes(chat_client):
"Chat director should be able to answer questions that require date aware aggregation across multiple notes" "Chat director should be able to answer questions that require date aware aggregation across multiple notes"
# Act # Act
response = chat_client.post(f'/api/chat?q="How much did I spend on dining this year?"') query = "How much did I spend on dining this year?"
response = chat_client.post(f"/api/chat", json={"q": query})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -523,7 +536,8 @@ def test_answer_general_question_not_in_chat_history_or_retrieved_content(chat_c
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = chat_client.post(f'/api/chat?q="Write a haiku about unit testing. Do not say anything else.') query = "Write a haiku about unit testing. Do not say anything else."
response = chat_client.post(f"/api/chat", json={"q": query})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -540,7 +554,8 @@ def test_answer_general_question_not_in_chat_history_or_retrieved_content(chat_c
@pytest.mark.chatquality @pytest.mark.chatquality
def test_ask_for_clarification_if_not_enough_context_in_question(chat_client_no_background): def test_ask_for_clarification_if_not_enough_context_in_question(chat_client_no_background):
# Act # Act
response = chat_client_no_background.post(f'/api/chat?q="What is the name of Namitas older son?"') query = "What is the name of Namitas older son?"
response = chat_client_no_background.post(f"/api/chat", json={"q": query})
response_message = response.json()["response"].lower() response_message = response.json()["response"].lower()
# Assert # Assert
@ -574,7 +589,8 @@ def test_answer_in_chat_history_beyond_lookback_window(chat_client, default_user
create_conversation(message_list, default_user2) create_conversation(message_list, default_user2)
# Act # Act
response = chat_client.post(f'/api/chat?q="What is my name?"') query = "What is my name?"
response = chat_client.post(f"/api/chat", json={"q": query})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert # Assert
@ -606,8 +622,8 @@ def test_answer_in_chat_history_by_conversation_id(chat_client, default_user2: K
create_conversation(message_list2, default_user2) create_conversation(message_list2, default_user2)
# Act # Act
query = urllib.parse.quote("/general What is my favorite color?") query = "/general What is my favorite color?"
response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}&stream=true") response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id, "stream": True})
response_message = response.content.decode("utf-8") response_message = response.content.decode("utf-8")
# Assert # Assert
@ -639,8 +655,8 @@ def test_answer_in_chat_history_by_conversation_id_with_agent(
conversation = create_conversation(message_list, default_user2, openai_agent) conversation = create_conversation(message_list, default_user2, openai_agent)
# Act # Act
query = urllib.parse.quote("/general What did I buy for breakfast?") query = "/general What did I buy for breakfast?"
response = chat_client.post(f"/api/chat?q={query}&conversation_id={conversation.id}") response = chat_client.post(f"/api/chat", json={"q": query, "conversation_id": conversation.id})
response_message = response.json()["response"] response_message = response.json()["response"]
# Assert that agent only responds with the summary of spending # Assert that agent only responds with the summary of spending
@ -657,7 +673,8 @@ def test_answer_in_chat_history_by_conversation_id_with_agent(
def test_answer_requires_multiple_independent_searches(chat_client): def test_answer_requires_multiple_independent_searches(chat_client):
"Chat director should be able to answer by doing multiple independent searches for required information" "Chat director should be able to answer by doing multiple independent searches for required information"
# Act # Act
response = chat_client.post(f'/api/chat?q="Is Xi Li older than Namita? Just say the older persons full name"') query = "Is Xi Li older than Namita? Just say the older persons full name"
response = chat_client.post(f"/api/chat", json={"q": query})
response_message = response.json()["response"].lower() response_message = response.json()["response"].lower()
# Assert # Assert
@ -678,11 +695,10 @@ def test_answer_requires_multiple_independent_searches(chat_client):
def test_answer_using_file_filter(chat_client): def test_answer_using_file_filter(chat_client):
"Chat should be able to use search filters in the query" "Chat should be able to use search filters in the query"
# Act # Act
query = urllib.parse.quote( query = (
'Is Xi Li older than Namita? Just say the older persons full name. file:"Namita.markdown" file:"Xi Li.markdown"' 'Is Xi Li older than Namita? Just say the older persons full name. file:"Namita.markdown" file:"Xi Li.markdown"'
) )
response = chat_client.post(f"/api/chat", json={"q": query})
response = chat_client.post(f"/api/chat?q={query}")
response_message = response.json()["response"].lower() response_message = response.json()["response"].lower()
# Assert # Assert