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)
);
response = client.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}");
要求
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 = client.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;
}
}
要求
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 = client.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 Safeaty: {ex.Message}");
}
else
{
throw;
}
}
要求
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
}
開始使用
部署為無伺服器 API 端點和受控線上端點的模型,目前支援 Azure AI 模型推斷 API。 部署任何支援的模型,並使用完全相同的程式碼來取用其預測。