Skip to content

Commit a66620a

Browse files
authored
Merge pull request #3649 from MicrosoftDocs/main
3/21 11:00 AM IST Publish
2 parents 34ecec7 + 566584a commit a66620a

File tree

39 files changed

+697
-736
lines changed

39 files changed

+697
-736
lines changed

articles/ai-foundry/model-inference/includes/use-chat-completions/csharp.md

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: mopeakande
77
reviewer: santiagxf
88
ms.service: azure-ai-model-inference
99
ms.topic: how-to
10-
ms.date: 1/21/2025
10+
ms.date: 03/20/2025
1111
ms.author: mopeakande
1212
ms.reviewer: fasantia
1313
ms.custom: references_regions, tool_generated
@@ -26,7 +26,7 @@ To use chat completion models in your application, you need:
2626

2727
[!INCLUDE [how-to-prerequisites-csharp](../how-to-prerequisites-csharp.md)]
2828

29-
* A chat completions model deployment. If you don't have one read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md) to add a chat completions model to your resource.
29+
* A chat completions model deployment. If you don't have one, read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md) to add a chat completions model to your resource.
3030

3131
* This example uses `mistral-large-2407`.
3232

@@ -42,7 +42,7 @@ ChatCompletionsClient client = new ChatCompletionsClient(
4242
);
4343
```
4444

45-
If you have configured the resource to with **Microsoft Entra ID** support, you can use the following code snippet to create a client.
45+
If you've configured the resource with **Microsoft Entra ID** support, you can use the following code snippet to create a client.
4646

4747

4848
```csharp
@@ -181,7 +181,7 @@ response = client.Complete(requestOptions);
181181
Console.WriteLine($"Response: {response.Value.Content}");
182182
```
183183

184-
Some models don't support JSON output formatting. You can always prompt the model to generate JSON outputs. However, such outputs are not guaranteed to be valid JSON.
184+
Some models don't support JSON output formatting. You can always prompt the model to generate JSON outputs. However, such outputs aren't guaranteed to be valid JSON.
185185

186186
If you want to pass a parameter that isn't in the list of supported parameters, you can pass it to the underlying model using *extra parameters*. See [Pass extra parameters to the model](#pass-extra-parameters-to-the-model).
187187

@@ -400,69 +400,3 @@ catch (RequestFailedException ex)
400400

401401
> [!TIP]
402402
> To learn more about how you can configure and control Azure AI content safety settings, check the [Azure AI content safety documentation](https://aka.ms/azureaicontentsafety).
403-
404-
## Use chat completions with images
405-
406-
Some models can reason across text and images and generate text completions based on both kinds of input. In this section, you explore the capabilities of Some models for vision in a chat fashion:
407-
408-
> [!IMPORTANT]
409-
> Some models support only one image for each turn in the chat conversation and only the last image is retained in context. If you add multiple images, it results in an error.
410-
411-
To see this capability, download an image and encode the information as `base64` string. The resulting data should be inside of a [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs):
412-
413-
414-
```csharp
415-
string imageUrl = "https://news.microsoft.com/source/wp-content/uploads/2024/04/The-Phi-3-small-language-models-with-big-potential-1-1900x1069.jpg";
416-
string imageFormat = "jpeg";
417-
HttpClient httpClient = new HttpClient();
418-
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0");
419-
byte[] imageBytes = httpClient.GetByteArrayAsync(imageUrl).Result;
420-
string imageBase64 = Convert.ToBase64String(imageBytes);
421-
string dataUrl = $"data:image/{imageFormat};base64,{imageBase64}";
422-
```
423-
424-
Visualize the image:
425-
426-
:::image type="content" source="../../../../ai-foundry/media/how-to/sdks/small-language-models-chart-example.jpg" alt-text="A chart displaying the relative capabilities between large language models and small language models." lightbox="../../../../ai-foundry/media/how-to/sdks/small-language-models-chart-example.jpg":::
427-
428-
Now, create a chat completion request with the image:
429-
430-
431-
```csharp
432-
ChatCompletionsOptions requestOptions = new ChatCompletionsOptions()
433-
{
434-
Messages = {
435-
new ChatRequestSystemMessage("You are an AI assistant that helps people find information."),
436-
new ChatRequestUserMessage([
437-
new ChatMessageTextContentItem("Which conclusion can be extracted from the following chart?"),
438-
new ChatMessageImageContentItem(new Uri(dataUrl))
439-
]),
440-
},
441-
MaxTokens=2048,
442-
Model = "phi-3.5-vision-instruct",
443-
};
444-
445-
var response = client.Complete(requestOptions);
446-
Console.WriteLine(response.Value.Content);
447-
```
448-
449-
The response is as follows, where you can see the model's usage statistics:
450-
451-
452-
```csharp
453-
Console.WriteLine($"{response.Value.Role}: {response.Value.Content}");
454-
Console.WriteLine($"Model: {response.Value.Model}");
455-
Console.WriteLine("Usage:");
456-
Console.WriteLine($"\tPrompt tokens: {response.Value.Usage.PromptTokens}");
457-
Console.WriteLine($"\tTotal tokens: {response.Value.Usage.TotalTokens}");
458-
Console.WriteLine($"\tCompletion tokens: {response.Value.Usage.CompletionTokens}");
459-
```
460-
461-
```console
462-
ASSISTANT: The chart illustrates that larger models tend to perform better in quality, as indicated by their size in billions of parameters. However, there are exceptions to this trend, such as Phi-3-medium and Phi-3-small, which outperform smaller models in quality. This suggests that while larger models generally have an advantage, there might be other factors at play that influence a model's performance.
463-
Model: phi-3.5-vision-instruct
464-
Usage:
465-
Prompt tokens: 2380
466-
Completion tokens: 126
467-
Total tokens: 2506
468-
```

articles/ai-foundry/model-inference/includes/use-chat-completions/java.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: mopeakande
77
reviewer: santiagxf
88
ms.service: azure-ai-model-inference
99
ms.topic: how-to
10-
ms.date: 1/21/2025
10+
ms.date: 03/20/2025
1111
ms.author: mopeakande
1212
ms.reviewer: fasantia
1313
ms.custom: references_regions, tool_generated
@@ -26,7 +26,7 @@ To use chat completion models in your application, you need:
2626

2727
[!INCLUDE [how-to-prerequisites-java](../how-to-prerequisites-java.md)]
2828

29-
* A chat completions model deployment. If you don't have one read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md) to add a chat completions model to your resource.
29+
* A chat completions model deployment. If you don't have one, read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md) to add a chat completions model to your resource.
3030

3131
* This example uses `mistral-large-2407`.
3232

@@ -41,7 +41,7 @@ ChatCompletionsClient client = new ChatCompletionsClientBuilder()
4141
.buildClient();
4242
```
4343

44-
If you have configured the resource to with **Microsoft Entra ID** support, you can use the following code snippet to create a client.
44+
If you've configured the resource with **Microsoft Entra ID** support, you can use the following code snippet to create a client.
4545

4646
```java
4747
TokenCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
@@ -120,7 +120,7 @@ client.completeStream(new ChatCompletionsOptions(chatMessages))
120120
#### Explore more parameters supported by the inference client
121121

122122
Explore other parameters that you can specify in the inference client. For a full list of all the supported parameters and their corresponding documentation, see [Azure AI Model Inference API reference](https://aka.ms/azureai/modelinference).
123-
Some models don't support JSON output formatting. You can always prompt the model to generate JSON outputs. However, such outputs are not guaranteed to be valid JSON.
123+
Some models don't support JSON output formatting. You can always prompt the model to generate JSON outputs. However, such outputs aren't guaranteed to be valid JSON.
124124

125125
If you want to pass a parameter that isn't in the list of supported parameters, you can pass it to the underlying model using *extra parameters*. See [Pass extra parameters to the model](#pass-extra-parameters-to-the-model).
126126

articles/ai-foundry/model-inference/includes/use-chat-completions/javascript.md

Lines changed: 4 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: mopeakande
77
reviewer: santiagxf
88
ms.service: azure-ai-model-inference
99
ms.topic: how-to
10-
ms.date: 1/21/2025
10+
ms.date: 03/20/2025
1111
ms.author: mopeakande
1212
ms.reviewer: fasantia
1313
ms.custom: references_regions, tool_generated
@@ -26,7 +26,7 @@ To use chat completion models in your application, you need:
2626

2727
[!INCLUDE [how-to-prerequisites-javascript](../how-to-prerequisites-javascript.md)]
2828

29-
* A chat completions model deployment. If you don't have one read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md) to add a chat completions model to your resource.
29+
* A chat completions model deployment. If you don't have one, read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md) to add a chat completions model to your resource.
3030

3131
## Use chat completions
3232

@@ -44,7 +44,7 @@ const client = new ModelClient(
4444
);
4545
```
4646

47-
If you have configured the resource to with **Microsoft Entra ID** support, you can use the following code snippet to create a client.
47+
If you've configured the resource with **Microsoft Entra ID** support, you can use the following code snippet to create a client.
4848

4949

5050
```javascript
@@ -177,7 +177,7 @@ var response = await client.path("/chat/completions").post({
177177
});
178178
```
179179
180-
Some models don't support JSON output formatting. You can always prompt the model to generate JSON outputs. However, such outputs are not guaranteed to be valid JSON.
180+
Some models don't support JSON output formatting. You can always prompt the model to generate JSON outputs. However, such outputs aren't guaranteed to be valid JSON.
181181
182182
If you want to pass a parameter that isn't in the list of supported parameters, you can pass it to the underlying model using *extra parameters*. See [Pass extra parameters to the model](#pass-extra-parameters-to-the-model).
183183
@@ -388,82 +388,3 @@ catch (error) {
388388
> [!TIP]
389389
> To learn more about how you can configure and control Azure AI content safety settings, check the [Azure AI content safety documentation](https://aka.ms/azureaicontentsafety).
390390
391-
## Use chat completions with images
392-
393-
Some models can reason across text and images and generate text completions based on both kinds of input. In this section, you explore the capabilities of Some models for vision in a chat fashion:
394-
395-
> [!IMPORTANT]
396-
> Some models support only one image for each turn in the chat conversation and only the last image is retained in context. If you add multiple images, it results in an error.
397-
398-
To see this capability, download an image and encode the information as `base64` string. The resulting data should be inside of a [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs):
399-
400-
401-
```javascript
402-
const image_url = "https://news.microsoft.com/source/wp-content/uploads/2024/04/The-Phi-3-small-language-models-with-big-potential-1-1900x1069.jpg";
403-
const image_format = "jpeg";
404-
405-
const response = await fetch(image_url, { headers: { "User-Agent": "Mozilla/5.0" } });
406-
const image_data = await response.arrayBuffer();
407-
const image_data_base64 = Buffer.from(image_data).toString("base64");
408-
const data_url = `data:image/${image_format};base64,${image_data_base64}`;
409-
```
410-
411-
Visualize the image:
412-
413-
414-
```javascript
415-
const img = document.createElement("img");
416-
img.src = data_url;
417-
document.body.appendChild(img);
418-
```
419-
420-
:::image type="content" source="../../../../ai-foundry/media/how-to/sdks/small-language-models-chart-example.jpg" alt-text="A chart displaying the relative capabilities between large language models and small language models." lightbox="../../../../ai-foundry/media/how-to/sdks/small-language-models-chart-example.jpg":::
421-
422-
Now, create a chat completion request with the image:
423-
424-
425-
```javascript
426-
var messages = [
427-
{ role: "system", content: "You are a helpful assistant that can generate responses based on images." },
428-
{ role: "user", content:
429-
[
430-
{ type: "text", text: "Which conclusion can be extracted from the following chart?" },
431-
{ type: "image_url", image:
432-
{
433-
url: data_url
434-
}
435-
}
436-
]
437-
}
438-
];
439-
440-
var response = await client.path("/chat/completions").post({
441-
body: {
442-
messages: messages,
443-
temperature: 0,
444-
top_p: 1,
445-
max_tokens: 2048,
446-
}
447-
});
448-
```
449-
450-
The response is as follows, where you can see the model's usage statistics:
451-
452-
453-
```javascript
454-
console.log(response.body.choices[0].message.role + ": " + response.body.choices[0].message.content);
455-
console.log("Model:", response.body.model);
456-
console.log("Usage:");
457-
console.log("\tPrompt tokens:", response.body.usage.prompt_tokens);
458-
console.log("\tCompletion tokens:", response.body.usage.completion_tokens);
459-
console.log("\tTotal tokens:", response.body.usage.total_tokens);
460-
```
461-
462-
```console
463-
ASSISTANT: The chart illustrates that larger models tend to perform better in quality, as indicated by their size in billions of parameters. However, there are exceptions to this trend, such as Phi-3-medium and Phi-3-small, which outperform smaller models in quality. This suggests that while larger models generally have an advantage, there might be other factors at play that influence a model's performance.
464-
Model: mistral-large-2407
465-
Usage:
466-
Prompt tokens: 2380
467-
Completion tokens: 126
468-
Total tokens: 2506
469-
```

articles/ai-foundry/model-inference/includes/use-chat-completions/python.md

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: mopeakande
77
reviewer: santiagxf
88
ms.service: azure-ai-model-inference
99
ms.topic: how-to
10-
ms.date: 1/21/2025
10+
ms.date: 03/20/2025
1111
ms.author: mopeakande
1212
ms.reviewer: fasantia
1313
ms.custom: references_regions, tool_generated
@@ -26,7 +26,7 @@ To use chat completion models in your application, you need:
2626

2727
[!INCLUDE [how-to-prerequisites-python](../how-to-prerequisites-python.md)]
2828

29-
* A chat completions model deployment. If you don't have one read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md) to add a chat completions model to your resource.
29+
* A chat completions model deployment. If you don't have one, read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md) to add a chat completions model to your resource.
3030

3131
* This example uses `mistral-large-2407`.
3232

@@ -46,7 +46,7 @@ client = ChatCompletionsClient(
4646
)
4747
```
4848

49-
If you have configured the resource to with **Microsoft Entra ID** support, you can use the following code snippet to create a client.
49+
If you've configured the resource with **Microsoft Entra ID** support, you can use the following code snippet to create a client.
5050

5151

5252
```python
@@ -166,7 +166,7 @@ response = client.complete(
166166
)
167167
```
168168

169-
Some models don't support JSON output formatting. You can always prompt the model to generate JSON outputs. However, such outputs are not guaranteed to be valid JSON.
169+
Some models don't support JSON output formatting. You can always prompt the model to generate JSON outputs. However, such outputs aren't guaranteed to be valid JSON.
170170

171171
If you want to pass a parameter that isn't in the list of supported parameters, you can pass it to the underlying model using *extra parameters*. See [Pass extra parameters to the model](#pass-extra-parameters-to-the-model).
172172

@@ -373,76 +373,3 @@ except HttpResponseError as ex:
373373
> [!TIP]
374374
> To learn more about how you can configure and control Azure AI content safety settings, check the [Azure AI content safety documentation](https://aka.ms/azureaicontentsafety).
375375
376-
## Use chat completions with images
377-
378-
Some models can reason across text and images and generate text completions based on both kinds of input. In this section, you explore the capabilities of Some models for vision in a chat fashion:
379-
380-
> [!IMPORTANT]
381-
> Some models support only one image for each turn in the chat conversation and only the last image is retained in context. If you add multiple images, it results in an error.
382-
383-
To see this capability, download an image and encode the information as `base64` string. The resulting data should be inside of a [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs):
384-
385-
386-
```python
387-
from urllib.request import urlopen, Request
388-
import base64
389-
390-
image_url = "https://news.microsoft.com/source/wp-content/uploads/2024/04/The-Phi-3-small-language-models-with-big-potential-1-1900x1069.jpg"
391-
image_format = "jpeg"
392-
393-
request = Request(image_url, headers={"User-Agent": "Mozilla/5.0"})
394-
image_data = base64.b64encode(urlopen(request).read()).decode("utf-8")
395-
data_url = f"data:image/{image_format};base64,{image_data}"
396-
```
397-
398-
Visualize the image:
399-
400-
401-
```python
402-
import requests
403-
import IPython.display as Disp
404-
405-
Disp.Image(requests.get(image_url).content)
406-
```
407-
408-
:::image type="content" source="../../../../ai-foundry/media/how-to/sdks/small-language-models-chart-example.jpg" alt-text="A chart displaying the relative capabilities between large language models and small language models." lightbox="../../../../ai-foundry/media/how-to/sdks/small-language-models-chart-example.jpg":::
409-
410-
Now, create a chat completion request with the image:
411-
412-
413-
```python
414-
from azure.ai.inference.models import TextContentItem, ImageContentItem, ImageUrl
415-
response = client.complete(
416-
messages=[
417-
SystemMessage("You are a helpful assistant that can generate responses based on images."),
418-
UserMessage(content=[
419-
TextContentItem(text="Which conclusion can be extracted from the following chart?"),
420-
ImageContentItem(image=ImageUrl(url=data_url))
421-
]),
422-
],
423-
temperature=0,
424-
top_p=1,
425-
max_tokens=2048,
426-
)
427-
```
428-
429-
The response is as follows, where you can see the model's usage statistics:
430-
431-
432-
```python
433-
print(f"{response.choices[0].message.role}:\n\t{response.choices[0].message.content}\n")
434-
print("Model:", response.model)
435-
print("Usage:")
436-
print("\tPrompt tokens:", response.usage.prompt_tokens)
437-
print("\tCompletion tokens:", response.usage.completion_tokens)
438-
print("\tTotal tokens:", response.usage.total_tokens)
439-
```
440-
441-
```console
442-
ASSISTANT: The chart illustrates that larger models tend to perform better in quality, as indicated by their size in billions of parameters. However, there are exceptions to this trend, such as Phi-3-medium and Phi-3-small, which outperform smaller models in quality. This suggests that while larger models generally have an advantage, there might be other factors at play that influence a model's performance.
443-
Model: mistral-large-2407
444-
Usage:
445-
Prompt tokens: 2380
446-
Completion tokens: 126
447-
Total tokens: 2506
448-
```

0 commit comments

Comments
 (0)