Skip to content

Commit 594cb94

Browse files
authored
Merge pull request #258302 from mrbullwinkle/mrb_11_09_2023_ask_mode_request
[Azure OpenAI] ASK Mode Request [Ship during feeze]
2 parents fc70b1e + c2d58f8 commit 594cb94

File tree

4 files changed

+195
-27
lines changed

4 files changed

+195
-27
lines changed

articles/ai-services/openai/how-to/function-calling.md

Lines changed: 2 additions & 2 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: how-to
9-
ms.date: 11/06/2023
9+
ms.date: 11/09/2023
1010
manager: nitinme
1111
---
1212

@@ -103,7 +103,7 @@ from openai import AzureOpenAI
103103
client = AzureOpenAI(
104104
api_key=os.getenv("AZURE_OPENAI_KEY"),
105105
api_version="2023-10-01-preview",
106-
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"
106+
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
107107
)
108108

109109
messages= [

articles/ai-services/openai/how-to/migration.md

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ ms.author: mbullwin
77
ms.service: azure-ai-openai
88
ms.custom:
99
ms.topic: how-to
10-
ms.date: 11/06/2023
10+
ms.date: 11/10/2023
1111
manager: nitinme
1212
---
1313

1414
# Migrating to the OpenAI Python API library 1.x
1515

16-
OpenAI has just released a new version of the [OpenAI Python API library](https://github.com/openai/openai-python/). This guide is supplemental to [OpenAI's migration guide](https://github.com/openai/openai-python/discussions/631) and will help bring you up to speed on the changes specific to Azure OpenAI.
16+
OpenAI has just released a new version of the [OpenAI Python API library](https://github.com/openai/openai-python/). This guide is supplemental to [OpenAI's migration guide](https://github.com/openai/openai-python/discussions/742) and will help bring you up to speed on the changes specific to Azure OpenAI.
1717

1818
## Updates
1919

@@ -30,7 +30,7 @@ OpenAI has just released a new version of the [OpenAI Python API library](https:
3030

3131
- The latest release of the [OpenAI Python library](https://pypi.org/project/openai/) doesn't currently support DALL-E when used with Azure OpenAI. DALL-E with Azure OpenAI is still supported with `0.28.1`. For those who can't wait for native support for DALL-E and Azure OpenAI we're providing [two code examples](#dall-e-fix) which can be used as a workaround.
3232
- `embeddings_utils.py` which was used to provide functionality like cosine similarity for semantic text search is [no longer part of the OpenAI Python API library](https://github.com/openai/openai-python/issues/676).
33-
- You should also check the active [GitHub Issues](https://github.com/openai/openai-python/issues/703) for the OpenAI Python library.
33+
- You should also check the active [GitHub Issues](https://github.com/openai/openai-python/issues/) for the OpenAI Python library.
3434

3535
## Test before you migrate
3636

@@ -252,6 +252,114 @@ completion = client.chat.completions.create(
252252
print(completion.model_dump_json(indent=2))
253253
```
254254

255+
## Use your data
256+
257+
For the full configuration steps that are required to make these code examples work, please consult the [use your data quickstart](../use-your-data-quickstart.md).
258+
# [OpenAI Python 0.28.1](#tab/python)
259+
260+
```python
261+
import os
262+
import openai
263+
import dotenv
264+
import requests
265+
266+
dotenv.load_dotenv()
267+
268+
openai.api_base = os.environ.get("AOAIEndpoint")
269+
openai.api_version = "2023-08-01-preview"
270+
openai.api_type = 'azure'
271+
openai.api_key = os.environ.get("AOAIKey")
272+
273+
def setup_byod(deployment_id: str) -> None:
274+
"""Sets up the OpenAI Python SDK to use your own data for the chat endpoint.
275+
276+
:param deployment_id: The deployment ID for the model to use with your own data.
277+
278+
To remove this configuration, simply set openai.requestssession to None.
279+
"""
280+
281+
class BringYourOwnDataAdapter(requests.adapters.HTTPAdapter):
282+
283+
def send(self, request, **kwargs):
284+
request.url = f"{openai.api_base}/openai/deployments/{deployment_id}/extensions/chat/completions?api-version={openai.api_version}"
285+
return super().send(request, **kwargs)
286+
287+
session = requests.Session()
288+
289+
# Mount a custom adapter which will use the extensions endpoint for any call using the given `deployment_id`
290+
session.mount(
291+
prefix=f"{openai.api_base}/openai/deployments/{deployment_id}",
292+
adapter=BringYourOwnDataAdapter()
293+
)
294+
295+
openai.requestssession = session
296+
297+
aoai_deployment_id = os.environ.get("AOAIDeploymentId")
298+
setup_byod(aoai_deployment_id)
299+
300+
completion = openai.ChatCompletion.create(
301+
messages=[{"role": "user", "content": "What are the differences between Azure Machine Learning and Azure AI services?"}],
302+
deployment_id=os.environ.get("AOAIDeploymentId"),
303+
dataSources=[ # camelCase is intentional, as this is the format the API expects
304+
{
305+
"type": "AzureCognitiveSearch",
306+
"parameters": {
307+
"endpoint": os.environ.get("SearchEndpoint"),
308+
"key": os.environ.get("SearchKey"),
309+
"indexName": os.environ.get("SearchIndex"),
310+
}
311+
}
312+
]
313+
)
314+
print(completion)
315+
```
316+
317+
# [OpenAI Python 1.x](#tab/python-new)
318+
319+
```python
320+
import os
321+
import openai
322+
import dotenv
323+
324+
dotenv.load_dotenv()
325+
326+
endpoint = os.environ.get("AOAIEndpoint")
327+
api_key = os.environ.get("AOAIKey")
328+
deployment = os.environ.get("AOAIDeploymentId")
329+
330+
client = openai.AzureOpenAI(
331+
base_url=f"{endpoint}/openai/deployments/{deployment}/extensions",
332+
api_key=api_key,
333+
api_version="2023-08-01-preview",
334+
)
335+
336+
completion = client.chat.completions.create(
337+
model=deployment,
338+
messages=[
339+
{
340+
"role": "user",
341+
"content": "How is Azure machine learning different than Azure OpenAI?",
342+
},
343+
],
344+
extra_body={
345+
"dataSources": [
346+
{
347+
"type": "AzureCognitiveSearch",
348+
"parameters": {
349+
"endpoint": os.environ["SearchEndpoint"],
350+
"key": os.environ["SearchKey"],
351+
"indexName": os.environ["SearchIndex"]
352+
}
353+
}
354+
]
355+
}
356+
)
357+
358+
print(completion.model_dump_json(indent=2))
359+
```
360+
361+
---
362+
255363
## DALL-E fix
256364

257365
# [DALLE-Fix](#tab/dalle-fix)
@@ -417,6 +525,7 @@ asyncio.run(dall_e())
417525
```
418526

419527
---
528+
420529
## Name changes
421530

422531
> [!NOTE]

articles/ai-services/openai/includes/use-your-data-python.md

Lines changed: 78 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
services: cognitive-services
33
manager: nitinme
4-
author: travisw
5-
ms.author: travisw
4+
author: mrbullwinkle #travisw
5+
ms.author: mbullwin #travisw
66
ms.service: azure-ai-openai
77
ms.topic: include
8-
ms.date: 08/29/2023
8+
ms.date: 11/09/2023
99
---
1010

1111
[!INCLUDE [Set up required variables](./use-your-data-common-variables.md)]
@@ -14,22 +14,35 @@ ms.date: 08/29/2023
1414

1515
1. Create a new folder named *openai-python* for your project and a new Python code file named *main.py*. Change into that directory:
1616

17-
```cmd
18-
mkdir openai-python
19-
cd openai-python
20-
```
17+
```cmd
18+
mkdir openai-python
19+
cd openai-python
20+
```
2121

22-
1. Install the following Python Libraries:
22+
2. Install the following Python Libraries:
2323

24-
```cmd
25-
pip install openai==0.28.1
26-
pip install python-dotenv
27-
```
24+
# [OpenAI Python 0.28.1](#tab/python)
25+
26+
```console
27+
pip install openai==0.28.1
28+
pip install python-dotenv
29+
```
2830

31+
# [OpenAI Python 1.x](#tab/python-new)
32+
33+
```console
34+
pip install openai
35+
pip install python-dotenv
36+
```
37+
38+
---
39+
2940
## Create the Python app
3041

3142
1. From the project directory, open the *main.py* file and add the following code:
3243

44+
# [OpenAI Python 0.28.1](#tab/python)
45+
3346
```python
3447
import os
3548
import openai
@@ -89,16 +102,62 @@ ms.date: 08/29/2023
89102
print(completion)
90103
```
91104

92-
> [!IMPORTANT]
93-
> For production, use a secure way of storing and accessing your credentials like [Azure Key Vault](../../../key-vault/general/overview.md). For more information about credential security, see the Azure AI services [security](../../security-features.md) article.
105+
# [OpenAI Python 1.x](#tab/python-new)
106+
107+
```python
108+
import os
109+
import openai
110+
import dotenv
111+
112+
dotenv.load_dotenv()
113+
114+
endpoint = os.environ.get("AOAIEndpoint")
115+
api_key = os.environ.get("AOAIKey")
116+
deployment = os.environ.get("AOAIDeploymentId")
117+
118+
client = openai.AzureOpenAI(
119+
base_url=f"{endpoint}/openai/deployments/{deployment}/extensions",
120+
api_key=api_key,
121+
api_version="2023-08-01-preview",
122+
)
123+
124+
completion = client.chat.completions.create(
125+
model=deployment,
126+
messages=[
127+
{
128+
"role": "user",
129+
"content": "How is Azure machine learning different than Azure OpenAI?",
130+
},
131+
],
132+
extra_body={
133+
"dataSources": [
134+
{
135+
"type": "AzureCognitiveSearch",
136+
"parameters": {
137+
"endpoint": os.environ["SearchEndpoint"],
138+
"key": os.environ["SearchKey"],
139+
"indexName": os.environ["SearchIndex"]
140+
}
141+
}
142+
]
143+
}
144+
)
145+
146+
print(completion.model_dump_json(indent=2))
147+
```
94148

95-
1. Execute the following command:
149+
---
96150

97-
```cmd
98-
python main.py
99-
```
151+
> [!IMPORTANT]
152+
> For production, use a secure way of storing and accessing your credentials like [Azure Key Vault](../../../key-vault/general/overview.md). For more information about credential security, see the Azure AI services [security](../../security-features.md) article.
153+
154+
2. Execute the following command:
155+
156+
```cmd
157+
python main.py
158+
```
100159

101-
The application prints the response in a JSON format suitable for use in many scenarios. It includes both answers to your query and citations from your uploaded files.
160+
The application prints the response in a JSON format suitable for use in many scenarios. It includes both answers to your query and citations from your uploaded files.
102161

103162
> [!div class="nextstepaction"]
104163
> [I ran into an issue when running the code samples.](https://microsoft.qualtrics.com/jfe/form/SV_0Cl5zkG3CnDjq6O?PLanguage=dotnet&Pillar=AOAI&Product=ownData&Page=quickstart&Section=Create-dotnet-application)

articles/ai-services/openai/toc.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ items:
8080
items:
8181
- name: Fine-tuning your model
8282
href: ./how-to/fine-tuning.md
83+
- name: Migrate to OpenAI Python v1.x
84+
href: ./how-to/migration.md
8385
- name: Models
8486
items:
8587
- name: Manage models
@@ -100,8 +102,6 @@ items:
100102
href: ./how-to/switching-endpoints.md
101103
- name: Manage quota
102104
href: ./how-to/quota.md
103-
- name: Migrate to OpenAI Python v1.x
104-
href: ./how-to/migration.md
105105
- name: Monitor Azure OpenAI
106106
href: ./how-to/monitoring.md
107107
- name: Plan and manage costs
@@ -148,7 +148,7 @@ items:
148148
- name: REST API (completions & embeddings)
149149
href: reference.md
150150
- name: REST API (fine-tuning)
151-
href: /rest/api/azureopenai/fine-tuning?view=rest-azureopenai-2023-10-01-preview
151+
href: /rest/api/azureopenai/fine-tuning?view=rest-azureopenai-2023-10-01-preview&preserve-view=true
152152
- name: REST API (resource creation & deployment)
153153
href: /rest/api/cognitiveservices/accountmanagement/deployments/create-or-update?tabs=HTTP
154154
- name: Resources

0 commit comments

Comments
 (0)