Skip to content

Commit 75f03a9

Browse files
committed
adding imports to code snippets
1 parent e3a89ab commit 75f03a9

File tree

1 file changed

+71
-8
lines changed

1 file changed

+71
-8
lines changed

articles/ai-foundry/foundry-models/how-to/use-chat-completions.md

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ms.custom: generated
1515

1616
# How to generate chat completions with Azure AI Foundry Models
1717

18-
This article explains how to generate chat completions with Azure AI Foundry Model deployments by using the unified OpenAI v1 chat completion endpoint, also referred to as the v1 Azure OpenAI APIs. The unified endpoint eliminates the need for separate Azure-specific code paths.
18+
This article explains how to generate chat completions using next generation v1 Azure OpenAI APIs.
1919

2020
## Prerequisites
2121

@@ -28,7 +28,7 @@ To use chat completion models in your application, you need:
2828

2929
## v1 Azure OpenAI APIs
3030

31-
The v1 Azure OpenAI APIs use the `OpenAI()` client instead of the deprecated `AzureOpenAI()` client. The v1 Azure OpenAI APIs add support for:
31+
The next generation v1 Azure OpenAI APIs let you use the `OpenAI()` client in the official OpenAI client libraries across languages instead of the `AzureOpenAI()` client. The v1 Azure OpenAI APIs add support for:
3232

3333
- Ongoing access to the latest features, with no need to frequently specify new values for the `api-version` parameter.
3434
- OpenAI client support with minimal code changes to swap between OpenAI and Azure OpenAI when using key-based authentication.
@@ -183,6 +183,8 @@ Microsoft Entra authentication only supports Azure OpenAI resources. Complete th
183183
**API key authentication**:
184184
185185
```javascript
186+
import { OpenAI } from "openai";
187+
186188
const client = new OpenAI({
187189
baseURL: "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
188190
apiKey: "{your-api-key}"
@@ -192,6 +194,8 @@ const client = new OpenAI({
192194
To use the API key with environment variables set for `OPENAI_BASE_URL` and `OPENAI_API_KEY`:
193195

194196
```javascript
197+
import { OpenAI } from "openai";
198+
195199
const client = new OpenAI();
196200
```
197201

@@ -204,6 +208,9 @@ npm install @azure/identity
204208
```
205209

206210
```javascript
211+
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
212+
import { OpenAI } from "openai";
213+
207214
const tokenProvider = getBearerTokenProvider(
208215
new DefaultAzureCredential(),
209216
'https://cognitiveservices.azure.com/.default');
@@ -220,6 +227,14 @@ const client = new OpenAI({
220227
**API key authentication**:
221228

222229
```go
230+
import (
231+
"context"
232+
"fmt"
233+
234+
"github.com/openai/openai-go/v2"
235+
"github.com/openai/openai-go/v2/option"
236+
)
237+
223238
client := openai.NewClient(
224239
option.WithBaseURL("https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/"),
225240
option.WithAPIKey("{your-api-key}")
@@ -229,6 +244,13 @@ client := openai.NewClient(
229244
To use the API key with environment variables set for `OPENAI_BASE_URL` and `OPENAI_API_KEY`:
230245

231246
```go
247+
import (
248+
"context"
249+
"fmt"
250+
251+
"github.com/openai/openai-go/v2"
252+
"github.com/openai/openai-go/v2/option"
253+
)
232254
client := openai.NewClient()
233255
```
234256

@@ -242,6 +264,16 @@ go get -u github.com/Azure/azure-sdk-for-go/sdk/azidentity
242264
```
243265

244266
```go
267+
import (
268+
"context"
269+
"fmt"
270+
271+
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
272+
"github.com/openai/openai-go/v3"
273+
"github.com/openai/openai-go/v3/azure"
274+
"github.com/openai/openai-go/v3/option"
275+
)
276+
245277
tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)
246278

247279
client := openai.NewClient(
@@ -257,6 +289,11 @@ client := openai.NewClient(
257289
**API key authentication**:
258290

259291
```java
292+
package com.example;
293+
294+
import com.openai.client.OpenAIClient;
295+
import com.openai.client.okhttp.OpenAIOkHttpClient;
296+
260297

261298
OpenAIClient client = OpenAIOkHttpClient.builder()
262299
.baseUrl("https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/")
@@ -267,14 +304,35 @@ OpenAIClient client = OpenAIOkHttpClient.builder()
267304
To use the API key with environment variables set for `OPENAI_BASE_URL` and `OPENAI_API_KEY`:
268305

269306
```java
307+
package com.example;
308+
309+
import com.openai.client.OpenAIClient;
310+
import com.openai.client.okhttp.OpenAIOkHttpClient;
311+
270312
OpenAIClient client = OpenAIOkHttpClient.builder()
271313
.fromEnv()
272314
.build();
273315
```
274316

275317
**Microsoft Entra authentication**:
276318

277-
First install the Azure Identity client library. For how to install this library, see [Azure Identity client library for Java](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/identity/azure-identity/README.md#include-the-package).
319+
Authentication with Microsoft Entra ID requires some initial setup. First install the Azure Identity client library. For more options on how to install this library, see [Azure Identity client library for Java](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/identity/azure-identity/README.md#include-the-package).
320+
321+
322+
Add the Azure Identity client library:
323+
324+
```xml
325+
<dependency>
326+
<groupId>com.azure</groupId>
327+
<artifactId>azure-identity</artifactId>
328+
<version>1.18.0</version>
329+
</dependency>
330+
```
331+
332+
After setup, you can choose which type of credential from `azure.identity` to use. As an example, `DefaultAzureCredential` can be used to authenticate the client.
333+
334+
Authentication is easiest using `DefaultAzureCredential`. It finds the best credential to use in its running environment.
335+
278336

279337
```java
280338
Credential tokenCredential = BearerTokenCredential.create(
@@ -439,16 +497,14 @@ Console.WriteLine($"[ASSISTANT]: {completion.Content[0].Text}");
439497
API keys aren't recommended for production use because they're less secure than other authentication methods.
440498

441499
```javascript
500+
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
442501
import { OpenAI } from "openai";
443502

444503
const client = new OpenAI({
445504
baseURL: "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
446505
apiKey: process.env['OPENAI_API_KEY'] //Your Azure OpenAI API key
447506
});
448507

449-
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
450-
import { OpenAI } from "openai";
451-
452508
const tokenProvider = getBearerTokenProvider(
453509
new DefaultAzureCredential(),
454510
'https://cognitiveservices.azure.com/.default');
@@ -478,14 +534,15 @@ console.log('Response content:', result.choices[0].message.content);
478534

479535
**Microsoft Entra authentication**:
480536

481-
```cmd
537+
First install the Azure Identity client library before you can use DefaultAzureCredential:
538+
539+
```bash
482540
npm install @azure/identity
483541
```
484542

485543
To authenticate the `OpenAI` client, use the `getBearerTokenProvider` function from the `@azure/identity` package. This function creates a token provider that `OpenAI` uses internally to obtain tokens for each request. Create the token provider as follows:
486544

487545
```javascript
488-
489546
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
490547
import { OpenAI } from "openai";
491548

@@ -557,6 +614,12 @@ func main() {
557614

558615
Use the [azidentity](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity) module for Microsoft Entra ID authentication with Azure OpenAI.
559616

617+
Install the Azure Identity module:
618+
619+
```bash
620+
go get -u github.com/Azure/azure-sdk-for-go/sdk/azidentity
621+
```
622+
560623
```go
561624
package main
562625

0 commit comments

Comments
 (0)