agent学习之qdrant记忆

1. 环境

# LLM_MODEL_ID="YOUR-MODEL"
# LLM_API_KEY="YOUR-API-KEY"
# LLM_BASE_URL="YOUR-URL"
# SERPAPI_API_KEY="YOUR_SERPAPI_API_KEY"

LLM_API_KEY = "w8e"  # 你的API密钥
LLM_BASE_URL = "http://aigc.cn-***.skyengine.net.cn/eliza/v1"  # 自定义API端点(如本地或代理)
LLM_MODEL_ID = 'qwen3-32b' # 推荐
LLM_MODEL_ID="gpt-3.5-turbo" # 推荐
LLM_TIMEOUT = 50

SERPAPI_API_KEY = "5d**233"


MODELSCOPE_API_KEY = "wang6:**8e"

# 嵌入模型(记忆/RAG 用,与 LLM 配置独立)
# 方案A(推荐先跑通):0.6B 体积小、易下载
EMBED_MODEL_TYPE=local
# EMBED_MODEL_NAME=Qwen/Qwen3-Embedding-0.6B
EMBED_MODEL_NAME=/data/model_llm/Qwen/models/Qwen3-Embedding-8B

# 方案B(8B,需约16GB显存 + 完整下载官方模型,不要用 dengcao 镜像):
# EMBED_MODEL_NAME=Qwen/Qwen3-Embedding-8B
# 国内可先: export HF_ENDPOINT=https://www.modelscope.cn

# 方案C(最快验证,不下载大模型):
# EMBED_MODEL_TYPE=tfidf
# EMBED_MODEL_NAME=

# ==========================
# Qdrant 向量库(episodic/semantic 记忆需要)
# ==========================
# 本地需先启动: docker run -p 6333:6333 qdrant/qdrant
# QDRANT_URL=http://localhost:6333

# 或使用 Qdrant Cloud(推荐无 Docker 环境):
# QDRANT_URL=https://your-cluster.qdrant.io:6333
QDRANT_URL=https://588.us-east-2-0.aws.cloud.qdrant.io
QDRANT_API_KEY=eyhsA
QDRANT_COLLECTION=hello_agents_vectors
QDRANT_VECTOR_SIZE=4096
QDRANT_DISTANCE=cosine

注册:

Vector Search Database · Qdrant Cloud

2. 测试脚本

# test_memory_tool.py
# 配置好同级文件夹下.env中的大模型API
# 必须先加载 .env,再 import hello_agents(否则 SemanticMemory 会用错误的 Qdrant 配置)
from dotenv import load_dotenv
load_dotenv()

from hello_agents import SimpleAgent, HelloAgentsLLM, ToolRegistry
from hello_agents.tools import MemoryTool, RAGTool
from hello_agents.core.database_config import DatabaseConfig, update_database_config

# 刷新数据库配置单例,确保读到 QDRANT_URL / NEO4J_URI 等
update_database_config(qdrant=DatabaseConfig.from_env().qdrant.model_dump())

# 创建LLM实例
llm = HelloAgentsLLM()

# 创建Agent
agent = SimpleAgent(
    name="智能助手",
    llm=llm,
    system_prompt="你是一个有记忆和知识检索能力的AI助手"
)

# 创建工具注册表
tool_registry = ToolRegistry()

# 添加记忆工具
import os
_memory_types = ["working", "episodic"]
# semantic 还需 Neo4j;配置 NEO4J_URI 后可取消下行注释
if os.getenv("NEO4J_URI") and os.getenv("NEO4J_PASSWORD"):
    _memory_types.append("semantic")
elif not os.getenv("QDRANT_URL") and not os.getenv("QDRANT_API_KEY"):
    _memory_types = ["working"]
    print("提示: 未配置 Qdrant,仅启用 working 记忆。")
else:
    print("提示: 未配置 Neo4j,已启用 working + episodic(未启用 semantic)。")
memory_tool = MemoryTool(user_id="user123", memory_types=_memory_types)
tool_registry.register_tool(memory_tool)

# 添加RAG工具
rag_tool = RAGTool(knowledge_base_path="./knowledge_base")
tool_registry.register_tool(rag_tool)

# 为Agent配置工具
agent.tool_registry = tool_registry

# 开始对话
response = agent.run("你好!请记住我叫张三,我是一名Python开发者")
print(response)

3. 输出

# python test_memory_tool.py 
INFO:hello_agents.core.database_config:✅ 数据库配置已更新
提示: 未配置 Neo4j,已启用 working + episodic(未启用 semantic)。
[OK] SQLite 数据库表和索引创建完成
[OK] SQLite 文档存储初始化完成: ./memory_data/memory.db
INFO:sentence_transformers.base.model:No device provided, using cuda:0
INFO:sentence_transformers.base.model:Loading SentenceTransformer model from /data/model_llm/Qwen/models/Qwen3-Embedding-8B.
Loading weights: 100%|█████████████████████████████████████████████████████| 398/398 [00:00<00:00, 768.48it/s]
INFO:sentence_transformers.base.model:Loaded 1 prompt with these keys: ['query']
Batches: 100%|██████████████████████████████████████████████████████████████████| 1/1 [00:01<00:00,  1.47s/it]
INFO:hello_agents.memory.storage.qdrant_store:✅ 成功连接到Qdrant云服务: https://58893cce-3da9-45c5-a75f-3ba25347115c.us-east-2-0.aws.cloud.qdrant.io
INFO:hello_agents.memory.storage.qdrant_store:✅ 使用现有Qdrant集合: hello_agents_vectors
INFO:hello_agents.memory.manager:MemoryManager初始化完成,启用记忆类型: ['working', 'episodic']
✅ 工具 'memory' 已注册。
INFO:hello_agents.memory.storage.qdrant_store:✅ 成功连接到Qdrant云服务: https://58893cce-3da9-45c5-a75f-3ba25347115c.us-east-2-0.aws.cloud.qdrant.io
INFO:hello_agents.memory.storage.qdrant_store:✅ 使用现有Qdrant集合: rag_knowledge_base
✅ RAG工具初始化成功: namespace=default, collection=rag_knowledge_base
✅ 工具 'rag' 已注册。
你好,张三!我已经记住你是一名Python开发者了。有什么我可以帮你的吗?

4. 学习路径

hello-agents/docs/chapter8/第八章 记忆与检索.md at main · datawhalechina/hello-agents · GitHub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值