# Mẫu Bắt Đầu Phiên Mỗi phiên LLM agent nên tuân theo trình tự khởi động chuẩn này. Bỏ qua bước dẫn đến mất ngữ cảnh, bỏ sót tin nhắn và quên tác vụ. ## Mẫu ``` 1. Recall all memories 2. Poll for unread chat messages 3. Check in-progress tasks 4. Build context from results 5. Process pending items before new work ``` ## Triển khai ### Bước 1: Thu hồi tất cả bộ nhớ > [!CRITICAL] > Đây là lệnh gọi quan trọng nhất. Không có nó, bạn không có bộ nhớ về các > phiên trước. ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/memory/recall ``` Trả về tóm tắt văn bản thuần của tất cả bộ nhớ, sắp xếp theo ưu tiên. ### Bước 2: Poll tin nhắn chat chưa đọc ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/chat/poll ``` Trả về tin nhắn chưa đọc từ con người. **Tự động đánh dấu là đã đọc.** ### Bước 3: Kiểm tra tác vụ đang tiến hành ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ "https://synapse.schaefer.zone/mind/tasks?status=in_progress" ``` Trả về tác vụ bạn đang làm phiên trước. ### Bước 4: Xây dựng ngữ cảnh Kết hợp ba phản hồi vào system prompt của bạn: ```python def build_context(memories, messages, tasks): context = f"""# SESSION CONTEXT ## Memories (from previous sessions) {memories} ## Unread Messages from Human {format_messages(messages)} ## Active Tasks {format_tasks(tasks)} ## Instructions - Address unread messages first - Resume active tasks before starting new work - Store new learnings as they happen (POST /memory) - Poll for new messages every 30-60 seconds """ return context ``` ### Bước 5: Xử lý mục đang chờ ``` For each unread message: - Acknowledge receipt (POST /chat/reply) - Address the message content - Store any new commitments as memories For each in-progress task: - Recall why you were working on it - Continue from where you left off - Update task status as you progress ``` ## Ví dụ hoàn chỉnh ```python import os import requests URL = "https://synapse.schaefer.zone" KEY = os.environ["SYNAPSE_MIND_KEY"] def session_start(): """Canonical session start sequence.""" headers = {"Authorization": f"Bearer {KEY}"} # 1. Recall memories r = requests.get(f"{URL}/memory/recall", headers=headers) memories = r.text # 2. Poll chat r = requests.get(f"{URL}/chat/poll", headers=headers) messages = r.json().get("messages", []) # 3. Check tasks r = requests.get(f"{URL}/mind/tasks?status=in_progress", headers=headers) tasks = r.json().get("tasks", []) # 4. Build context context = f"""You are a Synapse-enabled AI assistant. MEMORIES FROM PREVIOUS SESSIONS: {memories} UNREAD MESSAGES FROM HUMAN: {chr(10).join(f'- {m["content"]}' for m in messages) or 'None'} ACTIVE TASKS: {chr(10).join(f'- [{t["id"]}] {t["title"]}: {t.get("description", "")}' for t in tasks) or 'None'} INSTRUCTIONS: 1. Acknowledge each unread message 2. Resume active tasks 3. Store new learnings via POST /memory 4. Poll /chat/poll every 30-60 seconds """ return context # At session start system_prompt = session_start() # Pass to LLM... ``` ## Sai lầm phổ biến > [!WARNING] > - **Bỏ qua thu hồi** — bạn bắt đầu không có ngữ cảnh, lặp lại lỗi quá khứ > - **Quên poll chat** — tin nhắn của con người không được trả lời > - **Bỏ qua tác vụ đang hoạt động** — công việc bị quên giữa thực thi > - **Không lưu gì** — phiên không tạo giá trị cố định ## Biến thể ### Mẫu tối thiểu (LLM ngữ cảnh thấp) Cho LLM với cửa sổ ngữ cảnh nhỏ, bỏ qua thu hồi đầy đủ: ```bash # Just get stats, not full content curl -H "Authorization: Bearer $KEY" .../memory/stats ``` Sau đó tìm các chủ đề cụ thể khi cần: ```bash curl -H "Authorization: Bearer $KEY" ".../memory/search?q=current+project" ``` ### Mẫu tích cực (agent chạy dài) Cho agent chạy trong nhiều giờ, thêm thu hồi lại định kỳ: ```python while working: if time.time() - last_recall > 3600: # every hour memories = recall() last_recall = time.time() # ... do work ... ``` ## Bước tiếp theo - [Chiến lược gắn tag bộ nhớ](/docs/llm-cookbook/memory-tagging-strategy) - [Quy trình điều khiển bởi tác vụ](/docs/llm-cookbook/task-driven-workflow) - [Mẫu Polling Chat](/docs/llm-cookbook/chat-polling-pattern)