2023-03-14 23:44:16 +00:00
|
|
|
import os
|
|
|
|
from datetime import datetime
|
|
|
|
|
2023-07-07 22:18:55 +00:00
|
|
|
import freezegun
|
2023-12-28 12:34:02 +00:00
|
|
|
import pytest
|
2023-03-16 20:49:35 +00:00
|
|
|
from freezegun import freeze_time
|
2021-11-26 11:53:03 +00:00
|
|
|
|
2023-07-26 23:27:08 +00:00
|
|
|
from khoj.processor.conversation.openai.gpt import converse, extract_questions
|
2023-03-16 18:20:33 +00:00
|
|
|
from khoj.processor.conversation.utils import message_to_log
|
2024-03-13 11:19:13 +00:00
|
|
|
from khoj.routers.helpers import (
|
|
|
|
aget_relevant_information_sources,
|
|
|
|
aget_relevant_output_modes,
|
|
|
|
generate_online_subqueries,
|
Part 2: Add web UI updates for basic agent interactions (#675)
* Initial pass at backend changes to support agents
- Add a db model for Agents, attaching them to conversations
- When an agent is added to a conversation, override the system prompt to tweak the instructions
- Agents can be configured with prompt modification, model specification, a profile picture, and other things
- Admin-configured models will not be editable by individual users
- Add unit tests to verify agent behavior. Unit tests demonstrate imperfect adherence to prompt specifications
* Customize default behaviors for conversations without agents or with default agents
* Add a new web client route for viewing all agents
* Use agent_id for getting correct agent
* Add web UI views for agents
- Add a page to view all agents
- Add slugs to manage agents
- Add a view to view single agent
- Display active agent when in chat window
- Fix post-login redirect issue
* Fix agent view
* Spruce up the 404 page and improve the overall layout for agents pages
* Create chat actor for directly reading webpages based on user message
- Add prompt for the read webpages chat actor to extract, infer
webpage links
- Make chat actor infer or extract webpage to read directly from user
message
- Rename previous read_webpage function to more narrow
read_webpage_at_url function
* Rename agents_page -> agent_page
* Fix unit test for adding the filename to the compiled markdown entry
* Fix layout of agent, agents pages
* Merge migrations
* Let the name, slug of the default agent be Khoj, khoj
* Fix chat-related unit tests
* Add webpage chat command for read web pages requested by user
Update auto chat command inference prompt to show example of when to
use webpage chat command (i.e when url is directly provided in link)
* Support webpage command in chat API
- Fallback to use webpage when SERPER not setup and online command was
attempted
- Do not stop responding if can't retrieve online results. Try to
respond without the online context
* Test select webpage as data source and extract web urls chat actors
* Tweak prompts to extract information from webpages, online results
- Show more of the truncated messages for debugging context
- Update Khoj personality prompt to encourage it to remember it's capabilities
* Rename extract_content online results field to webpages
* Parallelize simple webpage read and extractor
Similar to what is being done with search_online with olostep
* Pass multiple webpages with their urls in online results context
Previously even if MAX_WEBPAGES_TO_READ was > 1, only 1 extracted
content would ever be passed.
URL of the extracted webpage content wasn't passed to clients in
online results context. This limited them from being rendered
* Render webpage read in chat response references on Web, Desktop apps
* Time chat actor responses & chat api request start for perf analysis
* Increase the keep alive timeout in the main application for testing
* Do not pipe access/error logs to separate files. Flow to stdout/stderr
* [Temp] Reduce to 1 gunicorn worker
* Change prod docker image to use jammy, rather than nvidia base image
* Use Khoj icon when Khoj web is installed on iOS as a PWA
* Make slug required for agents
* Simplify calling logic and prevent agent access for unauthenticated users
* Standardize to use personality over tuning in agent nomenclature
* Make filtering logic more stringent for accessible agents and remove unused method:
* Format chat message query
---------
Co-authored-by: Debanjum Singh Solanky <debanjum@gmail.com>
2024-03-26 12:43:24 +00:00
|
|
|
infer_webpage_urls,
|
2024-04-21 08:13:46 +00:00
|
|
|
schedule_query,
|
2024-04-21 14:36:42 +00:00
|
|
|
should_notify,
|
2024-03-13 11:19:13 +00:00
|
|
|
)
|
|
|
|
from khoj.utils.helpers import ConversationCommand
|
2024-04-21 08:13:46 +00:00
|
|
|
from khoj.utils.rawconfig import LocationData
|
2024-07-26 18:05:50 +00:00
|
|
|
from tests.conftest import default_user2
|
2021-11-26 11:53:03 +00:00
|
|
|
|
2023-01-09 03:09:24 +00:00
|
|
|
# Initialize variables for tests
|
2023-03-16 18:20:33 +00: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-02-17 16:04:26 +00:00
|
|
|
)
|
2021-11-26 11:53:03 +00:00
|
|
|
|
2023-07-07 22:18:55 +00:00
|
|
|
freezegun.configure(extend_ignore_list=["transformers"])
|
|
|
|
|
2021-11-26 11:53:03 +00:00
|
|
|
|
2023-03-16 18:20:33 +00:00
|
|
|
# Test
|
2023-03-16 20:49:35 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.chatquality
|
2024-02-05 14:32:54 +00:00
|
|
|
@freeze_time("1984-04-02", ignore=["transformers"])
|
2023-03-16 20:49:35 +00:00
|
|
|
def test_extract_question_with_date_filter_from_relative_day():
|
|
|
|
# Act
|
|
|
|
response = extract_questions("Where did I go for dinner yesterday?")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
expected_responses = [
|
2023-07-08 00:13:47 +00:00
|
|
|
("dt='1984-04-01'", ""),
|
|
|
|
("dt>='1984-04-01'", "dt<'1984-04-02'"),
|
|
|
|
("dt>'1984-03-31'", "dt<'1984-04-02'"),
|
2023-03-16 20:49:35 +00:00
|
|
|
]
|
|
|
|
assert len(response) == 1
|
|
|
|
assert any([start in response[0] and end in response[0] for start, end in expected_responses]), (
|
2023-03-17 00:14:41 +00:00
|
|
|
"Expected date filter to limit to 1st April 1984 in response but got: " + response[0]
|
2023-03-16 20:49:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.chatquality
|
2024-02-05 14:32:54 +00:00
|
|
|
@freeze_time("1984-04-02", ignore=["transformers"])
|
2023-03-16 20:49:35 +00:00
|
|
|
def test_extract_question_with_date_filter_from_relative_month():
|
|
|
|
# Act
|
|
|
|
response = extract_questions("Which countries did I visit last month?")
|
|
|
|
|
|
|
|
# Assert
|
2023-07-08 00:13:47 +00:00
|
|
|
expected_responses = [("dt>='1984-03-01'", "dt<'1984-04-01'"), ("dt>='1984-03-01'", "dt<='1984-03-31'")]
|
2023-03-16 20:49:35 +00:00
|
|
|
assert len(response) == 1
|
|
|
|
assert any([start in response[0] and end in response[0] for start, end in expected_responses]), (
|
2023-03-17 00:14:41 +00:00
|
|
|
"Expected date filter to limit to March 1984 in response but got: " + response[0]
|
2023-03-16 20:49:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.chatquality
|
2024-02-05 14:32:54 +00:00
|
|
|
@freeze_time("1984-04-02", ignore=["transformers"])
|
2023-03-16 20:49:35 +00:00
|
|
|
def test_extract_question_with_date_filter_from_relative_year():
|
|
|
|
# Act
|
2023-03-18 01:53:17 +00:00
|
|
|
response = extract_questions("Which countries have I visited this year?")
|
2023-03-16 20:49:35 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
expected_responses = [
|
2023-07-08 00:13:47 +00:00
|
|
|
("dt>='1984-01-01'", ""),
|
|
|
|
("dt>='1984-01-01'", "dt<'1985-01-01'"),
|
|
|
|
("dt>='1984-01-01'", "dt<='1984-12-31'"),
|
2023-03-16 20:49:35 +00:00
|
|
|
]
|
|
|
|
assert len(response) == 1
|
|
|
|
assert any([start in response[0] and end in response[0] for start, end in expected_responses]), (
|
2023-03-17 00:14:41 +00:00
|
|
|
"Expected date filter to limit to 1984 in response but got: " + response[0]
|
2023-03-16 20:49:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.chatquality
|
|
|
|
def test_extract_multiple_explicit_questions_from_message():
|
|
|
|
# Act
|
2024-09-11 21:04:14 +00:00
|
|
|
responses = extract_questions("What is the Sun? What is the Moon?")
|
2023-03-16 20:49:35 +00:00
|
|
|
|
|
|
|
# Assert
|
2024-09-11 21:04:14 +00:00
|
|
|
assert len(responses) >= 2
|
|
|
|
assert any(["sun" in response.lower() or "moon" in response.lower() for response in responses]), (
|
|
|
|
"Expected sun or moon mentioned in generated search queries but got: " + responses
|
2023-03-16 20:49:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.chatquality
|
|
|
|
def test_extract_multiple_implicit_questions_from_message():
|
|
|
|
# Act
|
|
|
|
response = extract_questions("Is Morpheus taller than Neo?")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
expected_responses = [
|
|
|
|
("morpheus", "neo"),
|
|
|
|
]
|
|
|
|
assert len(response) == 2
|
|
|
|
assert any([start in response[0].lower() and end in response[1].lower() for start, end in expected_responses]), (
|
2023-03-17 00:14:41 +00:00
|
|
|
"Expected two search queries in response but got: " + response[0]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.chatquality
|
|
|
|
def test_generate_search_query_using_question_from_chat_history():
|
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 14:55:22 +00:00
|
|
|
("What is the name of Mr. Vader's daughter?", "Princess Leia", []),
|
2023-03-17 00:14:41 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Act
|
2024-09-11 21:04:14 +00:00
|
|
|
responses = extract_questions("Does he have any sons?", conversation_log=populate_chat_history(message_list))
|
2023-03-17 00:14:41 +00:00
|
|
|
|
|
|
|
# Assert
|
2024-09-11 21:04:14 +00:00
|
|
|
assert all(["Vader" in response for response in responses])
|
2023-03-17 00:14:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.chatquality
|
|
|
|
def test_generate_search_query_using_answer_from_chat_history():
|
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 14:55:22 +00:00
|
|
|
("What is the name of Mr. Vader's daughter?", "Princess Leia", []),
|
2023-03-17 00:14:41 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Act
|
2024-09-11 21:04:14 +00:00
|
|
|
responses = extract_questions("Is she a Jedi?", conversation_log=populate_chat_history(message_list))
|
2023-03-17 00:14:41 +00:00
|
|
|
|
|
|
|
# Assert
|
2024-09-11 21:04:14 +00:00
|
|
|
assert all(["Leia" in response for response in responses])
|
2023-03-17 00:14:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.chatquality
|
|
|
|
def test_generate_search_query_using_question_and_answer_from_chat_history():
|
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 14:55:22 +00:00
|
|
|
("Does Luke Skywalker have any Siblings?", "Yes, Princess Leia", []),
|
2023-03-17 00:14:41 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = extract_questions("Who is their father?", conversation_log=populate_chat_history(message_list))
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(response) == 1
|
|
|
|
assert "Leia" in response[0] and "Luke" in response[0]
|
|
|
|
|
|
|
|
|
2021-11-26 11:53:03 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-15 00:56:14 +00:00
|
|
|
@pytest.mark.chatquality
|
2023-03-14 23:44:16 +00:00
|
|
|
def test_chat_with_no_chat_history_or_retrieved_content():
|
2021-11-26 11:53:03 +00:00
|
|
|
# Act
|
2023-07-07 22:23:44 +00:00
|
|
|
response_gen = converse(
|
2023-03-24 14:55:22 +00:00
|
|
|
references=[], # Assume no context retrieved from notes for the user_query
|
2023-03-14 17:27:58 +00:00
|
|
|
user_query="Hello, my name is Testatron. Who are you?",
|
|
|
|
api_key=api_key,
|
|
|
|
)
|
2023-07-07 22:23:44 +00:00
|
|
|
response = "".join([response_chunk for response_chunk in response_gen])
|
2021-11-26 11:53:03 +00:00
|
|
|
|
|
|
|
# Assert
|
2023-03-14 17:27:58 +00:00
|
|
|
expected_responses = ["Khoj", "khoj"]
|
2021-11-26 11:53:03 +00:00
|
|
|
assert len(response) > 0
|
2023-03-14 17:27:58 +00:00
|
|
|
assert any([expected_response in response for expected_response in expected_responses]), (
|
2023-03-17 00:14:41 +00:00
|
|
|
"Expected assistants name, [K|k]hoj, in response but got: " + response
|
2023-03-14 17:27:58 +00:00
|
|
|
)
|
2021-11-26 11:53:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-15 00:56:14 +00:00
|
|
|
@pytest.mark.chatquality
|
2023-03-14 23:44:16 +00:00
|
|
|
def test_answer_from_chat_history_and_no_content():
|
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 14:55:22 +00: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-14 23:44:16 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Act
|
2023-07-07 22:23:44 +00:00
|
|
|
response_gen = converse(
|
2023-03-24 14:55:22 +00:00
|
|
|
references=[], # Assume no context retrieved from notes for the user_query
|
2023-03-14 23:44:16 +00:00
|
|
|
user_query="What is my name?",
|
2023-03-16 20:49:35 +00:00
|
|
|
conversation_log=populate_chat_history(message_list),
|
2023-03-14 23:44:16 +00:00
|
|
|
api_key=api_key,
|
|
|
|
)
|
2023-07-07 22:23:44 +00:00
|
|
|
response = "".join([response_chunk for response_chunk in response_gen])
|
2023-03-14 23:44:16 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
expected_responses = ["Testatron", "testatron"]
|
|
|
|
assert len(response) > 0
|
|
|
|
assert any([expected_response in response for expected_response in expected_responses]), (
|
2023-03-17 00:14:41 +00:00
|
|
|
"Expected [T|t]estatron in response but got: " + response
|
2023-03-14 23:44:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-15 00:56:14 +00:00
|
|
|
@pytest.mark.chatquality
|
2023-03-14 23:44:16 +00:00
|
|
|
def test_answer_from_chat_history_and_previously_retrieved_content():
|
2023-03-15 20:18:08 +00:00
|
|
|
"Chat actor needs to use context in previous notes and chat history to answer question"
|
2023-03-14 23:44:16 +00:00
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 14:55:22 +00: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.",
|
2024-10-22 09:58:34 +00:00
|
|
|
[{"compiled": "Testatron was born on 1st April 1984 in Testville.", "file": "birth.org"}],
|
2023-03-24 14:55:22 +00:00
|
|
|
),
|
2023-03-14 23:44:16 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Act
|
2023-07-07 22:23:44 +00:00
|
|
|
response_gen = converse(
|
2023-03-24 14:55:22 +00:00
|
|
|
references=[], # Assume no context retrieved from notes for the user_query
|
2023-03-14 23:44:16 +00:00
|
|
|
user_query="Where was I born?",
|
2023-03-16 20:49:35 +00:00
|
|
|
conversation_log=populate_chat_history(message_list),
|
2023-03-14 23:44:16 +00:00
|
|
|
api_key=api_key,
|
|
|
|
)
|
2023-07-07 22:23:44 +00:00
|
|
|
response = "".join([response_chunk for response_chunk in response_gen])
|
2023-03-14 23:44:16 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(response) > 0
|
|
|
|
# Infer who I am and use that to infer I was born in Testville using chat history and previously retrieved notes
|
|
|
|
assert "Testville" in response
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-15 00:56:14 +00:00
|
|
|
@pytest.mark.chatquality
|
2023-03-14 23:44:16 +00:00
|
|
|
def test_answer_from_chat_history_and_currently_retrieved_content():
|
2023-03-15 20:18:08 +00:00
|
|
|
"Chat actor needs to use context across currently retrieved notes and chat history to answer question"
|
2023-03-14 23:44:16 +00:00
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 14:55:22 +00: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-14 23:44:16 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Act
|
2023-07-07 22:23:44 +00:00
|
|
|
response_gen = converse(
|
2023-03-24 14:55:22 +00:00
|
|
|
references=[
|
2024-06-09 01:46:55 +00:00
|
|
|
{"compiled": "Testatron was born on 1st April 1984 in Testville.", "file": "background.md"}
|
2023-03-24 14:55:22 +00:00
|
|
|
], # Assume context retrieved from notes for the user_query
|
2023-03-14 23:44:16 +00:00
|
|
|
user_query="Where was I born?",
|
2023-03-16 20:49:35 +00:00
|
|
|
conversation_log=populate_chat_history(message_list),
|
2023-03-14 23:44:16 +00:00
|
|
|
api_key=api_key,
|
2023-03-14 17:27:58 +00:00
|
|
|
)
|
2023-07-07 22:23:44 +00:00
|
|
|
response = "".join([response_chunk for response_chunk in response_gen])
|
2021-11-26 11:53:03 +00:00
|
|
|
|
2023-03-14 23:44:16 +00:00
|
|
|
# Assert
|
|
|
|
assert len(response) > 0
|
|
|
|
assert "Testville" in response
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-15 00:56:14 +00:00
|
|
|
@pytest.mark.chatquality
|
2023-05-12 10:35:14 +00:00
|
|
|
def test_refuse_answering_unanswerable_question():
|
|
|
|
"Chat actor should not try make up answers to unanswerable questions."
|
2023-03-14 23:44:16 +00:00
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 14:55:22 +00: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-14 23:44:16 +00:00
|
|
|
]
|
|
|
|
|
2022-01-12 15:36:01 +00:00
|
|
|
# Act
|
2023-07-07 22:23:44 +00:00
|
|
|
response_gen = converse(
|
2023-03-24 14:55:22 +00:00
|
|
|
references=[], # Assume no context retrieved from notes for the user_query
|
2023-03-14 23:44:16 +00:00
|
|
|
user_query="Where was I born?",
|
2023-03-16 20:49:35 +00:00
|
|
|
conversation_log=populate_chat_history(message_list),
|
2023-02-17 16:04:26 +00:00
|
|
|
api_key=api_key,
|
|
|
|
)
|
2023-07-07 22:23:44 +00:00
|
|
|
response = "".join([response_chunk for response_chunk in response_gen])
|
2021-11-26 11:53:03 +00:00
|
|
|
|
|
|
|
# Assert
|
2023-05-12 10:35:14 +00:00
|
|
|
expected_responses = [
|
|
|
|
"don't know",
|
|
|
|
"do not know",
|
|
|
|
"no information",
|
|
|
|
"do not have",
|
|
|
|
"don't have",
|
|
|
|
"cannot answer",
|
|
|
|
"I'm sorry",
|
|
|
|
]
|
2021-11-26 11:53:03 +00:00
|
|
|
assert len(response) > 0
|
2023-03-15 20:18:08 +00:00
|
|
|
assert any([expected_response in response for expected_response in expected_responses]), (
|
|
|
|
"Expected chat actor to say they don't know in response, but got: " + response
|
|
|
|
)
|
2023-03-14 23:44:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-15 00:56:14 +00:00
|
|
|
@pytest.mark.chatquality
|
2023-03-14 23:44:16 +00:00
|
|
|
def test_answer_requires_current_date_awareness():
|
2023-03-15 20:18:08 +00:00
|
|
|
"Chat actor should be able to answer questions relative to current date using provided notes"
|
2023-03-14 23:44:16 +00:00
|
|
|
# Arrange
|
2023-03-24 14:55:22 +00:00
|
|
|
context = [
|
2024-06-09 01:46:55 +00:00
|
|
|
{
|
|
|
|
"compiled": f"""{datetime.now().strftime("%Y-%m-%d")} "Naco Taco" "Tacos for Dinner"
|
2023-03-24 14:55:22 +00:00
|
|
|
Expenses:Food:Dining 10.00 USD""",
|
2024-06-09 01:46:55 +00:00
|
|
|
"file": "Ledger.org",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"compiled": f"""{datetime.now().strftime("%Y-%m-%d")} "Sagar Ratna" "Dosa for Lunch"
|
2023-03-24 14:55:22 +00:00
|
|
|
Expenses:Food:Dining 10.00 USD""",
|
2024-06-09 01:46:55 +00:00
|
|
|
"file": "Ledger.org",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"compiled": f"""2020-04-01 "SuperMercado" "Bananas"
|
2023-03-24 14:55:22 +00:00
|
|
|
Expenses:Food:Groceries 10.00 USD""",
|
2024-06-09 01:46:55 +00:00
|
|
|
"file": "Ledger.org",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"compiled": f"""2020-01-01 "Naco Taco" "Burittos for Dinner"
|
2023-03-24 14:55:22 +00:00
|
|
|
Expenses:Food:Dining 10.00 USD""",
|
2024-06-09 01:46:55 +00:00
|
|
|
"file": "Ledger.org",
|
|
|
|
},
|
2023-03-24 14:55:22 +00:00
|
|
|
]
|
2023-03-14 23:44:16 +00:00
|
|
|
|
|
|
|
# Act
|
2023-07-07 22:23:44 +00:00
|
|
|
response_gen = converse(
|
2023-03-24 14:55:22 +00:00
|
|
|
references=context, # Assume context retrieved from notes for the user_query
|
2023-03-14 23:44:16 +00:00
|
|
|
user_query="What did I have for Dinner today?",
|
|
|
|
api_key=api_key,
|
|
|
|
)
|
2023-07-07 22:23:44 +00:00
|
|
|
response = "".join([response_chunk for response_chunk in response_gen])
|
2023-03-14 23:44:16 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
expected_responses = ["tacos", "Tacos"]
|
|
|
|
assert len(response) > 0
|
|
|
|
assert any([expected_response in response for expected_response in expected_responses]), (
|
|
|
|
"Expected [T|t]acos in response, but got: " + response
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-15 00:56:14 +00:00
|
|
|
@pytest.mark.chatquality
|
2023-03-14 23:44:16 +00:00
|
|
|
def test_answer_requires_date_aware_aggregation_across_provided_notes():
|
2023-03-15 20:18:08 +00:00
|
|
|
"Chat actor should be able to answer questions that require date aware aggregation across multiple notes"
|
2023-03-14 23:44:16 +00:00
|
|
|
# Arrange
|
2023-03-24 14:55:22 +00:00
|
|
|
context = [
|
2024-06-09 01:46:55 +00:00
|
|
|
{
|
|
|
|
"compiled": f"""# {datetime.now().strftime("%Y-%m-%d")} "Naco Taco" "Tacos for Dinner"
|
2023-03-24 14:55:22 +00:00
|
|
|
Expenses:Food:Dining 10.00 USD""",
|
2024-06-09 01:46:55 +00:00
|
|
|
"file": "Ledger.md",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"compiled": f"""{datetime.now().strftime("%Y-%m-%d")} "Sagar Ratna" "Dosa for Lunch"
|
2023-03-24 14:55:22 +00:00
|
|
|
Expenses:Food:Dining 10.00 USD""",
|
2024-06-09 01:46:55 +00:00
|
|
|
"file": "Ledger.md",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"compiled": f"""2020-04-01 "SuperMercado" "Bananas"
|
2023-03-24 14:55:22 +00:00
|
|
|
Expenses:Food:Groceries 10.00 USD""",
|
2024-06-09 01:46:55 +00:00
|
|
|
"file": "Ledger.md",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"compiled": f"""2020-01-01 "Naco Taco" "Burittos for Dinner"
|
2023-03-24 14:55:22 +00:00
|
|
|
Expenses:Food:Dining 10.00 USD""",
|
2024-06-09 01:46:55 +00:00
|
|
|
"file": "Ledger.md",
|
|
|
|
},
|
2023-03-24 14:55:22 +00:00
|
|
|
]
|
2023-03-14 23:44:16 +00:00
|
|
|
|
|
|
|
# Act
|
2023-07-07 22:23:44 +00:00
|
|
|
response_gen = converse(
|
2023-03-24 14:55:22 +00:00
|
|
|
references=context, # Assume context retrieved from notes for the user_query
|
2023-03-14 23:44:16 +00:00
|
|
|
user_query="How much did I spend on dining this year?",
|
|
|
|
api_key=api_key,
|
|
|
|
)
|
2023-07-07 22:23:44 +00:00
|
|
|
response = "".join([response_chunk for response_chunk in response_gen])
|
2023-03-14 23:44:16 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(response) > 0
|
|
|
|
assert "20" in response
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-15 00:56:14 +00:00
|
|
|
@pytest.mark.chatquality
|
2023-03-14 23:44:16 +00:00
|
|
|
def test_answer_general_question_not_in_chat_history_or_retrieved_content():
|
2023-03-15 20:18:08 +00:00
|
|
|
"Chat actor should be able to answer general questions not requiring looking at chat history or notes"
|
2023-03-14 23:44:16 +00:00
|
|
|
# Arrange
|
|
|
|
message_list = [
|
2023-03-24 14:55:22 +00: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-14 23:44:16 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Act
|
2023-07-07 22:23:44 +00:00
|
|
|
response_gen = converse(
|
2023-03-24 14:55:22 +00:00
|
|
|
references=[], # Assume no context retrieved from notes for the user_query
|
2024-03-13 13:16:26 +00:00
|
|
|
user_query="Write a haiku about unit testing in 3 lines. Do not say anything else",
|
2023-03-16 20:49:35 +00:00
|
|
|
conversation_log=populate_chat_history(message_list),
|
2023-03-14 23:44:16 +00:00
|
|
|
api_key=api_key,
|
|
|
|
)
|
2023-07-07 22:23:44 +00:00
|
|
|
response = "".join([response_chunk for response_chunk in response_gen])
|
2023-03-14 23:44:16 +00:00
|
|
|
|
|
|
|
# Assert
|
2024-08-23 02:07:53 +00:00
|
|
|
expected_responses = ["test", "bug", "code"]
|
2023-03-14 23:44:16 +00:00
|
|
|
assert len(response.splitlines()) == 3 # haikus are 3 lines long
|
2024-08-23 02:07:53 +00:00
|
|
|
assert any([expected_response in response.lower() for expected_response in expected_responses]), (
|
|
|
|
"Expected haiku about unit test, but got: " + response
|
2023-03-14 23:44:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-15 00:56:14 +00:00
|
|
|
@pytest.mark.chatquality
|
2023-03-14 23:44:16 +00:00
|
|
|
def test_ask_for_clarification_if_not_enough_context_in_question():
|
2023-03-15 20:18:08 +00:00
|
|
|
"Chat actor should ask for clarification if question cannot be answered unambiguously with the provided context"
|
2023-03-14 23:44:16 +00:00
|
|
|
# Arrange
|
2023-03-24 14:55:22 +00:00
|
|
|
context = [
|
2024-07-26 18:05:50 +00:00
|
|
|
{
|
|
|
|
"compiled": f"""# Ramya
|
2024-10-22 09:58:34 +00:00
|
|
|
My sister, Ramya, is married to Kali Devi. They have 2 kids, Ravi and Rani.""",
|
|
|
|
"file": "Family.md",
|
2024-07-26 18:05:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"compiled": f"""# Fang
|
2024-10-22 09:58:34 +00:00
|
|
|
My sister, Fang Liu is married to Xi Li. They have 1 kid, Xiao Li.""",
|
|
|
|
"file": "Family.md",
|
2024-07-26 18:05:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"compiled": f"""# Aiyla
|
2024-10-22 09:58:34 +00:00
|
|
|
My sister, Aiyla is married to Tolga. They have 3 kids, Yildiz, Ali and Ahmet.""",
|
|
|
|
"file": "Family.md",
|
2024-07-26 18:05:50 +00:00
|
|
|
},
|
2023-03-24 14:55:22 +00:00
|
|
|
]
|
2023-03-14 23:44:16 +00:00
|
|
|
|
|
|
|
# Act
|
2023-07-07 22:23:44 +00:00
|
|
|
response_gen = converse(
|
2023-03-24 14:55:22 +00:00
|
|
|
references=context, # Assume context retrieved from notes for the user_query
|
2023-03-14 23:44:16 +00:00
|
|
|
user_query="How many kids does my older sister have?",
|
|
|
|
api_key=api_key,
|
|
|
|
)
|
2023-07-07 22:23:44 +00:00
|
|
|
response = "".join([response_chunk for response_chunk in response_gen])
|
2023-03-14 23:44:16 +00:00
|
|
|
|
|
|
|
# Assert
|
2024-08-23 02:07:53 +00:00
|
|
|
expected_responses = [
|
|
|
|
"which sister",
|
|
|
|
"Which sister",
|
|
|
|
"which of your sister",
|
|
|
|
"Which of your sister",
|
|
|
|
"Could you provide",
|
|
|
|
]
|
2023-03-14 23:44:16 +00:00
|
|
|
assert any([expected_response in response for expected_response in expected_responses]), (
|
2023-03-15 20:18:08 +00:00
|
|
|
"Expected chat actor to ask for clarification in response, but got: " + response
|
2023-03-14 23:44:16 +00:00
|
|
|
)
|
2023-03-16 20:49:35 +00:00
|
|
|
|
|
|
|
|
2024-03-23 16:39:38 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.chatquality
|
|
|
|
def test_agent_prompt_should_be_used(openai_agent):
|
|
|
|
"Chat actor should ask be tuned to think like an accountant based on the agent definition"
|
|
|
|
# Arrange
|
|
|
|
context = [
|
2024-06-09 01:46:55 +00:00
|
|
|
{"compiled": f"""I went to the store and bought some bananas for 2.20""", "file": "Ledger.md"},
|
|
|
|
{"compiled": f"""I went to the store and bought some apples for 1.30""", "file": "Ledger.md"},
|
|
|
|
{"compiled": f"""I went to the store and bought some oranges for 6.00""", "file": "Ledger.md"},
|
2024-03-23 16:39:38 +00:00
|
|
|
]
|
|
|
|
expected_responses = ["9.50", "9.5"]
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response_gen = converse(
|
|
|
|
references=context, # Assume context retrieved from notes for the user_query
|
|
|
|
user_query="What did I buy?",
|
|
|
|
api_key=api_key,
|
|
|
|
)
|
|
|
|
no_agent_response = "".join([response_chunk for response_chunk in response_gen])
|
|
|
|
response_gen = converse(
|
|
|
|
references=context, # Assume context retrieved from notes for the user_query
|
|
|
|
user_query="What did I buy?",
|
|
|
|
api_key=api_key,
|
|
|
|
agent=openai_agent,
|
|
|
|
)
|
|
|
|
agent_response = "".join([response_chunk for response_chunk in response_gen])
|
|
|
|
|
|
|
|
# Assert that the model without the agent prompt does not include the summary of purchases
|
|
|
|
assert all([expected_response not in no_agent_response for expected_response in expected_responses]), (
|
|
|
|
"Expected chat actor to summarize values of purchases" + no_agent_response
|
|
|
|
)
|
|
|
|
assert any([expected_response in agent_response for expected_response in expected_responses]), (
|
|
|
|
"Expected chat actor to summarize values of purchases" + agent_response
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-03-13 11:19:13 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.anyio
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
@freeze_time("2024-04-04", ignore=["transformers"])
|
2024-07-26 18:05:50 +00:00
|
|
|
async def test_websearch_with_operators(chat_client, default_user2):
|
2024-03-13 11:19:13 +00:00
|
|
|
# Arrange
|
|
|
|
user_query = "Share popular posts on r/worldnews this month"
|
|
|
|
|
|
|
|
# Act
|
2024-07-26 18:05:50 +00:00
|
|
|
responses = await generate_online_subqueries(user_query, {}, None, default_user2)
|
2024-03-13 11:19:13 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert any(
|
|
|
|
["reddit.com/r/worldnews" in response for response in responses]
|
|
|
|
), "Expected a search query to include site:reddit.com but got: " + str(responses)
|
|
|
|
|
|
|
|
assert any(
|
|
|
|
["site:reddit.com" in response for response in responses]
|
|
|
|
), "Expected a search query to include site:reddit.com but got: " + str(responses)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.anyio
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
2024-07-26 18:05:50 +00:00
|
|
|
async def test_websearch_khoj_website_for_info_about_khoj(chat_client, default_user2):
|
2024-03-13 11:19:13 +00:00
|
|
|
# Arrange
|
|
|
|
user_query = "Do you support image search?"
|
|
|
|
|
|
|
|
# Act
|
2024-07-26 18:05:50 +00:00
|
|
|
responses = await generate_online_subqueries(user_query, {}, None, default_user2)
|
2024-03-13 11:19:13 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert any(
|
|
|
|
["site:khoj.dev" in response for response in responses]
|
|
|
|
), "Expected search query to include site:khoj.dev but got: " + str(responses)
|
|
|
|
|
|
|
|
|
2024-03-06 08:18:41 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.anyio
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
2024-04-21 08:13:46 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"user_query, expected_mode",
|
|
|
|
[
|
2024-06-09 01:46:55 +00:00
|
|
|
("What's the latest in the Israel/Palestine conflict?", "text"),
|
|
|
|
("Summarize the latest tech news every Monday evening", "automation"),
|
2024-04-21 08:13:46 +00:00
|
|
|
("Paint a scenery in Timbuktu in the winter", "image"),
|
2024-06-09 01:46:55 +00:00
|
|
|
("Remind me, when did I last visit the Serengeti?", "text"),
|
2024-04-21 08:13:46 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_use_default_response_mode(chat_client, user_query, expected_mode):
|
2024-03-06 08:18:41 +00:00
|
|
|
# Act
|
|
|
|
mode = await aget_relevant_output_modes(user_query, {})
|
|
|
|
|
|
|
|
# Assert
|
2024-04-21 08:13:46 +00:00
|
|
|
assert mode.value == expected_mode
|
2024-03-06 08:18:41 +00:00
|
|
|
|
|
|
|
|
2024-03-13 11:19:13 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.anyio
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
2024-04-21 08:13:46 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"user_query, expected_conversation_commands",
|
|
|
|
[
|
|
|
|
("Where did I learn to swim?", [ConversationCommand.Notes]),
|
|
|
|
("Where is the nearest hospital?", [ConversationCommand.Online]),
|
|
|
|
("Summarize the wikipedia page on the history of the internet", [ConversationCommand.Webpage]),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_select_data_sources_actor_chooses_to_search_notes(
|
|
|
|
chat_client, user_query, expected_conversation_commands
|
|
|
|
):
|
2024-03-13 11:19:13 +00:00
|
|
|
# Act
|
2024-08-23 02:07:53 +00:00
|
|
|
conversation_commands = await aget_relevant_information_sources(user_query, {}, False, False)
|
2024-03-13 11:19:13 +00:00
|
|
|
|
|
|
|
# Assert
|
2024-06-09 01:46:55 +00:00
|
|
|
assert set(expected_conversation_commands) == set(conversation_commands)
|
2024-03-13 11:19:13 +00:00
|
|
|
|
|
|
|
|
Part 2: Add web UI updates for basic agent interactions (#675)
* Initial pass at backend changes to support agents
- Add a db model for Agents, attaching them to conversations
- When an agent is added to a conversation, override the system prompt to tweak the instructions
- Agents can be configured with prompt modification, model specification, a profile picture, and other things
- Admin-configured models will not be editable by individual users
- Add unit tests to verify agent behavior. Unit tests demonstrate imperfect adherence to prompt specifications
* Customize default behaviors for conversations without agents or with default agents
* Add a new web client route for viewing all agents
* Use agent_id for getting correct agent
* Add web UI views for agents
- Add a page to view all agents
- Add slugs to manage agents
- Add a view to view single agent
- Display active agent when in chat window
- Fix post-login redirect issue
* Fix agent view
* Spruce up the 404 page and improve the overall layout for agents pages
* Create chat actor for directly reading webpages based on user message
- Add prompt for the read webpages chat actor to extract, infer
webpage links
- Make chat actor infer or extract webpage to read directly from user
message
- Rename previous read_webpage function to more narrow
read_webpage_at_url function
* Rename agents_page -> agent_page
* Fix unit test for adding the filename to the compiled markdown entry
* Fix layout of agent, agents pages
* Merge migrations
* Let the name, slug of the default agent be Khoj, khoj
* Fix chat-related unit tests
* Add webpage chat command for read web pages requested by user
Update auto chat command inference prompt to show example of when to
use webpage chat command (i.e when url is directly provided in link)
* Support webpage command in chat API
- Fallback to use webpage when SERPER not setup and online command was
attempted
- Do not stop responding if can't retrieve online results. Try to
respond without the online context
* Test select webpage as data source and extract web urls chat actors
* Tweak prompts to extract information from webpages, online results
- Show more of the truncated messages for debugging context
- Update Khoj personality prompt to encourage it to remember it's capabilities
* Rename extract_content online results field to webpages
* Parallelize simple webpage read and extractor
Similar to what is being done with search_online with olostep
* Pass multiple webpages with their urls in online results context
Previously even if MAX_WEBPAGES_TO_READ was > 1, only 1 extracted
content would ever be passed.
URL of the extracted webpage content wasn't passed to clients in
online results context. This limited them from being rendered
* Render webpage read in chat response references on Web, Desktop apps
* Time chat actor responses & chat api request start for perf analysis
* Increase the keep alive timeout in the main application for testing
* Do not pipe access/error logs to separate files. Flow to stdout/stderr
* [Temp] Reduce to 1 gunicorn worker
* Change prod docker image to use jammy, rather than nvidia base image
* Use Khoj icon when Khoj web is installed on iOS as a PWA
* Make slug required for agents
* Simplify calling logic and prevent agent access for unauthenticated users
* Standardize to use personality over tuning in agent nomenclature
* Make filtering logic more stringent for accessible agents and remove unused method:
* Format chat message query
---------
Co-authored-by: Debanjum Singh Solanky <debanjum@gmail.com>
2024-03-26 12:43:24 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.anyio
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
2024-07-26 18:05:50 +00:00
|
|
|
async def test_infer_webpage_urls_actor_extracts_correct_links(chat_client, default_user2):
|
Part 2: Add web UI updates for basic agent interactions (#675)
* Initial pass at backend changes to support agents
- Add a db model for Agents, attaching them to conversations
- When an agent is added to a conversation, override the system prompt to tweak the instructions
- Agents can be configured with prompt modification, model specification, a profile picture, and other things
- Admin-configured models will not be editable by individual users
- Add unit tests to verify agent behavior. Unit tests demonstrate imperfect adherence to prompt specifications
* Customize default behaviors for conversations without agents or with default agents
* Add a new web client route for viewing all agents
* Use agent_id for getting correct agent
* Add web UI views for agents
- Add a page to view all agents
- Add slugs to manage agents
- Add a view to view single agent
- Display active agent when in chat window
- Fix post-login redirect issue
* Fix agent view
* Spruce up the 404 page and improve the overall layout for agents pages
* Create chat actor for directly reading webpages based on user message
- Add prompt for the read webpages chat actor to extract, infer
webpage links
- Make chat actor infer or extract webpage to read directly from user
message
- Rename previous read_webpage function to more narrow
read_webpage_at_url function
* Rename agents_page -> agent_page
* Fix unit test for adding the filename to the compiled markdown entry
* Fix layout of agent, agents pages
* Merge migrations
* Let the name, slug of the default agent be Khoj, khoj
* Fix chat-related unit tests
* Add webpage chat command for read web pages requested by user
Update auto chat command inference prompt to show example of when to
use webpage chat command (i.e when url is directly provided in link)
* Support webpage command in chat API
- Fallback to use webpage when SERPER not setup and online command was
attempted
- Do not stop responding if can't retrieve online results. Try to
respond without the online context
* Test select webpage as data source and extract web urls chat actors
* Tweak prompts to extract information from webpages, online results
- Show more of the truncated messages for debugging context
- Update Khoj personality prompt to encourage it to remember it's capabilities
* Rename extract_content online results field to webpages
* Parallelize simple webpage read and extractor
Similar to what is being done with search_online with olostep
* Pass multiple webpages with their urls in online results context
Previously even if MAX_WEBPAGES_TO_READ was > 1, only 1 extracted
content would ever be passed.
URL of the extracted webpage content wasn't passed to clients in
online results context. This limited them from being rendered
* Render webpage read in chat response references on Web, Desktop apps
* Time chat actor responses & chat api request start for perf analysis
* Increase the keep alive timeout in the main application for testing
* Do not pipe access/error logs to separate files. Flow to stdout/stderr
* [Temp] Reduce to 1 gunicorn worker
* Change prod docker image to use jammy, rather than nvidia base image
* Use Khoj icon when Khoj web is installed on iOS as a PWA
* Make slug required for agents
* Simplify calling logic and prevent agent access for unauthenticated users
* Standardize to use personality over tuning in agent nomenclature
* Make filtering logic more stringent for accessible agents and remove unused method:
* Format chat message query
---------
Co-authored-by: Debanjum Singh Solanky <debanjum@gmail.com>
2024-03-26 12:43:24 +00:00
|
|
|
# Arrange
|
|
|
|
user_query = "Summarize the wikipedia page on the history of the internet"
|
|
|
|
|
|
|
|
# Act
|
2024-07-26 18:05:50 +00:00
|
|
|
urls = await infer_webpage_urls(user_query, {}, None, default_user2)
|
Part 2: Add web UI updates for basic agent interactions (#675)
* Initial pass at backend changes to support agents
- Add a db model for Agents, attaching them to conversations
- When an agent is added to a conversation, override the system prompt to tweak the instructions
- Agents can be configured with prompt modification, model specification, a profile picture, and other things
- Admin-configured models will not be editable by individual users
- Add unit tests to verify agent behavior. Unit tests demonstrate imperfect adherence to prompt specifications
* Customize default behaviors for conversations without agents or with default agents
* Add a new web client route for viewing all agents
* Use agent_id for getting correct agent
* Add web UI views for agents
- Add a page to view all agents
- Add slugs to manage agents
- Add a view to view single agent
- Display active agent when in chat window
- Fix post-login redirect issue
* Fix agent view
* Spruce up the 404 page and improve the overall layout for agents pages
* Create chat actor for directly reading webpages based on user message
- Add prompt for the read webpages chat actor to extract, infer
webpage links
- Make chat actor infer or extract webpage to read directly from user
message
- Rename previous read_webpage function to more narrow
read_webpage_at_url function
* Rename agents_page -> agent_page
* Fix unit test for adding the filename to the compiled markdown entry
* Fix layout of agent, agents pages
* Merge migrations
* Let the name, slug of the default agent be Khoj, khoj
* Fix chat-related unit tests
* Add webpage chat command for read web pages requested by user
Update auto chat command inference prompt to show example of when to
use webpage chat command (i.e when url is directly provided in link)
* Support webpage command in chat API
- Fallback to use webpage when SERPER not setup and online command was
attempted
- Do not stop responding if can't retrieve online results. Try to
respond without the online context
* Test select webpage as data source and extract web urls chat actors
* Tweak prompts to extract information from webpages, online results
- Show more of the truncated messages for debugging context
- Update Khoj personality prompt to encourage it to remember it's capabilities
* Rename extract_content online results field to webpages
* Parallelize simple webpage read and extractor
Similar to what is being done with search_online with olostep
* Pass multiple webpages with their urls in online results context
Previously even if MAX_WEBPAGES_TO_READ was > 1, only 1 extracted
content would ever be passed.
URL of the extracted webpage content wasn't passed to clients in
online results context. This limited them from being rendered
* Render webpage read in chat response references on Web, Desktop apps
* Time chat actor responses & chat api request start for perf analysis
* Increase the keep alive timeout in the main application for testing
* Do not pipe access/error logs to separate files. Flow to stdout/stderr
* [Temp] Reduce to 1 gunicorn worker
* Change prod docker image to use jammy, rather than nvidia base image
* Use Khoj icon when Khoj web is installed on iOS as a PWA
* Make slug required for agents
* Simplify calling logic and prevent agent access for unauthenticated users
* Standardize to use personality over tuning in agent nomenclature
* Make filtering logic more stringent for accessible agents and remove unused method:
* Format chat message query
---------
Co-authored-by: Debanjum Singh Solanky <debanjum@gmail.com>
2024-03-26 12:43:24 +00:00
|
|
|
|
|
|
|
# Assert
|
2024-04-21 08:13:46 +00:00
|
|
|
assert "https://en.wikipedia.org/wiki/History_of_the_Internet" in urls
|
Part 2: Add web UI updates for basic agent interactions (#675)
* Initial pass at backend changes to support agents
- Add a db model for Agents, attaching them to conversations
- When an agent is added to a conversation, override the system prompt to tweak the instructions
- Agents can be configured with prompt modification, model specification, a profile picture, and other things
- Admin-configured models will not be editable by individual users
- Add unit tests to verify agent behavior. Unit tests demonstrate imperfect adherence to prompt specifications
* Customize default behaviors for conversations without agents or with default agents
* Add a new web client route for viewing all agents
* Use agent_id for getting correct agent
* Add web UI views for agents
- Add a page to view all agents
- Add slugs to manage agents
- Add a view to view single agent
- Display active agent when in chat window
- Fix post-login redirect issue
* Fix agent view
* Spruce up the 404 page and improve the overall layout for agents pages
* Create chat actor for directly reading webpages based on user message
- Add prompt for the read webpages chat actor to extract, infer
webpage links
- Make chat actor infer or extract webpage to read directly from user
message
- Rename previous read_webpage function to more narrow
read_webpage_at_url function
* Rename agents_page -> agent_page
* Fix unit test for adding the filename to the compiled markdown entry
* Fix layout of agent, agents pages
* Merge migrations
* Let the name, slug of the default agent be Khoj, khoj
* Fix chat-related unit tests
* Add webpage chat command for read web pages requested by user
Update auto chat command inference prompt to show example of when to
use webpage chat command (i.e when url is directly provided in link)
* Support webpage command in chat API
- Fallback to use webpage when SERPER not setup and online command was
attempted
- Do not stop responding if can't retrieve online results. Try to
respond without the online context
* Test select webpage as data source and extract web urls chat actors
* Tweak prompts to extract information from webpages, online results
- Show more of the truncated messages for debugging context
- Update Khoj personality prompt to encourage it to remember it's capabilities
* Rename extract_content online results field to webpages
* Parallelize simple webpage read and extractor
Similar to what is being done with search_online with olostep
* Pass multiple webpages with their urls in online results context
Previously even if MAX_WEBPAGES_TO_READ was > 1, only 1 extracted
content would ever be passed.
URL of the extracted webpage content wasn't passed to clients in
online results context. This limited them from being rendered
* Render webpage read in chat response references on Web, Desktop apps
* Time chat actor responses & chat api request start for perf analysis
* Increase the keep alive timeout in the main application for testing
* Do not pipe access/error logs to separate files. Flow to stdout/stderr
* [Temp] Reduce to 1 gunicorn worker
* Change prod docker image to use jammy, rather than nvidia base image
* Use Khoj icon when Khoj web is installed on iOS as a PWA
* Make slug required for agents
* Simplify calling logic and prevent agent access for unauthenticated users
* Standardize to use personality over tuning in agent nomenclature
* Make filtering logic more stringent for accessible agents and remove unused method:
* Format chat message query
---------
Co-authored-by: Debanjum Singh Solanky <debanjum@gmail.com>
2024-03-26 12:43:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.anyio
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
2024-04-21 08:13:46 +00:00
|
|
|
@pytest.mark.parametrize(
|
2024-06-09 01:46:55 +00:00
|
|
|
"user_query, expected_crontime, expected_qs, unexpected_qs",
|
2024-04-21 08:13:46 +00:00
|
|
|
[
|
|
|
|
(
|
2024-04-21 19:20:59 +00:00
|
|
|
"Share the weather forecast for the next day daily at 7:30pm",
|
2024-06-09 01:46:55 +00:00
|
|
|
"30 19 * * *",
|
|
|
|
["weather forecast"],
|
2024-04-21 19:20:59 +00:00
|
|
|
["7:30"],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Notify me when the new President of Brazil is announced",
|
|
|
|
"* *", # crontime is variable
|
|
|
|
["brazil", "president"],
|
|
|
|
["notify"], # ensure reminder isn't re-triggered on scheduled query run
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Let me know whenever Elon leaves Twitter. Check this every afternoon at 12",
|
2024-06-09 01:46:55 +00:00
|
|
|
"0 12 * * *", # ensure correctly converts to utc
|
2024-04-21 19:20:59 +00:00
|
|
|
["elon", "twitter"],
|
|
|
|
["12"],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Draw a wallpaper every morning using the current weather",
|
|
|
|
"* * *", # daily crontime
|
2024-06-09 01:46:55 +00:00
|
|
|
["weather", "wallpaper"],
|
2024-04-21 19:20:59 +00:00
|
|
|
["every"],
|
2024-04-21 08:13:46 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2024-10-22 09:58:34 +00:00
|
|
|
async def test_infer_task_scheduling_request(
|
|
|
|
chat_client, user_query, expected_crontime, expected_qs, unexpected_qs, default_user2
|
|
|
|
):
|
Part 2: Add web UI updates for basic agent interactions (#675)
* Initial pass at backend changes to support agents
- Add a db model for Agents, attaching them to conversations
- When an agent is added to a conversation, override the system prompt to tweak the instructions
- Agents can be configured with prompt modification, model specification, a profile picture, and other things
- Admin-configured models will not be editable by individual users
- Add unit tests to verify agent behavior. Unit tests demonstrate imperfect adherence to prompt specifications
* Customize default behaviors for conversations without agents or with default agents
* Add a new web client route for viewing all agents
* Use agent_id for getting correct agent
* Add web UI views for agents
- Add a page to view all agents
- Add slugs to manage agents
- Add a view to view single agent
- Display active agent when in chat window
- Fix post-login redirect issue
* Fix agent view
* Spruce up the 404 page and improve the overall layout for agents pages
* Create chat actor for directly reading webpages based on user message
- Add prompt for the read webpages chat actor to extract, infer
webpage links
- Make chat actor infer or extract webpage to read directly from user
message
- Rename previous read_webpage function to more narrow
read_webpage_at_url function
* Rename agents_page -> agent_page
* Fix unit test for adding the filename to the compiled markdown entry
* Fix layout of agent, agents pages
* Merge migrations
* Let the name, slug of the default agent be Khoj, khoj
* Fix chat-related unit tests
* Add webpage chat command for read web pages requested by user
Update auto chat command inference prompt to show example of when to
use webpage chat command (i.e when url is directly provided in link)
* Support webpage command in chat API
- Fallback to use webpage when SERPER not setup and online command was
attempted
- Do not stop responding if can't retrieve online results. Try to
respond without the online context
* Test select webpage as data source and extract web urls chat actors
* Tweak prompts to extract information from webpages, online results
- Show more of the truncated messages for debugging context
- Update Khoj personality prompt to encourage it to remember it's capabilities
* Rename extract_content online results field to webpages
* Parallelize simple webpage read and extractor
Similar to what is being done with search_online with olostep
* Pass multiple webpages with their urls in online results context
Previously even if MAX_WEBPAGES_TO_READ was > 1, only 1 extracted
content would ever be passed.
URL of the extracted webpage content wasn't passed to clients in
online results context. This limited them from being rendered
* Render webpage read in chat response references on Web, Desktop apps
* Time chat actor responses & chat api request start for perf analysis
* Increase the keep alive timeout in the main application for testing
* Do not pipe access/error logs to separate files. Flow to stdout/stderr
* [Temp] Reduce to 1 gunicorn worker
* Change prod docker image to use jammy, rather than nvidia base image
* Use Khoj icon when Khoj web is installed on iOS as a PWA
* Make slug required for agents
* Simplify calling logic and prevent agent access for unauthenticated users
* Standardize to use personality over tuning in agent nomenclature
* Make filtering logic more stringent for accessible agents and remove unused method:
* Format chat message query
---------
Co-authored-by: Debanjum Singh Solanky <debanjum@gmail.com>
2024-03-26 12:43:24 +00:00
|
|
|
# Act
|
2024-10-22 09:58:34 +00:00
|
|
|
crontime, inferred_query, _ = await schedule_query(user_query, {}, default_user2)
|
2024-04-21 19:20:59 +00:00
|
|
|
inferred_query = inferred_query.lower()
|
Part 2: Add web UI updates for basic agent interactions (#675)
* Initial pass at backend changes to support agents
- Add a db model for Agents, attaching them to conversations
- When an agent is added to a conversation, override the system prompt to tweak the instructions
- Agents can be configured with prompt modification, model specification, a profile picture, and other things
- Admin-configured models will not be editable by individual users
- Add unit tests to verify agent behavior. Unit tests demonstrate imperfect adherence to prompt specifications
* Customize default behaviors for conversations without agents or with default agents
* Add a new web client route for viewing all agents
* Use agent_id for getting correct agent
* Add web UI views for agents
- Add a page to view all agents
- Add slugs to manage agents
- Add a view to view single agent
- Display active agent when in chat window
- Fix post-login redirect issue
* Fix agent view
* Spruce up the 404 page and improve the overall layout for agents pages
* Create chat actor for directly reading webpages based on user message
- Add prompt for the read webpages chat actor to extract, infer
webpage links
- Make chat actor infer or extract webpage to read directly from user
message
- Rename previous read_webpage function to more narrow
read_webpage_at_url function
* Rename agents_page -> agent_page
* Fix unit test for adding the filename to the compiled markdown entry
* Fix layout of agent, agents pages
* Merge migrations
* Let the name, slug of the default agent be Khoj, khoj
* Fix chat-related unit tests
* Add webpage chat command for read web pages requested by user
Update auto chat command inference prompt to show example of when to
use webpage chat command (i.e when url is directly provided in link)
* Support webpage command in chat API
- Fallback to use webpage when SERPER not setup and online command was
attempted
- Do not stop responding if can't retrieve online results. Try to
respond without the online context
* Test select webpage as data source and extract web urls chat actors
* Tweak prompts to extract information from webpages, online results
- Show more of the truncated messages for debugging context
- Update Khoj personality prompt to encourage it to remember it's capabilities
* Rename extract_content online results field to webpages
* Parallelize simple webpage read and extractor
Similar to what is being done with search_online with olostep
* Pass multiple webpages with their urls in online results context
Previously even if MAX_WEBPAGES_TO_READ was > 1, only 1 extracted
content would ever be passed.
URL of the extracted webpage content wasn't passed to clients in
online results context. This limited them from being rendered
* Render webpage read in chat response references on Web, Desktop apps
* Time chat actor responses & chat api request start for perf analysis
* Increase the keep alive timeout in the main application for testing
* Do not pipe access/error logs to separate files. Flow to stdout/stderr
* [Temp] Reduce to 1 gunicorn worker
* Change prod docker image to use jammy, rather than nvidia base image
* Use Khoj icon when Khoj web is installed on iOS as a PWA
* Make slug required for agents
* Simplify calling logic and prevent agent access for unauthenticated users
* Standardize to use personality over tuning in agent nomenclature
* Make filtering logic more stringent for accessible agents and remove unused method:
* Format chat message query
---------
Co-authored-by: Debanjum Singh Solanky <debanjum@gmail.com>
2024-03-26 12:43:24 +00:00
|
|
|
|
|
|
|
# Assert
|
2024-04-21 08:13:46 +00:00
|
|
|
assert expected_crontime in crontime
|
2024-04-21 19:20:59 +00:00
|
|
|
for expected_q in expected_qs:
|
|
|
|
assert expected_q in inferred_query, f"Expected fragment {expected_q} in query: {inferred_query}"
|
|
|
|
for unexpected_q in unexpected_qs:
|
|
|
|
assert (
|
|
|
|
unexpected_q not in inferred_query
|
|
|
|
), f"Did not expect fragment '{unexpected_q}' in query: '{inferred_query}'"
|
Part 2: Add web UI updates for basic agent interactions (#675)
* Initial pass at backend changes to support agents
- Add a db model for Agents, attaching them to conversations
- When an agent is added to a conversation, override the system prompt to tweak the instructions
- Agents can be configured with prompt modification, model specification, a profile picture, and other things
- Admin-configured models will not be editable by individual users
- Add unit tests to verify agent behavior. Unit tests demonstrate imperfect adherence to prompt specifications
* Customize default behaviors for conversations without agents or with default agents
* Add a new web client route for viewing all agents
* Use agent_id for getting correct agent
* Add web UI views for agents
- Add a page to view all agents
- Add slugs to manage agents
- Add a view to view single agent
- Display active agent when in chat window
- Fix post-login redirect issue
* Fix agent view
* Spruce up the 404 page and improve the overall layout for agents pages
* Create chat actor for directly reading webpages based on user message
- Add prompt for the read webpages chat actor to extract, infer
webpage links
- Make chat actor infer or extract webpage to read directly from user
message
- Rename previous read_webpage function to more narrow
read_webpage_at_url function
* Rename agents_page -> agent_page
* Fix unit test for adding the filename to the compiled markdown entry
* Fix layout of agent, agents pages
* Merge migrations
* Let the name, slug of the default agent be Khoj, khoj
* Fix chat-related unit tests
* Add webpage chat command for read web pages requested by user
Update auto chat command inference prompt to show example of when to
use webpage chat command (i.e when url is directly provided in link)
* Support webpage command in chat API
- Fallback to use webpage when SERPER not setup and online command was
attempted
- Do not stop responding if can't retrieve online results. Try to
respond without the online context
* Test select webpage as data source and extract web urls chat actors
* Tweak prompts to extract information from webpages, online results
- Show more of the truncated messages for debugging context
- Update Khoj personality prompt to encourage it to remember it's capabilities
* Rename extract_content online results field to webpages
* Parallelize simple webpage read and extractor
Similar to what is being done with search_online with olostep
* Pass multiple webpages with their urls in online results context
Previously even if MAX_WEBPAGES_TO_READ was > 1, only 1 extracted
content would ever be passed.
URL of the extracted webpage content wasn't passed to clients in
online results context. This limited them from being rendered
* Render webpage read in chat response references on Web, Desktop apps
* Time chat actor responses & chat api request start for perf analysis
* Increase the keep alive timeout in the main application for testing
* Do not pipe access/error logs to separate files. Flow to stdout/stderr
* [Temp] Reduce to 1 gunicorn worker
* Change prod docker image to use jammy, rather than nvidia base image
* Use Khoj icon when Khoj web is installed on iOS as a PWA
* Make slug required for agents
* Simplify calling logic and prevent agent access for unauthenticated users
* Standardize to use personality over tuning in agent nomenclature
* Make filtering logic more stringent for accessible agents and remove unused method:
* Format chat message query
---------
Co-authored-by: Debanjum Singh Solanky <debanjum@gmail.com>
2024-03-26 12:43:24 +00:00
|
|
|
|
|
|
|
|
2024-04-21 14:36:42 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.anyio
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"scheduling_query, executing_query, generated_response, expected_should_notify",
|
|
|
|
[
|
|
|
|
(
|
2024-10-22 09:58:34 +00:00
|
|
|
"Notify me only if it is going to rain tomorrow?",
|
2024-04-21 14:36:42 +00:00
|
|
|
"What's the weather forecast for tomorrow?",
|
|
|
|
"It is sunny and warm tomorrow.",
|
|
|
|
False,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Summarize the latest news every morning",
|
|
|
|
"Summarize today's news",
|
|
|
|
"Today in the news: AI is taking over the world",
|
|
|
|
True,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Create a weather wallpaper every morning using the current weather",
|
|
|
|
"Paint a weather wallpaper using the current weather",
|
|
|
|
"https://khoj-generated-wallpaper.khoj.dev/user110/weathervane.webp",
|
|
|
|
True,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Let me know the election results once they are offically declared",
|
|
|
|
"What are the results of the elections? Has the winner been declared?",
|
|
|
|
"The election results has not been declared yet.",
|
|
|
|
False,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_decision_on_when_to_notify_scheduled_task_results(
|
2024-10-22 09:58:34 +00:00
|
|
|
chat_client, default_user2, scheduling_query, executing_query, generated_response, expected_should_notify
|
2024-04-21 14:36:42 +00:00
|
|
|
):
|
|
|
|
# Act
|
2024-10-22 09:58:34 +00:00
|
|
|
generated_should_notify = should_notify(scheduling_query, executing_query, generated_response, default_user2)
|
2024-04-21 14:36:42 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert generated_should_notify == expected_should_notify
|
|
|
|
|
|
|
|
|
2023-03-16 20:49:35 +00:00
|
|
|
# Helpers
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def populate_chat_history(message_list):
|
|
|
|
# Generate conversation logs
|
|
|
|
conversation_log = {"chat": []}
|
|
|
|
for user_message, gpt_message, context in message_list:
|
|
|
|
conversation_log["chat"] += message_to_log(
|
|
|
|
user_message,
|
|
|
|
gpt_message,
|
2024-07-26 18:05:50 +00:00
|
|
|
khoj_message_metadata={
|
|
|
|
"context": context,
|
|
|
|
"intent": {"query": user_message, "inferred-queries": f'["{user_message}"]'},
|
|
|
|
},
|
|
|
|
conversation_log=[],
|
2023-03-16 20:49:35 +00:00
|
|
|
)
|
|
|
|
return conversation_log
|