getfromcode

AI agent 大模型实战之 RAG检索本地知识库

AI
文章摘要

运行demo: 下载大模型 from modelscope import snapshot_download model_dir = snapshot_download( model_id='BAAI/bge-large-zh-v1.5', cache_dir = '/Volumes/c/workspace/ai-agent-test/rag/model'…

已有 236 人浏览 发布 2026-01-29 更新 2026-09-15

运行demo:

303f38c3fac01e8ae915df2d8930face.png


下载大模型

from modelscope import snapshot_download
model_dir = snapshot_download(
model_id='BAAI/bge-large-zh-v1.5',
cache_dir = '/Volumes/c/workspace/ai-agent-test/rag/model'
)

安装 向量数据库chromadb

client = chromadb.PersistentClient("./chroma_v2") #文件数据库
collections = client.get_or_create_collection(
name="my_db",
metadata={
"hnsw:space": "cosine"
}
)

读取本地txt  txt  =>  embeding

path_list = list(Path("docs").glob("*.txt"))
text_list = []
for path in path_list:
text = path.read_text(encoding="utf-8")
text_list.append(text)
embeddings = model.encode(text_list) #进行向量嵌入
collections.add(
embeddings=embeddings.tolist(),
documents=text_list,
metadatas=[{"id":i} for i,_ in enumerate(text_list)],
ids=[f"doc_{i}" for i,_ in enumerate(text_list)],
)
print(f"数据库中的数据量:{collections.count()}")

查询 embeding   =>  txt

query=["2023.12.1 做了什么"]
query_embedding = model.encode(query)
data = collections.query(query_embedding.tolist(),n_results=5)


课程购买
课程价格
金币29
290金币限时优惠会员免费
销量:0
💎 开通会员每日免费下载,比单买更划算 →
支付成功后会自动显示下载资源云盘的链接!点击链接即可下载到本地。