Skip to content

Commit 4d69ee9

Browse files
Merge pull request #6878 from MicrosoftDocs/main
Auto Publish – main to live - 2025-09-01 22:00 UTC
2 parents 0cdc7a4 + 72dc445 commit 4d69ee9

File tree

25 files changed

+231
-380
lines changed

25 files changed

+231
-380
lines changed

articles/ai-foundry/openai/how-to/chatgpt.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ ms.author: mbullwin #delegenz
77
ms.service: azure-ai-openai
88
ms.custom: build-2023, build-2023-dataai, devx-track-python
99
ms.topic: how-to
10-
ms.date: 07/02/2025
10+
ms.date: 09/01/2025
1111
manager: nitinme
1212
keywords: ChatGPT
1313
---
1414

1515
# Work with chat completions models
1616

17-
GPT-3.5-Turbo, GPT-4, and GPT-4o series models are language models that are optimized for conversational interfaces. The models behave differently than the older GPT-3 models. Previous models were text-in and text-out, which means they accepted a prompt string and returned a completion to append to the prompt. However, the latest models are conversation-in and message-out. The models expect input formatted in a specific chat-like transcript format. They return a completion that represents a model-written message in the chat. This format was designed specifically for multi-turn conversations, but it can also work well for nonchat scenarios.
17+
Chat models are language models that are optimized for conversational interfaces. The models behave differently than the older GPT-3 models. Previous models were text-in and text-out, which means they accepted a prompt string and returned a completion to append to the prompt. However, the latest models are conversation-in and message-out. The models expect input formatted in a specific chat-like transcript format. They return a completion that represents a model-written message in the chat. This format was designed specifically for multi-turn conversations, but it can also work well for nonchat scenarios.
1818

1919
This article walks you through getting started with chat completions models. To get the best results, use the techniques described here. Don't try to interact with the models the same way you did with the older model series because the models are often verbose and provide less useful responses.
2020

articles/ai-foundry/openai/includes/chat-completion.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: mrbullwinkle #dereklegenzoff
66
ms.author: mbullwin #delegenz
77
ms.service: azure-ai-openai
88
ms.topic: include
9-
ms.date: 08/29/2024
9+
ms.date: 09/01/2025
1010
manager: nitinme
1111
keywords: ChatGPT
1212

@@ -19,12 +19,11 @@ The following code snippet shows the most basic way to interact with models that
1919

2020
```python
2121
import os
22-
from openai import AzureOpenAI
22+
from openai import OpenAI
2323

24-
client = AzureOpenAI(
24+
client = OpenAI(
2525
api_key = os.getenv("AZURE_OPENAI_API_KEY"),
26-
api_version = "2024-10-21",
27-
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
26+
base_url="https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/"
2827
)
2928

3029
response = client.chat.completions.create(
@@ -238,12 +237,11 @@ Every time a new question is asked, a running transcript of the conversation so
238237

239238
```python
240239
import os
241-
from openai import AzureOpenAI
240+
from openai import OpenAI
242241

243-
client = AzureOpenAI(
242+
client = OpenAI(
244243
api_key = os.getenv("AZURE_OPENAI_API_KEY"),
245-
api_version = "2024-10-21",
246-
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") # Your Azure OpenAI resource's endpoint value.
244+
base_url="https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/"
247245
)
248246

249247
conversation=[{"role": "system", "content": "You are a helpful assistant."}]
@@ -279,12 +277,11 @@ The code uses tiktoken `0.5.1`. If you have an older version, run `pip install t
279277
```python
280278
import tiktoken
281279
import os
282-
from openai import AzureOpenAI
280+
from openai import OpenAI
283281

284-
client = AzureOpenAI(
282+
client = OpenAI(
285283
api_key = os.getenv("AZURE_OPENAI_API_KEY"),
286-
api_version = "2024-10-21",
287-
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") # Your Azure OpenAI resource's endpoint value.
284+
base_url="https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/"
288285
)
289286

290287
system_message = {"role": "system", "content": "You are a helpful assistant."}

articles/ai-foundry/openai/includes/embeddings-powershell.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,17 @@ Create and assign persistent environment variables for your key and endpoint.
3535
setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
3636
```
3737

38-
```CMD
39-
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
40-
```
41-
4238
# [PowerShell](#tab/powershell)
4339

4440
```powershell-interactive
4541
$Env:AZURE_OPENAI_API_KEY = '<YOUR_KEY_VALUE>'
46-
$Env:AZURE_OPENAI_ENDPOINT = '<YOUR_ENDPOINT>'
4742
$Env:AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT = '<YOUR_DEPLOYMENT_NAME>'
4843
```
4944

5045
# [Bash](#tab/bash)
5146

5247
```Bash
5348
echo export AZURE_OPENAI_API_KEY="<YOUR_KEY_VALUE>" >> /etc/environment
54-
echo export AZURE_OPENAI_ENDPOINT="<YOUR_ENDPOINT>" >> /etc/environment
5549
echo export AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT="<YOUR_DEPLOYMENT_NAME>" >> /etc/environment
5650
source /etc/environment
5751
```
@@ -193,16 +187,15 @@ the token count is more than the prompt limit for the model.
193187
# Azure OpenAI metadata variables
194188
$openai = @{
195189
api_key = $Env:AZURE_OPENAI_API_KEY
196-
api_base = $Env:AZURE_OPENAI_ENDPOINT # should look like 'https://<YOUR_RESOURCE_NAME>.openai.azure.com/'
197-
api_version = '2024-02-01' # may change in the future
190+
api_base = 'https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/'
198191
name = $Env:AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT # custom name you chose for your deployment
199192
}
200193
201194
$headers = [ordered]@{
202195
'api-key' = $openai.api_key
203196
}
204197
205-
$url = "$($openai.api_base)/openai/deployments/$($openai.name)/embeddings?api-version=$($openai.api_version)"
198+
$url = "$($openai.api_base)/embeddings"
206199
207200
$Datatable | ForEach-Object {
208201
$doc = $_

articles/ai-foundry/openai/includes/embeddings-python.md

Lines changed: 10 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ services: cognitive-services
33
manager: nitinme
44
ms.service: azure-ai-openai
55
ms.topic: include
6-
ms.date: 03/26/2025
6+
ms.date: 09/01/2025
77
author: mrbullwinkle #noabenefraim
88
ms.author: mbullwin
99
---
1010
## Prerequisites
1111

1212
* An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
13-
* An Azure OpenAI resource with the **text-embedding-ada-002 (Version 2)** model deployed. This model is currently only available in [certain regions](../concepts/models.md#model-summary-table-and-region-availability). If you don't have a resource the process of creating one is documented in our [resource deployment guide](../how-to/create-resource.md).
13+
* An Azure OpenAI resource with the **text-embedding-ada-002 (Version 2)** model deployed. This model is currently only available in [certain regions](../concepts/models.md#model-summary-table-and-region-availability). If you don't have a resource the process of creating one is documented in our [resource deployment guide](../how-to/create-resource.md).
1414
* <a href="https://www.python.org/" target="_blank">Python 3.8 or later version</a>
15-
* The following Python libraries: openai, num2words, matplotlib, plotly, scipy, scikit-learn, pandas, tiktoken.
15+
* The following Python libraries: `openai`, `num2words`, `matplotlib`, `plotly`, `scipy`, `scikit-learn`, `pandas`, `tiktoken`.
1616
* [Jupyter Notebooks](https://jupyter.org/)
1717

1818
## Set up
@@ -21,22 +21,11 @@ ms.author: mbullwin
2121

2222
If you haven't already, you need to install the following libraries:
2323

24-
# [OpenAI Python 1.x](#tab/python-new)
2524

2625
```console
2726
pip install openai num2words matplotlib plotly scipy scikit-learn pandas tiktoken
2827
```
2928

30-
# [OpenAI Python 0.28.1](#tab/python)
31-
32-
[!INCLUDE [Deprecation](../includes/deprecation.md)]
33-
34-
```cmd
35-
pip install "openai==0.28.1" num2words matplotlib plotly scipy scikit-learn pandas tiktoken
36-
```
37-
38-
---
39-
4029
<!--Alternatively, you can use our [requirements.txt file](https://github.com/Azure-Samples/Azure-OpenAI-Docs-Samples/blob/main/Samples/Tutorials/Embeddings/requirements.txt).-->
4130

4231
### Download the BillSum dataset
@@ -55,7 +44,7 @@ curl "https://raw.githubusercontent.com/Azure-Samples/Azure-OpenAI-Docs-Samples/
5544

5645
### Environment variables
5746

58-
Create and assign persistent environment variables for your key and endpoint.
47+
Create and assign persistent environment variables for your API key.
5948

6049
[!INCLUDE [Azure key vault](~/reusable-content/ce-skilling/azure/includes/ai-services/security/azure-key-vault.md)]
6150

@@ -65,26 +54,17 @@ Create and assign persistent environment variables for your key and endpoint.
6554
setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
6655
```
6756

68-
```CMD
69-
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
70-
```
71-
7257
# [PowerShell](#tab/powershell)
7358

7459
```powershell
7560
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_API_KEY', 'REPLACE_WITH_YOUR_KEY_VALUE_HERE', 'User')
7661
```
7762

78-
```powershell
79-
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_ENDPOINT', 'REPLACE_WITH_YOUR_ENDPOINT_HERE', 'User')
80-
```
8163

8264
# [Bash](#tab/bash)
8365

8466
```Bash
8567
echo export AZURE_OPENAI_API_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" >> /etc/environment
86-
echo export AZURE_OPENAI_ENDPOINT="REPLACE_WITH_YOUR_ENDPOINT_HERE" >> /etc/environment
87-
8868
source /etc/environment
8969
```
9070

@@ -113,97 +93,6 @@ import tiktoken
11393
from openai import AzureOpenAI
11494
```
11595

116-
117-
# [OpenAI Python 0.28.1](#tab/python)
118-
119-
```python
120-
import openai
121-
import os
122-
import re
123-
import requests
124-
import sys
125-
from num2words import num2words
126-
import os
127-
import pandas as pd
128-
import numpy as np
129-
from openai.embeddings_utils import get_embedding, cosine_similarity
130-
import tiktoken
131-
132-
API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
133-
RESOURCE_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
134-
135-
openai.api_type = "azure"
136-
openai.api_key = API_KEY
137-
openai.api_base = RESOURCE_ENDPOINT
138-
openai.api_version = "2024-10-21"
139-
140-
url = openai.api_base + "/openai/deployments?api-version=2024-10-21"
141-
142-
r = requests.get(url, headers={"api-key": API_KEY})
143-
144-
print(r.text)
145-
```
146-
147-
```output
148-
{
149-
"data": [
150-
{
151-
"scale_settings": {
152-
"scale_type": "standard"
153-
},
154-
"model": "text-embedding-ada-002",
155-
"owner": "organization-owner",
156-
"id": "text-embedding-ada-002",
157-
"status": "succeeded",
158-
"created_at": 1657572678,
159-
"updated_at": 1657572678,
160-
"object": "deployment"
161-
},
162-
{
163-
"scale_settings": {
164-
"scale_type": "standard"
165-
},
166-
"model": "code-cushman-001",
167-
"owner": "organization-owner",
168-
"id": "code-cushman-001",
169-
"status": "succeeded",
170-
"created_at": 1657572712,
171-
"updated_at": 1657572712,
172-
"object": "deployment"
173-
},
174-
{
175-
"scale_settings": {
176-
"scale_type": "standard"
177-
},
178-
"model": "text-search-curie-doc-001",
179-
"owner": "organization-owner",
180-
"id": "text-search-curie-doc-001",
181-
"status": "succeeded",
182-
"created_at": 1668620345,
183-
"updated_at": 1668620345,
184-
"object": "deployment"
185-
},
186-
{
187-
"scale_settings": {
188-
"scale_type": "standard"
189-
},
190-
"model": "text-search-curie-query-001",
191-
"owner": "organization-owner",
192-
"id": "text-search-curie-query-001",
193-
"status": "succeeded",
194-
"created_at": 1669048765,
195-
"updated_at": 1669048765,
196-
"object": "deployment"
197-
}
198-
],
199-
"object": "list"
200-
}
201-
```
202-
203-
The output of this command will vary based on the number and type of models you've deployed. In this case, we need to confirm that we have an entry for **text-embedding-ada-002**. If you find that you're missing this model, you'll need to [deploy the model](../how-to/create-resource.md#deploy-a-model) to your resource before proceeding.
204-
205-
---
206-
20796
Now we need to read our csv file and create a pandas DataFrame. After the initial DataFrame is created, we can view the contents of the table by running `df`.
20897

20998
```python
@@ -224,7 +113,7 @@ df_bills
224113

225114
**Output:**
226115

227-
:::image type="content" source="../media/tutorials/cleanup-dataframe.png" alt-text="Screenshot of the smaller DataFrame table results with only text, summary and title columns displayed." lightbox="../media/tutorials/cleanup-dataframe.png":::
116+
:::image type="content" source="../media/tutorials/cleanup-dataframe.png" alt-text="Screenshot of the smaller DataFrame table results with only text, summary, and title columns displayed." lightbox="../media/tutorials/cleanup-dataframe.png":::
228117

229118
Next we'll perform some light data cleaning by removing redundant whitespace and cleaning up the punctuation to prepare the data for tokenization.
230119

@@ -246,7 +135,7 @@ def normalize_text(s, sep_token = " \n "):
246135
df_bills['text']= df_bills["text"].apply(lambda x : normalize_text(x))
247136
```
248137

249-
Now we need to remove any bills that are too long for the token limit (8192 tokens).
138+
Now we need to remove any bills that are too long for the token limit (8,192 tokens).
250139

251140
```python
252141
tokenizer = tiktoken.get_encoding("cl100k_base")
@@ -260,7 +149,7 @@ len(df_bills)
260149
```
261150

262151
>[!NOTE]
263-
>In this case all bills are under the embedding model input token limit, but you can use the technique above to remove entries that would otherwise cause embedding to fail. When faced with content that exceeds the embedding limit, you can also chunk the content into smaller pieces and then embed those one at a time.
152+
>In this case all bills are under the embedding model input token limit, but you can use the technique above to remove entries that would otherwise cause embedding to fail. When faced with content that exceeds the embedding limit, you can also chunk the content into smaller pieces and then embed the chunks one at a time.
264153
265154
We'll once again examine **df_bills**.
266155

@@ -347,13 +236,10 @@ Now that we understand more about how tokenization works we can move on to embed
347236

348237
In the example below we're calling the embedding model once per every item that we want to embed. When working with large embedding projects you can alternatively pass the model an array of inputs to embed rather than one input at a time. When you pass the model an array of inputs the max number of input items per call to the embedding endpoint is 2048.
349238

350-
# [OpenAI Python 1.x](#tab/python-new)
351-
352239
```python
353-
client = AzureOpenAI(
240+
client = OpenAI(
354241
api_key = os.getenv("AZURE_OPENAI_API_KEY"),
355-
api_version = "2024-02-01",
356-
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
242+
base_url="https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/"
357243
)
358244

359245
def generate_embeddings(text, model="text-embedding-ada-002"): # model = "deployment_name"
@@ -362,13 +248,6 @@ def generate_embeddings(text, model="text-embedding-ada-002"): # model = "deploy
362248
df_bills['ada_v2'] = df_bills["text"].apply(lambda x : generate_embeddings (x, model = 'text-embedding-ada-002')) # model should be set to the deployment name you chose when you deployed the text-embedding-ada-002 (Version 2) model
363249
```
364250

365-
# [OpenAI Python 0.28.1](#tab/python)
366-
367-
```python
368-
df_bills['ada_v2'] = df_bills["text"].apply(lambda x : get_embedding(x, engine = 'text-embedding-ada-002')) # engine should be set to the deployment name you chose when you deployed the text-embedding-ada-002 (Version 2) model
369-
```
370-
371-
---
372251

373252
```python
374253
df_bills
@@ -380,8 +259,6 @@ df_bills
380259

381260
As we run the search code block below, we'll embed the search query *"Can I get information on cable company tax revenue?"* with the same **text-embedding-ada-002 (Version 2)** model. Next we'll find the closest bill embedding to the newly embedded text from our query ranked by [cosine similarity](../concepts/understand-embeddings.md).
382261

383-
# [OpenAI Python 1.x](#tab/python-new)
384-
385262
```python
386263
def cosine_similarity(a, b):
387264
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
@@ -408,36 +285,11 @@ def search_docs(df, user_query, top_n=4, to_print=True):
408285
res = search_docs(df_bills, "Can I get information on cable company tax revenue?", top_n=4)
409286
```
410287

411-
# [OpenAI Python 0.28.1](#tab/python)
412-
413-
```python
414-
# search through the reviews for a specific product
415-
def search_docs(df, user_query, top_n=3, to_print=True):
416-
embedding = get_embedding(
417-
user_query,
418-
engine="text-embedding-ada-002" # engine should be set to the deployment name you chose when you deployed the text-embedding-ada-002 (Version 2) model
419-
)
420-
df["similarities"] = df.ada_v2.apply(lambda x: cosine_similarity(x, embedding))
421-
422-
res = (
423-
df.sort_values("similarities", ascending=False)
424-
.head(top_n)
425-
)
426-
if to_print:
427-
display(res)
428-
return res
429-
430-
431-
res = search_docs(df_bills, "Can I get information on cable company tax revenue?", top_n=4)
432-
```
433-
434-
---
435-
436288
**Output**:
437289

438290
:::image type="content" source="../media/tutorials/query-result.png" alt-text="Screenshot of the formatted results of res once the search query has been run." lightbox="../media/tutorials/query-result.png":::
439291

440-
Finally, we'll show the top result from document search based on user query against the entire knowledge base. This returns the top result of the "Taxpayer's Right to View Act of 1993". This document has a cosine similarity score of 0.76 between the query and the document:
292+
Finally, we'll show the top result from document search based on user query against the entire knowledge base. This returns the top result of the "Taxpayer's Right to View Act of 1993." This document has a cosine similarity score of 0.76 between the query and the document:
441293

442294
```python
443295
res["summary"][9]

0 commit comments

Comments
 (0)