Skip to content

Commit 7743318

Browse files
Merge pull request #259330 from mrbullwinkle/mrb_11_22_2023_python_new
[Azure OpenAI add 1.x update
2 parents 55294d4 + ea0ce9f commit 7743318

File tree

1 file changed

+57
-55
lines changed

1 file changed

+57
-55
lines changed

articles/ai-services/openai/how-to/switching-endpoints.md

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22
title: How to switch between OpenAI and Azure OpenAI Service endpoints with Python
33
titleSuffix: Azure OpenAI Service
44
description: Learn about the changes you need to make to your code to swap back and forth between OpenAI and Azure OpenAI endpoints.
5-
author: mrbullwinkle #dereklegenzoff
6-
ms.author: mbullwin #delegenz
5+
author: mrbullwinkle
6+
ms.author: mbullwin
77
ms.service: azure-ai-openai
88
ms.custom: devx-track-python
99
ms.topic: how-to
10-
ms.date: 07/20/2023
10+
ms.date: 11/22/2023
1111
manager: nitinme
1212
---
1313

1414
# How to switch between OpenAI and Azure OpenAI endpoints with Python
1515

1616
While OpenAI and Azure OpenAI Service rely on a [common Python client library](https://github.com/openai/openai-python), there are small changes you need to make to your code in order to swap back and forth between endpoints. This article walks you through the common changes and differences you'll experience when working across OpenAI and Azure OpenAI.
1717

18-
> [!NOTE]
19-
> This library is maintained by OpenAI and is currently in preview. Refer to the [release history](https://github.com/openai/openai-python/releases) or the [version.py commit history](https://github.com/openai/openai-python/commits/main/openai/version.py) to track the latest updates to the library.
18+
This article only shows examples with the new OpenAI Python 1.x API library. For information on migrating from `0.28.1` to `1.x` refer to our [migration guide](./migration.md).
2019

2120
## Authentication
2221

@@ -32,10 +31,12 @@ We recommend using environment variables. If you haven't done this before our [P
3231
<td>
3332

3433
```python
35-
import openai
34+
from openai import OpenAI
35+
36+
client = OpenAI(
37+
api_key=os.environ['OPENAI_API_KEY']
38+
)
3639

37-
openai.api_key = "sk-..."
38-
openai.organization = "..."
3940

4041

4142
```
@@ -44,12 +45,14 @@ openai.organization = "..."
4445
<td>
4546

4647
```python
47-
import openai
48-
49-
openai.api_type = "azure"
50-
openai.api_key = "..."
51-
openai.api_base = "https://example-endpoint.openai.azure.com"
52-
openai.api_version = "2023-05-15" # subject to change
48+
import os
49+
from openai import AzureOpenAI
50+
51+
client = AzureOpenAI(
52+
api_key=os.getenv("AZURE_OPENAI_KEY"),
53+
api_version="2023-10-01-preview",
54+
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
55+
)
5356
```
5457

5558
</td>
@@ -68,10 +71,13 @@ openai.api_version = "2023-05-15" # subject to change
6871
<td>
6972

7073
```python
71-
import openai
74+
from openai import OpenAI
75+
76+
client = OpenAI(
77+
api_key=os.environ['OPENAI_API_KEY']
78+
)
79+
7280

73-
openai.api_key = "sk-..."
74-
openai.organization = "..."
7581

7682

7783

@@ -84,16 +90,19 @@ openai.organization = "..."
8490
<td>
8591

8692
```python
87-
import openai
88-
from azure.identity import DefaultAzureCredential
93+
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
94+
from openai import AzureOpenAI
8995

90-
credential = DefaultAzureCredential()
91-
token = credential.get_token("https://cognitiveservices.azure.com/.default")
96+
token_provider = get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default")
9297

93-
openai.api_type = "azure_ad"
94-
openai.api_key = token.token
95-
openai.api_base = "https://example-endpoint.openai.azure.com"
96-
openai.api_version = "2023-05-15" # subject to change
98+
api_version = "2023-12-01-preview"
99+
endpoint = "https://my-resource.openai.azure.com"
100+
101+
client = AzureOpenAI(
102+
api_version=api_version,
103+
azure_endpoint=endpoint,
104+
azure_ad_token_provider=token_provider,
105+
)
97106
```
98107

99108
</td>
@@ -114,47 +123,39 @@ For OpenAI `engine` still works in most instances, but it's deprecated and `mode
114123
<td>
115124

116125
```python
117-
completion = openai.Completion.create(
118-
prompt="<prompt>",
119-
model="text-davinci-003"
120-
)
121-
122-
chat_completion = openai.ChatCompletion.create(
123-
messages="<messages>",
124-
model="gpt-4"
126+
completion = client.completions.create(
127+
model='gpt-3.5-turbo-instruct',
128+
prompt="<prompt>)
125129
)
126130

127-
embedding = openai.Embedding.create(
128-
input="<input>",
129-
model="text-embedding-ada-002"
131+
chat_completion = client.chat.completions.create(
132+
model="gpt-4",
133+
messages="<messages>"
130134
)
131135

132-
133-
134-
136+
embedding = client.embeddings.create(
137+
input="<input>",
138+
model="text-embedding-ada-002"
139+
)
135140
```
136141

137142
</td>
138143
<td>
139144

140145
```python
141-
completion = openai.Completion.create(
142-
prompt="<prompt>",
143-
deployment_id="text-davinci-003" # This must match the custom deployment name you chose for your model.
144-
#engine="text-davinci-003"
146+
completion = client.completions.create(
147+
model=gpt-35-turbo-instruct, # This must match the custom deployment name you chose for your model.
148+
prompt=<"prompt">
145149
)
146-
147-
chat_completion = openai.ChatCompletion.create(
148-
messages="<messages>",
149-
deployment_id="gpt-4" # This must match the custom deployment name you chose for your model.
150-
#engine="gpt-4"
151150

151+
chat_completion = client.chat.completions.create(
152+
model="gpt-35-turbo", # model = "deployment_name".
153+
messages=<"messages">
152154
)
153155

154-
embedding = openai.Embedding.create(
155-
input="<input>",
156-
deployment_id="text-embedding-ada-002" # This must match the custom deployment name you chose for your model.
157-
#engine="text-embedding-ada-002"
156+
embedding = client.embeddings.create(
157+
input = "<input>",
158+
model= "text-embedding-ada-002" # model = "deployment_name".
158159
)
159160
```
160161

@@ -176,7 +177,7 @@ OpenAI currently allows a larger number of array inputs with text-embedding-ada-
176177
```python
177178
inputs = ["A", "B", "C"]
178179

179-
embedding = openai.Embedding.create(
180+
embedding = client.embeddings.create(
180181
input=inputs,
181182
model="text-embedding-ada-002"
182183
)
@@ -190,11 +191,12 @@ embedding = openai.Embedding.create(
190191
```python
191192
inputs = ["A", "B", "C"] #max array size=16
192193

193-
embedding = openai.Embedding.create(
194+
embedding = client.embeddings.create(
194195
input=inputs,
195-
deployment_id="text-embedding-ada-002" # This must match the custom deployment name you chose for your model.
196+
model="text-embedding-ada-002" # This must match the custom deployment name you chose for your model.
196197
#engine="text-embedding-ada-002"
197198
)
199+
198200
```
199201

200202
</td>

0 commit comments

Comments
 (0)