Spaces:
Sleeping
Sleeping
Download src/utils/message_builder.py from ABAO77/chatbot-rag-fi: direct link, hf CLI and curl.
- Browser
- Download file 989 Bytes
-
https://huggingface.co/spaces/ABAO77/chatbot-rag-fi/resolve/main/src/utils/message_builder.py
- Command line
-
hf download hf://spaces/ABAO77/chatbot-rag-fi/src/utils/message_builder.py
-
curl -L -o message_builder.py https://huggingface.co/spaces/ABAO77/chatbot-rag-fi/resolve/main/src/utils/message_builder.py
989 Bytes
| from __future__ import annotations | |
| from src.config import get_settings | |
| from src.schemas import ChatHistoryMessage | |
| from src.utils.agent_utils import redact_personal_info | |
| class MessageBuilder: | |
| def trim_history(history: list[ChatHistoryMessage]) -> list[ChatHistoryMessage]: | |
| settings = get_settings() | |
| if len(history) <= settings.conversation_history_window: | |
| return history | |
| trimmed = history[-settings.conversation_history_window :] | |
| while trimmed and trimmed[0].role == "assistant": | |
| trimmed = trimmed[1:] | |
| return trimmed | |
| def build_input_items(history: list[ChatHistoryMessage], question: str) -> list[dict[str, str]]: | |
| trimmed = MessageBuilder.trim_history(history) | |
| items = [{"role": item.role, "content": redact_personal_info(item.content)} for item in trimmed] | |
| items.append({"role": "user", "content": redact_personal_info(question)}) | |
| return items | |