如果要开发生成式 AI 应用程序,则可以使用 GitHub 模型免费查找和试验 AI 模型。 实验环境和免费 API 使用受到每分钟请求数、每天请求数、每个请求的令牌数和并发请求数的速率限制。 如果受到速率限制,则需要等待命中的速率限制重置,然后才能发出更多请求。
准备好将应用程序引入生产环境后,可以通过在 Azure 订阅中部署 Azure AI 服务资源并开始使用 Azure AI Foundry 模型服务来升级体验。 不需要更改代码中的其他任何内容。
以下文章介绍如何从 GitHub 模型开始,以及如何使用 Azure AI Foundry 模型部署 Azure AI 服务资源。
先决条件
要完成本教程,您需要:
- 有权访问 GitHub 模型的 GitHub 帐户。
- Azure 订阅。 如果没有,当准备好将模型部署到生产环境时,系统会提示创建或将 Azure 帐户更新为标准帐户。
升级到 Azure AI Foundry 模型
操场和免费 API 使用的速率限制旨在帮助你试验模型并开发 AI 应用程序。 准备好将应用程序引入生产环境后,请使用付费 Azure 帐户中的密钥和终结点。 不需要更改代码中的其他任何内容。
若要获取密钥和终结点,请执行以下操作:
转到“GitHub 模型”并选择你感兴趣的模型。
在模型的操场中,选择“获取 API 密钥”。
选择“获取生产密钥”。
如果没有 Azure 帐户,请选择“创建我的帐户”,然后按照步骤创建一个帐户。
如果有 Azure 帐户,请选择“登录”。
如果现有帐户是免费帐户,则首先必须升级到标准计划。 升级后,返回到操场并再次选择“获取 API 密钥”,然后使用升级后的帐户登录。
登录到 Azure 帐户后,会进入 Azure AI Foundry > GitHub。 在 AI Foundry 中加载初始模型详细信息可能需要一两分钟。
页面将加载模型的详细信息。 选择“部署”按钮,将模型部署到帐户。
部署后,模型 API 密钥和终结点会显示在概述中。 在代码中使用这些值,以在生产环境中使用该模型。
此时,所选的模型可供使用。
升级代码以使用新终结点
配置 Azure AI 服务资源后,即可开始从代码中使用它。 若要使用 Azure AI 服务资源,你需要终结点 URL 和密钥,可在“概览”部分中获取它们:
可以使用任何受支持的 SDK 从终结点中获取预测。 正式支持以下 SDK:
- OpenAI SDK
- Azure OpenAI SDK
- Azure AI 推理 SDK
有关更多详细信息和示例,请参阅支持的语言和 SDK 部分。 以下示例演示如何将 Azure AI Foundry 模型 SDK 与新部署的模型配合使用:
使用包管理器(例如 pip)安装包 azure-ai-inference
:
pip install azure-ai-inference
然后,可以使用包来使用模型。 以下示例演示如何创建客户端来使用聊天补全:
import os
from azure.ai.inference import ChatCompletionsClient
from azure.core.credentials import AzureKeyCredential
client = ChatCompletionsClient(
endpoint="https://<resource>.services.ai.azure.com/models",
credential=AzureKeyCredential(os.environ["AZURE_INFERENCE_CREDENTIAL"]),
)
浏览我们的示例,并阅读 API 参考文档以开始使用。
使用 npm 安装包 @azure-rest/ai-inference
:
npm install @azure-rest/ai-inference
然后,可以使用包来使用模型。 以下示例演示如何创建客户端来使用聊天补全:
import ModelClient from "@azure-rest/ai-inference";
import { isUnexpected } from "@azure-rest/ai-inference";
import { AzureKeyCredential } from "@azure/core-auth";
const client = new ModelClient(
"https://<resource>.services.ai.azure.com/models",
new AzureKeyCredential(process.env.AZURE_INFERENCE_CREDENTIAL)
);
浏览我们的示例,并阅读 API 参考文档以开始使用。
请使用以下命令安装 Azure AI 推理库:
dotnet add package Azure.AI.Inference --prerelease
导入下列命名空间:
using Azure;
using Azure.Identity;
using Azure.AI.Inference;
然后,可以使用包来使用模型。 以下示例演示如何创建客户端来使用聊天补全:
ChatCompletionsClient client = new ChatCompletionsClient(
new Uri("https://<resource>.services.ai.azure.com/models"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("AZURE_INFERENCE_CREDENTIAL"))
);
浏览我们的示例,并阅读 API 参考文档以开始使用。
将包添加到项目:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-inference</artifactId>
<version>1.0.0-beta.1</version>
</dependency>
然后,可以使用包来使用模型。 以下示例演示如何创建客户端来使用聊天补全:
ChatCompletionsClient client = new ChatCompletionsClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://<resource>.services.ai.azure.com/models")
.buildClient();
浏览我们的示例,并阅读 API 参考文档以开始使用。
使用参考部分来探索 API 设计以及可用的参数。 例如,聊天补全的参考部分详细介绍了如何使用路由 /chat/completions
根据聊天格式的指令生成预测。 请注意,URL 的根目录包含路径 /models
:
请求
POST https://<resource>.services.ai.azure.com/models/chat/completions?api-version=2024-05-01-preview
api-key: <api-key>
Content-Type: application/json
生成第一个聊天补全:
from azure.ai.inference.models import SystemMessage, UserMessage
response = client.complete(
messages=[
SystemMessage(content="You are a helpful assistant."),
UserMessage(content="Explain Riemann's conjecture in 1 paragraph"),
],
model="mistral-large"
)
print(response.choices[0].message.content)
var messages = [
{ role: "system", content: "You are a helpful assistant" },
{ role: "user", content: "Explain Riemann's conjecture in 1 paragraph" },
];
var response = await client.path("/chat/completions").post({
body: {
messages: messages,
model: "mistral-large"
}
});
console.log(response.body.choices[0].message.content)
requestOptions = new ChatCompletionsOptions()
{
Messages = {
new ChatRequestSystemMessage("You are a helpful assistant."),
new ChatRequestUserMessage("Explain Riemann's conjecture in 1 paragraph")
},
Model = "mistral-large"
};
response = client.Complete(requestOptions);
Console.WriteLine($"Response: {response.Value.Content}");
List<ChatRequestMessage> chatMessages = new ArrayList<>();
chatMessages.add(new ChatRequestSystemMessage("You are a helpful assistant"));
chatMessages.add(new ChatRequestUserMessage("Explain Riemann's conjecture in 1 paragraph"));
ChatCompletions chatCompletions = client.complete(new ChatCompletionsOptions(chatMessages));
for (ChatChoice choice : chatCompletions.getChoices()) {
ChatResponseMessage message = choice.getMessage();
System.out.println("Response:" + message.getContent());
}
请求
POST https://<resource>.services.ai.azure.com/models/chat/completions?api-version=2024-05-01-preview
api-key: <api-key>
Content-Type: application/json
{
"messages": [
{
"role": "system",
"content": "You are a helpful assistant"
},
{
"role": "user",
"content": "Explain Riemann's conjecture in 1 paragraph"
}
],
"model": "mistral-large"
}
使用参数 model="<deployment-name>
将请求路由到此部署。 部署在某些配置下充当给定模型的别名。 请参阅“路由概念”页,了解 Azure AI 服务如何路由部署。
重要
与已配置所有模型的 GitHub 模型相反,Azure AI 服务资源允许你控制哪些模型在终结点中可用以及在哪种配置下可用。 在 model
参数中指示模型之前,请添加你计划使用的任意数量的模型。 了解如何向资源添加更多模型。
探索其他功能
Azure AI Foundry 模型支持 GitHub 模型中不提供的其他功能,包括:
遇到麻烦?
请参阅“常见问题解答”部分 ,了解更多帮助。
后续步骤