Skip to content

Commit 3db918d

Browse files
trangevilmolkova
andauthored
[Azure.AI.Inference] Beta 2 (#45757)
Features Added Added new EmbeddingsClient, to provide support for generating text embeddings using supported models. Add support for passing a string file path on disk in order to provide an image for chat completions. Breaking Changes ChatCompletionsClientOptions has been renamed to AzureAIInferenceClientOptions. ChatCompletions response object has been flattened. ChatCompletions.Choices has been removed, and the underlying properties have been bubbled up to be on the ChatCompletions object instead. ChatCompletionsFunctionToolCall has been replaced with ChatCompletionsToolCall. ChatCompletionsFunctionToolDefinition has been replaced with ChatCompletionsToolDefinition. ChatCompletionsToolSelectionPreset has been replaced with ChatCompletionsToolChoicePreset. ChatCompletionsNamedFunctionToolSelection has been replaced with ChatCompletionsNamedToolChoice. ChatCompletionsFunctionToolSelection has been replaced with ChatCompletionsNamedToolChoiceFunction. Bugs Fixed Fixed support for chat completions streaming while using tools. Other Changes Removed the need to manually provide an api-key header when talking to Azure OpenAI. ---------------------------------------------------------- Signed-off-by: Travis Angevine <trangevi@microsoft.com> Co-authored-by: Liudmila Molkova <limolkova@microsoft.com>
1 parent 6d72f21 commit 3db918d

File tree

107 files changed

+5778
-2240
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+5778
-2240
lines changed

.vscode/cspell.json

+6
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@
183183
"warnaserror"
184184
]
185185
},
186+
{
187+
"filename": "**/sdk/ai/**/*.cs",
188+
"words": [
189+
"Ubinary"
190+
]
191+
},
186192
{
187193
"filename": "**/sdk/analysisservices/**/*.cs",
188194
"words": [

sdk/ai/Azure.AI.Inference/CHANGELOG.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
# Release History
22

3-
## 1.0.0-beta.2 (Unreleased)
3+
## 1.0.0-beta.2 (2024-10-24)
44

55
### Features Added
6+
- Added new `EmbeddingsClient`, to provide support for generating text embeddings using supported models.
7+
- Add support for passing a string file path on disk in order to provide an image for chat completions.
68

79
### Breaking Changes
10+
- `ChatCompletionsClientOptions` has been renamed to `AzureAIInferenceClientOptions`.
11+
- `ChatCompletions` response object has been flattened. `ChatCompletions.Choices` has been removed, and the underlying properties have been bubbled up to be on the `ChatCompletions` object instead.
12+
- `ChatCompletionsFunctionToolCall` has been replaced with `ChatCompletionsToolCall`.
13+
- `ChatCompletionsFunctionToolDefinition` has been replaced with `ChatCompletionsToolDefinition`.
14+
- `ChatCompletionsToolSelectionPreset` has been replaced with `ChatCompletionsToolChoicePreset`.
15+
- `ChatCompletionsNamedFunctionToolSelection` has been replaced with `ChatCompletionsNamedToolChoice`.
16+
- `ChatCompletionsFunctionToolSelection` has been replaced with `ChatCompletionsNamedToolChoiceFunction`.
17+
- `StreamingChatCompletionsUpdate.AuthorName` has been removed
18+
- Removed `extraParams` from the `complete` and `completeAsync` methods. It is now set implicitly if `additionalProperties` is provided in the options object.
819

920
### Bugs Fixed
21+
- Fixed support for chat completions streaming while using tools.
1022

1123
### Other Changes
24+
- Removed the need to manually provide an `api-key` header when talking to Azure OpenAI.
1225

1326
## 1.0.0-beta.1 (2024-08-06)
1427
### Features Added

sdk/ai/Azure.AI.Inference/README.md

+26-17
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ The package includes `ChatCompletionsClient` <!-- and `EmbeddingsClient`and `Ima
5353
var endpoint = new Uri(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_ENDPOINT"));
5454
var credential = new AzureKeyCredential(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_KEY"));
5555

56-
var client = new ChatCompletionsClient(endpoint, credential, new ChatCompletionsClientOptions());
56+
var client = new ChatCompletionsClient(endpoint, credential, new AzureAIInferenceClientOptions());
5757
```
5858

5959
<!--
@@ -90,7 +90,7 @@ All clients provide a `get_model_info` method to retrive AI model information. T
9090
var endpoint = new Uri(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_ENDPOINT"));
9191
var credential = new AzureKeyCredential(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_KEY"));
9292

93-
var client = new ChatCompletionsClient(endpoint, credential, new ChatCompletionsClientOptions());
93+
var client = new ChatCompletionsClient(endpoint, credential, new AzureAIInferenceClientOptions());
9494
Response<ModelInfo> modelInfo = client.GetModelInfo();
9595

9696
Console.WriteLine($"Model name: {modelInfo.Value.ModelName}");
@@ -106,13 +106,13 @@ The `ChatCompletionsClient` has a method named `complete`. The method makes a RE
106106

107107
See simple chat completion examples below. More can be found in the [samples](https://aka.ms/azsdk/azure-ai-inference/csharp/samples) folder.
108108

109-
<!--
110109
### Text Embeddings
111110

112-
The `EmbeddingsClient` has a method named `embedding`. The method makes a REST API call to the `/embeddings` route on the provided endpoint, as documented in [the REST API reference](https://learn.microsoft.com/azure/ai-studio/reference/reference-model-inference-embeddings).
111+
The `EmbeddingsClient` has a method named `embed`. The method makes a REST API call to the `/embeddings` route on the provided endpoint, as documented in [the REST API reference](https://learn.microsoft.com/azure/ai-studio/reference/reference-model-inference-embeddings).
113112

114113
See simple text embedding example below. More can be found in the [samples](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/ai/azure-ai-inference/samples) folder.
115114

115+
<!--
116116
### Image Embeddings
117117
118118
TODO: Add overview and link to explain image embeddings.
@@ -150,7 +150,7 @@ In the following sections you will find simple examples of:
150150
* [Chat completions](#chat-completions-example)
151151
* [Streaming chat completions](#streaming-chat-completions-example)
152152
* [Chat completions with additional model-specific parameters](#chat-completions-with-additional-model-specific-parameters)
153-
<!-- * [Text Embeddings](#text-embeddings-example) -->
153+
* [Text Embeddings](#text-embeddings-example)
154154
<!-- * [Image Embeddings](#image-embeddings-example) -->
155155

156156
The examples create a client as mentioned in [Create and authenticate a client directly, using key](#create-and-authenticate-a-client-directly-using-key). Only mandatory input settings are shown for simplicity.
@@ -165,7 +165,7 @@ This example demonstrates how to generate a single chat completions, with key au
165165
var endpoint = new Uri(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_ENDPOINT"));
166166
var credential = new AzureKeyCredential(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_KEY"));
167167

168-
var client = new ChatCompletionsClient(endpoint, credential, new ChatCompletionsClientOptions());
168+
var client = new ChatCompletionsClient(endpoint, credential, new AzureAIInferenceClientOptions());
169169

170170
var requestOptions = new ChatCompletionsOptions()
171171
{
@@ -177,12 +177,12 @@ var requestOptions = new ChatCompletionsOptions()
177177
};
178178

179179
Response<ChatCompletions> response = client.Complete(requestOptions);
180-
System.Console.WriteLine(response.Value.Choices[0].Message.Content);
180+
System.Console.WriteLine(response.Value.Content);
181181
```
182182

183183
The following types or messages are supported: `SystemMessage`,`UserMessage`, `AssistantMessage`, `ToolMessage`. See also samples:
184184

185-
* [Sample5_ChatCompletionsWithImageUrl.md](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/ai/Azure.AI.Inference/samples/Sample5_ChatCompletionsWithImageUrl.md) for usage of `UserMessage` that includes sending an image URL.
185+
* [Sample5_ChatCompletionsWithImages.md](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/ai/Azure.AI.Inference/samples/Sample5_ChatCompletionsWithImages.md) for usage of `UserMessage` that includes sending an image URL or image data from a local file.
186186
* [Sample7_ChatCompletionsWithTools.md](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/ai/Azure.AI.Inference/samples/Sample7_ChatCompletionsWithTools.md) for usage of `ToolMessage`.
187187

188188
Alternatively, you can read a `BinaryData` object based on a JSON string instead of using the strongly typed classes like `ChatRequestSystemMessage` and `ChatRequestUserMessage`:
@@ -191,7 +191,7 @@ Alternatively, you can read a `BinaryData` object based on a JSON string instead
191191
var endpoint = new Uri(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_ENDPOINT"));
192192
var credential = new AzureKeyCredential(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_KEY"));
193193

194-
var client = new ChatCompletionsClient(endpoint, credential, new ChatCompletionsClientOptions());
194+
var client = new ChatCompletionsClient(endpoint, credential, new AzureAIInferenceClientOptions());
195195

196196
var requestOptions = new ChatCompletionsOptions()
197197
{
@@ -207,7 +207,7 @@ BinaryData messages = BinaryData.FromString(jsonMessages);
207207
requestOptions = ModelReaderWriter.Read<ChatCompletionsOptions>(messages);
208208

209209
Response<ChatCompletions> response = client.Complete(requestOptions);
210-
System.Console.WriteLine(response.Value.Choices[0].Message.Content);
210+
System.Console.WriteLine(response.Value.Content);
211211
```
212212

213213
To generate completions for additional messages, simply call `client.Complete` multiple times using the same `client`.
@@ -220,7 +220,7 @@ This example demonstrates how to generate a single chat completions with streami
220220
var endpoint = new Uri(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_ENDPOINT"));
221221
var credential = new AzureKeyCredential(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_KEY"));
222222

223-
var client = new ChatCompletionsClient(endpoint, credential, new ChatCompletionsClientOptions());
223+
var client = new ChatCompletionsClient(endpoint, credential, new AzureAIInferenceClientOptions());
224224

225225
var requestOptions = new ChatCompletionsOptions()
226226
{
@@ -255,15 +255,13 @@ In this example, extra JSON elements are inserted at the root of the request bod
255255

256256
Note that by default, the service will reject any request payload that includes unknown parameters (ones that are not defined in the REST API [Request Body table](https://learn.microsoft.com/azure/ai-studio/reference/reference-model-inference-chat-completions#request-body)). In order to change the default service behaviour, when the `Complete` method includes `AdditonalProperties`, the client library will automatically add the HTTP request header `"unknown_params": "pass-through"`.
257257

258-
<!-- The input argument `Additional` is not restricted to chat completions. It is suppored on other client methods as well. -->
259-
260258
Azure_AI_Inference_ChatCompletionsWithAdditionalPropertiesScenario
261259

262260
```C# Snippet:Azure_AI_Inference_ChatCompletionsWithAdditionalPropertiesScenario
263261
var endpoint = new Uri(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_ENDPOINT"));
264262
var credential = new AzureKeyCredential(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_KEY"));
265263

266-
var client = new ChatCompletionsClient(endpoint, credential, new ChatCompletionsClientOptions());
264+
var client = new ChatCompletionsClient(endpoint, credential, new AzureAIInferenceClientOptions());
267265

268266
var requestOptions = new ChatCompletionsOptions()
269267
{
@@ -278,13 +276,25 @@ Response<ChatCompletions> response = client.Complete(requestOptions);
278276
System.Console.WriteLine(response.Value.Choices[0].Message.Content);
279277
```
280278

281-
<!--
282279
### Text Embeddings example
283280

284281
This example demonstrates how to get text embeddings, with key authentication, assuming `endpoint` and `key` are already defined.
285282

286-
```C#
283+
```C# Snippet:Azure_AI_Inference_BasicEmbedding
284+
var endpoint = new Uri(System.Environment.GetEnvironmentVariable("AZURE_AI_EMBEDDINGS_ENDPOINT"));
285+
var credential = new AzureKeyCredential(System.Environment.GetEnvironmentVariable("AZURE_AI_EMBEDDINGS_KEY"));
287286

287+
var client = new EmbeddingsClient(endpoint, credential, new AzureAIInferenceClientOptions());
288+
289+
var input = new List<string> { "King", "Queen", "Jack", "Page" };
290+
var requestOptions = new EmbeddingsOptions(input);
291+
292+
Response<EmbeddingsResult> response = client.Embed(requestOptions);
293+
foreach (EmbeddingItem item in response.Value.Data)
294+
{
295+
List<float> embedding = item.Embedding.ToObjectFromJson<List<float>>();
296+
Console.WriteLine($"Index: {item.Index}, Embedding: <{string.Join(", ", embedding)}>");
297+
}
288298
```
289299

290300
The length of the embedding vector depends on the model, but you should see something like this:
@@ -296,7 +306,6 @@ data[2]: length=1024, [0.04196167, 0.029083252, ..., -0.0027484894, 0.0073127747
296306
```
297307

298308
To generate embeddings for additional phrases, simply call `client.embed` multiple times using the same `client`.
299-
-->
300309

301310
<!--
302311
### Image Embeddings example

0 commit comments

Comments
 (0)