Spaces:
Sleeping
Sleeping
| import sys | |
| import os | |
| _root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| if _root not in sys.path: | |
| sys.path.insert(0, _root) | |
| import importlib.metadata | |
| import chainlit as cl | |
| def _log_versions() -> None: | |
| _PACKAGES = [ | |
| "langchain", "langchain-google-genai", "langchain-community", | |
| "langgraph", "chainlit", "qdrant-client", | |
| "tavily-python", "wikipedia", "neo4j", | |
| ] | |
| lines = [] | |
| for pkg in _PACKAGES: | |
| try: | |
| lines.append(f" {pkg}=={importlib.metadata.version(pkg)}") | |
| except importlib.metadata.PackageNotFoundError: | |
| lines.append(f" {pkg}==NOT_FOUND") | |
| print(f"[startup] Python {sys.version}\n[startup] packages:\n" + "\n".join(lines), flush=True) | |
| _log_versions() | |
| from src.core import final_answer, restore_from_qdrant | |
| from src.memory.session import get_store | |
| _WELCOME_MESSAGE = """\ | |
| Chào bạn! Mình là **Logi-chan** 👋, trợ lý ảo hỗ trợ đọc sách và onboard lớp Logic. | |
| Bạn có thể hỏi mình những câu như: | |
| - 📚 *"Muốn học section 3.2 thì phải học những section nào trước?"* | |
| - 🧩 *"Đọc section 2.5 cần nắm concept nào trước?"* | |
| - 🔍 *"Fallacy of relevance bao gồm những fallacy nào?"* | |
| - 💡 *"Để hiểu hasty generalization cần biết gì trước?"* | |
| - 🗂️ *"Tìm các concept liên quan đến 'ad hominem'"* | |
| - 📖 *"Missing the point nằm ở trang nào?"* | |
| - 🛠️ *"Hướng dẫn đăng ký tài khoản Library4GZ"* | |
| Nhấn Readme để biết thêm thông tin về cách sử dụng nhé! | |
| Cứ hỏi tự nhiên nha, mình sẽ tích cực hỗ trợ bạn! | |
| """ | |
| async def on_chat_start(): | |
| existing_id = cl.user_session.get("session_id") | |
| if existing_id: | |
| # Reconnection within the same Chainlit session — restore LangGraph state from Qdrant | |
| await restore_from_qdrant(existing_id) | |
| return | |
| store = get_store() | |
| session_id = store.new_session() | |
| cl.user_session.set("session_id", session_id) | |
| await cl.Message(content=_WELCOME_MESSAGE, author="Logi-chan").send() | |
| async def on_message(message: cl.Message): | |
| session_id: str = cl.user_session.get("session_id") | |
| store = get_store() | |
| store.log_turn(session_id, "user", message.content) | |
| async with cl.Step(name="Thinking", show_input=False) as step: | |
| response = await final_answer(message.content, session_id) | |
| step.output = "" | |
| store.log_turn(session_id, "assistant", response) | |
| await cl.Message(content=response, author="Logi-chan").send() | |
| async def on_chat_end(): | |
| session_id: str = cl.user_session.get("session_id") | |
| if session_id: | |
| store = get_store() | |
| store.touch(session_id) | |