2023-03-15 21:52:59 +01:00
|
|
|
import os
|
2023-08-27 03:11:18 +02:00
|
|
|
import urllib.parse
|
2023-11-22 08:08:36 +01:00
|
|
|
from urllib.parse import quote
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
from freezegun import freeze_time
|
|
|
|
|
2023-12-28 13:34:02 +01:00
|
|
|
from khoj.database.models import KhojUser
|
|
|
|
from khoj.processor.conversation import prompts
|
2023-03-15 21:52:59 +01:00
|
|
|
from khoj.processor.conversation.utils import message_to_log
|
2024-03-06 09:18:41 +01:00
|
|
|
from khoj.routers.helpers import (
|
|
|
|
aget_relevant_information_sources,
|
|
|
|
aget_relevant_output_modes,
|
|
|
|
)
|
2023-10-26 20:37:41 +02:00
|
|
|
from tests.helpers import ConversationFactory
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Initialize variables for tests
|
2023-03-16 19:20:33 +01:00
|
|
|
api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
if api_key is None:
|
|
|
|
pytest.skip(
|
|
|
|
reason="Set OPENAI_API_KEY environment variable to run tests below. Get OpenAI API key from https://platform.openai.com/account/api-keys",
|
|
|
|
allow_module_level=True,
|
|
|
|
)
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Helpers
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
Miscellaneous bugs and fixes for chat sessions (#646)
* Display given_name field only if it is not None
* Add default slugs in the migration script
* Ensure that updated_at is saved appropriately, make sure most recent chat is returned for default history
* Remove the bin button from the chat interface, given deletion is handled in the drop-down menus
* Refresh the side panel when a new chat is created
* Improveme tool retrieval prompt, don't let /online fail, and improve parsing of extract questions
* Fix ending chat response by offline chat on hitting a stop phrase
Previously the whole phrase wouldn't be in the same response chunk, so
chat response wouldn't stop on hitting a stop phrase
Now use a queue to keep track of last 3 chunks, and to stop responding
when hit a stop phrase
* Make chat on Obsidian backward compatible post chat session API updates
- Make chat on Obsidian get chat history from
`responseJson.response.chat' when available (i.e when using new api)
- Else fallback to loading chat history from
responseJson.response (i.e when using old api)
* Fix detecting success of indexing update in khoj.el
When khoj.el attempts to index on a Khoj server served behind an https
endpoint, the success reponse status contains plist with certs. This
doesn't mean the update failed.
Look for :errors key in status instead to determine if indexing API
call failed. This fixes detecting indexing API call success on the
Khoj Emacs client, even for Khoj servers running behind SSL/HTTPS
* Fix the mechanism for populating notes references in the conversation primer for both offline and online chat
* Return conversation.default when empty list for dynamic prompt selection, send all cmds in telemetry
* Fix making chat on Obsidian backward compatible post chat session API updates
New API always has conversation_id set, not `chat' which can be unset
when chat session is empty.
So use conversation_id to decide whether to get chat logs from
`responseJson.response.chat' or `responseJson.response' instead
---------
Co-authored-by: Debanjum Singh Solanky <debanjum@gmail.com>
2024-02-20 22:55:35 +01:00
|
|
|
def generate_history(message_list):
|
2023-03-15 21:52:59 +01:00
|
|
|
# Generate conversation logs
|
|
|
|
conversation_log = {"chat": []}
|
|
|
|
for user_message, gpt_message, context in message_list:
|
2023-03-16 21:49:35 +01:00
|
|
|
conversation_log["chat"] += message_to_log(
|
|
|
|
user_message,
|
|
|
|
gpt_message,
|
|
|
|
{"context": context, "intent": {"query": user_message, "inferred-queries": f'["{user_message}"]'}},
|
|
|
|
)
|
Miscellaneous bugs and fixes for chat sessions (#646)
* Display given_name field only if it is not None
* Add default slugs in the migration script
* Ensure that updated_at is saved appropriately, make sure most recent chat is returned for default history
* Remove the bin button from the chat interface, given deletion is handled in the drop-down menus
* Refresh the side panel when a new chat is created
* Improveme tool retrieval prompt, don't let /online fail, and improve parsing of extract questions
* Fix ending chat response by offline chat on hitting a stop phrase
Previously the whole phrase wouldn't be in the same response chunk, so
chat response wouldn't stop on hitting a stop phrase
Now use a queue to keep track of last 3 chunks, and to stop responding
when hit a stop phrase
* Make chat on Obsidian backward compatible post chat session API updates
- Make chat on Obsidian get chat history from
`responseJson.response.chat' when available (i.e when using new api)
- Else fallback to loading chat history from
responseJson.response (i.e when using old api)
* Fix detecting success of indexing update in khoj.el
When khoj.el attempts to index on a Khoj server served behind an https
endpoint, the success reponse status contains plist with certs. This
doesn't mean the update failed.
Look for :errors key in status instead to determine if indexing API
call failed. This fixes detecting indexing API call success on the
Khoj Emacs client, even for Khoj servers running behind SSL/HTTPS
* Fix the mechanism for populating notes references in the conversation primer for both offline and online chat
* Return conversation.default when empty list for dynamic prompt selection, send all cmds in telemetry
* Fix making chat on Obsidian backward compatible post chat session API updates
New API always has conversation_id set, not `chat' which can be unset
when chat session is empty.
So use conversation_id to decide whether to get chat logs from
`responseJson.response.chat' or `responseJson.response' instead
---------
Co-authored-by: Debanjum Singh Solanky <debanjum@gmail.com>
2024-02-20 22:55:35 +01:00
|
|
|
return conversation_log
|
|
|
|
|
|
|
|
|
|
|
|
def populate_chat_history(message_list, user):
|
|
|
|
# Generate conversation logs
|
|
|
|
conversation_log = generate_history(message_list)
|
2023-03-15 21:52:59 +01:00
|
|
|
|
2023-10-26 20:37:41 +02:00
|
|
|
# Update Conversation Metadata Logs in Database
|
|
|
|
ConversationFactory(user=user, conversation_log=conversation_log)
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Tests
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.chatquality
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-15 21:52:59 +01:00
|
|
|
def test_chat_with_no_chat_history_or_retrieved_content(chat_client):
|
|
|
|
# Act
|
2023-07-09 19:12:09 +02:00
|
|
|
response = chat_client.get(f'/api/chat?q="Hello, my name is Testatron. Who are you?"&stream=true')
|
2023-07-08 00:23:44 +02:00
|
|
|
response_message = response.content.decode("utf-8")
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
expected_responses = ["Khoj", "khoj"]
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert any([expected_response in response_message for expected_response in expected_responses]), (
|
2023-03-24 22:37:55 +01:00
|
|
|
"Expected assistants name, [K|k]hoj, in response but got: " + response_message
|
2023-03-15 21:52:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-11-22 08:08:36 +01:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2024-01-16 20:06:03 +01:00
|
|
|
@pytest.mark.skipif(os.getenv("SERPER_DEV_API_KEY") is None, reason="requires SERPER_DEV_API_KEY")
|
2023-11-22 08:08:36 +01:00
|
|
|
@pytest.mark.chatquality
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_chat_with_online_content(chat_client):
|
|
|
|
# Act
|
|
|
|
q = "/online give me the link to paul graham's essay how to do great work"
|
|
|
|
encoded_q = quote(q, safe="")
|
|
|
|
response = chat_client.get(f"/api/chat?q={encoded_q}&stream=true")
|
|
|
|
response_message = response.content.decode("utf-8")
|
|
|
|
|
|
|
|
response_message = response_message.split("### compiled references")[0]
|
|
|
|
|
|
|
|
# Assert
|
2024-01-16 20:06:03 +01:00
|
|
|
expected_responses = ["http://www.paulgraham.com/greatwork.html"]
|
2023-11-22 08:08:36 +01:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert any([expected_response in response_message for expected_response in expected_responses]), (
|
2023-12-04 01:13:28 +01:00
|
|
|
"Expected links or serper not setup in response but got: " + response_message
|
2023-11-22 08:08:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-29 09:46:50 +01:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
os.getenv("SERPER_DEV_API_KEY") is None or os.getenv("OLOSTEP_API_KEY") is None,
|
|
|
|
reason="requires SERPER_DEV_API_KEY and OLOSTEP_API_KEY",
|
|
|
|
)
|
|
|
|
@pytest.mark.chatquality
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_chat_with_online_webpage_content(chat_client):
|
|
|
|
# Act
|
|
|
|
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.get(f"/api/chat?q={encoded_q}&stream=true")
|
|
|
|
response_message = response.content.decode("utf-8")
|
|
|
|
|
|
|
|
response_message = response_message.split("### compiled references")[0]
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
expected_responses = ["185", "1871", "horse"]
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert any([expected_response in response_message for expected_response in expected_responses]), (
|
|
|
|
"Expected links or serper not setup in response but got: " + response_message
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-03-15 21:52:59 +01:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-15 21:52:59 +01:00
|
|
|
@pytest.mark.chatquality
|
2023-10-26 20:37:41 +02:00
|
|
|
def test_answer_from_chat_history(chat_client, default_user2: KhojUser):
|
2023-03-15 21:52:59 +01:00
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 15:55:22 +01:00
|
|
|
("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []),
|
|
|
|
("When was I born?", "You were born on 1st April 1984.", []),
|
2023-03-15 21:52:59 +01:00
|
|
|
]
|
2023-10-26 20:37:41 +02:00
|
|
|
populate_chat_history(message_list, default_user2)
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Act
|
2023-07-09 19:12:09 +02:00
|
|
|
response = chat_client.get(f'/api/chat?q="What is my name?"&stream=true')
|
2023-07-08 00:23:44 +02:00
|
|
|
response_message = response.content.decode("utf-8")
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
expected_responses = ["Testatron", "testatron"]
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert any([expected_response in response_message for expected_response in expected_responses]), (
|
2023-03-24 22:37:55 +01:00
|
|
|
"Expected [T|t]estatron in response but got: " + response_message
|
2023-03-15 21:52:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-15 21:52:59 +01:00
|
|
|
@pytest.mark.chatquality
|
2023-10-26 20:37:41 +02:00
|
|
|
def test_answer_from_currently_retrieved_content(chat_client, default_user2: KhojUser):
|
2023-03-15 21:52:59 +01:00
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 15:55:22 +01:00
|
|
|
("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []),
|
|
|
|
(
|
|
|
|
"When was I born?",
|
|
|
|
"You were born on 1st April 1984.",
|
|
|
|
["Testatron was born on 1st April 1984 in Testville."],
|
|
|
|
),
|
2023-03-15 21:52:59 +01:00
|
|
|
]
|
2023-10-26 20:37:41 +02:00
|
|
|
populate_chat_history(message_list, default_user2)
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Act
|
|
|
|
response = chat_client.get(f'/api/chat?q="Where was Xi Li born?"')
|
2023-07-08 00:23:44 +02:00
|
|
|
response_message = response.content.decode("utf-8")
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert "Fujiang" in response_message
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-15 21:52:59 +01:00
|
|
|
@pytest.mark.chatquality
|
2023-10-26 20:37:41 +02:00
|
|
|
def test_answer_from_chat_history_and_previously_retrieved_content(chat_client_no_background, default_user2: KhojUser):
|
2023-03-15 21:52:59 +01:00
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 15:55:22 +01:00
|
|
|
("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []),
|
|
|
|
(
|
|
|
|
"When was I born?",
|
|
|
|
"You were born on 1st April 1984.",
|
|
|
|
["Testatron was born on 1st April 1984 in Testville."],
|
|
|
|
),
|
2023-03-15 21:52:59 +01:00
|
|
|
]
|
2023-10-26 20:37:41 +02:00
|
|
|
populate_chat_history(message_list, default_user2)
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Act
|
2023-10-26 20:37:41 +02:00
|
|
|
response = chat_client_no_background.get(f'/api/chat?q="Where was I born?"')
|
2023-07-08 00:23:44 +02:00
|
|
|
response_message = response.content.decode("utf-8")
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
# 1. Infer who I am from chat history
|
|
|
|
# 2. Infer I was born in Testville from previously retrieved notes
|
|
|
|
assert "Testville" in response_message
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.xfail(AssertionError, reason="Chat director not capable of answering this question yet")
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-15 21:52:59 +01:00
|
|
|
@pytest.mark.chatquality
|
2023-10-26 20:37:41 +02:00
|
|
|
def test_answer_from_chat_history_and_currently_retrieved_content(chat_client, default_user2: KhojUser):
|
2023-03-15 21:52:59 +01:00
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 15:55:22 +01:00
|
|
|
("Hello, my name is Xi Li. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []),
|
|
|
|
("When was I born?", "You were born on 1st April 1984.", []),
|
2023-03-15 21:52:59 +01:00
|
|
|
]
|
2023-10-26 20:37:41 +02:00
|
|
|
populate_chat_history(message_list, default_user2)
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Act
|
|
|
|
response = chat_client.get(f'/api/chat?q="Where was I born?"')
|
2023-07-08 00:23:44 +02:00
|
|
|
response_message = response.content.decode("utf-8")
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
# Inference in a multi-turn conversation
|
|
|
|
# 1. Infer who I am from chat history
|
|
|
|
# 2. Search for notes about when <my_name_from_chat_history> was born
|
|
|
|
# 3. Extract where I was born from currently retrieved notes
|
|
|
|
assert "Fujiang" in response_message
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-15 21:52:59 +01:00
|
|
|
@pytest.mark.chatquality
|
2023-10-26 20:37:41 +02:00
|
|
|
def test_no_answer_in_chat_history_or_retrieved_content(chat_client, default_user2: KhojUser):
|
2023-03-15 21:52:59 +01:00
|
|
|
"Chat director should say don't know as not enough contexts in chat history or retrieved to answer question"
|
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 15:55:22 +01:00
|
|
|
("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []),
|
|
|
|
("When was I born?", "You were born on 1st April 1984.", []),
|
2023-03-15 21:52:59 +01:00
|
|
|
]
|
2023-10-26 20:37:41 +02:00
|
|
|
populate_chat_history(message_list, default_user2)
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Act
|
2023-07-09 19:12:09 +02:00
|
|
|
response = chat_client.get(f'/api/chat?q="Where was I born?"&stream=true')
|
2023-07-08 00:23:44 +02:00
|
|
|
response_message = response.content.decode("utf-8")
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Assert
|
2024-03-13 14:16:26 +01:00
|
|
|
expected_responses = [
|
|
|
|
"don't know",
|
|
|
|
"do not know",
|
|
|
|
"no information",
|
|
|
|
"do not have",
|
|
|
|
"don't have",
|
|
|
|
"where were you born?",
|
|
|
|
]
|
|
|
|
|
2023-03-15 21:52:59 +01:00
|
|
|
assert response.status_code == 200
|
2024-03-13 14:16:26 +01:00
|
|
|
assert any([expected_response in response_message.lower() for expected_response in expected_responses]), (
|
2023-07-08 00:23:44 +02:00
|
|
|
"Expected chat director to say they don't know in response, but got: " + response_message
|
2023-03-15 21:52:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-27 03:11:18 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-08-27 03:11:18 +02:00
|
|
|
@pytest.mark.chatquality
|
2023-10-26 20:37:41 +02:00
|
|
|
def test_answer_using_general_command(chat_client, default_user2: KhojUser):
|
2023-08-27 03:11:18 +02:00
|
|
|
# Arrange
|
|
|
|
query = urllib.parse.quote("/general Where was Xi Li born?")
|
|
|
|
message_list = []
|
2023-10-26 20:37:41 +02:00
|
|
|
populate_chat_history(message_list, default_user2)
|
2023-08-27 03:11:18 +02:00
|
|
|
|
|
|
|
# Act
|
|
|
|
response = chat_client.get(f"/api/chat?q={query}&stream=true")
|
|
|
|
response_message = response.content.decode("utf-8")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert "Fujiang" not in response_message
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-08-27 03:11:18 +02:00
|
|
|
@pytest.mark.chatquality
|
2023-10-26 20:37:41 +02:00
|
|
|
def test_answer_from_retrieved_content_using_notes_command(chat_client, default_user2: KhojUser):
|
2023-08-27 03:11:18 +02:00
|
|
|
# Arrange
|
|
|
|
query = urllib.parse.quote("/notes Where was Xi Li born?")
|
|
|
|
message_list = []
|
2023-10-26 20:37:41 +02:00
|
|
|
populate_chat_history(message_list, default_user2)
|
2023-08-27 03:11:18 +02:00
|
|
|
|
|
|
|
# Act
|
|
|
|
response = chat_client.get(f"/api/chat?q={query}&stream=true")
|
|
|
|
response_message = response.content.decode("utf-8")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert "Fujiang" in response_message
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-08-27 03:11:18 +02:00
|
|
|
@pytest.mark.chatquality
|
2023-10-26 20:37:41 +02:00
|
|
|
def test_answer_not_known_using_notes_command(chat_client_no_background, default_user2: KhojUser):
|
2023-08-27 03:11:18 +02:00
|
|
|
# Arrange
|
|
|
|
query = urllib.parse.quote("/notes Where was Testatron born?")
|
|
|
|
message_list = []
|
2023-10-26 20:37:41 +02:00
|
|
|
populate_chat_history(message_list, default_user2)
|
2023-08-27 03:11:18 +02:00
|
|
|
|
|
|
|
# Act
|
2023-10-26 20:37:41 +02:00
|
|
|
response = chat_client_no_background.get(f"/api/chat?q={query}&stream=true")
|
2023-08-27 03:11:18 +02:00
|
|
|
response_message = response.content.decode("utf-8")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2023-11-18 03:22:45 +01:00
|
|
|
assert response_message == prompts.no_entries_found.format()
|
2023-08-27 03:11:18 +02:00
|
|
|
|
|
|
|
|
2023-03-15 21:52:59 +01:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.xfail(AssertionError, reason="Chat director not capable of answering time aware questions yet")
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-15 21:52:59 +01:00
|
|
|
@pytest.mark.chatquality
|
2024-02-05 15:32:54 +01:00
|
|
|
@freeze_time("2023-04-01", ignore=["transformers"])
|
2023-03-15 21:52:59 +01:00
|
|
|
def test_answer_requires_current_date_awareness(chat_client):
|
|
|
|
"Chat actor should be able to answer questions relative to current date using provided notes"
|
|
|
|
# Act
|
2023-07-09 19:12:09 +02:00
|
|
|
response = chat_client.get(f'/api/chat?q="Where did I have lunch today?"&stream=true')
|
2023-07-08 00:23:44 +02:00
|
|
|
response_message = response.content.decode("utf-8")
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
expected_responses = ["Arak", "Medellin"]
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert any([expected_response in response_message for expected_response in expected_responses]), (
|
|
|
|
"Expected chat director to say Arak, Medellin, but got: " + response_message
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-15 21:52:59 +01:00
|
|
|
@pytest.mark.chatquality
|
2024-02-05 15:32:54 +01:00
|
|
|
@freeze_time("2023-04-01", ignore=["transformers"])
|
2023-03-15 21:52:59 +01:00
|
|
|
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"
|
|
|
|
# Act
|
2023-07-09 19:12:09 +02:00
|
|
|
response = chat_client.get(f'/api/chat?q="How much did I spend on dining this year?"&stream=true')
|
2023-07-08 00:23:44 +02:00
|
|
|
response_message = response.content.decode("utf-8")
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert "23" in response_message
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-15 21:52:59 +01:00
|
|
|
@pytest.mark.chatquality
|
2023-10-26 20:37:41 +02:00
|
|
|
def test_answer_general_question_not_in_chat_history_or_retrieved_content(chat_client, default_user2: KhojUser):
|
2023-03-15 21:52:59 +01:00
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 15:55:22 +01:00
|
|
|
("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []),
|
|
|
|
("When was I born?", "You were born on 1st April 1984.", []),
|
|
|
|
("Where was I born?", "You were born Testville.", []),
|
2023-03-15 21:52:59 +01:00
|
|
|
]
|
2023-10-26 20:37:41 +02:00
|
|
|
populate_chat_history(message_list, default_user2)
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Act
|
2024-03-13 14:16:26 +01:00
|
|
|
response = chat_client.get(f'/api/chat?q="Write a haiku about unit testing. Do not say anything else."&stream=true')
|
|
|
|
response_message = response.content.decode("utf-8").split("### compiled references")[0]
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
expected_responses = ["test", "Test"]
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert len(response_message.splitlines()) == 3 # haikus are 3 lines long
|
|
|
|
assert any([expected_response in response_message for expected_response in expected_responses]), (
|
|
|
|
"Expected [T|t]est in response, but got: " + response_message
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-15 21:52:59 +01:00
|
|
|
@pytest.mark.chatquality
|
2023-10-26 20:37:41 +02:00
|
|
|
def test_ask_for_clarification_if_not_enough_context_in_question(chat_client_no_background):
|
2023-03-15 21:52:59 +01:00
|
|
|
# Act
|
2023-10-26 20:37:41 +02:00
|
|
|
|
2024-03-13 14:16:26 +01:00
|
|
|
response = chat_client_no_background.get(f'/api/chat?q="What is the name of Namitas older son?"&stream=true')
|
|
|
|
response_message = response.content.decode("utf-8").split("### compiled references")[0].lower()
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
expected_responses = [
|
2023-08-28 00:22:33 +02:00
|
|
|
"which of them",
|
|
|
|
"which one is",
|
|
|
|
"which of namita's sons",
|
|
|
|
"the birth order",
|
2023-11-11 02:29:23 +01:00
|
|
|
"provide more context",
|
2023-11-11 07:45:00 +01:00
|
|
|
"provide me with more context",
|
2024-03-13 14:16:26 +01:00
|
|
|
"don't have that",
|
|
|
|
"haven't provided me",
|
2023-03-15 21:52:59 +01:00
|
|
|
]
|
|
|
|
assert response.status_code == 200
|
2024-03-13 14:16:26 +01:00
|
|
|
assert any([expected_response in response_message for expected_response in expected_responses]), (
|
2023-03-15 21:52:59 +01:00
|
|
|
"Expected chat director to ask for clarification in response, but got: " + response_message
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.xfail(reason="Chat director not capable of answering this question yet")
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-15 21:52:59 +01:00
|
|
|
@pytest.mark.chatquality
|
2023-10-26 20:37:41 +02:00
|
|
|
def test_answer_in_chat_history_beyond_lookback_window(chat_client, default_user2: KhojUser):
|
2023-03-15 21:52:59 +01:00
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 15:55:22 +01:00
|
|
|
("Hello, my name is Testatron. Who are you?", "Hi, I am Khoj, a personal assistant. How can I help?", []),
|
|
|
|
("When was I born?", "You were born on 1st April 1984.", []),
|
|
|
|
("Where was I born?", "You were born Testville.", []),
|
2023-03-15 21:52:59 +01:00
|
|
|
]
|
2023-10-26 20:37:41 +02:00
|
|
|
populate_chat_history(message_list, default_user2)
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Act
|
2023-07-09 19:12:09 +02:00
|
|
|
response = chat_client.get(f'/api/chat?q="What is my name?"&stream=true')
|
2023-07-08 00:23:44 +02:00
|
|
|
response_message = response.content.decode("utf-8")
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
expected_responses = ["Testatron", "testatron"]
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert any([expected_response in response_message.lower() for expected_response in expected_responses]), (
|
|
|
|
"Expected [T|t]estatron in response, but got: " + response_message
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-15 21:52:59 +01:00
|
|
|
@pytest.mark.chatquality
|
|
|
|
def test_answer_requires_multiple_independent_searches(chat_client):
|
|
|
|
"Chat director should be able to answer by doing multiple independent searches for required information"
|
|
|
|
# Act
|
2024-03-13 14:16:26 +01:00
|
|
|
response = chat_client.get(f'/api/chat?q="Is Xi older than Namita? Just the older persons full name"&stream=true')
|
|
|
|
response_message = response.content.decode("utf-8").split("### compiled references")[0].lower()
|
2023-03-15 21:52:59 +01:00
|
|
|
|
|
|
|
# Assert
|
2023-07-09 19:12:09 +02:00
|
|
|
expected_responses = ["he is older than namita", "xi is older than namita", "xi li is older than namita"]
|
2024-03-13 14:16:26 +01:00
|
|
|
only_full_name_check = "xi li" in response_message and "namita" not in response_message
|
|
|
|
comparative_statement_check = any(
|
|
|
|
[expected_response in response_message for expected_response in expected_responses]
|
|
|
|
)
|
|
|
|
|
2023-03-15 21:52:59 +01:00
|
|
|
assert response.status_code == 200
|
2024-03-13 14:16:26 +01:00
|
|
|
assert only_full_name_check or comparative_statement_check, (
|
2023-03-15 21:52:59 +01:00
|
|
|
"Expected Xi is older than Namita, but got: " + response_message
|
|
|
|
)
|
2023-08-28 09:14:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 20:37:41 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-08-28 09:14:40 +02:00
|
|
|
def test_answer_using_file_filter(chat_client):
|
|
|
|
"Chat should be able to use search filters in the query"
|
|
|
|
# Act
|
2024-03-13 14:16:26 +01:00
|
|
|
query = urllib.parse.quote(
|
|
|
|
'Is Xi older than Namita? Just say the older persons full name. file:"Namita.markdown" file:"Xi Li.markdown"'
|
|
|
|
)
|
2023-10-26 20:37:41 +02:00
|
|
|
|
2023-08-28 09:14:40 +02:00
|
|
|
response = chat_client.get(f"/api/chat?q={query}&stream=true")
|
2024-03-13 14:16:26 +01:00
|
|
|
response_message = response.content.decode("utf-8").split("### compiled references")[0].lower()
|
2023-08-28 09:14:40 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
expected_responses = ["he is older than namita", "xi is older than namita", "xi li is older than namita"]
|
2024-03-13 14:16:26 +01:00
|
|
|
only_full_name_check = "xi li" in response_message and "namita" not in response_message
|
|
|
|
comparative_statement_check = any(
|
|
|
|
[expected_response in response_message for expected_response in expected_responses]
|
|
|
|
)
|
|
|
|
|
2023-08-28 09:14:40 +02:00
|
|
|
assert response.status_code == 200
|
2024-03-13 14:16:26 +01:00
|
|
|
assert only_full_name_check or comparative_statement_check, (
|
2023-08-28 09:14:40 +02:00
|
|
|
"Expected Xi is older than Namita, but got: " + response_message
|
|
|
|
)
|
2024-02-11 12:41:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.anyio
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
async def test_get_correct_tools_online(chat_client):
|
|
|
|
# Arrange
|
|
|
|
user_query = "What's the weather in Patagonia this week?"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
tools = await aget_relevant_information_sources(user_query, {})
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
tools = [tool.value for tool in tools]
|
|
|
|
assert tools == ["online"]
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.anyio
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
async def test_get_correct_tools_notes(chat_client):
|
|
|
|
# Arrange
|
|
|
|
user_query = "Where did I go for my first battleship training?"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
tools = await aget_relevant_information_sources(user_query, {})
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
tools = [tool.value for tool in tools]
|
|
|
|
assert tools == ["notes"]
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.anyio
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
async def test_get_correct_tools_online_or_general_and_notes(chat_client):
|
|
|
|
# Arrange
|
|
|
|
user_query = "What's the highest point in Patagonia and have I been there?"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
tools = await aget_relevant_information_sources(user_query, {})
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
tools = [tool.value for tool in tools]
|
|
|
|
assert len(tools) == 2
|
|
|
|
assert "online" in tools or "general" in tools
|
|
|
|
assert "notes" in tools
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.anyio
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
async def test_get_correct_tools_general(chat_client):
|
|
|
|
# Arrange
|
|
|
|
user_query = "How many noble gases are there?"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
tools = await aget_relevant_information_sources(user_query, {})
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
tools = [tool.value for tool in tools]
|
|
|
|
assert tools == ["general"]
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.anyio
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
async def test_get_correct_tools_with_chat_history(chat_client):
|
|
|
|
# Arrange
|
|
|
|
user_query = "What's the latest in the Israel/Palestine conflict?"
|
|
|
|
chat_log = [
|
|
|
|
(
|
|
|
|
"Let's talk about the current events around the world.",
|
|
|
|
"Sure, let's discuss the current events. What would you like to know?",
|
Miscellaneous bugs and fixes for chat sessions (#646)
* Display given_name field only if it is not None
* Add default slugs in the migration script
* Ensure that updated_at is saved appropriately, make sure most recent chat is returned for default history
* Remove the bin button from the chat interface, given deletion is handled in the drop-down menus
* Refresh the side panel when a new chat is created
* Improveme tool retrieval prompt, don't let /online fail, and improve parsing of extract questions
* Fix ending chat response by offline chat on hitting a stop phrase
Previously the whole phrase wouldn't be in the same response chunk, so
chat response wouldn't stop on hitting a stop phrase
Now use a queue to keep track of last 3 chunks, and to stop responding
when hit a stop phrase
* Make chat on Obsidian backward compatible post chat session API updates
- Make chat on Obsidian get chat history from
`responseJson.response.chat' when available (i.e when using new api)
- Else fallback to loading chat history from
responseJson.response (i.e when using old api)
* Fix detecting success of indexing update in khoj.el
When khoj.el attempts to index on a Khoj server served behind an https
endpoint, the success reponse status contains plist with certs. This
doesn't mean the update failed.
Look for :errors key in status instead to determine if indexing API
call failed. This fixes detecting indexing API call success on the
Khoj Emacs client, even for Khoj servers running behind SSL/HTTPS
* Fix the mechanism for populating notes references in the conversation primer for both offline and online chat
* Return conversation.default when empty list for dynamic prompt selection, send all cmds in telemetry
* Fix making chat on Obsidian backward compatible post chat session API updates
New API always has conversation_id set, not `chat' which can be unset
when chat session is empty.
So use conversation_id to decide whether to get chat logs from
`responseJson.response.chat' or `responseJson.response' instead
---------
Co-authored-by: Debanjum Singh Solanky <debanjum@gmail.com>
2024-02-20 22:55:35 +01:00
|
|
|
[],
|
2024-02-11 12:41:32 +01:00
|
|
|
),
|
Miscellaneous bugs and fixes for chat sessions (#646)
* Display given_name field only if it is not None
* Add default slugs in the migration script
* Ensure that updated_at is saved appropriately, make sure most recent chat is returned for default history
* Remove the bin button from the chat interface, given deletion is handled in the drop-down menus
* Refresh the side panel when a new chat is created
* Improveme tool retrieval prompt, don't let /online fail, and improve parsing of extract questions
* Fix ending chat response by offline chat on hitting a stop phrase
Previously the whole phrase wouldn't be in the same response chunk, so
chat response wouldn't stop on hitting a stop phrase
Now use a queue to keep track of last 3 chunks, and to stop responding
when hit a stop phrase
* Make chat on Obsidian backward compatible post chat session API updates
- Make chat on Obsidian get chat history from
`responseJson.response.chat' when available (i.e when using new api)
- Else fallback to loading chat history from
responseJson.response (i.e when using old api)
* Fix detecting success of indexing update in khoj.el
When khoj.el attempts to index on a Khoj server served behind an https
endpoint, the success reponse status contains plist with certs. This
doesn't mean the update failed.
Look for :errors key in status instead to determine if indexing API
call failed. This fixes detecting indexing API call success on the
Khoj Emacs client, even for Khoj servers running behind SSL/HTTPS
* Fix the mechanism for populating notes references in the conversation primer for both offline and online chat
* Return conversation.default when empty list for dynamic prompt selection, send all cmds in telemetry
* Fix making chat on Obsidian backward compatible post chat session API updates
New API always has conversation_id set, not `chat' which can be unset
when chat session is empty.
So use conversation_id to decide whether to get chat logs from
`responseJson.response.chat' or `responseJson.response' instead
---------
Co-authored-by: Debanjum Singh Solanky <debanjum@gmail.com>
2024-02-20 22:55:35 +01:00
|
|
|
("What's up in New York City?", "A Pride parade has recently been held in New York City, on July 31st.", []),
|
2024-02-11 12:41:32 +01:00
|
|
|
]
|
Miscellaneous bugs and fixes for chat sessions (#646)
* Display given_name field only if it is not None
* Add default slugs in the migration script
* Ensure that updated_at is saved appropriately, make sure most recent chat is returned for default history
* Remove the bin button from the chat interface, given deletion is handled in the drop-down menus
* Refresh the side panel when a new chat is created
* Improveme tool retrieval prompt, don't let /online fail, and improve parsing of extract questions
* Fix ending chat response by offline chat on hitting a stop phrase
Previously the whole phrase wouldn't be in the same response chunk, so
chat response wouldn't stop on hitting a stop phrase
Now use a queue to keep track of last 3 chunks, and to stop responding
when hit a stop phrase
* Make chat on Obsidian backward compatible post chat session API updates
- Make chat on Obsidian get chat history from
`responseJson.response.chat' when available (i.e when using new api)
- Else fallback to loading chat history from
responseJson.response (i.e when using old api)
* Fix detecting success of indexing update in khoj.el
When khoj.el attempts to index on a Khoj server served behind an https
endpoint, the success reponse status contains plist with certs. This
doesn't mean the update failed.
Look for :errors key in status instead to determine if indexing API
call failed. This fixes detecting indexing API call success on the
Khoj Emacs client, even for Khoj servers running behind SSL/HTTPS
* Fix the mechanism for populating notes references in the conversation primer for both offline and online chat
* Return conversation.default when empty list for dynamic prompt selection, send all cmds in telemetry
* Fix making chat on Obsidian backward compatible post chat session API updates
New API always has conversation_id set, not `chat' which can be unset
when chat session is empty.
So use conversation_id to decide whether to get chat logs from
`responseJson.response.chat' or `responseJson.response' instead
---------
Co-authored-by: Debanjum Singh Solanky <debanjum@gmail.com>
2024-02-20 22:55:35 +01:00
|
|
|
chat_history = generate_history(chat_log)
|
2024-02-11 12:41:32 +01:00
|
|
|
|
|
|
|
# Act
|
|
|
|
tools = await aget_relevant_information_sources(user_query, chat_history)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
tools = [tool.value for tool in tools]
|
|
|
|
assert tools == ["online"]
|