You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
18
18
19
19
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.
* 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).
14
14
* <ahref="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`.
16
16
*[Jupyter Notebooks](https://jupyter.org/)
17
17
18
18
## Set up
@@ -21,22 +21,11 @@ ms.author: mbullwin
21
21
22
22
If you haven't already, you need to install the following libraries:
<!--Alternatively, you can use our [requirements.txt file](https://github.com/Azure-Samples/Azure-OpenAI-Docs-Samples/blob/main/Samples/Tutorials/Embeddings/requirements.txt).-->
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
-
207
96
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`.
208
97
209
98
```python
@@ -224,7 +113,7 @@ df_bills
224
113
225
114
**Output:**
226
115
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":::
228
117
229
118
Next we'll perform some light data cleaning by removing redundant whitespace and cleaning up the punctuation to prepare the data for tokenization.
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).
250
139
251
140
```python
252
141
tokenizer = tiktoken.get_encoding("cl100k_base")
@@ -260,7 +149,7 @@ len(df_bills)
260
149
```
261
150
262
151
>[!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.
264
153
265
154
We'll once again examine **df_bills**.
266
155
@@ -347,13 +236,10 @@ Now that we understand more about how tokenization works we can move on to embed
347
236
348
237
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.
df_bills['ada_v2'] = df_bills["text"].apply(lambdax : 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
363
249
```
364
250
365
-
# [OpenAI Python 0.28.1](#tab/python)
366
-
367
-
```python
368
-
df_bills['ada_v2'] = df_bills["text"].apply(lambdax : 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
-
---
372
251
373
252
```python
374
253
df_bills
@@ -380,8 +259,6 @@ df_bills
380
259
381
260
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).
382
261
383
-
# [OpenAI Python 1.x](#tab/python-new)
384
-
385
262
```python
386
263
defcosine_similarity(a, b):
387
264
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
res = search_docs(df_bills, "Can I get information on cable company tax revenue?", top_n=4)
432
-
```
433
-
434
-
---
435
-
436
288
**Output**:
437
289
438
290
:::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":::
439
291
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:
0 commit comments