如何从 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
  • ChatGPT|制作清晰有效的Prompt指南

    本文介绍了如何制作清晰有效的ChatGPT提示以推动引人入胜且信息丰富的对话,从ChatGPT的基础知识及其工作原理到制作引人注目的提示和解决常见问题的高级技术的所有内容,本文提供了详细的指南和建议。

    ChatGPT 2023-02-18
    012.6K
  • ChatGPT_Academic|ChatGPT多功能拓展

    ChatGPT_Academic是一款科研工作专用的ChatGPT拓展插件,支持函数插件、自动润色、中英互译、代码解释、程序剖析、PDF和Word文献总结翻译、Tex公式、自我解析报告和源代码生成。

    2023-04-09
    034.3K
  • api_request_parallel_processor.py

    API 请求并行处理器使用OpenAI API快速处理大量文本需要小心。如果您逐一提交百万个API请求,它们将需要数天时间才能完成。如果您并行涌入一百万个API请求,它们将超出速率限制并因错误而失败。

    ChatGPT 2023-02-18
    002.3K
  • openAI|使用嵌入的建议

    本文将介绍如何使用嵌入和最近邻搜索来进行推荐,以找到与给定文章最相似的其他文章。本文将引入如何使用嵌入计算文章之间的距离,并使用最近邻搜索寻找与给定文章最相似的其他文章。

    ChatGPT 2023-02-20
    001.1K

发表回复

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

微信