You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from langchain_community.docstore import InMemoryDocstore
from langchain_community.vectorstores import FAISS
from langchain_core.documents import Document
import faiss;
from langchain_huggingface import HuggingFaceEmbeddings
# 下载好FAISS
# 准备好向量化的对象
model_name = "BAAI/bge-small-zh-v1.5" # 模型名
model_kwargs = {'device': 'cpu'} # 没有显卡就用cpu有英伟达显卡写cuda
encode_kwargs = {'normalize_embeddings': True} # set True to compute cosine similarity
# 第一次运行会自动下载模型去huggingface上下载下载到hf默认的缓存目录。
hf_embedding = HuggingFaceEmbeddings(
model_name=model_name,
model_kwargs=model_kwargs,
encode_kwargs=encode_kwargs
)
# 1、初始化数据库
# 创建索引
index = faiss.IndexFlatL2(len(hf_embedding.embed_query('Hello world!')));
db = FAISS(
# 指定好初始化的Embedding的算法模型引用
embedding_function = hf_embedding,
# 索引长度
index = index,
# 内存存储文档 doc嘛非关系型数据库
docstore = InMemoryDocstore(),
# 字典
index_to_docstore_id = {}
);
# 2、准备数据
document_1 = Document(
# 具体数据key要定死是page_content
page_content = "今天早餐我吃了巧克力薄煎饼和炒蛋。",
# 额外数据随便写keyvalue格式
metadata = {"source": "tweet"}
);
document_2 = Document(
page_content="明天的天气预报是阴天多云最高气温62华氏度。",
metadata={"source": "news"},
)
document_3 = Document(
page_content="正在用LangChain构建一个激动人心的新项目——快来看看吧",
metadata={"source": "tweet"},
)
document_4 = Document(
page_content="劫匪闯入城市银行盗走了100万美元现金。",
metadata={"source": "news"},
)
document_5 = Document(
page_content="哇!那部电影太精彩了,我已经迫不及待想再看一遍。",
metadata={"source": "tweet"},
)
document_6 = Document(
page_content="新iPhone值得这个价格吗阅读这篇评测一探究竟。",
metadata={"source": "website"},
)
document_7 = Document(
page_content="当今世界排名前十的足球运动员。",
metadata={"source": "website"},
)
document_8 = Document(
page_content="LangGraph是构建有状态智能体应用的最佳框架",
metadata={"source": "tweet"},
)
document_9 = Document(
page_content="由于对经济衰退的担忧今日股市下跌500点。",
metadata={"source": "news"},
)
document_10 = Document(
page_content="我有种不好的预感,我要被删除了 :(",
metadata={"source": "tweet"},
)
documents = [
document_1,
document_2,
document_3,
document_4,
document_5,
document_6,
document_7,
document_8,
document_9,
document_10,
]
# 生成个id1~documents长度的
ids = [ 'id'+str(i+1) for i in range(len(documents))]
# 数据插入到faiss,并且指定ID保存到内存~
db.add_documents(documents,ids = ids);
# 把数据库写入磁盘
db.save_local('./faiss_db')
# 语义检索k=返回几条
# results = db.similarity_search('今天的金融投资新闻', k=2)
results = db.similarity_search('有美食的内容吗', k=2)
for res in results:
print(type(res))
print(res.id)
print(f"* {res.page_content} [{res.metadata}]")