import os
from azure.ai.inference import ChatCompletionsClient
from azure.core.credentials import AzureKeyCredential
model = ChatCompletionsClient(
endpoint=os.environ["AZUREAI_ENDPOINT_URL"],
credential=AzureKeyCredential(os.environ["AZUREAI_ENDPOINT_KEY"]),
)
如果使用支持 Entra ID 的终结点,可以按如下所示创建客户端:
import os
from azure.ai.inference import ChatCompletionsClient
from azure.identity import AzureDefaultCredential
model = ChatCompletionsClient(
endpoint=os.environ["AZUREAI_ENDPOINT_URL"],
credential=AzureDefaultCredential(),
)
import ModelClient from "@azure-rest/ai-inference";
import { isUnexpected } from "@azure-rest/ai-inference";
import { AzureKeyCredential } from "@azure/core-auth";
const client = new ModelClient(
process.env.AZUREAI_ENDPOINT_URL,
new AzureKeyCredential(process.env.AZUREAI_ENDPOINT_KEY)
);
对于支持 Microsoft Entra ID 的终结点,则可以按如下所示创建客户端:
import ModelClient from "@azure-rest/ai-inference";
import { isUnexpected } from "@azure-rest/ai-inference";
import { AzureDefaultCredential } from "@azure/identity";
const client = new ModelClient(
process.env.AZUREAI_ENDPOINT_URL,
new AzureDefaultCredential()
);
对于支持 Microsoft Entra ID (以前称为 Azure Active Directory) 的终结点,请安装 Azure.Identity 包:
dotnet add package Azure.Identity
导入下列命名空间:
using Azure;
using Azure.Identity;
using Azure.AI.Inference;
然后,可以使用包来使用模型。 以下示例演示如何创建客户端来使用聊天补全:
ChatCompletionsClient client = new ChatCompletionsClient(
new Uri(Environment.GetEnvironmentVariable("AZURE_INFERENCE_ENDPOINT")),
new AzureKeyCredential(Environment.GetEnvironmentVariable("AZURE_INFERENCE_CREDENTIAL"))
);
对于支持 Microsoft Entra ID (以前称为 Azure Active Directory) 的终结点:
ChatCompletionsClient client = new ChatCompletionsClient(
new Uri(Environment.GetEnvironmentVariable("AZURE_INFERENCE_ENDPOINT")),
new DefaultAzureCredential(includeInteractiveCredentials: true)
);
from azure.ai.inference.models import SystemMessage, UserMessage
response = model.complete(
messages=[
SystemMessage(content="You are a helpful assistant."),
UserMessage(content="How many languages are in the world?"),
],
model_extras={
"safe_mode": True
}
)
print(response.choices[0].message.content)
var messages = [
{ role: "system", content: "You are a helpful assistant" },
{ role: "user", content: "How many languages are in the world?" },
];
var response = await client.path("/chat/completions").post({
"extra-parameters": "pass-through",
body: {
messages: messages,
safe_mode: true
}
});
console.log(response.choices[0].message.content)
requestOptions = new ChatCompletionsOptions()
{
Messages = {
new ChatRequestSystemMessage("You are a helpful assistant."),
new ChatRequestUserMessage("How many languages are in the world?")
},
AdditionalProperties = { { "logprobs", BinaryData.FromString("true") } },
};
response = client.Complete(requestOptions, extraParams: ExtraParameters.PassThrough);
Console.WriteLine($"Response: {response.Value.Choices[0].Message.Content}");
Request
POST /chat/completions?api-version=2024-04-01-preview
Authorization: Bearer <bearer-token>
Content-Type: application/json
extra-parameters: pass-through
import json
from azure.ai.inference.models import SystemMessage, UserMessage, ChatCompletionsResponseFormatJSON
from azure.core.exceptions import HttpResponseError
try:
response = model.complete(
messages=[
SystemMessage(content="You are a helpful assistant."),
UserMessage(content="How many languages are in the world?"),
],
response_format=ChatCompletionsResponseFormatJSON()
)
except HttpResponseError as ex:
if ex.status_code == 422:
response = json.loads(ex.response._content.decode('utf-8'))
if isinstance(response, dict) and "detail" in response:
for offending in response["detail"]:
param = ".".join(offending["loc"])
value = offending["input"]
print(
f"Looks like the model doesn't support the parameter '{param}' with value '{value}'"
)
else:
raise ex
try {
var messages = [
{ role: "system", content: "You are a helpful assistant" },
{ role: "user", content: "How many languages are in the world?" },
];
var response = await client.path("/chat/completions").post({
body: {
messages: messages,
response_format: { type: "json_object" }
}
});
}
catch (error) {
if (error.status_code == 422) {
var response = JSON.parse(error.response._content)
if (response.detail) {
for (const offending of response.detail) {
var param = offending.loc.join(".")
var value = offending.input
console.log(`Looks like the model doesn't support the parameter '${param}' with value '${value}'`)
}
}
}
else
{
throw error
}
}
try
{
requestOptions = new ChatCompletionsOptions()
{
Messages = {
new ChatRequestSystemMessage("You are a helpful assistant"),
new ChatRequestUserMessage("How many languages are in the world?"),
},
ResponseFormat = new ChatCompletionsResponseFormatJSON()
};
response = client.Complete(requestOptions);
Console.WriteLine(response.Value.Choices[0].Message.Content);
}
catch (RequestFailedException ex)
{
if (ex.Status == 422)
{
Console.WriteLine($"Looks like the model doesn't support a parameter: {ex.Message}");
}
else
{
throw;
}
}
Request
POST /chat/completions?api-version=2024-04-01-preview
Authorization: Bearer <bearer-token>
Content-Type: application/json
from azure.ai.inference.models import AssistantMessage, UserMessage, SystemMessage
from azure.core.exceptions import HttpResponseError
try:
response = model.complete(
messages=[
SystemMessage(content="You are an AI assistant that helps people find information."),
UserMessage(content="Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills."),
]
)
print(response.choices[0].message.content)
except HttpResponseError as ex:
if ex.status_code == 400:
response = json.loads(ex.response._content.decode('utf-8'))
if isinstance(response, dict) and "error" in response:
print(f"Your request triggered an {response['error']['code']} error:\n\t {response['error']['message']}")
else:
raise ex
else:
raise ex
try {
var messages = [
{ role: "system", content: "You are an AI assistant that helps people find information." },
{ role: "user", content: "Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills." },
]
var response = await client.path("/chat/completions").post({
body: {
messages: messages,
}
});
console.log(response.body.choices[0].message.content)
}
catch (error) {
if (error.status_code == 400) {
var response = JSON.parse(error.response._content)
if (response.error) {
console.log(`Your request triggered an ${response.error.code} error:\n\t ${response.error.message}`)
}
else
{
throw error
}
}
}
try
{
requestOptions = new ChatCompletionsOptions()
{
Messages = {
new ChatRequestSystemMessage("You are an AI assistant that helps people find information."),
new ChatRequestUserMessage(
"Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills."
),
},
};
response = client.Complete(requestOptions);
Console.WriteLine(response.Value.Choices[0].Message.Content);
}
catch (RequestFailedException ex)
{
if (ex.ErrorCode == "content_filter")
{
Console.WriteLine($"Your query has trigger Azure Content Safety: {ex.Message}");
}
else
{
throw;
}
}
Request
POST /chat/completions?api-version=2024-04-01-preview
Authorization: Bearer <bearer-token>
Content-Type: application/json
{
"messages": [
{
"role": "system",
"content": "You are a helpful assistant"
},
{
"role": "user",
"content": "Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills."
}
],
"temperature": 0,
"top_p": 1,
}
响应
{
"status": 400,
"code": "content_filter",
"message": "The response was filtered",
"param": "messages",
"type": null
}
使用入门
Azure AI 模型推理 API 目前在某些部署为无服务器 API 终结点和托管联机终结点的模型中受支持。 部署任何受支持的模型并使用完全相同的代码来利用其预测。