@@ -72,6 +72,7 @@ client = OpenAI(
72
72
If you use the default environment variables of ` OPENAI_BASE_URL ` and ` OPENAI_API_KEY ` they are automatically used by the client with no further configuration required.
73
73
74
74
| Environment Variable | Value |
75
+ | ----------------| -------------|
75
76
| ` OPENAI_BASE_URL ` | ` https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/ ` |
76
77
| ` OPENAI_API_KEY ` | Azure OpenAI or AI Foundry API key. |
77
78
@@ -140,7 +141,6 @@ For more examples, see the [Responses API](../../how-to/responses.md) documentat
140
141
# [ Environment Variables] ( #tab/python-env )
141
142
142
143
``` python
143
- import os
144
144
from openai import OpenAI
145
145
146
146
client = OpenAI()
@@ -228,15 +228,26 @@ For more examples, see the [Responses API](../../how-to/responses.md) documentat
228
228
}
229
229
```
230
230
231
+ ---
232
+
231
233
## Chat
232
234
233
235
### chat.completions.create()
234
236
235
- # [ Python ] ( #tab/command )
237
+ # [ Microsoft Entra ID ] ( #tab/python-entra )
236
238
237
239
``` python
238
- # from openai import OpenAI
239
- # client = OpenAI()
240
+ from openai import OpenAI
241
+ from azure.identity import DefaultAzureCredential, get_bearer_token_provider
242
+
243
+ token_provider = get_bearer_token_provider(
244
+ DefaultAzureCredential(), " https://cognitiveservices.azure.com/.default"
245
+ )
246
+
247
+ client = OpenAI(
248
+ base_url = " https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/" ,
249
+ api_key = token_provider,
250
+ )
240
251
241
252
completion = client.chat.completions.create(
242
253
model = " gpt-4o" , # Replace with your model deployment name.
@@ -247,10 +258,52 @@ completion = client.chat.completions.create(
247
258
)
248
259
249
260
# print(completion.choices[0].message)
250
- print (completion.model_dump_json(indent = 2 )
261
+ print (completion.model_dump_json(indent = 2 ))
251
262
```
252
263
253
- # [Response](#tab/response)
264
+
265
+ # [ API Key] ( #tab/python-key )
266
+
267
+ ``` python
268
+ import os
269
+ from openai import OpenAI
270
+
271
+ client = OpenAI(
272
+ api_key = os.getenv(" AZURE_OPENAI_API_KEY" ),
273
+ base_url = " https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/" ,
274
+ )
275
+
276
+ completion = client.chat.completions.create(
277
+ model = " gpt-4o" , # Replace with your model deployment name.
278
+ messages = [
279
+ {" role" : " system" , " content" : " You are a helpful assistant." },
280
+ {" role" : " user" , " content" : " When was Microsoft founded?" }
281
+ ]
282
+ )
283
+
284
+ # print(completion.choices[0].message)
285
+ print (completion.model_dump_json(indent = 2 ))
286
+ ```
287
+
288
+ # [ Environment Variables] ( #tab/python-env )
289
+
290
+ ``` python
291
+ from openai import OpenAI
292
+ client = OpenAI()
293
+
294
+ completion = client.chat.completions.create(
295
+ model = " gpt-4o" , # Replace with your model deployment name.
296
+ messages = [
297
+ {" role" : " system" , " content" : " You are a helpful assistant." },
298
+ {" role" : " user" , " content" : " When was Microsoft founded?" }
299
+ ]
300
+ )
301
+
302
+ # print(completion.choices[0].message)
303
+ print (completion.model_dump_json(indent = 2 ))
304
+ ```
305
+
306
+ # [ Response] ( #tab/python-output )
254
307
255
308
``` json
256
309
{
@@ -333,11 +386,20 @@ print(completion.model_dump_json(indent=2)
333
386
334
387
### chat.completions.create() - streaming
335
388
336
- # [Python ](#tab/command )
389
+ # [ Microsoft Entra ID ] ( #tab/python-entra )
337
390
338
391
``` python
339
- # from openai import OpenAI
340
- # client = OpenAI()
392
+ from openai import OpenAI
393
+ from azure.identity import DefaultAzureCredential, get_bearer_token_provider
394
+
395
+ token_provider = get_bearer_token_provider(
396
+ DefaultAzureCredential(), " https://cognitiveservices.azure.com/.default"
397
+ )
398
+
399
+ client = OpenAI(
400
+ base_url = " https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/" ,
401
+ api_key = token_provider,
402
+ )
341
403
342
404
completion = client.chat.completions.create(
343
405
model = " gpt-4o" , # Replace with your model deployment name.
@@ -353,7 +415,53 @@ for chunk in completion:
353
415
print (chunk.choices[0 ].delta.content, end = ' ' ,)
354
416
```
355
417
356
- # [Response](#tab/response)
418
+ # [ API Key] ( #tab/python-key )
419
+
420
+ ``` python
421
+ import os
422
+ from openai import OpenAI
423
+
424
+ client = OpenAI(
425
+ api_key = os.getenv(" AZURE_OPENAI_API_KEY" ),
426
+ base_url = " https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/" ,
427
+ )
428
+
429
+ completion = client.chat.completions.create(
430
+ model = " gpt-4o" , # Replace with your model deployment name.
431
+ messages = [
432
+ {" role" : " system" , " content" : " You are a helpful assistant." },
433
+ {" role" : " user" , " content" : " When was Microsoft founded?" }
434
+ ],
435
+ stream = True
436
+ )
437
+
438
+ for chunk in completion:
439
+ if chunk.choices and chunk.choices[0 ].delta.content is not None :
440
+ print (chunk.choices[0 ].delta.content, end = ' ' ,)
441
+ ```
442
+
443
+ # [ Environment Variables] ( #tab/python-env )
444
+
445
+ ``` python
446
+ from openai import OpenAI
447
+
448
+ client = OpenAI()
449
+
450
+ completion = client.chat.completions.create(
451
+ model = " gpt-4o" , # Replace with your model deployment name.
452
+ messages = [
453
+ {" role" : " system" , " content" : " You are a helpful assistant." },
454
+ {" role" : " user" , " content" : " When was Microsoft founded?" }
455
+ ],
456
+ stream = True
457
+ )
458
+
459
+ for chunk in completion:
460
+ if chunk.choices and chunk.choices[0 ].delta.content is not None :
461
+ print (chunk.choices[0 ].delta.content, end = ' ' ,)
462
+ ```
463
+
464
+ # [ Response] ( #tab/python-output )
357
465
358
466
``` text
359
467
Microsoft was founded on April 4, 1975, by Bill Gates and Paul Allen.
@@ -363,10 +471,56 @@ Microsoft was founded on April 4, 1975, by Bill Gates and Paul Allen.
363
471
364
472
### chat.completions.create() - image input
365
473
366
- # [Python](#tab/command)
474
+ # [ Microsoft Entra ID] ( #tab/python-entra )
475
+
476
+ ``` python
477
+ from openai import OpenAI
478
+ from azure.identity import DefaultAzureCredential, get_bearer_token_provider
479
+
480
+ token_provider = get_bearer_token_provider(
481
+ DefaultAzureCredential(), " https://cognitiveservices.azure.com/.default"
482
+ )
483
+
484
+ client = OpenAI(
485
+ base_url = " https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/" ,
486
+ api_key = token_provider,
487
+ )
488
+
489
+ completion = client.chat.completions.create(
490
+ model = " gpt-4o" ,
491
+ messages = [
492
+ {
493
+ " role" : " user" ,
494
+ " content" : [
495
+ {" type" : " text" , " text" : " What's in this image?" },
496
+ {
497
+ " type" : " image_url" ,
498
+ " image_url" : {
499
+ " url" : " https://raw.githubusercontent.com/MicrosoftDocs/azure-ai-docs/main/articles/ai-foundry/openai/media/how-to/generated-seattle.png" ,
500
+ }
501
+ },
502
+ ],
503
+ }
504
+ ],
505
+ max_tokens = 300 ,
506
+ )
507
+
508
+ print (completion.model_dump_json(indent = 2 ))
509
+
510
+ ```
511
+
512
+ # [ API Key] ( #tab/python-key )
367
513
368
514
369
515
``` python
516
+ import os
517
+ from openai import OpenAI
518
+
519
+ client = OpenAI(
520
+ api_key = os.getenv(" AZURE_OPENAI_API_KEY" ),
521
+ base_url = " https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/" ,
522
+ )
523
+
370
524
completion = client.chat.completions.create(
371
525
model = " gpt-4o" ,
372
526
messages = [
@@ -389,7 +543,36 @@ completion = client.chat.completions.create(
389
543
print (completion.model_dump_json(indent = 2 ))
390
544
```
391
545
392
- # [Response](#tab/response)
546
+ # [ Environment Variables] ( #tab/python-env )
547
+
548
+ ``` python
549
+ from openai import OpenAI
550
+
551
+ client = OpenAI()
552
+
553
+ completion = client.chat.completions.create(
554
+ model = " gpt-4o" ,
555
+ messages = [
556
+ {
557
+ " role" : " user" ,
558
+ " content" : [
559
+ {" type" : " text" , " text" : " What's in this image?" },
560
+ {
561
+ " type" : " image_url" ,
562
+ " image_url" : {
563
+ " url" : " https://raw.githubusercontent.com/MicrosoftDocs/azure-ai-docs/main/articles/ai-foundry/openai/media/how-to/generated-seattle.png" ,
564
+ }
565
+ },
566
+ ],
567
+ }
568
+ ],
569
+ max_tokens = 300 ,
570
+ )
571
+
572
+ print (completion.model_dump_json(indent = 2 ))
573
+ ```
574
+
575
+ # [ Response] ( #tab/python-output )
393
576
394
577
``` json
395
578
{
@@ -479,11 +662,31 @@ print(completion.model_dump_json(indent=2))
479
662
480
663
### embeddings.create()
481
664
482
- # [Python](#tab/command)
665
+ # [ Microsoft Entra ID] ( #tab/python-entra )
666
+
667
+ Embeddings currently do not support Microsoft Entra ID with Azure OpenAI and the v1 API.
668
+
669
+ # [ API Key] ( #tab/python-key )
483
670
484
671
``` python
485
- # from openai import OpenAI
486
- # client = OpenAI()
672
+ client = OpenAI(
673
+ api_key = os.getenv(" AZURE_OPENAI_API_KEY" ),
674
+ base_url = " https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/" ,
675
+ )
676
+
677
+ embedding = client.embeddings.create(
678
+ model = " text-embedding-3-large" , # Replace with your model deployment name
679
+ input = " Attenion is all you need" ,
680
+ encoding_format = " float"
681
+ )
682
+
683
+ print (embedding)
684
+ ```
685
+
686
+ # [ Environment Variables] ( #tab/python-env )
687
+
688
+ ``` python
689
+ client = OpenAI()
487
690
488
691
embedding = client.embeddings.create(
489
692
model = " text-embedding-3-large" , # Replace with your model deployment name
0 commit comments