网站导航素材下载,wordpress 增加磁盘容量,企业网站做的漂亮,dede自动生成网站地图①.基于 FAISS#xff08;Facebook 开源的高效向量检索库#xff09;和 LangChain 构建的中文文本向量检索系统#xff0c;核心功能是将文本数据向量化后存入 FAISS 向量库#xff0c;并实现相似性检索。1.安装FAISSpip install faiss-cpu#xff0c;也可以是gpu#xff…①.基于 FAISSFacebook 开源的高效向量检索库和 LangChain 构建的中文文本向量检索系统核心功能是将文本数据向量化后存入 FAISS 向量库并实现相似性检索。1.安装FAISSpip install faiss-cpu也可以是gpu看具体使用情况2.实现代码import faiss from langchain_community.docstore import InMemoryDocstore from langchain_community.vectorstores import FAISS from langchain_huggingface import HuggingFaceEmbeddings from langchain_core.documents import Document model_name BAAI/bge-large-zh-v1.5 model_kwargs {device: cpu} encode_kwargs {normalize_embeddings: True} # set True to compute cosine similarity bge_hf_embedding HuggingFaceEmbeddings( model_namemodel_name, model_kwargsmodel_kwargs, encode_kwargsencode_kwargs, ) #1.初始化数据库 #先创建索引 #向量维度 indexfaiss.IndexFlatL2(1024) vector_storeFAISS( embedding_functionbge_hf_embedding, indexindex, docstoreInMemoryDocstore(), index_to_docstore_id{} ) # 2、 准备数据Document #page_content里面的数据必须经过向量化不需要向量化的数据放在metadata document_1 Document( page_content今天早餐我吃了巧克力薄煎饼和炒蛋。, metadata{source: tweet, time: 上午}, ) 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_contentLangGraph是构建有状态智能体应用的最佳框架, 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, ] ids[idstr(i1) for i in range(len(documents))] vector_store.add_documents(documents,idsids) #把数据库写入磁盘 vector_store.save_local(../faiss_db) #语言检索 respvector_store.similarity_search(今天的投资建议,2) for i in resp: print(i.page_content) print(type(i))输出左边的文件栏也会出现保存的数据库②.从保存的向量数据库中进行检索from langchain_community.docstore import InMemoryDocstore from langchain_community.vectorstores import FAISS from langchain_core.embeddings import Embeddings from sentence_transformers import SentenceTransformer from langchain_core.documents import Document import faiss class CustomQwen3Embeddings(Embeddings): 定义一个Qwen3的Embedding和langchain整合的类 def __init__(self,model_name): self.qwen3_embedding SentenceTransformer(model_name) #输入的问题向量化 def embed_query(self, text: str) - list[float]: return self.embed_documents([text])[0] #文本内容向量化 def embed_documents(self, texts: list[str]) - list[list[float]]: return self.qwen3_embedding.encode(texts) qwen3CustomQwen3Embeddings(Qwen/Qwen3-Embedding-0.6B) #加载数据库 vector_storeFAISS.load_local(../faiss_db,embeddingsqwen3,allow_dangerous_deserializationTrue) resp vector_store.similarity_search_with_score(有美食的内容吗, k4, filter{source: tweet}) # 带分数 for i,score in resp: print(type(i)) print(i) print(i.id) print(f{score:3f})输出