mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-02 13:18:18 +00:00
Pass deep typed chat history for more ergonomic, readable, safe code
The chat dictionary is an artifact from earlier non-db chat history storage. We've been ensuring new chat messages have valid type before being written to DB for more than 6 months now. Move to using the deeply typed chat history helps avoids null refs, makes code more readable and easier to reason about. Next Steps: The current update entangles chat_history written to DB with any virtual chat history message generated for intermediate steps. The chat message type written to DB should be decoupled from type that can be passed to AI model APIs (maybe?). For now we've made the ChatMessage.message type looser to allow for list[dict] type (apart from string). But later maybe a good idea to decouple the chat_history recieved by send_message_to_model from the chat_history saved to DB (which can then have its stricter type check)
This commit is contained in:
@@ -6,6 +6,7 @@ from django.utils.timezone import make_aware
|
||||
|
||||
from khoj.database.models import (
|
||||
AiModelApi,
|
||||
ChatMessageModel,
|
||||
ChatModel,
|
||||
Conversation,
|
||||
KhojApiUser,
|
||||
@@ -46,15 +47,15 @@ def get_chat_api_key(provider: ChatModel.ModelType = None):
|
||||
|
||||
def generate_chat_history(message_list):
|
||||
# Generate conversation logs
|
||||
conversation_log = {"chat": []}
|
||||
chat_history: list[ChatMessageModel] = []
|
||||
for user_message, chat_response, context in message_list:
|
||||
message_to_log(
|
||||
user_message,
|
||||
chat_response,
|
||||
{"context": context, "intent": {"query": user_message, "inferred-queries": f'["{user_message}"]'}},
|
||||
conversation_log=conversation_log.get("chat", []),
|
||||
chat_history=chat_history,
|
||||
)
|
||||
return conversation_log
|
||||
return chat_history
|
||||
|
||||
|
||||
class UserFactory(factory.django.DjangoModelFactory):
|
||||
|
||||
Reference in New Issue
Block a user