使用向量数据库pinecone构建应用01:相似语义检索 Semantic Search

本文主要是介绍使用向量数据库pinecone构建应用01:相似语义检索 Semantic Search,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Building Applications with Vector Databases

下面是DeepLearning.AI上面这门课的学习笔记:https://www.deeplearning.ai/short-courses/building-applications-vector-databases/

Learn to create six exciting applications of vector databases and implement them using Pinecone.

Build a hybrid search app that combines both text and images for improved multimodal search results.

Learn how to build an app that measures and ranks facial similarity.

文章目录

  • Building Applications with Vector Databases
    • Lesson 1 - Semantic Search
      • Import the Needed Packages
      • Load the Dataset
      • Check cuda and Setup the model
      • Setup Pinecone
      • Create Embeddings and Upsert to Pinecone
      • Run Your Query

ChatGPT对Pinecone的介绍

Pinecone 是一个专门用于快速高效地进行向量索引和检索的托管服务。它主要用于存储和查询大规模的向量数据,例如文本嵌入、图像特征等。Pinecone 提供了一个简单易用的 API,使开发人员能够轻松地构建和部署基于向量的应用程序,而无需担心底层的基础架构。

使用 Pinecone,开发人员可以将向量数据加载到 Pinecone 的索引中,然后通过查询接口快速地检索相似向量。这种快速检索的能力使 Pinecone 在许多应用场景下都非常有用,如推荐系统、相似性搜索、聚类分析等。

总的来说,Pinecone 提供了一个高性能、可扩展且易于使用的平台,使开发人员能够利用向量数据来构建更智能和响应更快的应用程序。

Lesson 1 - Semantic Search

相似语义检索:检索相似的问题

在这里插入图片描述

requirements.txt

# requirements file
# note which revision of python, for example 3.9.6
# in this file, insert all the pip install needs, include revision#for example:
#torch==2.0.1
#matplotlib==3.7.2python-dotenv==1.0.0numpy==1.25.2
pandas==2.1.3
scikit-learn==1.3.2
sentence-transformers==2.2.2
matplotlib==3.8.2
torch==2.1.1langchain==0.0.346
openai==0.28.1 ## From the notebookspinecone-client==3.0.0dev4
pinecone-datasets==0.5.0rc11
pinecone-text==0.7.1tiktoken==0.5.2
tqdm==4.66.1datasets==2.15.0
deepface==0.0.79

Import the Needed Packages

import warnings
warnings.filterwarnings('ignore')
from datasets import load_dataset
from sentence_transformers import SentenceTransformer
from pinecone import Pinecone, ServerlessSpec
from DLAIUtils import Utils
import DLAIUtilsimport os
import time
import torch
from tqdm.auto import tqdm

其中DLAIUtils.py文件如下所示:

import os
import sys
from dotenv import load_dotenv, find_dotenvclass Utils:def __init__(self):passdef create_dlai_index_name(self, index_name):openai_key = ''if self.is_colab(): # google colabfrom google.colab import userdataopenai_key = userdata.get("OPENAI_API_KEY")else: # jupyter notebookopenai_key = os.getenv("OPENAI_API_KEY")return f'{index_name}-{openai_key[-36:].lower().replace("_", "-")}'def is_colab(self):return 'google.colab' in sys.modulesdef get_openai_api_key(self):_ = load_dotenv(find_dotenv())return os.getenv("OPENAI_API_KEY")def get_pinecone_api_key(self):_ = load_dotenv(find_dotenv())return os.getenv("PINECONE_API_KEY")

Load the Dataset

dataset = load_dataset('quora', split='train[240000:290000]')
dataset[:5]

output

{'questions': [{'id': [207550, 351729],'text': ['What is the truth of life?', "What's the evil truth of life?"]},{'id': [33183, 351730],'text': ['Which is the best smartphone under 20K in India?','Which is the best smartphone with in 20k in India?']},{'id': [351731, 351732],'text': ['Steps taken by Canadian government to improve literacy rate?','Can I send homemade herbal hair oil from India to US via postal or private courier services?']},{'id': [37799, 94186],'text': ['What is a good way to lose 30 pounds in 2 months?','What can I do to lose 30 pounds in 2 months?']},{'id': [351733, 351734],'text': ['Which of the following most accurately describes the translation of the graph y = (x+3)^2 -2 to the graph of y = (x -2)^2 +2?','How do you graph x + 2y = -2?']}],'is_duplicate': [False, True, False, True, False]}

把问题都保存在questions列表中

questions = []
for record in dataset['questions']:questions.extend(record['text'])
question = list(set(questions))
print('\n'.join(questions[:10]))
print('-' * 50)
print(f'Number of questions: {len(questions)}')
  1. question = list(set(questions)): 使用 set() 函数去除 questions 列表中的重复项,然后将其转换为集合。再将集合转换回列表,得到一个去重后的问题列表,存储在 question 变量中。
  2. print('\n'.join(questions[:10])): 将去重前的前十个问题打印出来,join() 函数用于将列表中的元素连接成一个字符串,并使用换行符 \n 分隔。

Output:输出前10个问题

What is the truth of life?
What's the evil truth of life?
Which is the best smartphone under 20K in India?
Which is the best smartphone with in 20k in India?
Steps taken by Canadian government to improve literacy rate?
Can I send homemade herbal hair oil from India to US via postal or private courier services?
What is a good way to lose 30 pounds in 2 months?
What can I do to lose 30 pounds in 2 months?
Which of the following most accurately describes the translation of the graph y = (x+3)^2 -2 to the graph of y = (x -2)^2 +2?
How do you graph x + 2y = -2?
--------------------------------------------------
Number of questions: 100000

Check cuda and Setup the model

Note: “Checking cuda” refers to checking if you have access to GPUs (faster compute). In this course, we are using CPUs. So, you might notice some code cells taking a little longer to run.

We are using all-MiniLM-L6-v2 sentence-transformers model that maps sentences to a 384 dimensional dense vector space.

device = 'cuda' if torch.cuda.is_available() else 'cpu'
if device != 'cuda':print('Sorry no cuda.')
model = SentenceTransformer('all-MiniLM-L6-v2', device=device)

这行代码使用了 SentenceTransformer 库中的 SentenceTransformer 类来加载一个预训练的文本嵌入模型。具体地,它加载了一个名为 ‘all-MiniLM-L6-v2’ 的预训练模型,并指定了一个设备(可能是 GPU 或 CPU)来运行模型。

通过加载这个预训练的文本嵌入模型,可以将文本转换为高维向量表示,这些向量可以用于各种自然语言处理任务,如文本相似度计算、文本分类、聚类等。

query = 'which city is the most populated in the world?'
xq = model.encode(query)
xq.shape

Output维度为384维

(384,)

Setup Pinecone

utils = Utils()
PINECONE_API_KEY = utils.get_pinecone_api_key()
pinecone = Pinecone(api_key=PINECONE_API_KEY)
INDEX_NAME = utils.create_dlai_index_name('dl-ai')if INDEX_NAME in [index.name for index in pinecone.list_indexes()]:pinecone.delete_index(INDEX_NAME)
print(INDEX_NAME)
pinecone.create_index(name=INDEX_NAME, dimension=model.get_sentence_embedding_dimension(), metric='cosine',spec=ServerlessSpec(cloud='aws', region='us-west-2'))index = pinecone.Index(INDEX_NAME)
print(index)

这段代码使用 Pinecone 库中的 create_index() 函数创建了一个新的索引。下面是对每个参数的解释:

  • name=INDEX_NAME: 这个参数指定了要创建的索引的名称。INDEX_NAME 是一个变量,用于存储索引的名称。

  • dimension=model.get_sentence_embedding_dimension(): 这个参数指定了向量的维度。在这里,使用了一个叫做 model 的对象的 get_sentence_embedding_dimension() 方法来获取向量的维度。这通常是一个预训练模型返回的嵌入向量的维度。

  • metric='cosine': 这个参数指定了用于衡量向量相似性的距离度量。在这里,使用的是余弦相似度作为度量标准,它用于衡量两个向量之间的夹角余弦值,常用于文本和图像等嵌入向量的相似性比较。

  • spec=ServerlessSpec(cloud='aws', region='us-west-2'): 这个参数指定了索引的部署规格,即在哪个云提供商的哪个地区部署索引。在这里,使用了一个名为 ServerlessSpec 的规格对象,其中 cloud 参数指定了云提供商,这里是 AWS,region 参数指定了部署的地区,这里是美国西部的 us-west-2 区域。

总的来说,这段代码创建了一个新的 Pinecone 索引,指定了索引的名称、向量维度、相似性度量和部署规格。

Output

dl-ai-z7nf9tjs3lswddk6jinpkrukpb7cjfq0mwts
<pinecone.data.index.Index object at 0x7ff8e7697970>

Create Embeddings and Upsert to Pinecone

batch_size=200
vector_limit=10000questions = question[:vector_limit] # 从问题列表中选择前 10000 个问题import json
# 使用 tqdm 函数创建一个进度条,循环处理问题列表中的每个批次,每次处理 batch_size 个问题
for i in tqdm(range(0, len(questions), batch_size)): # find end of batchi_end = min(i+batch_size, len(questions))# create IDs batchids = [str(x) for x in range(i, i_end)]# create metadata batchmetadatas = [{'text': text} for text in questions[i:i_end]] # 每个元数据包含一个问题的文本信息# create embeddingsxc = model.encode(questions[i:i_end]) #  使用预训练的模型将当前批次的问题文本转换为嵌入向量。# create records list for upsertrecords = zip(ids, xc, metadatas) # `zip()` 函数将 `ids`、`xc` 和 `metadatas` 中对应位置的元素组合成一个元组# upsert to Pineconeindex.upsert(vectors=records)

解释zip函数

zip() 函数是 Python 中的一个内置函数,它接受任意数量的可迭代对象作为参数,并返回一个由这些可迭代对象的元素组成的元组序列,同时会截断到最短的可迭代对象的长度。

下面是 zip() 函数的基本语法:

zip(iterable1, iterable2, ...)
  • iterable1, iterable2, …:表示一个或多个可迭代对象,比如列表、元组、集合等。

zip() 函数会将每个可迭代对象中相同位置的元素组合成一个元组,然后返回一个由这些元组组成的迭代器。

例如:

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
result = zip(list1, list2)
print(list(result))

输出结果为:

[(1, 'a'), (2, 'b'), (3, 'c')]

在给定的示例中:

records = zip(ids, xc, metadatas)

zip() 函数将 idsxcmetadatas 中对应位置的元素组合成一个元组,并返回一个由这些元组组成的迭代器。这样可以方便地将多个列表中对应位置的元素一起处理,通常用于迭代处理多个列表或集合。

index.describe_index_stats()

Output

index.describe_index_stats()
{'dimension': 384,'index_fullness': 0.0,'namespaces': {'': {'vector_count': 10000}},'total_vector_count': 10000}

Run Your Query

# small helper function so we can repeat queries later
def run_query(query):embedding = model.encode(query).tolist()results = index.query(top_k=10, vector=embedding, include_metadata=True, include_values=False)for result in results['matches']:print(f"{round(result['score'], 2)}: {result['metadata']['text']}")

测试run_query:输出10条最相近的问题

run_query('which city has the highest population in the world?')

Output

0.67: Which city in the world is the most beautiful to live in?
0.62: How many cities are on Earth?
0.58: Which country has the highest per capita income?
0.55: What is the recent population of India?
0.54: What is the most remote place in the world?
0.54: What are the largest slums in the world?
0.53: What is the least known country in the world?
0.53: Which is the coldest country in the world?
0.52: Which are the worst cities of India?
0.49: Which are the most dangerous places on the earth to live? Why?

另一个query进行测试

query = 'how do i make chocolate cake?'
run_query(query)

Output

0.58: What's a good recipe for cake featuring Ciroc?
0.56: How do you bake cakes in a convection oven?
0.55: How do I bake a cake in a microwave oven?
0.54: How do you make homemade frosting without powdered sugar?
0.54: How do you make a crispy batter?
0.53: How do you make a perfume out of Skittles?
0.49: What is the difference between chocolate and truffles?
0.49: How can I make a banana pudding without bananas?
0.46: Where can I get custom decorated cakes for any occasion at Gold Coast?
0.46: How can one make the Mint Mojito coffee at home similar to the one at Phillz?

这篇关于使用向量数据库pinecone构建应用01:相似语义检索 Semantic Search的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/739968

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.