如何从 Azure OpenAI 获取嵌入

本文提供了一个Azure嵌入示例,介绍了如何使用OpenAI API创建一个可用于创建嵌入的部署,以及如何将示例嵌入发送到部署。通过可以快速了解如何在Azure中使用OpenAI API进行嵌入操作。

从 Azure OpenAI 获取嵌入

本文提供了一个 Azure 嵌入示例,介绍了如何使用 OpenAI API 创建一个可用于创建嵌入的部署,以及如何将示例嵌入发送到部署。

Azure 嵌入示例

在此示例中,我们将尝试遍历使用 Azure 端点完成工作所需的所有操作。
此示例侧重于完成,但也涉及使用 API 也可用的其他一些操作。 此示例旨在快速展示简单操作,并非教程。

import openai
from openai import cli

设置

为了使以下部分正常工作,我们首先必须设置一些东西。 让我们从 api_baseapi_version 开始。 要找到您的 api_base,请转到 https://portal.azure.com,找到您的资源,然后在“资源管理”->“键和端点”下查找“端点”值。

openai.api_version = '2022-12-01'
openai.api_base = '' # Please add your endpoint here

接下来我们必须设置 api_typeapi_key。 我们可以从门户网站获取密钥,也可以通过 Microsoft Active Directory 身份验证获取密钥。 取决于此,api_typeazureazure_ad

设置:门户

让我们首先看看从门户中获取密钥。 转到 https://portal.azure.com,找到您的资源,然后在“资源管理”->“Keys and Endpoints”下查找“Keys”值之一。

openai.api_type = 'azure'
openai.api_key = ''  # Please add your api key here

(可选)设置:Microsoft Active Directory 身份验证

现在让我们看看如何通过 Microsoft Active Directory 身份验证获取密钥。 如果您想使用 Active Directory 身份验证而不是门户中的密钥,请取消注释以下代码。

# from azure.identity import DefaultAzureCredential

# default_credential = DefaultAzureCredential()
# token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

# openai.api_type = 'azure_ad'
# openai.api_key = token.token

部署

在本节中,我们将创建一个可用于创建嵌入的部署。

部署:手动创建

让我们使用 text-similarity-curie-001 模型创建部署。 通过转到门户中“资源管理”->“模型部署”下的资源来创建新部署。

(可选)部署:以编程方式创建

我们还可以使用代码创建部署:

model = "text-similarity-curie-001"

# Now let's create the deployment
print(f'Creating a new deployment with model: {model}')
result = openai.Deployment.create(model=model, scale_settings={"scale_type":"standard"})
deployment_id = result["id"]

(可选)部署:检索

现在让我们检查新创建的部署的状态

print(f'Checking for deployment status.')
resp = openai.Deployment.retrieve(id=deployment_id)
status = resp["status"]
print(f'Deployment {deployment_id} is with status: {status}')

部署:列表

现在因为创建新部署需要很长时间,让我们在订阅中查看已成功完成的部署。

print('While deployment running, selecting a completed one that supports embeddings.')
deployment_id = None
result = openai.Deployment.list()
for deployment in result.data:
    if deployment["status"] != "succeeded":
        continue
    
    model = openai.Model.retrieve(deployment["model"])
    if model["capabilities"]["embeddings"] != True:
        continue
    
    deployment_id = deployment["id"]
    break

if not deployment_id:
    print('No deployment with status: succeeded found.')
else:
    print(f'Found a succeeded deployment that supports embeddings with id: {deployment_id}.')

嵌入

现在让我们将示例嵌入发送到部署。

embeddings = openai.Embedding.create(deployment_id=deployment_id,
                                     input="The food was delicious and the waiter...")
                                
print(embeddings)

(可选)部署:删除

最后让我们删除部署

print(f'Deleting deployment: {deployment_id}')
openai.Deployment.delete(sid=deployment_id)

此文章由OpenAI开源维基百科原创发布,如若转载请注明出处:https://openai.wiki/openai-azure-embeddings-example.html

(0)
上一篇 2023-02-20 21:06
下一篇 2023-02-20 21:36

相关推荐

  • Auto-GPT|ChatGPT自动化

    Auto-GPT是一个实验性开源应用程序,展示了GPT-4语言模型的能力。它具备互联网搜索、长期和短期记忆管理、文本生成、访问流行网站和平台等功能,使用GPT-3.5和GPT-4进行文件存储和摘要。

    ChatGPT 2023-04-13
    0315.6K
  • CPT-3|微调GPT-3以对文本进行分类的最佳做法

    本文介绍了如何使用GPT-3对文本进行分类,包括微调GPT-3模型、训练数据的规模、质量、代表性和指定充分,以及如何设置训练数据的格式和使用分隔符序列,微调模型可以超过文本分类基准上的最新记录。

    2023-02-20
    023.0K
  • ChatGPT|插件教程 – 1

    ChatGPT插件AITickerChat、DEV Community、GetYourGuide、Trip.com、Cloudflare Radar、Savvy Trader AI等使用教程。

    2023-05-19
    002.2K
  • ChatGPT|插件教程 – 2

    ChatGPT插件Keyplays Live Soccer、OwlJourney、Prompt Perfect、AskYourPDF、Word Sneak、Link Reader等使用教程。

    2023-05-21
    001.5K
  • ChatGPT|常见报错问题汇总

    本文介绍了ChatGPT常见的网络错误及其解决方法,包括:网络错误, 请求过多, 解决方法, API 接口, Plus 账号,openAI,1小时内请求过多,模型过载,引擎不存在,发生错误等解决方法。

    ChatGPT 2023-02-21
    0210.0K

发表回复

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

微信