From a6905a9f0cea8d30662094408020c75467dce3b0 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Wed, 9 Oct 2024 19:00:54 -0700 Subject: [PATCH] Pass background context to iterating chat director --- src/khoj/processor/conversation/prompts.py | 8 ++++++-- src/khoj/routers/api_chat.py | 1 + src/khoj/routers/research.py | 15 ++++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/khoj/processor/conversation/prompts.py b/src/khoj/processor/conversation/prompts.py index 6ef1cb94..ae4f8230 100644 --- a/src/khoj/processor/conversation/prompts.py +++ b/src/khoj/processor/conversation/prompts.py @@ -498,6 +498,11 @@ You are an extremely methodical planner. Your goal is to make a plan to execute If you already know the answer to the question, return an empty response, e.g., {{}}. +Background Context: +- Current Date: {day_of_week}, {current_date} +- User's Location: {location} +- {username} + Which of the data sources listed below you would use to answer the user's question? You **only** have access to the following data sources: {tools} @@ -523,8 +528,7 @@ previous_iteration = PromptTemplate.from_template( data_source: {data_source} query: {query} summary: {summary} ---- -""".strip() +---""" ) pick_relevant_information_collection_tools = PromptTemplate.from_template( diff --git a/src/khoj/routers/api_chat.py b/src/khoj/routers/api_chat.py index b5394d8d..505c0248 100644 --- a/src/khoj/routers/api_chat.py +++ b/src/khoj/routers/api_chat.py @@ -713,6 +713,7 @@ async def chat( uploaded_image_url=uploaded_image_url, agent=agent, send_status_func=partial(send_event, ChatEvent.STATUS), + user_name=user_name, location=location, file_filters=conversation.file_filters if conversation else [], ): diff --git a/src/khoj/routers/research.py b/src/khoj/routers/research.py index b3327f35..028fffb7 100644 --- a/src/khoj/routers/research.py +++ b/src/khoj/routers/research.py @@ -1,5 +1,6 @@ import json import logging +from datetime import datetime from typing import Any, Callable, Dict, List, Optional from fastapi import Request @@ -48,6 +49,8 @@ async def apick_next_tool( conversation_history: dict, subscribed: bool, uploaded_image_url: str = None, + location: LocationData = None, + user_name: str = None, agent: Agent = None, previous_iterations: List[InformationCollectionIteration] = None, ): @@ -84,12 +87,21 @@ async def apick_next_tool( prompts.personality_context.format(personality=agent.personality) if agent and agent.personality else "" ) + # Extract Past User Message and Inferred Questions from Conversation Log + today = datetime.today() + location_data = f"{location}" if location else "Unknown" + username = prompts.user_name.format(name=user_name) if user_name else "" + # TODO Add current date/time to the query function_planning_prompt = prompts.plan_function_execution.format( query=query, tools=tool_options_str, chat_history=chat_history, personality_context=personality_context, + current_date=today.strftime("%Y-%m-%d"), + day_of_week=today.strftime("%A"), + username=username, + location=location_data, previous_iterations=previous_iterations_history, ) @@ -135,6 +147,7 @@ async def execute_information_collection( uploaded_image_url: str = None, agent: Agent = None, send_status_func: Optional[Callable] = None, + user_name: str = None, location: LocationData = None, file_filters: List[str] = [], ): @@ -150,7 +163,7 @@ async def execute_information_collection( result: str = "" this_iteration = await apick_next_tool( - query, conversation_history, subscribed, uploaded_image_url, agent, previous_iterations + query, conversation_history, subscribed, uploaded_image_url, location, user_name, agent, previous_iterations ) if this_iteration.data_source == ConversationCommand.Notes: ## Extract Document References