Skip to content

Commit b01a559

Browse files
committed
fix numbering
1 parent dba9576 commit b01a559

File tree

2 files changed

+123
-123
lines changed

2 files changed

+123
-123
lines changed

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

Lines changed: 2 additions & 2 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/09/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

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

Lines changed: 121 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -14,144 +14,144 @@ ms.date: 11/09/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-
```
21-
22-
1. Install the following Python Libraries:
23-
24-
# [OpenAI Python 0.28.1](#tab/python)
25-
26-
```console
27-
pip install openai==0.28.1
28-
pip install python-dotenv
29-
```
30-
31-
# [OpenAI Python 1.x](#tab/python-new)
32-
33-
```console
34-
pip install openai
35-
pip install python-dotenv
36-
```
17+
```cmd
18+
mkdir openai-python
19+
cd openai-python
20+
```
21+
22+
2. Install the following Python Libraries:
3723
38-
---
24+
# [OpenAI Python 0.28.1](#tab/python)
25+
26+
```console
27+
pip install openai==0.28.1
28+
pip install python-dotenv
29+
```
30+
31+
# [OpenAI Python 1.x](#tab/python-new)
32+
33+
```console
34+
pip install openai
35+
pip install python-dotenv
36+
```
37+
38+
---
3939
4040
## Create the Python app
4141
4242
1. From the project directory, open the *main.py* file and add the following code:
4343
44-
# [OpenAI Python 0.28.1](#tab/python)
45-
46-
```python
47-
import os
48-
import openai
49-
import dotenv
50-
import requests
51-
52-
dotenv.load_dotenv()
53-
54-
openai.api_base = os.environ.get("AOAIEndpoint")
55-
56-
# Azure OpenAI on your own data is only supported by the 2023-08-01-preview API version
57-
openai.api_version = "2023-08-01-preview"
58-
openai.api_type = 'azure'
59-
openai.api_key = os.environ.get("AOAIKey")
60-
61-
def setup_byod(deployment_id: str) -> None:
62-
"""Sets up the OpenAI Python SDK to use your own data for the chat endpoint.
44+
# [OpenAI Python 0.28.1](#tab/python)
6345
64-
:param deployment_id: The deployment ID for the model to use with your own data.
65-
66-
To remove this configuration, simply set openai.requestssession to None.
67-
"""
68-
69-
class BringYourOwnDataAdapter(requests.adapters.HTTPAdapter):
70-
71-
def send(self, request, **kwargs):
72-
request.url = f"{openai.api_base}/openai/deployments/{deployment_id}/extensions/chat/completions?api-version={openai.api_version}"
73-
return super().send(request, **kwargs)
74-
75-
session = requests.Session()
76-
77-
# Mount a custom adapter which will use the extensions endpoint for any call using the given `deployment_id`
78-
session.mount(
79-
prefix=f"{openai.api_base}/openai/deployments/{deployment_id}",
80-
adapter=BringYourOwnDataAdapter()
81-
)
82-
83-
openai.requestssession = session
84-
85-
aoai_deployment_id = os.environ.get("AOAIDeploymentId")
86-
setup_byod(aoai_deployment_id)
87-
88-
completion = openai.ChatCompletion.create(
89-
messages=[{"role": "user", "content": "What are the differences between Azure Machine Learning and Azure AI services?"}],
90-
deployment_id=os.environ.get("AOAIDeploymentId"),
91-
dataSources=[ # camelCase is intentional, as this is the format the API expects
92-
{
93-
"type": "AzureCognitiveSearch",
94-
"parameters": {
95-
"endpoint": os.environ.get("SearchEndpoint"),
96-
"key": os.environ.get("SearchKey"),
97-
"indexName": os.environ.get("SearchIndex"),
46+
```python
47+
import os
48+
import openai
49+
import dotenv
50+
import requests
51+
52+
dotenv.load_dotenv()
53+
54+
openai.api_base = os.environ.get("AOAIEndpoint")
55+
56+
# Azure OpenAI on your own data is only supported by the 2023-08-01-preview API version
57+
openai.api_version = "2023-08-01-preview"
58+
openai.api_type = 'azure'
59+
openai.api_key = os.environ.get("AOAIKey")
60+
61+
def setup_byod(deployment_id: str) -> None:
62+
"""Sets up the OpenAI Python SDK to use your own data for the chat endpoint.
63+
64+
:param deployment_id: The deployment ID for the model to use with your own data.
65+
66+
To remove this configuration, simply set openai.requestssession to None.
67+
"""
68+
69+
class BringYourOwnDataAdapter(requests.adapters.HTTPAdapter):
70+
71+
def send(self, request, **kwargs):
72+
request.url = f"{openai.api_base}/openai/deployments/{deployment_id}/extensions/chat/completions?api-version={openai.api_version}"
73+
return super().send(request, **kwargs)
74+
75+
session = requests.Session()
76+
77+
# Mount a custom adapter which will use the extensions endpoint for any call using the given `deployment_id`
78+
session.mount(
79+
prefix=f"{openai.api_base}/openai/deployments/{deployment_id}",
80+
adapter=BringYourOwnDataAdapter()
81+
)
82+
83+
openai.requestssession = session
84+
85+
aoai_deployment_id = os.environ.get("AOAIDeploymentId")
86+
setup_byod(aoai_deployment_id)
87+
88+
completion = openai.ChatCompletion.create(
89+
messages=[{"role": "user", "content": "What are the differences between Azure Machine Learning and Azure AI services?"}],
90+
deployment_id=os.environ.get("AOAIDeploymentId"),
91+
dataSources=[ # camelCase is intentional, as this is the format the API expects
92+
{
93+
"type": "AzureCognitiveSearch",
94+
"parameters": {
95+
"endpoint": os.environ.get("SearchEndpoint"),
96+
"key": os.environ.get("SearchKey"),
97+
"indexName": os.environ.get("SearchIndex"),
98+
}
9899
}
99-
}
100-
]
101-
)
102-
print(completion)
103-
```
100+
]
101+
)
102+
print(completion)
103+
```
104104
105105
# [OpenAI Python 1.x](#tab/python-new)
106106
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": [
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=[
134127
{
135-
"type": "AzureCognitiveSearch",
136-
"parameters": {
137-
"endpoint": os.environ["SearchEndpoint"],
138-
"key": os.environ["SearchKey"],
139-
"indexName": os.environ["SearchIndex"]
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+
}
140141
}
141-
}
142-
]
143-
}
144-
)
145-
146-
print(completion.model_dump_json(indent=2))
147-
```
148-
149-
---
142+
]
143+
}
144+
)
145+
146+
print(completion.model_dump_json(indent=2))
147+
```
148+
149+
---
150150
151151
> [!IMPORTANT]
152152
> 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.
153153
154-
1. Execute the following command:
154+
2. Execute the following command:
155155
156156
```cmd
157157
python main.py

0 commit comments

Comments
 (0)