官方文档
Azure OpenAI 是微软提供的一项云服务,旨在将 OpenAI 的先进人工智能模型与 Azure 的基础设施和服务相结合。通过 Azure OpenAI,开发者和企业可以访问 OpenAI 的各种模型,如 GPT-3、Codex 和 DALL-E 等,并将其集成到自己的应用程序和服务中。
调用方式
-
API 调用:用户可以通过
HTTP 请求
来调用 Azure OpenAI 提供的 REST API。请求中需要包含 API 密钥进行身份验证,并指定所需的模型和参数。例如,发送 POST 请求到特定的 API 端点,附带请求体中的输入数据和请求头中的 API 密钥。import requests url = "https://<your-resource-name>.openai.azure.com/openai/deployments/<deployment-id>/completions?api-version=2023-07-01-preview" headers = { "Content-Type": "application/json", "api-key": "<your-api-key>" } data = { "prompt": "Hello, world!", "max_tokens": 5 } response = requests.post(url, headers=headers, json=data) print(response.json())
-
Azure SDK:微软提供了多种编程语言的 SDK(软件开发工具包),如 Python、C# 等,使得开发者能够更简便地与 Azure 服务进行交互。SDK 会封装底层的 API 调用,使得开发者可以通过更高层次的抽象和简化的代码来使用 Azure OpenAI 服务。
以 Python SDK 为例:
from azure.ai.openai import OpenAIClient from azure.identity import DefaultAzureCredential # 创建 OpenAI 客户端 credential = DefaultAzureCredential() client = OpenAIClient(endpoint="https://<your-resource-name>.openai.azure.com/", credential=credential) # 调用模型生成文本 response = client.completions.create( deployment_id="<deployment-id>", prompt="Hello, world!", max_tokens=5 ) print(response.choices[0].text)
Key 与 SDK 的区别
-
API 密钥(Key):
- 使用方式:通常直接通过 HTTP 请求的请求头或 URL 参数来传递。
- 管理:需要在 Azure 门户中生成和管理 API 密钥。
- 灵活性:可以在
不依赖特定编程语言
或 SDK 的情况下使用,适合任何可以发出 HTTP 请求的环境。
-
SDK: 需要endpoint和api-key
-
使用方式:通过调用封装在 SDK 中的函数或方法来进行 API 调用。
-
集成:通常与 Azure 生态系统中的其他服务更紧密集成,提供更一致的开发体验。
-
总的来说,API 密钥适合需要直接和灵活访问 API 的场景,而 SDK 适合需要简化开发流程和更深度集成的场景。
在pycharm的终端terminal运行
pip install openai
$env:AZURE_OPENAI_API_KEY = 'REPLACE_WITH_YOUR_KEY_VALUE_HERE'
$env:AZURE_OPENAI_ENDPOINT = 'REPLACE_WITH_YOUR_ENDPOINT_HERE'
# [System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_API_KEY', 'REPLACE_WITH_YOUR_KEY_VALUE_HERE', 'User')
# [System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_ENDPOINT', 'REPLACE_WITH_YOUR_ENDPOINT_HERE', 'User')
创建一个新的.py运行即可测试
import os
from openai import AzureOpenAI
client = AzureOpenAI(
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version="2024-02-01"
)
response = client.chat.completions.create(
model="gpt-35-turbo", # model = "deployment_name".
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Does Azure OpenAI support customer managed keys?"},
{"role": "assistant", "content": "Yes, customer managed keys are supported by Azure OpenAI."},
{"role": "user", "content": "Do other Azure AI services support this too?"}
]
)
print(response.choices[0].message.content)
运行即可python test.py
。如果要换其他模型比如GPT-4o(可用于标准和全球标准模型部署。) 需要在该模型可用的受支持标准或全球标准区域中创建或使用现有资源。
创建资源后,可以部署 GPT-4o 模型。 如果要执行编程部署,则“模型”名称为 gpt-4o,“版本”名称为 2024-05-13
报错没有找到环境变量,在终端查看:Get-ChildItem Env:
发现没有设置成功
正确的添加,(开头代码已修改为正确
$env:AZURE_OPENAI_API_KEY = 'REPLACE_WITH_YOUR_KEY_VALUE_HERE'
$env:AZURE_OPENAI_ENDPOINT = 'REPLACE_WITH_YOUR_ENDPOINT_HERE'
原因,一个立即生效,一个有作用时间和范围
response内容:
{
"id": "chatcmpl-9k4JY8EuQR3TLfai4ze8TMNylKTB0",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "Yes, several other Azure AI services also support customer managed keys (CMK). This capability allows you to have greater control over the encryption of your data by using your own encryption keys stored in Azure Key Vault. Some of the Azure AI services that support CMK include:\n\n1. **Azure Cognitive Services**:\n - Text Analytics\n - Computer Vision\n - Translator\n - Speech Services\n\n2. **Azure Machine Learning**:\n - Offers encryption of data at rest using customer managed keys.\n\n3. **Azure Synapse Analytics**:\n - Provides support for customer managed keys for data encryption.\n\nBy using customer managed keys, you can satisfy compliance requirements and have more control over key rotation and revocation. For specific implementation details and supported features, it's always a good idea to check the latest documentation for each service, as Microsoft regularly updates and expands their support for security features.",
"role": "assistant",
"function_call": null,
"tool_calls": null
},
"content_filter_results": {
"hate": {"filtered": false, "severity": "safe"},
"self_harm": {"filtered": false, "severity": "safe"},
"sexual": {"filtered": false, "severity": "safe"},
"violence": {"filtered": false, "severity": "safe"}
}
}
],
"created": 1720766700,
"model": "gpt-4o-2024-05-13",
"object": "chat.completion",
"service_tier": null,
"system_fingerprint": "fp_abc28019ad",
"usage": {
"completion_tokens": 179,
"prompt_tokens": 55,
"total_tokens": 234
},
"prompt_filter_results": [
{
"prompt_index": 0,
"content_filter_results": {
"hate": {"filtered": false, "severity": "safe"},
"self_harm": {"filtered": false, "severity": "safe"},
"sexual": {"filtered": false, "severity": "safe"},
"violence": {"filtered": false, "severity": "safe"}
}
}
]
}
可以看到response里面只有一个choice 为choice[0],所以是response.choices[0].message.content。