This sample demonstrates how to get a chat completions response from the service using a synchronous call. It shows how to include an image URL in the input chat messages.
This sample will only work on AI models that support image input.
Set these two environment variables before running the sample:
-
AZURE_AI_CHAT_ENDPOINT - Your endpoint URL, in the form
https://your-deployment-name.your-azure-region.inference.ai.azure.comwhereyour-deployment-nameis your unique AI Model deployment name, andyour-azure-regionis the Azure region where your model is deployed. -
AZURE_AI_CHAT_KEY - Your model key (a 32-character string). Keep it secret.
var endpoint = new Uri(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_ENDPOINT"));
var credential = new AzureKeyCredential(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_KEY"));
var client = new ChatCompletionsClient(endpoint, credential, new AzureAIInferenceClientOptions());
ChatMessageImageContentItem imageContentItem =
new ChatMessageImageContentItem(
new Uri("https://example.com/image.jpg"),
ChatMessageImageDetailLevel.Low
);
var requestOptions = new ChatCompletionsOptions()
{
Messages =
{
new ChatRequestSystemMessage("You are a helpful assistant that helps describe images."),
new ChatRequestUserMessage(
new ChatMessageTextContentItem("describe this image"),
imageContentItem),
},
};
Response<ChatCompletions> response = client.Complete(requestOptions);
System.Console.WriteLine(response.Value.Choices[0].Message.Content);An async option is also available.
var endpoint = new Uri(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_ENDPOINT"));
var credential = new AzureKeyCredential(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_KEY"));
var client = new ChatCompletionsClient(endpoint, credential, new AzureAIInferenceClientOptions());
ChatMessageImageContentItem imageContentItem =
new ChatMessageImageContentItem(
new Uri("https://example.com/image.jpg"),
ChatMessageImageDetailLevel.Low
);
var requestOptions = new ChatCompletionsOptions()
{
Messages =
{
new ChatRequestSystemMessage("You are a helpful assistant that helps describe images."),
new ChatRequestUserMessage(
new ChatMessageTextContentItem("describe this image"),
imageContentItem),
},
};
Response<ChatCompletions> response = await client.CompleteAsync(requestOptions);
System.Console.WriteLine(response.Value.Choices[0].Message.Content);