Skip to content

Commit 4aa5771

Browse files
committed
updatE
1 parent b5e0238 commit 4aa5771

File tree

1 file changed

+218
-15
lines changed
  • articles/ai-foundry/openai/includes/language-overview

1 file changed

+218
-15
lines changed

articles/ai-foundry/openai/includes/language-overview/python.md

Lines changed: 218 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ client = OpenAI(
7272
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.
7373

7474
| Environment Variable | Value |
75+
|----------------|-------------|
7576
| `OPENAI_BASE_URL` | `https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/`|
7677
| `OPENAI_API_KEY` | Azure OpenAI or AI Foundry API key. |
7778

@@ -140,7 +141,6 @@ For more examples, see the [Responses API](../../how-to/responses.md) documentat
140141
# [Environment Variables](#tab/python-env)
141142

142143
```python
143-
import os
144144
from openai import OpenAI
145145

146146
client = OpenAI()
@@ -228,15 +228,26 @@ For more examples, see the [Responses API](../../how-to/responses.md) documentat
228228
}
229229
```
230230

231+
---
232+
231233
## Chat
232234

233235
### chat.completions.create()
234236

235-
# [Python](#tab/command)
237+
# [Microsoft Entra ID](#tab/python-entra)
236238

237239
```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+
)
240251

241252
completion = client.chat.completions.create(
242253
model="gpt-4o", # Replace with your model deployment name.
@@ -247,10 +258,52 @@ completion = client.chat.completions.create(
247258
)
248259

249260
#print(completion.choices[0].message)
250-
print(completion.model_dump_json(indent=2)
261+
print(completion.model_dump_json(indent=2))
251262
```
252263

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)
254307

255308
```json
256309
{
@@ -333,11 +386,20 @@ print(completion.model_dump_json(indent=2)
333386

334387
### chat.completions.create() - streaming
335388

336-
# [Python](#tab/command)
389+
# [Microsoft Entra ID](#tab/python-entra)
337390

338391
```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+
)
341403

342404
completion = client.chat.completions.create(
343405
model="gpt-4o", # Replace with your model deployment name.
@@ -353,7 +415,53 @@ for chunk in completion:
353415
print(chunk.choices[0].delta.content, end='',)
354416
```
355417

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)
357465

358466
```text
359467
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.
363471

364472
### chat.completions.create() - image input
365473

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)
367513

368514

369515
```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+
370524
completion = client.chat.completions.create(
371525
model="gpt-4o",
372526
messages=[
@@ -389,7 +543,36 @@ completion = client.chat.completions.create(
389543
print(completion.model_dump_json(indent=2))
390544
```
391545

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)
393576

394577
```json
395578
{
@@ -479,11 +662,31 @@ print(completion.model_dump_json(indent=2))
479662

480663
### embeddings.create()
481664

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)
483670

484671
```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()
487690

488691
embedding = client.embeddings.create(
489692
model="text-embedding-3-large", # Replace with your model deployment name

0 commit comments

Comments
 (0)