@@ -22,6 +22,7 @@ ms.date: 05/20/2024
22
22
23
23
- An Azure subscription - [ Create one for free] ( https://azure.microsoft.com/free/cognitive-services?azure-portal=true )
24
24
- [ LTS versions of Node.js] ( https://github.com/nodejs/release#release-schedule )
25
+ - [ TypeScript] ( https://www.typescriptlang.org/download/ )
25
26
- An Azure OpenAI Service resource with the ` gpt-35-turbo-instruct ` model deployed. For more information about model deployment, see the [ resource deployment guide] ( ../how-to/create-resource.md ) .
26
27
27
28
> [ !div class="nextstepaction"]
@@ -31,7 +32,6 @@ ms.date: 05/20/2024
31
32
32
33
- An Azure subscription - [ Create one for free] ( https://azure.microsoft.com/free/cognitive-services?azure-portal=true )
33
34
- [ LTS versions of Node.js] ( https://github.com/nodejs/release#release-schedule )
34
- - [ TypeScript] ( https://www.typescriptlang.org/download/ )
35
35
- An Azure OpenAI Service resource with the ` gpt-35-turbo-instruct ` model deployed. For more information about model deployment, see the [ resource deployment guide] ( ../how-to/create-resource.md ) .
36
36
37
37
> [ !div class="nextstepaction"]
@@ -66,6 +66,68 @@ Open a command prompt where you created the new project, and create a new file n
66
66
67
67
## [ ** TypeScript** ] ( #tab/typescript )
68
68
69
+ ``` typescript
70
+ import { AzureOpenAI } from " openai" ;
71
+ import { type Completion } from " openai/resources/index" ;
72
+ import " dotenv/config" ;
73
+
74
+ // You will need to set these environment variables or edit the following values
75
+ const endpoint = process .env [" AZURE_OPENAI_ENDPOINT" ] || " <endpoint>" ;
76
+ const apiKey = process .env [" AZURE_OPENAI_API_KEY" ] || " <api key>" ;
77
+
78
+ // Azure OpenAI API version and deployment
79
+ const apiVersion = " 2024-04-01-preview" ;
80
+ const deployment = " gpt-35-turbo-instruct" ;
81
+
82
+ // Chat prompt and max tokens
83
+ const prompt = [" When was Microsoft founded?" ];
84
+ const maxTokens = 128 ;
85
+
86
+ function getClient(): AzureOpenAI {
87
+ return new AzureOpenAI ({ endpoint , apiKey , apiVersion , deployment });
88
+ }
89
+ async function getCompletion(
90
+ client : AzureOpenAI ,
91
+ prompt : string [],
92
+ model : string ,
93
+ max_tokens : number
94
+ ): Promise <Completion > {
95
+ return await client .completions .create ({
96
+ prompt ,
97
+ model ,
98
+ max_tokens ,
99
+ });
100
+ }
101
+ async function getChoices(completion : Completion ): Promise <void > {
102
+ for (const choice of completion .choices ) {
103
+ console .log (choice .text );
104
+ }
105
+ }
106
+ export async function main() {
107
+ console .log (" == Get completions Sample ==" );
108
+
109
+ const client = getClient ();
110
+ const completion = await getCompletion (client , prompt , deployment , maxTokens );
111
+ await getChoices (completion );
112
+ }
113
+
114
+ main ().catch ((err ) => {
115
+ console .error (" Error occurred:" , err );
116
+ });
117
+ ```
118
+
119
+ Build the script with the following command:
120
+
121
+ ``` cmd
122
+ tsc
123
+ ```
124
+
125
+ Run the script with the following command:
126
+
127
+ ``` cmd
128
+ node.exe Completion.js
129
+ ```
130
+
69
131
## [ ** JavaScript** ] ( #tab/javascript )
70
132
71
133
``` javascript
@@ -125,6 +187,68 @@ Microsoft was founded on April 4, 1975.
125
187
126
188
## [ ** TypeScript** ] ( #tab/typescript )
127
189
190
+ ``` typescript
191
+ import { AzureOpenAI } from " openai" ;
192
+ import { type Completion } from " openai/resources/index" ;
193
+ import { DefaultAzureCredential , getBearerTokenProvider } from " @azure/identity" ;
194
+ import " dotenv/config" ;
195
+
196
+ // Azure OpenAI API version and deployment
197
+ const apiVersion = " 2024-04-01-preview" ;
198
+ const deployment = " gpt-35-turbo-instruct" ;
199
+
200
+ // Chat prompt and max tokens
201
+ const prompt = [" When was Microsoft founded?" ];
202
+ const maxTokens = 128 ;
203
+
204
+ function getClient(): AzureOpenAI {
205
+ const scope = " https://cognitiveservices.azure.com/.default" ;
206
+ const azureADTokenProvider = getBearerTokenProvider (new DefaultAzureCredential (), scope );
207
+ return new AzureOpenAI ({ azureADTokenProvider , deployment , apiVersion });
208
+
209
+ }
210
+ async function getCompletion(
211
+ client : AzureOpenAI ,
212
+ prompt : string [],
213
+ model : string ,
214
+ max_tokens : number
215
+ ): Promise <Completion > {
216
+ return await client .completions .create ({
217
+ prompt ,
218
+ model ,
219
+ max_tokens ,
220
+ });
221
+ }
222
+ async function getChoices(completion : Completion ): Promise <void > {
223
+ for (const choice of completion .choices ) {
224
+ console .log (choice .text );
225
+ }
226
+ }
227
+ export async function main() {
228
+ console .log (" == Get completions Sample ==" );
229
+
230
+ const client = getClient ();
231
+ const completion = await getCompletion (client , prompt , deployment , maxTokens );
232
+ await getChoices (completion );
233
+ }
234
+
235
+ main ().catch ((err ) => {
236
+ console .error (" Error occurred:" , err );
237
+ });
238
+ ```
239
+
240
+ Build the script with the following command:
241
+
242
+ ``` cmd
243
+ tsc
244
+ ```
245
+
246
+ Run the script with the following command:
247
+
248
+ ``` cmd
249
+ node.exe Completion.js
250
+ ```
251
+
128
252
129
253
## [ ** JavaScript** ] ( #tab/javascript )
130
254
0 commit comments