-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨feat(chat): add edit button Edit history title * 💄 ux(chat): add title to page header * 🩹 fix(chat): tidy phrases * ⚡️ ref(chat): move view code to views and some tidying * fix(chat): rename edit label to rename_label --------- Co-authored-by: zilaei <zzilaei@gmail.com>
- Loading branch information
Showing
9 changed files
with
181 additions
and
75 deletions.
There are no files selected for viewing
41 changes: 0 additions & 41 deletions
41
fai-rag-app/fai-backend/fai_backend/assistant/chat_state.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from fai_backend.new_chat.service import ChatStateService | ||
from fai_backend.repositories import chat_history_repo | ||
|
||
|
||
async def get_chat_state_service(): | ||
return ChatStateService(chat_history_repo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from pydantic import BaseModel | ||
|
||
from fai_backend.assistant.models import LLMClientChatMessage | ||
|
||
|
||
class ClientChatState(BaseModel): | ||
user: str | ||
chat_id: str | ||
timestamp: str | ||
title: str | ||
delete_label: str = "Delete" # TODO: fix hack for allowing something to show up in list to click on. | ||
rename_label: str = "Rename" # TODO: fix hack for allowing something to show up in list to click on. | ||
history: list[LLMClientChatMessage] | ||
|
||
|
||
class ChatHistoryEditPayload(BaseModel): | ||
chat_id: str | ||
title: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from typing import List | ||
|
||
from fai_backend.assistant.models import (AssistantChatHistoryModel, AssistantStreamMessage, LLMClientChatMessage) | ||
from fai_backend.new_chat.models import ClientChatState | ||
from fai_backend.repositories import ChatHistoryRepository, chat_history_repo | ||
from fai_backend.repository.query.component import AttributeAssignment | ||
|
||
|
||
class ChatStateService: | ||
def __init__(self, history_repo: ChatHistoryRepository): | ||
self.history_repo = history_repo | ||
|
||
async def get_states(self, user: str) -> List[ClientChatState]: | ||
results = await self.history_repo.list(query=AttributeAssignment('user', user)) | ||
return [ChatStateService.assistant_chat_history_model_to_chat_state(r) for r in results if len(r.history) > 0] | ||
|
||
async def get_state(self, chat_id: str) -> ClientChatState: | ||
history = await self.history_repo.get(chat_id) | ||
return ChatStateService.assistant_chat_history_model_to_chat_state(history) | ||
|
||
async def delete_state(self, chat_id: str) -> None: | ||
await self.history_repo.delete(chat_id) | ||
|
||
async def update_state(self, chat_id: str, new_state: ClientChatState) -> None: | ||
new_history = await self.chat_state_to_assistant_chat_history_model(new_state) | ||
ignore_keys = ['id', 'timestamp'] | ||
|
||
await self.history_repo.update(chat_id, {key: new_history.model_dump()[key] | ||
for key in filter(lambda key: key not in ignore_keys, | ||
new_history.model_dump().keys())}) | ||
|
||
async def chat_state_to_assistant_chat_history_model(self, | ||
chat_state: ClientChatState) -> AssistantChatHistoryModel: | ||
supplementary_history = await self.history_repo.get(chat_state.chat_id) | ||
return AssistantChatHistoryModel(user=chat_state.user, | ||
title=chat_state.title, | ||
assistant=supplementary_history.assistant, | ||
history=[AssistantStreamMessage( | ||
timestamp=a.timestamp, | ||
role=a.source, | ||
content=a.content) for a in chat_state.history]) | ||
|
||
@staticmethod | ||
def assistant_chat_history_model_to_chat_state(chat_history_model: AssistantChatHistoryModel) -> ClientChatState: | ||
def convert_message(m: AssistantStreamMessage) -> LLMClientChatMessage: | ||
return LLMClientChatMessage(timestamp=m.timestamp, source=m.role, content=m.content) | ||
|
||
title = chat_history_model.title | ||
default_title = title if title else chat_history_model.history[0].content[ | ||
:30] + '...' # TODO: replace title with pre-generated AI title | ||
|
||
return ClientChatState(user=chat_history_model.user, | ||
chat_id=str(chat_history_model.id), | ||
timestamp=chat_history_model.history[0].timestamp, | ||
title=default_title, | ||
history=[convert_message(message) for message in chat_history_model.history]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from fai_backend.framework import components as c | ||
from fai_backend.framework import events as e | ||
from fai_backend.framework.display import DisplayAs | ||
from fai_backend.framework.table import DataColumn | ||
from fai_backend.new_chat.models import ClientChatState | ||
from fai_backend.phrase import phrase as _ | ||
|
||
|
||
async def chat_history_edit_view(view, | ||
chat_history: ClientChatState, | ||
submit_url: str) -> list: | ||
return view( | ||
[c.Div(components=[ | ||
c.Div(components=[ | ||
c.Form( | ||
submit_url=submit_url, | ||
method='PATCH', | ||
submit_text=_('update_chat_history_title_button', 'Update title'), | ||
components=[ | ||
c.InputField( | ||
name='title', | ||
title=_('input_title_label', 'Edit title'), | ||
placeholder=_('input_title_placeholder', 'Enter new title here'), | ||
required=True, | ||
html_type='text', | ||
), | ||
c.InputField( | ||
name='chat_id', | ||
value=chat_history.chat_id, | ||
hidden=True, | ||
html_type='hidden', | ||
) | ||
], | ||
) | ||
], class_name='card-body'), | ||
], class_name='card')], | ||
_('edit_chat_history_title', f'Chat history - Edit ({chat_history.title})'), | ||
) | ||
|
||
|
||
async def chat_history_list_view(view, states: list[ClientChatState]) -> list: | ||
return view( | ||
[c.DataTable(data=states, | ||
columns=[DataColumn(key='title', | ||
id='title', | ||
width=100, | ||
display=DisplayAs.link, | ||
on_click=e.GoToEvent(url='/chat/{chat_id}'), | ||
sortable=True, | ||
label=_('title', 'Title')), | ||
DataColumn(key='rename_label', | ||
display=DisplayAs.link, | ||
on_click=e.GoToEvent(url='/chat/edit/{chat_id}'), | ||
label=_('actions', 'Action')), | ||
DataColumn(key='delete_label', | ||
width=1, | ||
display=DisplayAs.link, | ||
on_click=e.GoToEvent(url='/chat/delete/{chat_id}'), | ||
label=_('actions', 'Action'))], | ||
include_view_action=False)], | ||
_('chat_history', 'Chat history') | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters