Skip to content

Commit c834902

Browse files
committed
update
1 parent dbffff5 commit c834902

File tree

1 file changed

+10
-194
lines changed

1 file changed

+10
-194
lines changed

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

Lines changed: 10 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -15,192 +15,7 @@ manager: nitinme
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-
# [OpenAI Python 0.28.1](#tab/python)
19-
20-
## Authentication
21-
22-
We recommend using environment variables. If you haven't done this before our [Python quickstarts](../quickstart.md) walk you through this configuration.
23-
24-
### API key
25-
26-
<table>
27-
<tr>
28-
<td> OpenAI </td> <td> Azure OpenAI </td>
29-
</tr>
30-
<tr>
31-
<td>
32-
33-
```python
34-
import openai
35-
36-
openai.api_key = "sk-..."
37-
openai.organization = "..."
38-
39-
40-
```
41-
42-
</td>
43-
<td>
44-
45-
```python
46-
import openai
47-
48-
openai.api_type = "azure"
49-
openai.api_key = "..."
50-
openai.api_base = "https://example-endpoint.openai.azure.com"
51-
openai.api_version = "2023-05-15" # subject to change
52-
```
53-
54-
</td>
55-
</tr>
56-
</table>
57-
58-
<a name='azure-active-directory-authentication'></a>
59-
60-
### Microsoft Entra authentication
61-
62-
<table>
63-
<tr>
64-
<td> OpenAI </td> <td> Azure OpenAI </td>
65-
</tr>
66-
<tr>
67-
<td>
68-
69-
```python
70-
import openai
71-
72-
openai.api_key = "sk-..."
73-
openai.organization = "..."
74-
75-
76-
77-
78-
79-
80-
```
81-
82-
</td>
83-
<td>
84-
85-
```python
86-
import openai
87-
from azure.identity import DefaultAzureCredential
88-
89-
credential = DefaultAzureCredential()
90-
token = credential.get_token("https://cognitiveservices.azure.com/.default")
91-
92-
openai.api_type = "azure_ad"
93-
openai.api_key = token.token
94-
openai.api_base = "https://example-endpoint.openai.azure.com"
95-
openai.api_version = "2023-05-15" # subject to change
96-
```
97-
98-
</td>
99-
</tr>
100-
</table>
101-
102-
## Keyword argument for model
103-
104-
OpenAI uses the `model` keyword argument to specify what model to use. Azure OpenAI has the concept of [deployments](create-resource.md?pivots=web-portal#deploy-a-model) and uses the `deployment_id` keyword argument to describe which model deployment to use. Azure OpenAI also supports the use of `engine` interchangeably with `deployment_id`. `deployment_id` corresponds to the custom name you chose for your model during model deployment. By convention in our docs, we often show `deployment_id`'s which match the underlying model name, but if you chose a different deployment name that doesn't match the model name you need to use that name when working with models in Azure OpenAI.
105-
106-
For OpenAI `engine` still works in most instances, but it's deprecated and `model` is preferred.
107-
108-
<table>
109-
<tr>
110-
<td> OpenAI </td> <td> Azure OpenAI </td>
111-
</tr>
112-
<tr>
113-
<td>
114-
115-
```python
116-
completion = openai.Completion.create(
117-
prompt="<prompt>",
118-
model="text-davinci-003"
119-
)
120-
121-
chat_completion = openai.ChatCompletion.create(
122-
messages="<messages>",
123-
model="gpt-4"
124-
)
125-
126-
embedding = openai.Embedding.create(
127-
input="<input>",
128-
model="text-embedding-ada-002"
129-
)
130-
131-
132-
133-
134-
```
135-
136-
</td>
137-
<td>
138-
139-
```python
140-
completion = openai.Completion.create(
141-
prompt="<prompt>",
142-
deployment_id="text-davinci-003" # This must match the custom deployment name you chose for your model.
143-
#engine="text-davinci-003"
144-
)
145-
146-
chat_completion = openai.ChatCompletion.create(
147-
messages="<messages>",
148-
deployment_id="gpt-4" # This must match the custom deployment name you chose for your model.
149-
#engine="gpt-4"
150-
151-
)
152-
153-
embedding = openai.Embedding.create(
154-
input="<input>",
155-
deployment_id="text-embedding-ada-002" # This must match the custom deployment name you chose for your model.
156-
#engine="text-embedding-ada-002"
157-
)
158-
```
159-
160-
</td>
161-
</tr>
162-
</table>
163-
164-
## Azure OpenAI embeddings multiple input support
165-
166-
OpenAI currently allows a larger number of array inputs with text-embedding-ada-002. Azure OpenAI currently supports input arrays up to 16 for text-embedding-ada-002 Version 2. Both require the max input token limit per API request to remain under 8191 for this model.
167-
168-
<table>
169-
<tr>
170-
<td> OpenAI </td> <td> Azure OpenAI </td>
171-
</tr>
172-
<tr>
173-
<td>
174-
175-
```python
176-
inputs = ["A", "B", "C"]
177-
178-
embedding = openai.Embedding.create(
179-
input=inputs,
180-
model="text-embedding-ada-002"
181-
)
182-
183-
184-
```
185-
186-
</td>
187-
<td>
188-
189-
```python
190-
inputs = ["A", "B", "C"] #max array size=16
191-
192-
embedding = openai.Embedding.create(
193-
input=inputs,
194-
deployment_id="text-embedding-ada-002" # This must match the custom deployment name you chose for your model.
195-
#engine="text-embedding-ada-002"
196-
)
197-
```
198-
199-
</td>
200-
</tr>
201-
</table>
202-
203-
# [OpenAI Python 1.x](#tab/python-new)
18+
This article onlys 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).
20419

20520
## Authentication
20621

@@ -223,6 +38,7 @@ client = OpenAI(
22338
)
22439

22540

41+
22642
```
22743

22844
</td>
@@ -266,6 +82,8 @@ client = OpenAI(
26682

26783

26884

85+
86+
26987
```
27088

27189
</td>
@@ -310,7 +128,7 @@ completion = client.completions.create(
310128
prompt="<prompt>)
311129
)
312130

313-
chat_completion = openai.chat.completions.create(
131+
chat_completion = client.chat.completions.create(
314132
model="gpt-4",
315133
messages="<messages>"
316134
)
@@ -325,17 +143,17 @@ embedding = client.embeddings.create(
325143
<td>
326144

327145
```python
328-
client.completions.create(
146+
completion = client.completions.create(
329147
model=gpt-35-turbo-instruct, # This must match the custom deployment name you chose for your model.
330148
prompt=<"prompt">
331149
)
332150

333-
client.chat.completions.create(
151+
chat_completion = client.chat.completions.create(
334152
model="gpt-35-turbo", # model = "deployment_name".
335153
messages=<"messages">
336154
)
337155

338-
client.embeddings.create(
156+
embedding = client.embeddings.create(
339157
input = "<input>",
340158
model= "text-embedding-ada-002" # model = "deployment_name".
341159
)
@@ -364,7 +182,6 @@ embedding = client.embeddings.create(
364182
model="text-embedding-ada-002"
365183
)
366184

367-
368185
```
369186

370187
</td>
@@ -378,15 +195,14 @@ embedding = client.embeddings.create(
378195
model="text-embedding-ada-002" # This must match the custom deployment name you chose for your model.
379196
#engine="text-embedding-ada-002"
380197
)
198+
381199
```
382200

383201
</td>
384202
</tr>
385203
</table>
386204

387-
---
388-
389205
## Next steps
390206

391207
* Learn more about how to work with GPT-35-Turbo and the GPT-4 models with [our how-to guide](../how-to/chatgpt.md).
392-
* For more examples, check out the [Azure OpenAI Samples GitHub repository](https://aka.ms/AOAICodeSamples)
208+
* For more examples, check out the [Azure OpenAI Samples GitHub repository](https://aka.ms/AOAICodeSamples)

0 commit comments

Comments
 (0)