依赖安装
根据 requirements.txt 文件,安装项目所需要的核心依赖。
包名 | 版本要求 | 功能说明 |
crewai | >=0.80.0 | AI 代理协作框架,用于构建多代理系统 |
openai | >=1.0.0 | OpenAI API 客户端,用于模型调用 |
pydantic | >=2.0.0 | 数据验证和序列化库 |
crewai>=0.80.0openai>=1.0.0pydantic>=2.0.0tdaimemory # TDAI Memory SDK
运行以下命令安装依赖包。
pip3 install -r requirements.txt
配置文件 (config.py)
在 config.py 文件中分别配置模型服务(生成回复)和 Memory 服务(存储和检索记忆)相关参数。
# config.py# 模型服务配置 (兼容 OpenAI SDK)MODEL_CONFIG = {"base_url": "https://api.hunyuan.cloud.tencent.com/v1/", # 请填写实际模型服务请求地址"api_key": "your_model_api_key", # 请填写对应的模型服务API Key"model": "model_name" # 请填写模型名称}# Memory 服务配置MEMORY_CONFIG = {"endpoint": "https://memory.tdai.tencentyun.com","api_key": "your_memory_api_key","memory_id": "tdai-mem-xxxxxx","actor_id": "user_a"}
Running Environment
Operating System: Ubuntu 24.04.3 LTS / x86_64
Runtime Version: Python 3.11.1
工具封装说明
在 memory_tool.py 中,初始化全局 MemoryClient,获取配置文件中 Memory 服务配置,并提供记忆搜索服务封装的函数 search_user_memory。
# memory_tool.pyimport jsonfrom tdaimemory import MemoryClientfrom config import MEMORY_CONFIG# 创建全局 Memory 客户端_memory_client = MemoryClient(endpoint=MEMORY_CONFIG["endpoint"],api_key=MEMORY_CONFIG["api_key"],memory_id=MEMORY_CONFIG["memory_id"])def search_user_memory(query: str, actor_id: str, limit: int = 10, memory_type: str = "persona") -> str:"""搜索用户的历史记忆"""try:result = _memory_client.search_records(actor_id=actor_id,query=query,type=memory_type,limit=limit,enable_rerank=True)if not result.get("records"):return "未找到相关的用户记忆信息。"formatted_records = []for record in result.get("records", []):formatted_records.append({"content": record.get("record_content", ""),"type": record.get("type", ""),"updated_time": record.get("updated_time", "")})return json.dumps({"total_count": result.get("total_count", 0),"records": formatted_records}, ensure_ascii=False, indent=2)except Exception as e:return f"搜索记忆时发生错误: {str(e)}"