diff --git a/.dockerignore b/.dockerignore index a2e4e89..ccf4b90 100644 --- a/.dockerignore +++ b/.dockerignore @@ -53,4 +53,5 @@ projects/_cache/ # Docker Dockerfile docker-compose.yml -.dockerignore \ No newline at end of file +.dockerignore +worktree \ No newline at end of file diff --git a/.gitignore b/.gitignore index a893b9e..401af2b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ __pycache__ */__pycache__ models projects/queue_data +worktree diff --git a/CLAUDE.md b/CLAUDE.md index 1f805bb..81fed20 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -35,4 +35,25 @@ curl --request POST \ ## 添加依赖 如果需要添加依赖,需要在 pyproject.toml 里添加,然后执行 poetry install 来安装。 -并执行`poetry export -f requirements.txt -o requirements.txt --without-hashes`来更新 requirements.txt 文件。 \ No newline at end of file +并执行`poetry export -f requirements.txt -o requirements.txt --without-hashes`来更新 requirements.txt 文件。 + + +## Git Worktree 管理 + +用于在多个分支间并行工作,无需频繁切换分支。 + +### 创建新的 worktree 和分支 + +```bash +# 1. 创建 worktree 目录(如果不存在) +mkdir -p worktrees + +# 2. 创建新的 worktree +git worktree add worktrees/branch-name branch-name + +# 3. 切换到新的 worktree 目录 +cd worktrees/branch-name + +# 4. 确认当前分支 +git branch # 应该显示 branch-name +``` \ No newline at end of file diff --git a/agent/mem0_config.py b/agent/mem0_config.py index dc17acd..8c05d1e 100644 --- a/agent/mem0_config.py +++ b/agent/mem0_config.py @@ -3,7 +3,9 @@ Mem0 配置数据类 用于管理 Mem0 长期记忆系统的配置参数 """ +from datetime import datetime from dataclasses import dataclass +from pathlib import Path from typing import TYPE_CHECKING, Optional # 避免循环导入 @@ -11,6 +13,38 @@ if TYPE_CHECKING: from langchain_core.language_models import BaseChatModel +# 缓存已加载的提示词模板 +_fact_extraction_prompt_template: Optional[str] = None + + +def _load_fact_extraction_prompt() -> str: + """从 prompt/FACT_RETRIEVAL_PROMPT.md 加载提示词模板 + + Returns: + str: 提示词模板内容 + """ + global _fact_extraction_prompt_template + + if _fact_extraction_prompt_template is not None: + return _fact_extraction_prompt_template + + # 获取项目根目录(假设 prompt/ 在项目根目录下) + project_root = Path(__file__).parent.parent + prompt_file = project_root / "prompt" / "FACT_RETRIEVAL_PROMPT.md" + + try: + _fact_extraction_prompt_template = prompt_file.read_text(encoding="utf-8") + except FileNotFoundError: + # 如果文件不存在,返回默认提示词 + _fact_extraction_prompt_template = ( + "Extract relevant facts about the user from the conversation.\n" + "Today's date is {current_time}.\n" + "Return response in JSON format: {\"facts\": [\"fact1\", \"fact2\"]}" + ) + + return _fact_extraction_prompt_template + + @dataclass class Mem0Config: """Mem0 长期记忆配置类""" @@ -69,6 +103,18 @@ class Mem0Config: memory_text = "\n".join(f"- {m}" for m in memories) return self.memory_prompt_template.format(memories=memory_text) + def get_custom_fact_extraction_prompt(self) -> str: + """获取自定义记忆提取提示词 + + 从 prompt/FACT_RETRIEVAL_PROMPT.md 读取模板并替换 {current_time} 为当前日期 + + Returns: + str: 自定义记忆提取提示词 + """ + template = _load_fact_extraction_prompt() + current_date = datetime.now().strftime("%Y-%m-%d") + return template.format(current_time=current_date) + def with_session(self, session_id: str) -> "Mem0Config": """创建带有新 session_id 的配置副本 diff --git a/agent/mem0_manager.py b/agent/mem0_manager.py index 5865e46..96a020a 100644 --- a/agent/mem0_manager.py +++ b/agent/mem0_manager.py @@ -245,7 +245,9 @@ class Mem0Manager: # 配置 Mem0 使用 Pgvector # 注意:这里使用 huggingface_base_url 来绕过本地模型加载 # 设置一个假的 base_url,这样 HuggingFaceEmbedding 就不会加载 SentenceTransformer + config_dict = { + "custom_fact_extraction_prompt": config.get_custom_fact_extraction_prompt(), "vector_store": { "provider": "pgvector", "config": {