使用Azure Cognitive Search结合OpenAI嵌入技术构建向量数据库

在人工智能和大数据的时代,将先进的搜索技术与强大的嵌入式模型相结合,已成为开发高效智能应用的关键。本教程将向您展示如何使用Azure Cognitive Search作为OpenAI嵌入技术的向量数据库,帮助您在云中构建一个丰富的搜索体验。

开始之前:必要条件和准备工作

环境准备

在开始实践之前,确保您已经具备以下条件:

  1. Azure Cognitive Search服务:注册并配置您的Azure Cognitive Search服务。
  2. OpenAI密钥或Azure OpenAI凭据:获取并配置您的OpenAI API密钥。
  3. 安装必要的库:通过pip安装如wgetazure-search-documents等必要的Python库。

导入所需库

import openai
import json
import wget
import pandas as pd
import zipfile
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.models import Vector
from azure.search.documents import SearchIndexingBufferedSender
from azure.search.documents.indexes.models import (
    SearchIndex, 
    SearchField, 
    SearchFieldDataType,
    SimpleField, 
    SearchableField, 
    SemanticConfiguration, 
    PrioritizedFields, 
    SemanticField, 
    VectorSearch, 
    HnswVectorSearchAlgorithmConfiguration
)

配置OpenAI和Azure Cognitive Search

设置OpenAI

openai.api_type = "azure"
openai.api_base = "YOUR_AZURE_OPENAI_ENDPOINT"
openai.api_version = "2023-05-15"
openai.api_key = "YOUR_AZURE_OPENAI_KEY"
model: str = "text-embedding-ada-002"

设置Azure Cognitive Search

search_service_endpoint: str = "YOUR_AZURE_SEARCH_ENDPOINT"
search_service_api_key: str = "YOUR_AZURE_SEARCH_ADMIN_KEY"
index_name: str = "azure-cognitive-search-vector-demo"
credential = AzureKeyCredential(search_service_api_key)

加载和准备数据

下载数据

使用wget下载并解压包含嵌入向量的Wikipedia文章数据。

embeddings_url = "https://cdn.openai.com/API/examples/data/vector_database_wikipedia_articles_embedded.zip"
wget.download(embeddings_url)
with zipfile.ZipFile("vector_database_wikipedia_articles_embedded.zip", "r") as zip_ref:
    zip_ref.extractall("../../data")

数据预处理

将下载的数据加载到Pandas DataFrame中,并准备用于上传到Azure Search。

article_df = pd.read_csv('../../data/vector_database_wikipedia_articles_embedded.csv')
article_df["title_vector"] = article_df.title_vector.apply(json.loads)
article_df["content_vector"] = article_df.content_vector.apply(json.loads)
article_df['vector_id'] = article_df['vector_id'].apply(str)

创建和配置搜索索引

定义索引结构

在Azure Search中创建一个新索引,定义包括文本字段和向量字段。

index_client = SearchIndexClient(
    endpoint=search_service_endpoint, credential=credential)

fields = [
    SimpleField(name="id", type=SearchFieldDataType.String),
    # ... 其他字段定义
]

index = SearchIndex(name=index_name, fields=fields, vector_search=vector_search)
result = index_client.create_or_update_index(index)
print(f'{result.name} created')

上传文档和嵌入向量

将数据上传至Azure Search

使用SearchIndexingBufferedSender将文档和嵌入向量上传至Azure Search。

documents = article_df.to_dict(orient='records')

with SearchIndexingBufferedSender(search_service_endpoint, index_name, credential) as batch_client:
    batch_client.upload_documents(documents=documents)

print(f"Uploaded {len(documents)} documents in total")

实现向量相似度搜索

搜索函数

定义一个函数,根据用户的查询生成嵌入,并在Azure Search中执行向量搜索。

def generate_embeddings(text):
    response = openai.Embedding.create(input=text, engine=model)
    return response['data'][0]['embedding']

query = "modern art in Europe"
search_client = SearchClient(search_service_endpoint, index_name, credential)
vector = Vector(value=generate_embeddings(query), k=3, fields="content_vector")

results = search_client.search(search_text=None, vectors=[vector])
for result in results:
    print(f"Title: {result['title']}\nURL: {result['url']}")

结语

通过本教程的学习,您已经掌握了如何使用Azure Cognitive Search结合OpenAI嵌入技术构建高效的向量数据库。这种方法不仅有利于提升搜索质量,还能为您的应用带来更加丰富和深入的搜索体验。

本文由作者 王大神 原创发布于 大神网的AI博客。

转载请注明作者:王大神

原文出处:使用Azure Cognitive Search结合OpenAI嵌入技术构建向量数据库

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 2023年11月18日
下一篇 2023年11月18日

相关推荐

  • Depay注册教程:免费申请美国Master/Visa信用卡/薅羊毛专用卡

    Depay(迪派)是一款数字钱包应用,它不仅支持加密货币充值和管理,还提供虚拟信用卡功能,让您可以在全球范围内进行在线支付和线下消费。本教程将为您详细介绍如何注册Depay,免费申请美国Master/Visa信用卡,以及…

    2023年2月14日
    00
  • 腾讯云向量数据库:引领大数据时代的新篇章

    在数字化时代,数据被视为黄金,而如何高效地管理和查询海量数据成为了企业和科研机构面临的挑战之一。腾讯云向量数据库的全面开放公测,为解决这一挑战提供了强大的解决方案。本文将深入探讨腾讯云向量数据库的特…

    2023年11月1日
    00
  • 如何通过Cloudflare Access连接到Google Workspace

    大家好,我是王大神,欢迎来到我的AI技术博客!在今天的数字化世界中,云服务和单点登录(SSO)变得越来越重要。谷歌工作空间(Google Workspace)作为一种广泛使用的云办公套件,为企业提供了高效的协作工具。但如…

    2023年9月25日
    00
  • 项目介绍:onedrive-cf-index-ng – 让你的OneDrive更强大

    在数字化时代,云存储已经成为了我们日常生活的一部分。而OneDrive作为微软提供的云存储服务,已经在全球范围内广泛使用。但是,你是否曾经感到过OneDrive的界面和功能有些局限?你是否曾经希望有一种更好的方式来…

    2023年10月21日
    00
  • 解锁高速云端世界:搬瓦工VPS 一站式解决方案

    想象一下,您是一位数字游民,对云端世界充满好奇和渴望,但却被繁琐的服务器设置所困扰。您渴望一种简单、高效的解决方案,以便快速启动您的项目或个人网站。搬瓦工VPS(Virtual Private Server)诞生了,正是为了…

    2024年3月12日
    00
  • 利用Azure OpenAI和Azure Cognitive Search打造定制聊天应用的教程

    在数字化时代,人工智能(AI)已经成为许多企业和开发者的不可或缺的一部分。聊天机器人和对话式AI已经在各个领域中发挥着关键作用,从客户支持到虚拟助手,再到智能搜索引擎。如果您希望构建自己的聊天应用程序,…

    2023年11月1日
    00

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注