From a47a54f2079ac754df5bf5bd3a321870071268b2 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 26 Jul 2024 22:56:24 +0530 Subject: [PATCH 1/5] Pass user name to document and online search actors prompts This should improve the quality of personal information extraction from document and online sources. The user name is only used when it is set --- .../conversation/anthropic/anthropic_chat.py | 5 ++++- .../processor/conversation/offline/chat_model.py | 5 ++++- src/khoj/processor/conversation/openai/gpt.py | 5 ++++- src/khoj/processor/conversation/openai/utils.py | 2 +- src/khoj/processor/conversation/prompts.py | 5 +++++ src/khoj/processor/tools/online_search.py | 12 +++++++++--- src/khoj/routers/api.py | 3 +++ src/khoj/routers/api_chat.py | 4 ++-- src/khoj/routers/helpers.py | 12 ++++++++++-- 9 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/khoj/processor/conversation/anthropic/anthropic_chat.py b/src/khoj/processor/conversation/anthropic/anthropic_chat.py index d5778885..72bf9250 100644 --- a/src/khoj/processor/conversation/anthropic/anthropic_chat.py +++ b/src/khoj/processor/conversation/anthropic/anthropic_chat.py @@ -6,7 +6,7 @@ from typing import Dict, Optional from langchain.schema import ChatMessage -from khoj.database.models import Agent +from khoj.database.models import Agent, KhojUser from khoj.processor.conversation import prompts from khoj.processor.conversation.anthropic.utils import ( anthropic_chat_completion_with_backoff, @@ -26,12 +26,14 @@ def extract_questions_anthropic( api_key=None, temperature=0, location_data: LocationData = None, + user: KhojUser = None, ): """ Infer search queries to retrieve relevant notes to answer user query """ # Extract Past User Message and Inferred Questions from Conversation Log location = f"{location_data.city}, {location_data.region}, {location_data.country}" if location_data else "Unknown" + username = prompts.user_name.format(name=user.get_full_name()) if user and user.get_full_name() else "" # Extract Past User Message and Inferred Questions from Conversation Log chat_history = "".join( @@ -55,6 +57,7 @@ def extract_questions_anthropic( current_new_year_date=current_new_year.strftime("%Y-%m-%d"), yesterday_date=(today - timedelta(days=1)).strftime("%Y-%m-%d"), location=location, + username=username, ) prompt = prompts.extract_questions_anthropic_user_message.format( diff --git a/src/khoj/processor/conversation/offline/chat_model.py b/src/khoj/processor/conversation/offline/chat_model.py index 2da0c186..2fabddc7 100644 --- a/src/khoj/processor/conversation/offline/chat_model.py +++ b/src/khoj/processor/conversation/offline/chat_model.py @@ -7,7 +7,7 @@ from typing import Any, Iterator, List, Union from langchain.schema import ChatMessage from llama_cpp import Llama -from khoj.database.models import Agent +from khoj.database.models import Agent, KhojUser from khoj.processor.conversation import prompts from khoj.processor.conversation.offline.utils import download_model from khoj.processor.conversation.utils import ( @@ -30,6 +30,7 @@ def extract_questions_offline( use_history: bool = True, should_extract_questions: bool = True, location_data: LocationData = None, + user: KhojUser = None, max_prompt_size: int = None, ) -> List[str]: """ @@ -45,6 +46,7 @@ def extract_questions_offline( offline_chat_model = loaded_model or download_model(model, max_tokens=max_prompt_size) location = f"{location_data.city}, {location_data.region}, {location_data.country}" if location_data else "Unknown" + username = prompts.user_name.format(name=user.get_full_name()) if user and user.get_full_name() else "" # Extract Past User Message and Inferred Questions from Conversation Log chat_history = "" @@ -68,6 +70,7 @@ def extract_questions_offline( last_year=last_year, this_year=today.year, location=location, + username=username, ) messages = generate_chatml_messages_with_context( diff --git a/src/khoj/processor/conversation/openai/gpt.py b/src/khoj/processor/conversation/openai/gpt.py index f1608fba..7649a6df 100644 --- a/src/khoj/processor/conversation/openai/gpt.py +++ b/src/khoj/processor/conversation/openai/gpt.py @@ -5,7 +5,7 @@ from typing import Dict, Optional from langchain.schema import ChatMessage -from khoj.database.models import Agent +from khoj.database.models import Agent, KhojUser from khoj.processor.conversation import prompts from khoj.processor.conversation.openai.utils import ( chat_completion_with_backoff, @@ -27,11 +27,13 @@ def extract_questions( temperature=0, max_tokens=100, location_data: LocationData = None, + user: KhojUser = None, ): """ Infer search queries to retrieve relevant notes to answer user query """ location = f"{location_data.city}, {location_data.region}, {location_data.country}" if location_data else "Unknown" + username = prompts.user_name.format(name=user.get_full_name()) if user and user.get_full_name() else "" # Extract Past User Message and Inferred Questions from Conversation Log chat_history = "".join( @@ -59,6 +61,7 @@ def extract_questions( text=text, yesterday_date=(today - timedelta(days=1)).strftime("%Y-%m-%d"), location=location, + username=username, ) messages = [ChatMessage(content=prompt, role="user")] diff --git a/src/khoj/processor/conversation/openai/utils.py b/src/khoj/processor/conversation/openai/utils.py index b17e5c3d..841e6aa7 100644 --- a/src/khoj/processor/conversation/openai/utils.py +++ b/src/khoj/processor/conversation/openai/utils.py @@ -36,7 +36,7 @@ def completion_with_backoff( messages, model, temperature=0, openai_api_key=None, api_base_url=None, model_kwargs=None ) -> str: client_key = f"{openai_api_key}--{api_base_url}" - client: openai.OpenAI = openai_clients.get(client_key) + client: openai.OpenAI | None = openai_clients.get(client_key) if not client: client = openai.OpenAI( api_key=openai_api_key, diff --git a/src/khoj/processor/conversation/prompts.py b/src/khoj/processor/conversation/prompts.py index 289bafbc..6412e232 100644 --- a/src/khoj/processor/conversation/prompts.py +++ b/src/khoj/processor/conversation/prompts.py @@ -212,6 +212,7 @@ Construct search queries to retrieve relevant information to answer the user's q Current Date: {day_of_week}, {current_date} User's Location: {location} +{username} Examples: Q: How was my trip to Cambodia? @@ -258,6 +259,7 @@ Construct search queries to retrieve relevant information to answer the user's q What searches will you perform to answer the users question? Respond with search queries as list of strings in a JSON object. Current Date: {day_of_week}, {current_date} User's Location: {location} +{username} Q: How was my trip to Cambodia? Khoj: {{"queries": ["How was my trip to Cambodia?"]}} @@ -310,6 +312,7 @@ What searches will you perform to answer the users question? Respond with a JSON Current Date: {day_of_week}, {current_date} User's Location: {location} +{username} Here are some examples of how you can construct search queries to answer the user's question: @@ -525,6 +528,7 @@ Which webpages will you need to read to answer the user's question? Provide web page links as a list of strings in a JSON object. Current Date: {current_date} User's Location: {location} +{username} Here are some examples: History: @@ -571,6 +575,7 @@ What Google searches, if any, will you need to perform to answer the user's ques Provide search queries as a list of strings in a JSON object. Do not wrap the json in a codeblock. Current Date: {current_date} User's Location: {location} +{username} Here are some examples: History: diff --git a/src/khoj/processor/tools/online_search.py b/src/khoj/processor/tools/online_search.py index c087de70..29b0f850 100644 --- a/src/khoj/processor/tools/online_search.py +++ b/src/khoj/processor/tools/online_search.py @@ -10,6 +10,7 @@ import aiohttp from bs4 import BeautifulSoup from markdownify import markdownify +from khoj.database.models import KhojUser from khoj.routers.helpers import ( ChatEvent, extract_relevant_info, @@ -51,6 +52,7 @@ async def search_online( query: str, conversation_history: dict, location: LocationData, + user: KhojUser, send_status_func: Optional[Callable] = None, custom_filters: List[str] = [], ): @@ -61,7 +63,7 @@ async def search_online( return # Breakdown the query into subqueries to get the correct answer - subqueries = await generate_online_subqueries(query, conversation_history, location) + subqueries = await generate_online_subqueries(query, conversation_history, location, user) response_dict = {} if subqueries: @@ -126,14 +128,18 @@ async def search_with_google(query: str) -> Tuple[str, Dict[str, List[Dict]]]: async def read_webpages( - query: str, conversation_history: dict, location: LocationData, send_status_func: Optional[Callable] = None + query: str, + conversation_history: dict, + location: LocationData, + user: KhojUser, + send_status_func: Optional[Callable] = None, ): "Infer web pages to read from the query and extract relevant information from them" logger.info(f"Inferring web pages to read") if send_status_func: async for event in send_status_func(f"**🧐 Inferring web pages to read**"): yield {ChatEvent.STATUS: event} - urls = await infer_webpage_urls(query, conversation_history, location) + urls = await infer_webpage_urls(query, conversation_history, location, user) logger.info(f"Reading web pages at: {urls}") if send_status_func: diff --git a/src/khoj/routers/api.py b/src/khoj/routers/api.py index 15d7cbc7..5f89cb72 100644 --- a/src/khoj/routers/api.py +++ b/src/khoj/routers/api.py @@ -343,6 +343,7 @@ async def extract_references_and_questions( conversation_log=meta_log, should_extract_questions=True, location_data=location_data, + user=user, max_prompt_size=conversation_config.max_prompt_size, ) elif conversation_config.model_type == ChatModelOptions.ModelType.OPENAI: @@ -357,6 +358,7 @@ async def extract_references_and_questions( api_base_url=base_url, conversation_log=meta_log, location_data=location_data, + user=user, max_tokens=conversation_config.max_prompt_size, ) elif conversation_config.model_type == ChatModelOptions.ModelType.ANTHROPIC: @@ -368,6 +370,7 @@ async def extract_references_and_questions( api_key=api_key, conversation_log=meta_log, location_data=location_data, + user=user, ) # Collate search results as context for GPT diff --git a/src/khoj/routers/api_chat.py b/src/khoj/routers/api_chat.py index 63529b8e..d9223192 100644 --- a/src/khoj/routers/api_chat.py +++ b/src/khoj/routers/api_chat.py @@ -800,7 +800,7 @@ async def chat( if ConversationCommand.Online in conversation_commands: try: async for result in search_online( - defiltered_query, meta_log, location, partial(send_event, ChatEvent.STATUS), custom_filters + defiltered_query, meta_log, location, user, partial(send_event, ChatEvent.STATUS), custom_filters ): if isinstance(result, dict) and ChatEvent.STATUS in result: yield result[ChatEvent.STATUS] @@ -817,7 +817,7 @@ async def chat( if ConversationCommand.Webpage in conversation_commands: try: async for result in read_webpages( - defiltered_query, meta_log, location, partial(send_event, ChatEvent.STATUS) + defiltered_query, meta_log, location, user, partial(send_event, ChatEvent.STATUS) ): if isinstance(result, dict) and ChatEvent.STATUS in result: yield result[ChatEvent.STATUS] diff --git a/src/khoj/routers/helpers.py b/src/khoj/routers/helpers.py index 846f5c8f..69ef74b1 100644 --- a/src/khoj/routers/helpers.py +++ b/src/khoj/routers/helpers.py @@ -315,11 +315,14 @@ async def aget_relevant_output_modes(query: str, conversation_history: dict, is_ return ConversationCommand.Text -async def infer_webpage_urls(q: str, conversation_history: dict, location_data: LocationData) -> List[str]: +async def infer_webpage_urls( + q: str, conversation_history: dict, location_data: LocationData, user: KhojUser +) -> List[str]: """ Infer webpage links from the given query """ location = f"{location_data.city}, {location_data.region}, {location_data.country}" if location_data else "Unknown" + username = prompts.user_name.format(name=user.get_full_name()) if user.get_full_name() else "" chat_history = construct_chat_history(conversation_history) utc_date = datetime.utcnow().strftime("%Y-%m-%d") @@ -328,6 +331,7 @@ async def infer_webpage_urls(q: str, conversation_history: dict, location_data: query=q, chat_history=chat_history, location=location, + username=username, ) with timer("Chat actor: Infer webpage urls to read", logger): @@ -345,11 +349,14 @@ async def infer_webpage_urls(q: str, conversation_history: dict, location_data: raise ValueError(f"Invalid list of urls: {response}") -async def generate_online_subqueries(q: str, conversation_history: dict, location_data: LocationData) -> List[str]: +async def generate_online_subqueries( + q: str, conversation_history: dict, location_data: LocationData, user: KhojUser +) -> List[str]: """ Generate subqueries from the given query """ location = f"{location_data.city}, {location_data.region}, {location_data.country}" if location_data else "Unknown" + username = prompts.user_name.format(name=user.get_full_name()) if user.get_full_name() else "" chat_history = construct_chat_history(conversation_history) utc_date = datetime.utcnow().strftime("%Y-%m-%d") @@ -358,6 +365,7 @@ async def generate_online_subqueries(q: str, conversation_history: dict, locatio query=q, chat_history=chat_history, location=location, + username=username, ) with timer("Chat actor: Generate online search subqueries", logger): From 878cc023a0022b382f235c49c0259a5cb90e0a8e Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 26 Jul 2024 23:35:50 +0530 Subject: [PATCH 2/5] Fix and improve openai chat actor tests - Use new form of passing doc references to now passing chat actor test - Fix message list generation from conversation logs provided Strangely the parent conversation_log gets passed down to message_to_log func when the kwarg is not explicitly specified --- tests/test_openai_chat_actors.py | 38 ++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/tests/test_openai_chat_actors.py b/tests/test_openai_chat_actors.py index ae2a8c55..83aee27f 100644 --- a/tests/test_openai_chat_actors.py +++ b/tests/test_openai_chat_actors.py @@ -17,6 +17,7 @@ from khoj.routers.helpers import ( ) from khoj.utils.helpers import ConversationCommand from khoj.utils.rawconfig import LocationData +from tests.conftest import default_user2 # Initialize variables for tests api_key = os.getenv("OPENAI_API_KEY") @@ -412,18 +413,23 @@ def test_answer_general_question_not_in_chat_history_or_retrieved_content(): # ---------------------------------------------------------------------------------------------------- -@pytest.mark.xfail(reason="Chat actor not consistently capable of asking for clarification yet.") @pytest.mark.chatquality def test_ask_for_clarification_if_not_enough_context_in_question(): "Chat actor should ask for clarification if question cannot be answered unambiguously with the provided context" # Arrange context = [ - f"""# Ramya -My sister, Ramya, is married to Kali Devi. They have 2 kids, Ravi and Rani.""", - f"""# Fang -My sister, Fang Liu is married to Xi Li. They have 1 kid, Xiao Li.""", - f"""# Aiyla -My sister, Aiyla is married to Tolga. They have 3 kids, Yildiz, Ali and Ahmet.""", + { + "compiled": f"""# Ramya +My sister, Ramya, is married to Kali Devi. They have 2 kids, Ravi and Rani.""" + }, + { + "compiled": f"""# Fang +My sister, Fang Liu is married to Xi Li. They have 1 kid, Xiao Li.""" + }, + { + "compiled": f"""# Aiyla +My sister, Aiyla is married to Tolga. They have 3 kids, Yildiz, Ali and Ahmet.""" + }, ] # Act @@ -481,12 +487,12 @@ def test_agent_prompt_should_be_used(openai_agent): @pytest.mark.anyio @pytest.mark.django_db(transaction=True) @freeze_time("2024-04-04", ignore=["transformers"]) -async def test_websearch_with_operators(chat_client): +async def test_websearch_with_operators(chat_client, default_user2): # Arrange user_query = "Share popular posts on r/worldnews this month" # Act - responses = await generate_online_subqueries(user_query, {}, None) + responses = await generate_online_subqueries(user_query, {}, None, default_user2) # Assert assert any( @@ -501,12 +507,12 @@ async def test_websearch_with_operators(chat_client): # ---------------------------------------------------------------------------------------------------- @pytest.mark.anyio @pytest.mark.django_db(transaction=True) -async def test_websearch_khoj_website_for_info_about_khoj(chat_client): +async def test_websearch_khoj_website_for_info_about_khoj(chat_client, default_user2): # Arrange user_query = "Do you support image search?" # Act - responses = await generate_online_subqueries(user_query, {}, None) + responses = await generate_online_subqueries(user_query, {}, None, default_user2) # Assert assert any( @@ -558,12 +564,12 @@ async def test_select_data_sources_actor_chooses_to_search_notes( # ---------------------------------------------------------------------------------------------------- @pytest.mark.anyio @pytest.mark.django_db(transaction=True) -async def test_infer_webpage_urls_actor_extracts_correct_links(chat_client): +async def test_infer_webpage_urls_actor_extracts_correct_links(chat_client, default_user2): # Arrange user_query = "Summarize the wikipedia page on the history of the internet" # Act - urls = await infer_webpage_urls(user_query, {}, None) + urls = await infer_webpage_urls(user_query, {}, None, default_user2) # Assert assert "https://en.wikipedia.org/wiki/History_of_the_Internet" in urls @@ -667,6 +673,10 @@ def populate_chat_history(message_list): conversation_log["chat"] += message_to_log( user_message, gpt_message, - {"context": context, "intent": {"query": user_message, "inferred-queries": f'["{user_message}"]'}}, + khoj_message_metadata={ + "context": context, + "intent": {"query": user_message, "inferred-queries": f'["{user_message}"]'}, + }, + conversation_log=[], ) return conversation_log From 3675938df6cc67ff1e738d73b3f48576a581546c Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Tue, 13 Aug 2024 12:51:25 +0530 Subject: [PATCH 3/5] Support passing temperature to offline chat model chat actors - Use temperature of 0 by default for extract questions offline chat actor - Use temperature of 0.2 for send_message_to_model_offline (this is the default temperature set by llama.cpp) --- .../processor/conversation/offline/chat_model.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/khoj/processor/conversation/offline/chat_model.py b/src/khoj/processor/conversation/offline/chat_model.py index 2fabddc7..0244480f 100644 --- a/src/khoj/processor/conversation/offline/chat_model.py +++ b/src/khoj/processor/conversation/offline/chat_model.py @@ -32,6 +32,7 @@ def extract_questions_offline( location_data: LocationData = None, user: KhojUser = None, max_prompt_size: int = None, + temperature: float = 0, ) -> List[str]: """ Infer search queries to retrieve relevant notes to answer user query @@ -80,7 +81,11 @@ def extract_questions_offline( state.chat_lock.acquire() try: response = send_message_to_model_offline( - messages, loaded_model=offline_chat_model, model=model, max_prompt_size=max_prompt_size + messages, + loaded_model=offline_chat_model, + model=model, + max_prompt_size=max_prompt_size, + temperature=temperature, ) finally: state.chat_lock.release() @@ -232,6 +237,7 @@ def send_message_to_model_offline( messages: List[ChatMessage], loaded_model=None, model="NousResearch/Hermes-2-Pro-Mistral-7B-GGUF", + temperature: float = 0.2, streaming=False, stop=[], max_prompt_size: int = None, @@ -239,7 +245,9 @@ def send_message_to_model_offline( assert loaded_model is None or isinstance(loaded_model, Llama), "loaded_model must be of type Llama, if configured" offline_chat_model = loaded_model or download_model(model, max_tokens=max_prompt_size) messages_dict = [{"role": message.role, "content": message.content} for message in messages] - response = offline_chat_model.create_chat_completion(messages_dict, stop=stop, stream=streaming) + response = offline_chat_model.create_chat_completion( + messages_dict, stop=stop, stream=streaming, temperature=temperature + ) if streaming: return response else: From f75606d7f57c8962b5cccd73217987ea2ec06a0d Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Sat, 27 Jul 2024 01:33:36 +0530 Subject: [PATCH 4/5] Improve doc search actor performance on vague, random or meta questions - Issue Previously the doc search actor wouldn't extract good search queries to run on user's documents for broad, vague questions. - Fix The updated extract questions prompt shows and tells the doc search actor on how to deal with such questions The doc search actor's temperature was also increased to support more creative/random questions. The previous temp of 0 was meant to encourage structured json output. But now with json mode, a low temp is not necessary to get json output --- .../conversation/anthropic/anthropic_chat.py | 3 ++- .../processor/conversation/offline/chat_model.py | 3 ++- src/khoj/processor/conversation/openai/gpt.py | 3 ++- src/khoj/processor/conversation/prompts.py | 15 +++++++++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/khoj/processor/conversation/anthropic/anthropic_chat.py b/src/khoj/processor/conversation/anthropic/anthropic_chat.py index 72bf9250..ef8539b3 100644 --- a/src/khoj/processor/conversation/anthropic/anthropic_chat.py +++ b/src/khoj/processor/conversation/anthropic/anthropic_chat.py @@ -24,7 +24,7 @@ def extract_questions_anthropic( model: Optional[str] = "claude-instant-1.2", conversation_log={}, api_key=None, - temperature=0, + temperature=0.7, location_data: LocationData = None, user: KhojUser = None, ): @@ -52,6 +52,7 @@ def extract_questions_anthropic( system_prompt = prompts.extract_questions_anthropic_system_prompt.format( current_date=today.strftime("%Y-%m-%d"), day_of_week=today.strftime("%A"), + current_month=today.strftime("%Y-%m"), last_new_year=last_new_year.strftime("%Y"), last_new_year_date=last_new_year.strftime("%Y-%m-%d"), current_new_year_date=current_new_year.strftime("%Y-%m-%d"), diff --git a/src/khoj/processor/conversation/offline/chat_model.py b/src/khoj/processor/conversation/offline/chat_model.py index 0244480f..ec4c7367 100644 --- a/src/khoj/processor/conversation/offline/chat_model.py +++ b/src/khoj/processor/conversation/offline/chat_model.py @@ -32,7 +32,7 @@ def extract_questions_offline( location_data: LocationData = None, user: KhojUser = None, max_prompt_size: int = None, - temperature: float = 0, + temperature: float = 0.7, ) -> List[str]: """ Infer search queries to retrieve relevant notes to answer user query @@ -67,6 +67,7 @@ def extract_questions_offline( chat_history=chat_history, current_date=today.strftime("%Y-%m-%d"), day_of_week=today.strftime("%A"), + current_month=today.strftime("%Y-%m"), yesterday_date=yesterday, last_year=last_year, this_year=today.year, diff --git a/src/khoj/processor/conversation/openai/gpt.py b/src/khoj/processor/conversation/openai/gpt.py index 7649a6df..a0ab38ac 100644 --- a/src/khoj/processor/conversation/openai/gpt.py +++ b/src/khoj/processor/conversation/openai/gpt.py @@ -24,7 +24,7 @@ def extract_questions( conversation_log={}, api_key=None, api_base_url=None, - temperature=0, + temperature=0.7, max_tokens=100, location_data: LocationData = None, user: KhojUser = None, @@ -52,6 +52,7 @@ def extract_questions( prompt = prompts.extract_questions.format( current_date=today.strftime("%Y-%m-%d"), day_of_week=today.strftime("%A"), + current_month=today.strftime("%Y-%m"), last_new_year=last_new_year.strftime("%Y"), last_new_year_date=last_new_year.strftime("%Y-%m-%d"), current_new_year_date=current_new_year.strftime("%Y-%m-%d"), diff --git a/src/khoj/processor/conversation/prompts.py b/src/khoj/processor/conversation/prompts.py index 6412e232..6a8db9db 100644 --- a/src/khoj/processor/conversation/prompts.py +++ b/src/khoj/processor/conversation/prompts.py @@ -208,6 +208,7 @@ Construct search queries to retrieve relevant information to answer the user's q - Add as much context from the previous questions and answers as required into your search queries. - Break messages into multiple search queries when required to retrieve the relevant information. - Add date filters to your search queries from questions and answers when required to retrieve the relevant information. +- When asked a meta, vague or random questions, search for a variety of broad topics to answer the user's question. - Share relevant search queries as a JSON list of strings. Do not say anything else. Current Date: {day_of_week}, {current_date} @@ -239,6 +240,9 @@ Khoj: ["What kind of plants do I have?", "What issues do my plants have?"] Q: Who all did I meet here yesterday? Khoj: ["Met in {location} on {yesterday_date} dt>='{yesterday_date}' dt<'{current_date}'"] +Q: Share some random, interesting experiences from this month +Khoj: ["Exciting travel adventures from {current_month}", "Fun social events dt>='{current_month}-01' dt<'{current_date}'", "Intense emotional experiences in {current_month}"] + Chat History: {chat_history} What searches will you perform to answer the following question, using the chat history as reference? Respond only with relevant search queries as a valid JSON list of strings. @@ -255,6 +259,7 @@ Construct search queries to retrieve relevant information to answer the user's q - Add as much context from the previous questions and answers as required into your search queries. - Break messages into multiple search queries when required to retrieve the relevant information. - Add date filters to your search queries from questions and answers when required to retrieve the relevant information. +- When asked a meta, vague or random questions, search for a variety of broad topics to answer the user's question. What searches will you perform to answer the users question? Respond with search queries as list of strings in a JSON object. Current Date: {day_of_week}, {current_date} @@ -281,6 +286,10 @@ Q: How many tennis balls fit in the back of a 2002 Honda Civic? Khoj: {{"queries": ["What is the size of a tennis ball?", "What is the trunk size of a 2002 Honda Civic?"]}} A: 1085 tennis balls will fit in the trunk of a Honda Civic +Q: Share some random, interesting experiences from this month +Khoj: {{"queries": ["Exciting travel adventures from {current_month}", "Fun social events dt>='{current_month}-01' dt<'{current_date}'", "Intense emotional experiences in {current_month}"]}} +A: You had a great time at the local beach with your friends, attended a music concert and had a deep conversation with your friend, Khalid. + Q: Is Bob older than Tom? Khoj: {{"queries": ["When was Bob born?", "What is Tom's age?"]}} A: Yes, Bob is older than Tom. As Bob was born on 1984-01-01 and Tom is 30 years old. @@ -307,6 +316,7 @@ Construct search queries to retrieve relevant information to answer the user's q - Add as much context from the previous questions and answers as required into your search queries. - Break messages into multiple search queries when required to retrieve the relevant information. - Add date filters to your search queries from questions and answers when required to retrieve the relevant information. +- When asked a meta, vague or random questions, search for a variety of broad topics to answer the user's question. What searches will you perform to answer the users question? Respond with a JSON object with the key "queries" mapping to a list of searches you would perform on the user's knowledge base. Just return the queries and nothing else. @@ -331,6 +341,11 @@ A: I can help you live healthier and happier across work and personal life User: Who all did I meet here yesterday? Assistant: {{"queries": ["Met in {location} on {yesterday_date} dt>='{yesterday_date}' dt<'{current_date}'"]}} A: Yesterday's note mentions your visit to your local beach with Ram and Shyam. + +User: Share some random, interesting experiences from this month +Assistant: {{"queries": ["Exciting travel adventures from {current_month}", "Fun social events dt>='{current_month}-01' dt<'{current_date}'", "Intense emotional experiences in {current_month}"]}} +A: You had a great time at the local beach with your friends, attended a music concert and had a deep conversation with your friend, Khalid. + """.strip() ) From 27ad9b130294a5f5d373223c679019f2b366c4c7 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Sat, 27 Jul 2024 01:38:53 +0530 Subject: [PATCH 5/5] Remove unused max tokns arg to extract qs func of doc search actor --- src/khoj/processor/conversation/openai/gpt.py | 1 - src/khoj/routers/api.py | 1 - 2 files changed, 2 deletions(-) diff --git a/src/khoj/processor/conversation/openai/gpt.py b/src/khoj/processor/conversation/openai/gpt.py index a0ab38ac..5dad883b 100644 --- a/src/khoj/processor/conversation/openai/gpt.py +++ b/src/khoj/processor/conversation/openai/gpt.py @@ -25,7 +25,6 @@ def extract_questions( api_key=None, api_base_url=None, temperature=0.7, - max_tokens=100, location_data: LocationData = None, user: KhojUser = None, ): diff --git a/src/khoj/routers/api.py b/src/khoj/routers/api.py index 5f89cb72..d7e26d9e 100644 --- a/src/khoj/routers/api.py +++ b/src/khoj/routers/api.py @@ -359,7 +359,6 @@ async def extract_references_and_questions( conversation_log=meta_log, location_data=location_data, user=user, - max_tokens=conversation_config.max_prompt_size, ) elif conversation_config.model_type == ChatModelOptions.ModelType.ANTHROPIC: api_key = conversation_config.openai_config.api_key