亚马逊基石
Amazon Bedrock 是一款全托管服务,提供来自 AI21 Labs、Anthropic、Cohere、Meta、Mistral AI、Stability AI 和 Amazon 等领先 AI 公司的高性能基础模型(FM)选择。每个模型都可以通过一个通用 API 访问,这个 API 提供了一整套功能,帮助你在考虑安全、隐私和负责任 AI 的前提下构建生成式 AI 应用。
本指南将带你通过一个使用 Amazon Bedrock SDK 和 vecs 的示例。我们将使用 Amazon Titan Embeddings G1 – Text v1.2(amazon.titan-embed-text-v1)模型创建嵌入,将这些嵌入使用 vecs 插入到 Postgres 数据库中,然后查询集合以找到与给定查询句子最相似的句子。
🌐 This guide will walk you through an example using Amazon Bedrock SDK with vecs. We will create embeddings using the Amazon Titan Embeddings G1 – Text v1.2 (amazon.titan-embed-text-v1) model, insert these embeddings into a Postgres database using vecs, and then query the collection to find the most similar sentences to a given query sentence.
创建一个环境 #
🌐 Create an environment
首先,你需要设置你的环境。你需要安装 Python 3.7 以上版本,并安装 vecs 和 boto3 库。
🌐 First, you need to set up your environment. You will need Python 3.7+ with the vecs and boto3 libraries installed.
你可以用 pip 安装所需的 Python 库:
🌐 You can install the necessary Python libraries using pip:
1pip install vecs boto3你还需要:
🌐 You'll also need:
创建嵌入 #
🌐 Create embeddings
接下来,我们将使用亚马逊的 Titan Embedding G1 - Text v1.2 模型为一组句子创建嵌入。
🌐 Next, we will use Amazon’s Titan Embedding G1 - Text v1.2 model to create embeddings for a set of sentences.
1import boto32import vecs3import json45client = boto3.client(6 'bedrock-runtime',7 region_name='us-east-1',8 # Credentials from your AWS account9 aws_access_key_id='<replace_your_own_credentials>',10 aws_secret_access_key='<replace_your_own_credentials>',11 aws_session_token='<replace_your_own_credentials>',12)1314dataset = [15 "The cat sat on the mat.",16 "The quick brown fox jumps over the lazy dog.",17 "Friends, Romans, countrymen, lend me your ears",18 "To be or not to be, that is the question.",19]2021embeddings = []2223for sentence in dataset:24 # invoke the embeddings model for each sentence25 response = client.invoke_model(26 body= json.dumps({"inputText": sentence}),27 modelId= "amazon.titan-embed-text-v1",28 accept = "application/json",29 contentType = "application/json"30 )31 # collect the embedding from the response32 response_body = json.loads(response["body"].read())33 # add the embedding to the embedding list34 embeddings.append((sentence, response_body.get("embedding"), {}))把嵌入存到 vecs 里 #
🌐 Store the embeddings with vecs
既然我们有了嵌入,现在可以用 vecs 把它们插入到 Postgres 数据库里了。
🌐 Now that we have our embeddings, we can insert them into a Postgres database using vecs.
1import vecs23DB_CONNECTION = "postgresql://<user>:<password>@<host>:<port>/<db_name>"45# create vector store client6vx = vecs.Client(DB_CONNECTION)78# create a collection named 'sentences' with 1536 dimensional vectors9# to match the default dimension of the Titan Embeddings G1 - Text model10sentences = vx.get_or_create_collection(name="sentences", dimension=1536)1112# upsert the embeddings into the 'sentences' collection13sentences.upsert(records=embeddings)1415# create an index for the 'sentences' collection16sentences.create_index()查询最相似的句子 #
🌐 Querying for most similar sentences
现在,我们查询 sentences 集合以找到与示例查询句子最相似的句子。首先需要为查询句子创建一个向量表示。接着,我们查询之前创建的集合以找到最相似的句子。
🌐 Now, we query the sentences collection to find the most similar sentences to a sample query sentence. First need to create an embedding for the query sentence. Next, we query the collection we created earlier to find the most similar sentences.
1query_sentence = "A quick animal jumps over a lazy one."23# create vector store client4vx = vecs.Client(DB_CONNECTION)56# create an embedding for the query sentence7response = client.invoke_model(8 body= json.dumps({"inputText": query_sentence}),9 modelId= "amazon.titan-embed-text-v1",10 accept = "application/json",11 contentType = "application/json"12 )1314response_body = json.loads(response["body"].read())1516query_embedding = response_body.get("embedding")1718# query the 'sentences' collection for the most similar sentences19results = sentences.query(20 data=query_embedding,21 limit=3,22 include_value = True23)2425# print the results26for result in results:27 print(result)这会返回与查询向量最相似的3条记录及它们的距离。
🌐 This returns the most similar 3 records and their distance to the query vector.
1('The quick brown fox jumps over the lazy dog.', 0.27600620558852)2('The cat sat on the mat.', 0.609986272479202)3('To be or not to be, that is the question.', 0.744849503688346)资源 #
🌐 Resources