Move construct_chat_history method back to conversation.utils.py

This commit is contained in:
sabaimran 2024-11-09 18:27:46 -08:00
parent bc95a99fb4
commit e5ac076fc4
4 changed files with 38 additions and 32 deletions

View file

@ -524,6 +524,35 @@ def get_image_from_url(image_url: str, type="pil"):
return ImageWithType(content=None, type=None) return ImageWithType(content=None, type=None)
def construct_chat_history(conversation_history: dict, n: int = 4, agent_name="AI") -> str:
chat_history = ""
for chat in conversation_history.get("chat", [])[-n:]:
if chat["by"] == "khoj" and chat["intent"].get("type") in ["remember", "reminder", "summarize"]:
chat_history += f"User: {chat['intent']['query']}\n"
if chat["intent"].get("inferred-queries"):
chat_history += f'{agent_name}: {{"queries": {chat["intent"].get("inferred-queries")}}}\n'
chat_history += f"{agent_name}: {chat['message']}\n\n"
elif chat["by"] == "khoj" and ("text-to-image" in chat["intent"].get("type")):
chat_history += f"User: {chat['intent']['query']}\n"
chat_history += f"{agent_name}: [generated image redacted for space]\n"
elif chat["by"] == "khoj" and ("excalidraw" in chat["intent"].get("type")):
chat_history += f"User: {chat['intent']['query']}\n"
chat_history += f"{agent_name}: {chat['intent']['inferred-queries'][0]}\n"
elif chat["by"] == "you":
raw_attached_files = chat.get("attachedFiles")
if raw_attached_files:
attached_files: Dict[str, str] = {}
for file in raw_attached_files:
attached_files[file["name"]] = file["content"]
attached_file_context = gather_raw_attached_files(attached_files)
chat_history += f"User: {attached_file_context}\n"
return chat_history
def commit_conversation_trace( def commit_conversation_trace(
session: list[ChatMessage], session: list[ChatMessage],
response: str | list[dict], response: str | list[dict],

View file

@ -10,8 +10,13 @@ import aiohttp
from khoj.database.adapters import ais_user_subscribed from khoj.database.adapters import ais_user_subscribed
from khoj.database.models import Agent, KhojUser from khoj.database.models import Agent, KhojUser
from khoj.processor.conversation import prompts from khoj.processor.conversation import prompts
from khoj.processor.conversation.utils import ChatEvent, clean_code_python, clean_json from khoj.processor.conversation.utils import (
from khoj.routers.helpers import construct_chat_history, send_message_to_model_wrapper ChatEvent,
clean_code_python,
clean_json,
construct_chat_history,
)
from khoj.routers.helpers import send_message_to_model_wrapper
from khoj.utils.helpers import timer from khoj.utils.helpers import timer
from khoj.utils.rawconfig import LocationData from khoj.utils.rawconfig import LocationData

View file

@ -91,6 +91,7 @@ from khoj.processor.conversation.utils import (
ChatEvent, ChatEvent,
ThreadedGenerator, ThreadedGenerator,
clean_json, clean_json,
construct_chat_history,
generate_chatml_messages_with_context, generate_chatml_messages_with_context,
save_to_conversation_log, save_to_conversation_log,
) )
@ -270,35 +271,6 @@ def gather_raw_attached_files(
return f"I have attached the following files:\n\n{contextual_data}" return f"I have attached the following files:\n\n{contextual_data}"
def construct_chat_history(conversation_history: dict, n: int = 4, agent_name="AI") -> str:
chat_history = ""
for chat in conversation_history.get("chat", [])[-n:]:
if chat["by"] == "khoj" and chat["intent"].get("type") in ["remember", "reminder", "summarize"]:
chat_history += f"User: {chat['intent']['query']}\n"
if chat["intent"].get("inferred-queries"):
chat_history += f'{agent_name}: {{"queries": {chat["intent"].get("inferred-queries")}}}\n'
chat_history += f"{agent_name}: {chat['message']}\n\n"
elif chat["by"] == "khoj" and ("text-to-image" in chat["intent"].get("type")):
chat_history += f"User: {chat['intent']['query']}\n"
chat_history += f"{agent_name}: [generated image redacted for space]\n"
elif chat["by"] == "khoj" and ("excalidraw" in chat["intent"].get("type")):
chat_history += f"User: {chat['intent']['query']}\n"
chat_history += f"{agent_name}: {chat['intent']['inferred-queries'][0]}\n"
elif chat["by"] == "you":
raw_attached_files = chat.get("attachedFiles")
if raw_attached_files:
attached_files: Dict[str, str] = {}
for file in raw_attached_files:
attached_files[file["name"]] = file["content"]
attached_file_context = gather_raw_attached_files(attached_files)
chat_history += f"User: {attached_file_context}\n"
return chat_history
async def acreate_title_from_history( async def acreate_title_from_history(
user: KhojUser, user: KhojUser,
conversation: Conversation, conversation: Conversation,

View file

@ -11,6 +11,7 @@ from khoj.processor.conversation import prompts
from khoj.processor.conversation.utils import ( from khoj.processor.conversation.utils import (
InformationCollectionIteration, InformationCollectionIteration,
clean_json, clean_json,
construct_chat_history,
construct_iteration_history, construct_iteration_history,
construct_tool_chat_history, construct_tool_chat_history,
) )
@ -19,7 +20,6 @@ from khoj.processor.tools.run_code import run_code
from khoj.routers.api import extract_references_and_questions from khoj.routers.api import extract_references_and_questions
from khoj.routers.helpers import ( from khoj.routers.helpers import (
ChatEvent, ChatEvent,
construct_chat_history,
generate_summary_from_files, generate_summary_from_files,
send_message_to_model_wrapper, send_message_to_model_wrapper,
) )