wap建站php源码网站设计目的与规划
- 作者: 多梦笔记
- 时间: 2026年02月18日 07:50
当前位置: 首页 > news >正文
wap建站php源码,网站设计目的与规划,家在深圳业主论坛,网站优化策略一、向量数据库 向量数据库#xff08;Vector Database#xff09;#xff0c;也叫矢量数据库#xff0c;主要用来存储和处理向量数据。 在数学中#xff0c;向量是有大小和方向的量#xff0c;可以使用带箭头的线段表示#xff0c;箭头指向即为向量的方向#xff0c…一、向量数据库 向量数据库Vector Database也叫矢量数据库主要用来存储和处理向量数据。 在数学中向量是有大小和方向的量可以使用带箭头的线段表示箭头指向即为向量的方向线段的长度表示向量的大小。两个向量的距离或者相似性可以通过欧式距离或者余弦距离得到。 图像、文本和音视频这种非结构化数据都可以通过某种变换或者嵌入学习转化为向量数据存储到向量数据库中从而实现对图像、文本和音视频的相似性搜索和检索。这意味着您可以使用向量数据库根据语义或上下文含义查找最相似或相关的数据而不是使用基于精确匹配或预定义标准查询数据库的传统方法。 向量数据库的主要特点是高效存储与检索。利用索引技术和向量检索算法能实现高维大数据下的快速响应。向量数据库也是一种数据库除了要管理向量数据外还是支持对传统结构化数据的管理。实际使用时有很多场景会同时对向量字段和结构化字段进行过滤检索这对向量数据库来说也是一种挑战。 二、向量嵌入(Vector Embeddings) 对于传统数据库搜索功能都是基于不同的索引方式B Tree、倒排索引等加上精确匹配和排序算法BM25、TF-IDF等实现的。本质还是基于文本的精确匹配这种索引和搜索算法对于关键字的搜索功能非常合适但对于语义搜索功能就非常弱。 例如如果你搜索 “小狗”那么你只能得到带有“小狗” 关键字相关的结果而无法得到 “柯基”、“金毛” 等结果因为 “小狗” 和“金毛”是不同的词传统数据库无法识别它们的语义关系所以传统的应用需要人为的将 “小狗” 和“金毛”等词之间打上特征标签进行关联这样才能实现语义搜索。而如何将生成和挑选特征这个过程也被称为 Feature Engineering (特征工程)它是将原始数据转化成更好的表达问题本质的特征的过程。 但是如果你需要处理非结构化的数据就会发现非结构化数据的特征数量会开始快速膨胀例如我们处理的是图像、音频、视频等数据这个过程就变得非常困难。例如对于图像可以标注颜色、形状、纹理、边缘、对象、场景等特征但是这些特征太多了而且很难人为的进行标注所以我们需要一种自动化的方式来提取这些特征而这可以通过 Vector Embedding 实现 Vector Embedding 是由 AI 模型例如大型语言模型 LLM生成的它会根据不同的算法生成高维度的向量数据代表着数据的不同特征这些特征代表了数据的不同维度。例如对于文本这些特征可能包括词汇、语法、语义、情感、情绪、主题、上下文等。对于音频这些特征可能包括音调、节奏、音高、音色、音量、语音、音乐等。 例如对于目前来说文本向量可以通过 OpenAI 的 text-embedding-ada-002 模型生成图像向量可以通过 clip-vit-base-patch32 模型生成而音频向量可以通过 wav2vec2-base-960h 模型生成。这些向量都是通过 AI 模型生成的所以它们都是具有语义信息的。 例如我们将这句话 “Your text string goes here” 用 text-embedding-ada-002 模型进行文本 Embedding它会生成一个 1536 维的向量得到的结果是这样“-0.006929283495992422, -0.005336422007530928, … -4547132266452536e-05,-0.024047505110502243”它是一个长度为 1536 的数组。这个向量就包含了这句话的所有特征这些特征包括词汇、语法我们可以将它存入向量数据库中以便我们后续进行语义搜索。 三、特征和向量 虽然向量数据库的核心在于相似性搜索 (Similarity Search)但在深入了解相似性搜索前我们需要先详细了解一下特征和向量的概念和原理。 我们先思考一个问题为什么我们在生活中区分不同的物品和事物 如果从理论角度出发这是因为我们会通过识别不同事物之间不同的特征来识别种类例如分别不同种类的小狗就可以通过体型大小、毛发长度、鼻子长短等特征来区分。如下面这张照片按照体型排序可以看到体型越大的狗越靠近坐标轴右边这样就能得到一个体型特征的一维坐标和对应的数值从 0 到 1 的数字中得到每只狗在坐标系中的位置。 然而单靠一个体型大小的特征并不够像照片中哈士奇、金毛和拉布拉多的体型就非常接近我们无法区分。所以我们会继续观察其它的特征例如毛发的长短。 这样每只狗对应一个二维坐标点我们就能轻易的将哈士奇、金毛和拉布拉多区分开来如果这时仍然无法很好的区分德牧和罗威纳犬。我们就可以继续再从其它的特征区分比如鼻子的长短这样就能得到一个三维的坐标系和每只狗在三维坐标系中的位置。 在这种情况下只要特征足够多就能够将所有的狗区分开来最后就能得到一个高维的坐标系虽然我们想象不出高维坐标系长什么样但是在数组中我们只需要一直向数组中追加数字就可以了。 实际上只要维度够多我们就能够将所有的事物区分开来世间万物都可以用一个多维坐标系来表示它们都在一个高维的特征空间中对应着一个坐标点。 那这和相似性搜索 (Similarity Search) 有什么关系呢你会发现在上面的二维坐标中德牧和罗威纳犬的坐标就非常接近这就意味着它们的特征也非常接近。我们都知道向量是具有大小和方向的数学结构所以可以将这些特征用向量来表示这样就能够通过计算向量之间的距离来判断它们的相似度这就是相似性测量 四、相似性测量 (Similarity Measurement) 上面我们讨论了向量数据库的不同搜索算法但是还没有讨论如何衡量相似性。在相似性搜索中需要计算两个向量之间的距离然后根据距离来判断它们的相似度。 而如何计算向量在高维空间的距离呢有三种常见的向量相似度算法欧几里德距离、余弦相似度和点积相似度。 五、欧几里得距离Euclidean Distance 欧几里得距离是用于衡量两个点或向量之间的直线距离的一种方法。它是最常用的距离度量之一尤其在几何和向量空间中。可以将其理解为两点之间的“直线距离”。 其中A 和 B 分别表示两个向量表示向量的维度。 欧几里得距离算法的优点是可以反映向量的绝对距离适用于需要考虑向量长度的相似性计算。例如推荐系统中需要根据用户的历史行为来推荐相似的商品这时就需要考虑用户的历史行为的数量而不仅仅是用户的历史行为的相似度。 六、 余弦相似度Cosine Similarity 余弦相似度Cosine Similarity是一种用于衡量两个向量之间相似度的指标。它通过计算两个向量夹角的余弦值来判断它们的相似程度。其值介于 -1 和 1 之间通常用于文本分析和推荐系统等领域。 特点 值域结果在 -1 到 1 之间。1 表示完全相似0 表示不相关-1 表示完全相反。 余弦相似度对向量的长度不敏感只关注向量的方向因此适用于高维向量的相似性计算。例如语义搜索和文档分类。 七、相似性搜索 (Similarity Search) 既然我们知道了可以通过比较向量之间的距离来判断它们的相似度那么如何将它应用到真实的场景中呢如果想要在一个海量的数据中找到和某个向量最相似的向量我们需要对数据库中的每个向量进行一次比较计算但这样的计算量是非常巨大的所以我们需要一种高效的算法来解决这个问题。 高效的搜索算法有很多其主要思想是通过两种方式提高搜索效率 1减少向量大小——通过降维或减少表示向量值的长度。 2缩小搜索范围——可以通过聚类或将向量组织成基于树形、图形结构来实现并限制搜索范围仅在最接近的簇中进行或者通过最相似的分支进行过滤。 我们首先来介绍一下大部分算法共有的核心概念也就是聚类。 我们可以在保存向量数据后先对向量数据先进行聚类。例如下图在二维坐标系中划定了 4 个聚类中心然后将每个向量分配到最近的聚类中心经过聚类算法不断调整聚类中心位置这样就可以将向量数据分成 4 个簇。每次搜索时只需要先判断搜索向量属于哪个簇然后再在这一个簇中进行搜索这样就从 4 个簇的搜索范围减少到了 1 个簇大大减少了搜索的范围。 工作原理 初始化选择 \( k \) 个随机点作为初始聚类中心。分配将每个向量分配给离它最近的聚类中心形成 \( k \) 个簇。更新重新计算每个簇的聚类中心即簇内所有向量的平均值。迭代重复“分配”和“更新”步骤直到聚类中心不再变化或达到最大迭代次数。 举例说明 假设我们有一组二维向量点例如 [(1, 2), (2, 1), (4, 5), (5, 4), (8, 9)] 我们希望将它们聚成四个簇 k 4 。 初始化随机选择四个点作为初始聚类中心比如 \( (1, 2) 、 、 、 (2, 1) 、 、 、 (4, 5) \) 和 \( (8, 9) \)。分配计算每个点到四个聚类中心的距离将每个点分配给最近的聚类中心。 形成四个簇\( [(1, 2)] [(2, 1)] [(4, 5), (5, 4)] [(8, 9)] \)。
点 $ (1, 2) 更接近 (1, 2) 。
点 \( (2, 1) \)更接近 (2, 1) $。
点 \( (4, 5) \) 更接近 \( (4, 5) \)。
点 \( (5, 4) \) 更接近 \( (4, 5) \)。
点 \( (8, 9) \) 更接近 \( (8, 9) \)。更新计算每个簇的新聚类中心。 第一个簇的聚类中心是 \( (1, 2) \)。第二个簇的聚类中心是 \( (2, 1) \)。第三个簇的新聚类中心是 \( ((45)/2, (54)/2) (4.5, 4.5) \)。第四个簇的聚类中心是 \( (8, 9) \)。 迭代重复分配和更新步骤直到聚类中心不再变化。 优点通过将向量聚类我们可以在进行相似性搜索时只需计算查询向量与每个聚类中心之间的距离而不是与所有向量计算距离。这大大减少了计算量提高了搜索效率。这种方法在处理大规模数据时尤其有效可以显著加快搜索速度。 缺点例如在搜索的时候如果搜索的内容正好处于两个分类区域的中间就很有可能遗漏掉最相似的向量。 八、Hierarchical Navigable Small Worlds (HNSW) 除了聚类以外也可以通过构建树或者构建图的方式来实现近似最近邻搜索。这种方法的基本思想是每次将向量加到数据库中的时候就先找到与它最相邻的向量然后将它们连接起来这样就构成了一个图。当需要搜索的时候就可以从图中的某个节点开始不断的进行最相邻搜索和最短路径计算直到找到最相似的向量。 这种算法能保证搜索的质量但是如果图中所以的节点都以最短的路径相连如图中最下面的一层那么在搜索的时候就同样需要遍历所有的节点。 解决这个问题的思路与常见的跳表算法相似如下图要搜索跳表从最高层开始沿着具有最长 “跳过” 的边向右移动。如果发现当前节点的值大于要搜索的值 - 我们知道已经超过了目标因此我们会在下一级中向前一个节点。 HNSW 继承了相同的分层格式最高层具有更长的边缘用于快速搜索而较低层具有较短的边缘用于准确搜索。 具体来说可以将图分为多层每一层都是一个小世界图中的节点都是相互连接的。而且每一层的节点都会连接到上一层的节点当需要搜索的时候就可以从第一层开始因为第一层的节点之间距离很长可以减少搜索的时间然后再逐层向下搜索又因为最下层相似节点之间相互关联所以可以保证搜索的质量能够找到最相似的向量。 HNSW 算法是一种经典的空间换时间的算法它的搜索质量和搜索速度都比较高但是它的内存开销也比较大因为不仅需要将所有的向量都存储在内存中。还需要维护一个图的结构也同样需要存储。所以这类算法需要根据实际的场景来选择。 通过一个简单的例子来说明 HNSW 的工作原理。 假设我们有以下五个二维向量 \( A (1, 2) \)\( B (2, 3) \)\( C (3, 4) \)\( D (8, 9) \)\( E (9, 10) \) 我们要使用 HNSW 来找到与查询向量 \( Q (2, 2) 最相似的向量。 构建阶段 初始化 首先我们为每个向量随机选择其在 HNSW 中的层数。例如假设 在第2层和第1层\) B \( 在第1层\) C \( 在第1层\) D \( 在第1层\) E \( 在第1层 构建图 从最高层开始构建\) A \( 是第2层唯一的节点。在第1层所有节点都存在。我们通过计算欧氏距离来连接节点确保每个节点只连接到几个最近的邻居。 搜索阶段 从最高层开始搜索 从第2层的 \) A f o n t s t y l e c o l o r : r g b ( 51 , 51 , 51 ) ; 开始计算 / f o n t font stylecolor:rgb(51, 51, 51); 开始计算 /font fontstylecolor:rgb(51,51,51);开始计算/font Q f o n t s t y l e c o l o r : r g b ( 51 , 51 , 51 ) ; 与 / f o n t font stylecolor:rgb(51, 51, 51); 与 /font fontstylecolor:rgb(51,51,51);与/font A f o n t s t y l e c o l o r : r g b ( 51 , 51 , 51 ) ; 的距离 / f o n t font stylecolor:rgb(51, 51, 51); 的距离/font fontstylecolor:rgb(51,51,51);的距离/font d(Q, A) \sqrt{(2-1)^2 (2-2)^2} 1 \(。因为 \) A \( 是唯一的节点直接进入下一层。 在第1层搜索 从 \) A f o n t s t y l e c o l o r : r g b ( 51 , 51 , 51 ) ; 开始计算 / f o n t font stylecolor:rgb(51, 51, 51); 开始计算 /font fontstylecolor:rgb(51,51,51);开始计算/font Q f o n t s t y l e c o l o r : r g b ( 51 , 51 , 51 ) ; 与 / f o n t font stylecolor:rgb(51, 51, 51); 与 /font fontstylecolor:rgb(51,51,51);与/font B f o n t s t y l e c o l o r : r g b ( 51 , 51 , 51 ) ; 、 / f o n t font stylecolor:rgb(51, 51, 51);、/font fontstylecolor:rgb(51,51,51);、/font C f o n t s t y l e c o l o r : r g b ( 51 , 51 , 51 ) ; 、 / f o n t font stylecolor:rgb(51, 51, 51);、/font fontstylecolor:rgb(51,51,51);、/font D f o n t s t y l e c o l o r : r g b ( 51 , 51 , 51 ) ; 、 / f o n t font stylecolor:rgb(51, 51, 51);、/font fontstylecolor:rgb(51,51,51);、/font E \( 的距离 \) d(Q, B) \sqrt{(2-2)^2 (2-3)^2} 1 $\( d(Q, C) \sqrt{(2-3)^2 (2-4)^2} \sqrt{5} \)\( d(Q, D) \sqrt{(2-8)^2 (2-9)^2} \sqrt{85} \)\( d(Q, E) \sqrt{(2-9)^2 (2-10)^2} \sqrt{113} \) \( B \) 是最近的邻居。 返回结果 在第2层找到的最近邻是 \( A f o n t s t y l e c o l o r : r g b ( 51 , 51 , 51 ) ; 第 1 层找到的最近邻是 / f o n t font stylecolor:rgb(51, 51, 51);第1层找到的最近邻是 /font fontstylecolor:rgb(51,51,51);第1层找到的最近邻是/font B f o n t s t y l e c o l o r : r g b ( 51 , 51 , 51 ) ; 即与查询向量 / f o n t font stylecolor:rgb(51, 51, 51);即与查询向量 /font fontstylecolor:rgb(51,51,51);即与查询向量/font Q \) 最相似的向量。
总结 通过这种层次化的搜索过程HNSW 能够快速缩小搜索范围在大规模数据中高效找到近似最近邻。 九、 基于 Embedding 的问答助手和意图匹配 Embedding models(嵌入模型 Embeddings类是一个专为与文本嵌入模型进行交互而设计的类。有许多嵌入模型提供商如OpenAI、Cohere、Hugging Face等- 这个类旨在为它们提供一个标准接口。 Embeddings类会为文本创建一个向量表示。这很有用因为这意味着我们可以在向量空间中思考文本并做一些类似语义搜索的事情比如在向量空间中寻找最相似的文本片段。 LangChain中的基本Embeddings类提供了两种方法一个用于嵌入文档另一个用于嵌入查询。前者.embed_documents 接受多个文本作为输入而后者.embed_query接受单个文本。之所以将它们作为两个单独的方法是因为一些嵌入提供商对文档要搜索的文档和查询搜索查询本身有不同的嵌入方法。.embed_query将返回一个浮点数列表.embed_documents将返回一个浮点数列表的列表。
十、设置 OpenAI 首先我们需要安装OpenAI合作伙伴包 pip install langchain-openai访问API需要一个API密钥您可以通过创建帐户并转到这里来获取它。一旦我们有了密钥我们希望通过运行以下命令将其设置为环境变量 export OPENAI_API_KEY…如果您不想设置环境变量可以在初始化OpenAI LLM类时通过api_key命名参数直接传递密钥 from langchain_openai import OpenAIEmbeddings embeddings_model OpenAIEmbeddings(api_key…)否则您可以不带任何参数初始化 from langchain_openai import OpenAIEmbeddings embeddings_model OpenAIEmbeddings()十一、嵌入文本列表(embed_documents) 使用.embed_documents来嵌入一个字符串列表恢复一个嵌入列表 #示例embed_documents.py embeddings embeddings_model.embed_documents([嗨,哦你好,你叫什么名字,我的朋友们叫我World,Hello World] ) len(embeddings), len(embeddings[0])(5, 1536)十二、嵌入单个查询(embed_query) 使用.embed_query来嵌入单个文本片段例如用于与其他嵌入的文本片段进行比较。 #示例embed_query.py embedded_query embeddings_model.embed_query(对话中提到的名字是什么) embedded_query[:5][0.003292066277936101, -0.009463472291827202, 0.03418809175491333, -0.0011715596774592996, -0.015508134849369526]十三、问答助手 #示例embed_search.py from openai import OpenAI #pip install numpy from numpy import dot from numpy.linalg import normclient OpenAI()# 定义调用 Embedding API 的函数 def get_embedding(text):response client.embeddings.create(inputtext,modeltext-embedding-ada-002)return response.data[0].embedding# 计算余弦相似度参考文章https://blog.csdn.net/Hyman_Qiu/article/details/137743190 #定义一个函数 cosine_similarity该函数接受两个向量 vec1 和 vec2 作为输入。 def cosine_similarity(vec1, vec2):#计算并返回两个向量之间的余弦相似度公式为两个向量的点积除以它们范数的乘积。return dot(vec1, vec2) / (norm(vec1) * norm(vec2))# 实现文本搜索功能定义一个函数 search_documents该函数接受一个查询字符串 query 和一个文档列表 documents 作为输入。
def search_documents(query, documents):# 调用 get_embedding 函数生成查询字符串的嵌入向量 query_embedding。query_embedding get_embedding(query)# 对每个文档调用 get_embedding 函数生成文档的嵌入向量存储在 document_embeddings 列表中。document_embeddings [get_embedding(doc) for doc in documents]# 计算查询嵌入向量与每个文档嵌入向量之间的余弦相似度存储在 similarities 列表中similarities [cosine_similarity(query_embedding, doc_embedding) for doc_embedding in document_embeddings]# 找到相似度最高的文档的索引 most_similar_index。most_similar_index similarities.index(max(similarities))# 返回相似度最高的文档和相似度得分。return documents[most_similar_index], max(similarities)# 测试文本搜索功能 if name main:documents [OpenAI的ChatGPT是一个强大的语言模型。,天空是蓝色的,阳光灿烂。,人工智能正在改变世界。,Python是一种流行的编程语言。]#.././resource/knowledge.txt 文件内容转换为上述documents#documents [line.strip() for line in open(.././resource/knowledge.txt, r, encodingutf-8)]query 天空是什么颜色的most_similar_document, similarity_score search_documents(query, documents)print(f最相似的文档: {most_similar_document})print(f相似性得分: {similarity_score}) 最相似的文档: 天空是蓝色的,阳光灿烂。 相似性得分: 0.9166142096488017十四、意图匹配 #示例embed_intention_recognition.py from langchain.chains.combine_documents import create_stuff_documents_chain from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import FAISS from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI from langchain_openai import OpenAIEmbeddings# 数据导入 loader TextLoader(.././resource/qa.txt, encodingUTF-8) docs loader.load()
数据切分
text_splitter RecursiveCharacterTextSplitter() documents text_splitter.split_documents(docs)
创建embedding
embeddings OpenAIEmbeddings()
通过向量数据库存储
vector FAISS.from_documents(documents, embeddings)
查询检索
创建 prompt
prompt ChatPromptTemplate.from_template(仅根据提供的上下文回答以下问题: context {context} /context Question: {input})
创建模型
llm ChatOpenAI()
创建 document 的chain 查询
document_chain create_stuff_documents_chain(llm, prompt) from langchain.chains import create_retrieval_chain# # 创建搜索chain 返回值为 VectorStoreRetriever retriever vector.as_retriever() retrieval_chain create_retrieval_chain(retriever, document_chain)
# 执行请求
response retrieval_chain.invoke({input: 天气}) print(response[answer]) 抱歉我无法回答关于天气的问题。如果您需要查询天气信息请使用天气应用程序或者访问天气网站。我可以帮您订餐、播放音乐、设置闹钟或者讲笑话。有什么其他问题我可以帮助您解决吗十五、基于 LangChain 构建向量存储和查询Chroma, Weaviate, Qdrant, Milvus 向量存储) 存储和搜索非结构化数据的最常见方法之一是将其嵌入并存储生成的嵌入向量 然后在查询时将非结构化查询嵌入并检索与嵌入查询“最相似”的嵌入向量。 向量存储会处理存储嵌入数据并为您执行向量搜索。 可以通过以下方式将向量存储转换为检索器接口 Retrievers(检索器)是一个接口根据非结构化查询返回文档。 它比向量存储更通用。 检索器不需要能够存储文档只需要能够返回或检索它们。 检索器可以从向量存储器创建但也足够广泛包括Wikipedia搜索和Amazon Kendra。 检索器接受字符串查询作为输入并返回文档列表作为输出。 vectorstore MyVectorStore() retriever vectorstore.as_retriever()Chroma 官方文档https://docs.trychroma.com/ Chroma ( /kromə/ n. 色彩的浓度色度 )是一个以人工智能为基础的开源向量数据库专注于开发者的生产力和幸福感。Chroma 使用 Apache 2.0 许可证。 使用以下命令安装 Chroma pip install langchain-chromaChroma 可以以多种模式运行。以下是每种模式的示例均与 LangChain 集成 in-memory在 Python -脚本或 Jupyter 笔记本中in-memory with persistance -在脚本或笔记本中保存/加载到磁盘in a docker container- 作为在本地机器或云中运行的服务器 与任何其他数据库一样您可以进行以下操作 .add .get .update .upsert .delete .peek而.query则运行相似性搜索。 查看完整文档请访问 docs。要直接访问这些方法可以使用._collection.method() 基本示例 在这个基本示例中我们获取《乔布斯演讲稿》将其分割成片段使用开源嵌入模型进行嵌入加载到 Chroma 中然后进行查询。 #示例chroma_base.py
pip install langchain-chroma
from langchain_chroma import Chroma from langchain_community.document_loaders import TextLoader
pip install -U langchain-huggingface
from langchain_huggingface import HuggingFaceEmbeddings from langchain_text_splitters import CharacterTextSplitter# 加载文档并将其分割成片段 loader TextLoader(../../resource/knowledge.txt, encodingUTF-8) documents loader.load()
将其分割成片段
text_splitter CharacterTextSplitter(chunk_size1500, chunk_overlap0) docs text_splitter.split_documents(documents)
创建开源嵌入函数
embedding_function HuggingFaceEmbeddings(model_nameall-MiniLM-L6-v2)
将其加载到 Chroma 中
db Chroma.from_documents(docs, embedding_function)
进行查询
query Pixar公司是做什么的? docs db.similarity_search(query)
打印结果
print(docs[0].page_content)During the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together. 在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。基本示例包括保存到磁盘 在上一个示例的基础上如果您想要保存到磁盘只需初始化 Chroma 客户端并传递要保存数据的目录。 注意Chroma 尽最大努力自动将数据保存到磁盘但多个内存客户端可能会相互干扰。最佳做法是任何给定时间只运行一个客户端。 #示例chroma_disk.py# 保存到磁盘 db2 Chroma.from_documents(docs, embedding_function, persist_directory./chroma_db) docs db2.similarity_search(query)
从磁盘加载
db3 Chroma(persist_directory./chroma_db, embedding_functionembedding_function) docs db3.similarity_search(query) print(docs[0].page_content)During the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together. 在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。将 Chroma 客户端传递给 Langchain 您还可以创建一个 Chroma 客户端并将其传递给 LangChain。如果您希望更轻松地访问底层数据库这将特别有用。 您还可以指定要让 LangChain 使用的集合名称。 #示例chroma_client.py import chromadb persistent_client chromadb.PersistentClient() collection persistent_client.get_or_create_collection(collection_name) collection.add(ids[1, 2, 3], documents[a, b, c]) langchain_chroma Chroma(clientpersistent_client,collection_namecollection_name,embedding_functionembedding_function, ) print(在集合中有, langchain_chroma._collection.count(), 个文档)Insert of existing embedding ID: 1 Insert of existing embedding ID: 2 Insert of existing embedding ID: 3 Add of existing embedding ID: 1 Add of existing embedding ID: 2 Add of existing embedding ID: 3 在集合中有 3 个项目 更新和删除 在构建真实应用程序时您不仅希望添加数据还希望更新和删除数据。 Chroma 要求用户提供ids来简化这里的簿记工作。 ids可以是文件名也可以是类似 filename_paragraphNumber的组合哈希值。 Chroma 支持所有这些操作尽管有些操作仍在通过 LangChain 接口进行整合。额外的工作流改进将很快添加。 以下是一个基本示例展示如何执行各种操作 #示例chroma_update.py
pip install langchain-chroma
from langchain_chroma import Chroma from langchain_community.document_loaders import TextLoader
pip install -U langchain-huggingface
from langchain_huggingface import HuggingFaceEmbeddings from langchain_text_splitters import CharacterTextSplitter# 加载文档并将其分割成片段 loader TextLoader(../../resource/knowledge.txt, encodingUTF-8) documents loader.load()
将其分割成片段
text_splitter CharacterTextSplitter(chunk_size1500, chunk_overlap0) docs text_splitter.split_documents(documents)
创建开源嵌入函数
embedding_function HuggingFaceEmbeddings(model_nameall-MiniLM-L6-v2) query Pixar公司是做什么的?
创建简单的 ids
ids [str(i) for i in range(1, len(docs) 1)]
添加数据
example_db Chroma.from_documents(docs, embedding_function, idsids) docs example_db.similarity_search(query)
更新文档的元数据
docs[0].metadata {source: ../../resource/knowledge.txt,new_value: hello world, } print(更新前内容) print(example_db._collection.get(ids[ids[0]])) example_db.update_document(ids[0], docs[0]) print(更新后内容) print(example_db._collection.get(ids[ids[0]]))
删除最后一个文档
print(删除前计数, example_db._collection.count()) print(example_db._collection.get(ids[ids[-1]])) example_db._collection.delete(ids[ids[-1]]) print(删除后计数, example_db._collection.count()) print(example_db._collection.get(ids[ids[-1]]))更新前内容 {ids: [1], embeddings: None, metadatas: [{source: ../../resource/knowledge.txt}], documents: [\ufeffI am honored to be with you today at your commencement from one of the finest universities in the world. I never graduated from college. Truth be told, this is the closest Ive ever gotten to a college graduation. Today I want to tell you three stories from my life. Thats it. No big deal. Just three stories.\n我今天很荣幸能和你们一起参加毕业典礼斯坦福大学是世界上最好的大学之一。我从来没有从大学中毕业。说实话,今天也许是在我的生命中离大学毕业最近的一天了。今天我想向你们讲述我生活中的三个故事。不是什么大不了的事情,只是三个故事而已。\n\nThe first story is about connecting the dots.\n第一个故事是关于如何把生命中的点点滴滴串连起来。\n\nI dropped out of Reed College after the first 6 months, but then stayed around as a drop-in for another 18 months or so before I really quit. So why did I drop out?\n我在Reed大学读了六个月之后就退学了但是在十八个月以后——我真正的作出退学决定之前我还经常去学校。我为什么要退学呢], uris: None, data: None, included: [metadatas, documents]} 更新后内容 {ids: [1], embeddings: None, metadatas: [{new_value: hello world, source: ../../resource/knowledge.txt}], documents: [During the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together.\n在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。], uris: None, data: None, included: [metadatas, documents]} 删除前计数 16 {ids: [16], embeddings: None, metadatas: [{source: ../../resource/knowledge.txt}], documents: [Stewart and his team put out several issues of The Whole Earth Catalog, and then when it had run its course, they put out a final issue. It was the mid-1970s, and I was your age. On the back cover of their final issue was a photograph of an early morning country road, the kind you might find yourself hitchhiking on if you were so adventurous. Beneath it were the words: Stay Hungry. Stay Foolish. It was their farewell message as they signed off. Stay Hungry. Stay Foolish. And I have always wished that for myself. And now, as you graduate to begin anew, I wish that for you.\nStewart和他的伙伴出版了几期的“整个地球的目录”当它完成了自己使命的时候他们做出了最后一期的目录。那是在七十年代的中期你们的时代。在最后一期的封底上是清晨乡村公路的照片如果你有冒险精神的话你可以自己找到这条路的在照片之下有这样一段话“求知若饥虚心若愚。”这是他们停止了发刊的告别语。“求知若饥虚心若愚。”我总是希望自己能够那样现在在你们即将毕业开始新的旅程的时候我也希望你们能这样\n\nStay Hungry. Stay Foolish.\n求知若饥虚心若愚。\n\nThank you all very much.\n非常感谢你们。], uris: None, data: None, included: [metadatas, documents]} 删除后计数 15 {ids: [], embeddings: None, metadatas: [], documents: [], uris: None, data: None, included: [metadatas, documents]}使用 OpenAI Embeddings 许多人喜欢使用 OpenAIEmbeddings以下是如何设置它。 #示例chroma_openai.py from langchain_openai import OpenAIEmbeddings
pip install langchain-chroma
from langchain_chroma import Chroma import chromadb from langchain_text_splitters import CharacterTextSplitter from langchain_community.document_loaders import TextLoaderembeddings OpenAIEmbeddings() persistent_client chromadb.PersistentClient() new_client chromadb.EphemeralClient()
加载文档并将其分割成片段
loader TextLoader(../../resource/knowledge.txt, encodingUTF-8) documents loader.load()
将其分割成片段
text_splitter CharacterTextSplitter(chunk_size1500, chunk_overlap0) docs text_splitter.split_documents(documents)openai_lc_client Chroma.from_documents(docs, embeddings, clientnew_client, collection_nameopenai_collection ) query Pixar公司是做什么的? docs openai_lc_client.similarity_search(query) print(docs[0].page_content) During the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together. 在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。其他功能 带分数的相似性搜索 返回的距离分数是余弦距离。因此分数越低越好。 #示例chroma_other.py docs db.similarity_search_with_score(query)docs[0] #0.8513926863670349(Document(metadata{source: ../../resource/knowledge.txt}, page_contentDuring the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together.\n在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。), 0.8513926863670349)检索器选项 本节介绍如何在检索器中使用 Chroma 的不同选项。 MMR 除了在检索器对象中使用相似性搜索外还可以使用 MMRMaximal Marginal Relevance最大边际相关性是一种信息检索和文本摘要技术用于在选择文档或文本片段时平衡相关性和多样性。其主要目的是在检索结果中既包含与查询高度相关的内容又避免结果之间的高度冗余。 MMR的作用 提高结果的多样性通过引入多样性MMR可以避免检索结果中出现重复信息从而提供更全面的答案。平衡相关性和新颖性MMR在选择结果时既考虑与查询的相关性也考虑新信息的引入以确保结果的多样性和覆盖面。减少冗余通过避免选择与已选结果高度相似的文档MMR可以减少冗余提高信息的利用效率。 在检索器对象中使用MMR 在使用向量检索器如FAISS时通常通过相似性搜索来查找与查询最相关的文档。然而这种方法有时可能会返回许多相似的结果导致信息冗余。MMR可以在这种情况下发挥作用通过以下步骤实现 计算相关性首先计算每个候选文档与查询的相似性得分。计算多样性然后计算每个候选文档与已选文档集合的相似性得分。选择文档在每一步选择一个文档使得该文档在相关性和多样性之间达到最佳平衡。 #示例chroma_other.py retriever db.as_retriever(search_typemmr)retriever.invoke(query)[0]page_contentDuring the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together. 在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。 metadata{source: ../../resource/knowledge.txt}Weaviate 如何使用 langchain-weaviate包在 LangChain 中开始使用 Weaviate 向量存储。 Weaviate 是一个开源的向量数据库。它允许您存储来自您喜爱的机器学习模型的数据对象和向量嵌入并能够无缝地扩展到数十亿个数据对象。 官方文档https://weaviate.io/developers/weaviate 要使用此集成您需要运行一个 Weaviate 数据库实例。 最低版本 此模块需要 Weaviate1.23.7或更高版本。但是我们建议您使用最新版本的 Weaviate。 连接到 Weaviate 在本文中我们假设您在http://localhost:8080上运行了一个本地的 Weaviate 实例并且端口 50051 用于 gRPC 通信。因此我们将使用以下代码连接到 Weaviate weaviate_client weaviate.connect_to_local()其他部署选项 Weaviate 可以以许多不同的方式进行部署例如使用Weaviate Cloud Services (WCS)、Docker或Kubernetes。 如果您的 Weaviate 实例以其他方式部署可以在此处信息关于连接到 Weaviate 的不同方式。您可以使用不同的辅助函数或者创建一个自定义实例。 请注意您需要一个 v4 客户端 API它将创建一个 weaviate.WeaviateClient 对象。 认证 一些 Weaviate 实例例如在 WCS 上运行的实例启用了认证例如 API 密钥和/或用户名密码认证。 阅读客户端认证指南以获取更多信息以及深入的认证配置页面。 安装
安装包
%pip install -Uqq langchain-weaviate
%pip install openai tiktoken langchain环境设置
本文使用OpenAIEmbeddings,通过 OpenAI API,我们建议获取一个 OpenAI API 密钥并将其作为名为OPENAI_API_KEY的环境变量导出。 完成后您的 OpenAI API 密钥将被自动读取。如果您对环境变量不熟悉可以在此处或此指南中关于它们的信息。 配置Weaviate的WCD_DEMO_URL和WCD_DEMO_RO_KEY setx WCD_DEMO_URL setx WCD_DEMO_RO_KEY 用法 通过相似性查找对象 以下是一个示例演示如何通过查询查找与之相似的对象从数据导入到查询 Weaviate 实例。 步骤 1数据导入 首先我们将创建要添加到Weaviate的数据方法是加载并分块长文本文件的内容。 from langchain_community.document_loaders import TextLoader from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter现在我们可以导入数据。要这样做连接到 Weaviate 实例并使用生成的 weaviate_client 对象。例如我们可以将文档导入如下所示 #示例weaviate_client.py weaviate_client weaviate.connect_to_weaviate_cloud(cluster_urlwcd_url, # Replace with your Weaviate Cloud URLauth_credentialsAuth.api_key(wcd_api_key), # Replace with your Weaviate Cloud keyheaders{X-OpenAI-Api-key: openai_api_key} # Replace with your OpenAI API key ) db WeaviateVectorStore.from_documents(docs, embeddings, clientweaviate_client)第二步执行搜索 现在我们可以执行相似度搜索。这将返回与查询文本最相似的文档基于存储在 Weaviate 中的嵌入和从查询文本生成的等效嵌入 #示例weaviate_search.py
pip install -Uqq langchain-weaviate
pip install openai tiktoken langchain
import os import weaviate from langchain_community.document_loaders import TextLoader from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter from langchain_weaviate.vectorstores import WeaviateVectorStore from weaviate.classes.init import Authembeddings OpenAIEmbeddings()
加载文档并将其分割成片段
loader TextLoader(../../resource/knowledge.txt, encodingUTF-8) documents loader.load()
将其分割成片段
text_splitter CharacterTextSplitter(chunk_size1500, chunk_overlap0) docs text_splitter.split_documents(documents)# Best practice: store your credentials in environment variables wcd_url os.environ[WCD_DEMO_URL] wcd_api_key os.environ[WCD_DEMO_RO_KEY] openai_api_key os.environ[OPENAI_API_KEY]weaviate_client weaviate.connect_to_weaviate_cloud(cluster_urlwcd_url, # Replace with your Weaviate Cloud URLauth_credentialsAuth.api_key(wcd_api_key), # Replace with your Weaviate Cloud keyheaders{X-OpenAI-Api-key: openai_api_key} # Replace with your OpenAI API key ) db WeaviateVectorStore.from_documents(docs, embeddings, clientweaviate_client) query Pixar公司是做什么的? docs db.similarity_search(query) print(docs[0].page_content) weaviate_client.close()During the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together. 在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。量化结果相似性 您可以选择检索相关性“分数”。这是一个相对分数表示特定搜索结果在搜索结果池中的好坏程度。 请注意这是相对分数意味着不应用于确定相关性的阈值。但是它可用于比较整个搜索结果集中不同搜索结果的相关性。 #示例weaviate_similarity.py query Pixar公司是做什么的? docs db.similarity_search_with_score(query, k5) for doc in docs:print(f{doc[1]:.3f}, :, doc[0].page_content[:100] …)0.700 : During the next five years, I started a company named NeXT, another company named Pixar, and fell in… 0.337 : I was lucky – I found what I loved to do early in life. Woz and I started Apple in my parents garage… 0.271 : I really didnt know what to do for a few months. I felt that I had let the previous generation of e… 0.256 : Im pretty sure none of this would have happened if I hadnt been fired from Apple. It was awful tas… 0.191 : Stewart and his team put out several issues of The Whole Earth Catalog, and then when it had run its…Qdrant Qdrant介绍 Qdrant读作quadrant /kwɑdrənt/ n. 象限象限仪四分之一圆是一个向量相似度搜索引擎。它提供了一个生产就绪的服务具有方便的 API 来存储、搜索和管理点 带有附加载荷的向量。Qdrant 专门支持扩展过滤功能使其对各种神经网络或基于语义的匹配、分面搜索和其他应用非常有用。 官方文档https://qdrant.tech/documentation/ 以下展示了如何使用与Qdrant向量数据库相关的功能。 有各种运行Qdrant的模式取决于所选择的模式会有一些细微的差异。选项包括- 本地模式无需服务器Qdrant 云 请参阅安装说明。 %pip install –upgrade –quiet langchain-qdrant langchain-openai langchain#示例qdrant_local.py from langchain_community.document_loaders import TextLoader from langchain_openai import OpenAIEmbeddings from langchain_qdrant import Qdrant from langchain_text_splitters import CharacterTextSplitterloader TextLoader(../../resource/knowledge.txt, encodingUTF-8) documents loader.load() text_splitter CharacterTextSplitter(chunk_size1500, chunk_overlap0) docs text_splitter.split_documents(documents) embeddings OpenAIEmbeddings()本地模式 Python 客户端允许您在本地模式下运行相同的代码而无需运行 Qdrant 服务器。这对于测试和调试或者如果您计划仅存储少量向量非常有用。嵌入可能完全保存在内存中或者持久化在磁盘上。 内存中 对于一些测试场景和快速实验您可能更喜欢仅将所有数据保存在内存中因此当客户端被销毁时数据会丢失 - 通常在脚本/笔记本的末尾。 #示例qdrant_local.py qdrant Qdrant.from_documents(docs,embeddings,location:memory:, # 本地模式仅内存存储collection_namemy_documents, )磁盘存储 在不使用 Qdrant 服务器的本地模式下还可以将您的向量存储在磁盘上以便它们在运行之间持久化。 #示例qdrant_disk.py qdrant Qdrant.from_documents(docs,embeddings,path/tmp/local_qdrant,collection_namemy_documents, )相似度搜索 使用 Qdrant 向量存储的最简单场景是执行相似度搜索。在幕后我们的查询将使用embedding_function对查询进行编码并用于在 Qdrant 集合中查找相似的文档。 query Pixar公司是做什么的? found_docs qdrant.similarity_search(query)print(found_docs[0].page_content)During the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together. 在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。Qdrant 云 如果您不想忙于管理基础设施可以选择在 Qdrant 云上设置一个完全托管的 Qdrant 集群。包括一个永久免费的 1GB 集群供试用。使用托管版本与使用 Qdrant 的主要区别在于您需要提供 API 密钥以防止公开访问您的部署。该值也可以设置在QDRANT_API_KEY环境变量中。 url —qdrant cloud cluster url here — api_key —api key here— qdrant Qdrant.from_documents(docs,embeddings,urlurl,prefer_grpcTrue,api_keyapi_key,collection_namemy_documents, )Milvus Milvus介绍 Milvus 是一个数据库用于存储、索引和管理由深度神经网络和其他机器学习ML模型生成的大规模嵌入向量。 官方文档https://milvus.io/docs/overview.md 下面讲解如何使用与 Milvus 向量数据库相关的功能。 配置环境变量MILVUS_API_URL和MILVUS_API_KEY setx MILVUS_API_URL setx MILVUS_API_KEY zillizhttps://cloud.zilliz.com/ 要运行您应该已经启动并运行了一个Milvus 实例。 #示例milvus_zilliz.py #pip install –upgrade –quiet pymilvus from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import Milvus from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter from langchain_community.document_loaders import TextLoader loader TextLoader(../../resource/knowledge.txt, encodingUTF-8) documents loader.load() text_splitter CharacterTextSplitter(chunk_size1500, chunk_overlap0) docs text_splitter.split_documents(documents) embeddings OpenAIEmbeddings()vector_db Zilliz.from_documents( # or Milvus.from_documentsdocs,embeddings,#存储到collection_1中collection_namecollection_1,connection_args{uri: os.getenv(MILVUS_API_URL), token: os.getenv(MILVUS_API_KEY)},#drop_oldTrue, # Drop the old Milvus collection if it existsauto_idTrue, )query Pixar公司是做什么的? docs vector_db.similarity_search(query)print(docs[0].page_content)During the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together. 在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。总结 下面是 Chroma、Weaviate、Qdrant 和 Milvus 四个向量数据库的功能对比 功能/特性ChromaWeaviateQdrantMilvus数据模型向量 元数据向量 元数据向量 元数据向量 元数据支持的索引类型HNSWHNSW, IVF, FlatHNSWIVF, HNSW, ANNOY, Flat扩展性高高高高实时性实时更新实时更新实时更新实时更新多样化查询向量相似性搜索向量相似性搜索 混合查询向量相似性搜索向量相似性搜索 混合查询分布式架构是是是是支持的语言Python, JavaScriptPython, Java, Go, TypeScriptPython, GoPython, Java, Go, Node.js社区支持活跃活跃活跃活跃开源许可证Apache 2.0BSD-3-ClauseApache 2.0Apache 2.0部署选项本地, 云本地, 云本地, 云本地, 云额外功能数据版本控制知识图谱集成, 模型管理集成向量处理工具数据管理工具, 集成分析工具 这些数据库在功能和特性上各有优势选择合适的数据库应根据具体的应用需求和技术栈来决定。 FAISS, Pinecone, Lance Similarity search FAISS Faiss介绍 Facebook AI Similarity Search (Faiss /Fez/) 是一个用于高效相似度搜索和密集向量聚类的库。它包含了在任意大小的向量集合中进行搜索的算法甚至可以处理可能无法完全放入内存的向量集合。它还包含用于评估和参数调整的支持代码。 Faiss 官方文档https://faiss.ai/ 下面展示如何使用与 FAISS向量数据库相关的功能。它将展示特定于此集成的功能。在学习完这些内容后探索相关的用例页面可能会很有帮助以了解如何将这个向量存储作为更大链条的一部分来使用。 设置 该集成位于langchain-community包中。我们还需要安装faiss包本身。我们还将使用 OpenAI 进行嵌入因此需要安装这些要求。我们可以使用以下命令进行安装 pip install -U langchain-community faiss-cpu langchain-openai tiktoken请注意如果您想使用启用了 GPU 的版本也可以安装 faiss-gpu 设置 LangSmith 以获得最佳的可观测性也会很有帮助但不是必需的。
os.environ[LANGCHAIN_TRACING_V2] true
os.environ[LANGCHAIN_API_KEY] 导入
在这里我们将文档导入到向量存储中。 #示例faiss_search.py
如果您需要使用没有 AVX2 优化的 FAISS 进行初始化请取消下面一行的注释
os.environ[FAISS_NO_AVX2] 1
from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import FAISS from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter loader TextLoader(../../resource/knowledge.txt, encodingUTF-8) documents loader.load() text_splitter CharacterTextSplitter(chunk_size1500, chunk_overlap0) docs text_splitter.split_documents(documents) embeddings OpenAIEmbeddings() db FAISS.from_documents(docs, embeddings) print(db.index.ntotal)16查询 现在我们可以查询向量存储。有几种方法可以做到这一点。最常见的方法是使用similarity_search #示例faiss_search.py query Pixar公司是做什么的? docs db.similarity_search(query)print(docs[0].page_content)During the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together. 在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。作为检索器 我们还可以将向量存储转换为 Retriever 类。这使我们能够轻松地在其他 LangChain 方法中使用它这些方法主要用于检索器。 #示例faiss_retriever.py retriever db.as_retriever() docs retriever.invoke(query)print(docs[0].page_content)During the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together. 在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。带分数的相似度搜索 还有一些 FAISS 特定的方法。其中之一是similarity_search_with_score它允许您返回文档以及查询与它们之间的距离分数。返回的距离分数是 L2 距离。因此得分越低越好。 #示例faiss_similarity.py #返回文档以及查询与它们之间的距离分数。返回的距离分数是 L2 距离。因此得分越低越好。 docs_and_scores db.similarity_search_with_score(query) print(docs_and_scores) #还可以使用similarity_search_by_vector来搜索与给定嵌入向量相似的文档该函数接受一个嵌入向量作为参数而不是字符串。 embedding_vector embeddings.embed_query(query) docs_and_scores db.similarity_search_by_vector(embedding_vector) print(docs_and_scores)[(Document(metadata{source: ../../resource/knowledge.txt}, page_contentDuring the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together.\n在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。), 0.3155345), (Document(metadata{source: ../../resource/knowledge.txt}, page_contentI was lucky – I found what I loved to do early in life. Woz and I started Apple in my parents garage when I was 20. We worked hard, and in 10 years Apple had grown from just the two of us in a garage into a billion company with over 4000 employees. We had just released our finest creation - the Macintosh - a year earlier, and I had just turned 30. And then I got fired. How can you get fired from a company you started? Well, as Apple grew we hired someone who I thought was very talented to run the company with me, and for the first year or so things went well. But then our visions of the future began to diverge and eventually we had a falling out. When we did, our Board of Directors sided with him. So at 30 I was out. And very publicly out. What had been the focus of my entire adult life was gone, and it was devastating.\n我非常幸运因为我在很早的时候就找到了我钟爱的东西。沃兹和我在二十岁的时候就在父母的车库里面开创了苹果公司。我们工作得很努力十年之后这个公司从那两个车库中的穷光蛋发展到了超过四千名的雇员、价值超过二十亿的大公司。在公司成立的第九年我们刚刚发布了最好的产品那就是 Macintosh。我也快要到三十岁了。在那一年我被炒了鱿鱼。你怎么可能被你自己创立的公司炒了鱿鱼呢嗯在苹果快速成长的时候我们雇用了一个很有天分的家伙和我一起管理这个公司在最初的几年公司运转的很好。但是后来我们对未来的看法发生了分歧, 最终我们吵了起来。当争吵不可开交的时候董事会站在了他的那一边。所以在三十岁的时候我被炒了。在这么多人的眼皮下我被炒了。在而立之年我生命的全部支柱离自己远去这真是毁灭性的打击。), 0.44481623), (Document(metadata{source: ../../resource/knowledge.txt}, page_contentI really didnt know what to do for a few months. I felt that I had let the previous generation of entrepreneurs down - that I had dropped the baton as it was being passed to me. I met with David Packard and Bob Noyce and tried to apologize for screwing up so badly. I was a very public failure, and I even thought about running away from the valley. But something slowly began to dawn on me – I still loved what I did. The turn of events at Apple had not changed that one bit. I had been rejected, but I was still in love. And so I decided to start over.\n在最初的几个月里我真是不知道该做些什么。我把从前的创业激情给丢了我觉得自己让与我一同创业的人都很沮丧。我和 David Pack 和 Bob Boyce 见面并试图向他们道歉。我把事情弄得糟糕透顶了。但是我渐渐发现了曙光我仍然喜爱我从事的这些东西。苹果公司发生的这些事情丝毫的没有改变这些一点也没有。我被驱逐了但是我仍然钟爱它。所以我决定从头再来。\n\nI didnt see it then, but it turned out that getting fired from Apple was the best thing that could have ever happened to me. The heaviness of being successful was replaced by the lightness of being a beginner again, less sure about everything. It freed me to enter one of the most creative periods of my life.\n我当时没有觉察但是事后证明从苹果公司被炒是我这辈子发生的最棒的事情。因为作为一个成功者的极乐感觉被作为一个创业者的轻松感觉所重新代替对任何事情都不那么特别看重。这让我觉得如此自由进入了我生命中最有创造力的一个阶段。), 0.46826816), (Document(metadata{source: ../../resource/knowledge.txt}, page_contentIm pretty sure none of this would have happened if I hadnt been fired from Apple. It was awful tasting medicine, but I guess the patient needed it. Sometimes life hits you in the head with a brick. Dont lose faith. Im convinced that the only thing that kept me going was that I loved what I did. Youve got to find what you love. And that is as true for your work as it is for your lovers. Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you havent found it yet, keep looking. Dont settle. As with all matters of the heart, youll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Dont settle.\n我可以非常肯定,如果我不被苹果公司开除的话这其中一件事情也不会发生的。这个良药的味道实在是太苦了但是我想病人需要这个药。有些时候生活会拿起一块砖头向你的脑袋上猛拍一下。不要失去信心我很清楚唯一使我一直走下去的就是我做的事情令我无比钟爱。你需要去找到你所爱的东西对于工作是如此对于你的爱人也是如此。你的工作将会占据生活中很大的一部分。你只有相信自己所做的是伟大的工作你才能怡然自得。如果你现在还没有找到那么继续找、不要停下来、全心全意的去找当你找到的时候你就会知道的。就像任何真诚的关系随着岁月的流逝只会越来越紧密。所以继续找直到你找到它不要停下来。\n\nMy third story is about death.\n我的第三个故事是关于死亡的。), 0.4740282)]保存和加载 您还可以保存和加载 FAISS 索引。这样做很有用因为您不必每次使用时都重新创建它。 #示例faiss_save.py #保存索引 db.save_local(faiss_index) #读取索引 new_db FAISS.load_local(faiss_index, embeddings,allow_dangerous_deserializationTrue) docs new_db.similarity_search(query)docs[0]page_contentDuring the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together. 在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。 metadata{source: ../../resource/knowledge.txt}Pinecone Pinecone介绍 Pinecone (/ˈpaɪnˌkon/ n. 松果松球)是一个功能广泛的向量数据库。 官方文档https://docs.pinecone.io/guides/get-started/quickstart 下面展示了如何使用与 Pinecone向量数据库相关的功能。 设置以下环境变量以便在本文档中进行操作 OPENAI_API_KEY您的 OpenAI API 密钥用于使用OpenAIEmbeddings %pip install –upgrade –quiet \langchain-pinecone \langchain-openai \langchain \pinecone-notebooks配置PINECONE_API_KEY 环境变量 setx PINECONE_API_KEY 首先让我们将我们的文本库拆分成分块的 docs #示例pinecone_search.py from langchain_community.document_loaders import TextLoader from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter loader TextLoader(../../resource/knowledge.txt,encodingUTF-8) documents loader.load() text_splitter CharacterTextSplitter(chunk_size1500, chunk_overlap0) docs text_splitter.split_documents(documents) embeddings OpenAIEmbeddings()新创建的 API 密钥已存储在PINECONE_API_KEY环境变量中。我们将使用它来设置 Pinecone 客户端。 import os pinecone_api_key os.environ.get(PINECONE_API_KEY) pinecone_api_key import time from pinecone import Pinecone, ServerlessSpec pc Pinecone(api_keypinecone_api_key)接下来让我们连接到您的 Pinecone 索引。如果名为index_name的索引不存在将会被创建。 import time index_name langchain-index # 如果需要可以更改 existing_indexes [index_info[name] for index_info in pc.list_indexes()] if index_name not in existing_indexes:pc.create_index(nameindex_name,dimension1536,metriccosine,specServerlessSpec(cloudaws, regionus-east-1),)while not pc.describe_index(index_name).status[ready]:time.sleep(1) index pc.Index(index_name)现在我们的 Pinecone 索引已经设置好我们可以使用PineconeVectorStore.from_documents将这些分块的文档作为内容进行更新。 from langchain_pinecone import PineconeVectorStore docsearch PineconeVectorStore.from_documents(docs, embeddings, index_nameindex_name)query Pixar docs docsearch.similarity_search(query) print(docs[0].page_content)During the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together. 在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。查看index创建https://app.pinecone.io Lance LanceDB介绍 Lance /læns/ 长矛执矛战士[医]柳叶刀是一个基于持久存储构建的用于向量搜索的开源数据库极大地简化了嵌入式的检索、过滤和管理。完全开源。 官网https://lancedb.com/ 官方文档https://lancedb.github.io/lancedb/basic/ 下面展示如何使用与 LanceDB向量数据库相关的功能基于 Lance 数据格式。 ! pip install -U langchain-openai! pip install lancedb#示例lance_search.py from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import LanceDB from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitterloader TextLoader(../../resource/knowledge.txt, encodingUTF-8) documents loader.load() text_splitter CharacterTextSplitter(chunk_size1500, chunk_overlap0) documents text_splitter.split_documents(documents) embeddings OpenAIEmbeddings()向量存储 docsearch LanceDB.from_documents(documents, embeddings) query Pixar公司是做什么的? docs docsearch.similarity_search(query) print(docs[0].page_content)During the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I retuned to Apple, and the technology we developed at NeXT is at the heart of Apples current renaissance. And Laurene and I have a wonderful family together. 在接下来的五年里, 我创立了一个名叫 NeXT 的公司还有一个叫Pixar的公司然后和一个后来成为我妻子的优雅女人相识。Pixar 制作了世界上第一个用电脑制作的动画电影——“”玩具总动员”Pixar 现在也是世界上最成功的电脑制作工作室。在后来的一系列运转中Apple 收购了NeXT然后我又回到了苹果公司。我们在NeXT 发展的技术在 Apple 的复兴之中发挥了关键的作用。我还和 Laurence 一起建立了一个幸福的家庭。print(docs[0].metadata){source: ../../resource/knowledge.txt}此外要探索表格可以将其加载到数据框中或将其保存在 csv 文件中 tbl docsearch.get_table() print(tbl:, tbl) pd_df tbl.to_pandas() pd_df.to_csv(docsearch.csv, indexFalse)tbl: LanceTable(connectionLanceDBConnection(D:\tmp\lancedb), namevectorstore)总结 下面是 Pinecone、FAISS 和 Lance 三个向量数据库的功能对比表格 功能/特性PineconeFAISSLance数据模型向量 元数据向量向量 元数据支持的索引类型HNSWIVF, HNSW, PQ, OPQHNSW扩展性高中高实时性实时更新批量处理实时更新多样化查询向量相似性搜索向量相似性搜索向量相似性搜索分布式架构是否是支持的语言Python, JavaScript, GoPython, CPython社区支持活跃活跃新兴开源许可证专有MITApache 2.0部署选项云本地本地, 云额外功能自动扩展, 数据管理工具高度优化的搜索性能数据版本控制, 集成分析工具 这些数据库在功能和特性上各有优势选择合适的数据库应根据具体的应用需求和技术栈来决定。
- 上一篇: wap多用户网站使用php如何做购物网站
- 下一篇: wap建站wordpress html 标签
相关文章
-
wap多用户网站使用php如何做购物网站
wap多用户网站使用php如何做购物网站
- 站长
- 2026年02月18日
-
wap版网站建设方案网站推广的意义和方法
wap版网站建设方案网站推广的意义和方法
- 站长
- 2026年02月18日
-
wap 网站源码有没有做公章的网站
wap 网站源码有没有做公章的网站
- 站长
- 2026年02月18日
-
wap建站wordpress html 标签
wap建站wordpress html 标签
- 站长
- 2026年02月18日
-
wap建站程序做网站选关键词
wap建站程序做网站选关键词
- 站长
- 2026年02月18日
-
wap建站后只能访问首页工地建筑劳务公司招工平台
wap建站后只能访问首页工地建筑劳务公司招工平台
- 站长
- 2026年02月18日
