Skip to content

Commit 7c86be0

Browse files
Merge pull request #260847 from soferreira/patch-22
Update integrate-synapseml.md
2 parents 6c4e998 + 11addf6 commit 7c86be0

File tree

1 file changed

+76
-7
lines changed

1 file changed

+76
-7
lines changed

articles/ai-services/openai/how-to/integrate-synapseml.md

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ This tutorial shows how to apply large language models at a distributed scale by
3535
- To install SynapseML for your Apache Spark cluster, see [Install SynapseML](#install-synapseml).
3636

3737
> [!NOTE]
38-
> This article is designed to work with the [Azure OpenAI Service legacy models](/azure/ai-services/openai/concepts/legacy-models) like `Text-Davinci-003`, which support prompt-based completions. Newer models like the current `GPT-3.5 Turbo` and `GPT-4` model series are designed to work with the new chat completion API that expects a specially formatted array of messages as input.
38+
> The `OpenAICompletion()` transformer is designed to work with the [Azure OpenAI Service legacy models](/azure/ai-services/openai/concepts/legacy-models) like `Text-Davinci-003`, which supports prompt-based completions. Newer models like the current `GPT-3.5 Turbo` and `GPT-4` model series are designed to work with the new chat completion API that expects a specially formatted array of messages as input. If you working with embeddings or chat completion models, please check the [Chat Completion](#chat-completion) and [Generating Text Embeddings](#generating-text-embeddings) sections bellow.
3939
>
40-
> The Azure OpenAI SynapseML integration supports the latest models via the [OpenAIChatCompletion()](https://github.com/microsoft/SynapseML/blob/0836e40efd9c48424e91aa10c8aa3fbf0de39f31/cognitive/src/main/scala/com/microsoft/azure/synapse/ml/cognitive/openai/OpenAIChatCompletion.scala#L24) transformer, which isn't demonstrated in this article. After the [release of the GPT-3.5 Turbo Instruct model](https://techcommunity.microsoft.com/t5/azure-ai-services-blog/announcing-updates-to-azure-openai-service-models/ba-p/3866757), the newer model will be the preferred model to use with this article.
40+
> The Azure OpenAI SynapseML integration supports the latest models via the [OpenAIChatCompletion()](https://github.com/microsoft/SynapseML/blob/0836e40efd9c48424e91aa10c8aa3fbf0de39f31/cognitive/src/main/scala/com/microsoft/azure/synapse/ml/cognitive/openai/OpenAIChatCompletion.scala#L24) transformer.
4141
4242
We recommend that you [create an Azure Synapse workspace](../../../synapse-analytics/get-started-create-workspace.md). However, you can also use Azure Databricks, Azure HDInsight, Spark on Kubernetes, or the Python environment with the `pyspark` package.
4343

@@ -187,15 +187,87 @@ The following image shows example output with completions in Azure Synapse Analy
187187

188188
Here are some other use cases for working with Azure OpenAI Service and large datasets.
189189

190-
### Improve throughput with request batching
190+
### Generating Text Embeddings
191+
192+
In addition to completing text, we can also embed text for use in downstream algorithms or vector retrieval architectures. Creating embeddings allows you to search and retrieve documents from large collections and can be used when prompt engineering isn't sufficient for the task. For more information on using [OpenAIEmbedding](https://mmlspark.blob.core.windows.net/docs/0.11.1/pyspark/_modules/synapse/ml/cognitive/openai/OpenAIEmbedding.html), see our [embedding guide](https://microsoft.github.io/SynapseML/docs/Explore%20Algorithms/OpenAI/Quickstart%20-%20OpenAI%20Embedding/).
193+
194+
from synapse.ml.services.openai import OpenAIEmbedding
195+
196+
```python
197+
embedding = (
198+
OpenAIEmbedding()
199+
.setSubscriptionKey(key)
200+
.setDeploymentName(deployment_name_embeddings)
201+
.setCustomServiceName(service_name)
202+
.setTextCol("prompt")
203+
.setErrorCol("error")
204+
.setOutputCol("embeddings")
205+
)
206+
207+
display(embedding.transform(df))
208+
```
209+
210+
### Chat Completion
211+
Models such as ChatGPT and GPT-4 are capable of understanding chats instead of single prompts. The [OpenAIChatCompletion](https://mmlspark.blob.core.windows.net/docs/0.11.1/pyspark/_modules/synapse/ml/cognitive/openai/OpenAIChatCompletion.html) transformer exposes this functionality at scale.
212+
213+
```python
214+
from synapse.ml.services.openai import OpenAIChatCompletion
215+
from pyspark.sql import Row
216+
from pyspark.sql.types import *
217+
218+
219+
def make_message(role, content):
220+
return Row(role=role, content=content, name=role)
221+
222+
223+
chat_df = spark.createDataFrame(
224+
[
225+
(
226+
[
227+
make_message(
228+
"system", "You are an AI chatbot with red as your favorite color"
229+
),
230+
make_message("user", "Whats your favorite color"),
231+
],
232+
),
233+
(
234+
[
235+
make_message("system", "You are very excited"),
236+
make_message("user", "How are you today"),
237+
],
238+
),
239+
]
240+
).toDF("messages")
241+
242+
chat_completion = (
243+
OpenAIChatCompletion()
244+
.setSubscriptionKey(key)
245+
.setDeploymentName(deployment_name)
246+
.setCustomServiceName(service_name)
247+
.setMessagesCol("messages")
248+
.setErrorCol("error")
249+
.setOutputCol("chat_completions")
250+
)
251+
252+
display(
253+
chat_completion.transform(chat_df).select(
254+
"messages", "chat_completions.choices.message.content"
255+
)
256+
)
257+
```
258+
259+
### Improve throughput with request batching from OpenAICompletion
191260

192261
You can use Azure OpenAI Service with large datasets to improve throughput with request batching. In the previous example, you make several requests to the service, one for each prompt. To complete multiple prompts in a single request, you can use batch mode.
193262

194-
In the `OpenAICompletion` object definition, you specify the `"batchPrompt"` value to configure the dataframe to use a **batchPrompt** column. Create the dataframe with a list of prompts for each row.
263+
In the [OpenAItCompletion](https://mmlspark.blob.core.windows.net/docs/0.11.1/pyspark/_modules/synapse/ml/cognitive/openai/OpenAICompletion.html) object definition, you specify the `"batchPrompt"` value to configure the dataframe to use a **batchPrompt** column. Create the dataframe with a list of prompts for each row.
195264

196265
> [!NOTE]
197266
> There's currently a limit of 20 prompts in a single request and a limit of 2048 tokens, or approximately 1500 words.
198267
268+
> [!NOTE]
269+
> Currently, request batching is not supported by the `OpenAIChatCompletion()` transformer.
270+
199271
```python
200272
batch_df = spark.createDataFrame(
201273
[
@@ -227,9 +299,6 @@ completed_batch_df = batch_completion.transform(batch_df).cache()
227299
display(completed_batch_df)
228300
```
229301

230-
> [!NOTE]
231-
> There's currently a limit of 20 prompts in a single request and a limit of 2048 tokens, or approximately 1500 words.
232-
233302
### Use an automatic mini-batcher
234303

235304
You can use Azure OpenAI Service with large datasets to transpose the data format. If your data is in column format, you can transpose it to row format by using the SynapseML `FixedMiniBatcherTransformer` object.

0 commit comments

Comments
 (0)