|
| 1 | +--- |
| 2 | +title: How to switch between OpenAI and Azure OpenAI Service endpoints with Python |
| 3 | +titleSuffix: Azure OpenAI Service |
| 4 | +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 |
| 7 | +ms.service: cognitive-services |
| 8 | +ms.custom: |
| 9 | +ms.topic: how-to |
| 10 | +ms.date: 05/24/2023 |
| 11 | +manager: nitinme |
| 12 | +--- |
| 13 | + |
| 14 | +# How to switch between OpenAI and Azure OpenAI endpoints with Python |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 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. |
| 20 | +
|
| 21 | +## Authentication |
| 22 | + |
| 23 | +We recommend using environment variables. If you haven't done this before our [Python quickstarts](../quickstart.md) walk you through this configuration. |
| 24 | + |
| 25 | +### API key |
| 26 | + |
| 27 | +<table> |
| 28 | +<tr> |
| 29 | +<td> OpenAI </td> <td> Azure OpenAI </td> |
| 30 | +</tr> |
| 31 | +<tr> |
| 32 | +<td> |
| 33 | + |
| 34 | +```python |
| 35 | +import openai |
| 36 | + |
| 37 | +openai.api_key = "sk-..." |
| 38 | +openai.organization = "..." |
| 39 | + |
| 40 | + |
| 41 | +``` |
| 42 | + |
| 43 | +</td> |
| 44 | +<td> |
| 45 | + |
| 46 | +```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 |
| 53 | +``` |
| 54 | + |
| 55 | +</td> |
| 56 | +</tr> |
| 57 | +</table> |
| 58 | + |
| 59 | +### Azure Active Directory authentication |
| 60 | + |
| 61 | +<table> |
| 62 | +<tr> |
| 63 | +<td> OpenAI </td> <td> Azure OpenAI </td> |
| 64 | +</tr> |
| 65 | +<tr> |
| 66 | +<td> |
| 67 | + |
| 68 | +```python |
| 69 | +import openai |
| 70 | + |
| 71 | +openai.api_key = "sk-..." |
| 72 | +openai.organization = "..." |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +``` |
| 79 | + |
| 80 | +</td> |
| 81 | +<td> |
| 82 | + |
| 83 | +```python |
| 84 | +import openai |
| 85 | +from azure.identity import DefaultAzureCredential |
| 86 | + |
| 87 | +credential = DefaultAzureCredential() |
| 88 | +token = credential.get_token("https://cognitiveservices.azure.com/.default") |
| 89 | + |
| 90 | +openai.api_type = "azuread" |
| 91 | +openai.api_key = token.token |
| 92 | +openai.api_base = "https://example-endpoint.openai.azure.com" |
| 93 | +openai.api_version = "2023-05-15" # subject to change |
| 94 | +``` |
| 95 | + |
| 96 | +</td> |
| 97 | +</tr> |
| 98 | +</table> |
| 99 | + |
| 100 | +## Keyword argument for model |
| 101 | + |
| 102 | +OpenAI uses the `model` keyword argument to specify what model to use. Azure OpenAI has the concept of [deployments](/azure/cognitive-services/openai/how-to/create-resource?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 the `engine` interchangeably with `deployment_id`. |
| 103 | + |
| 104 | +For OpenAI `engine` still works in most instances, but it's deprecated and `model` is preferred. |
| 105 | + |
| 106 | +<table> |
| 107 | +<tr> |
| 108 | +<td> OpenAI </td> <td> Azure OpenAI </td> |
| 109 | +</tr> |
| 110 | +<tr> |
| 111 | +<td> |
| 112 | + |
| 113 | +```python |
| 114 | +completion = openai.Completion.create( |
| 115 | + prompt="<prompt>", |
| 116 | + model="text-davinci-003" |
| 117 | +) |
| 118 | + |
| 119 | +chat_completion = openai.ChatCompletion.create( |
| 120 | + messages="<messages>", |
| 121 | + model="gpt-4" |
| 122 | +) |
| 123 | + |
| 124 | +embedding = openai.Embedding.create( |
| 125 | + input="<input>", |
| 126 | + model="text-embedding-ada-002" |
| 127 | +) |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +``` |
| 133 | + |
| 134 | +</td> |
| 135 | +<td> |
| 136 | + |
| 137 | +```python |
| 138 | +completion = openai.Completion.create( |
| 139 | + prompt="<prompt>", |
| 140 | + deployment_id="text-davinci-003" |
| 141 | + #engine="text-davinci-003" |
| 142 | +) |
| 143 | + |
| 144 | +chat_completion = openai.ChatCompletion.create( |
| 145 | + messages="<messages>", |
| 146 | + deployment_id="gpt-4" |
| 147 | + #engine="gpt-4" |
| 148 | + |
| 149 | +) |
| 150 | + |
| 151 | +embedding = openai.Embedding.create( |
| 152 | + input="<input>", |
| 153 | + deployment_id="text-embedding-ada-002" |
| 154 | + #engine="text-embedding-ada-002" |
| 155 | +) |
| 156 | +``` |
| 157 | + |
| 158 | +</td> |
| 159 | +</tr> |
| 160 | +</table> |
| 161 | + |
| 162 | +## Azure OpenAI embeddings doesn't support multiple inputs |
| 163 | + |
| 164 | +Many examples show passing multiple inputs into the embeddings API. For Azure OpenAI, currently we must pass a single text input per call. |
| 165 | + |
| 166 | +<table> |
| 167 | +<tr> |
| 168 | +<td> OpenAI </td> <td> Azure OpenAI </td> |
| 169 | +</tr> |
| 170 | +<tr> |
| 171 | +<td> |
| 172 | + |
| 173 | +```python |
| 174 | +inputs = ["A", "B", "C"] |
| 175 | + |
| 176 | +embedding = openai.Embedding.create( |
| 177 | + input=inputs, |
| 178 | + model="text-embedding-ada-002" |
| 179 | +) |
| 180 | + |
| 181 | + |
| 182 | +``` |
| 183 | + |
| 184 | +</td> |
| 185 | +<td> |
| 186 | + |
| 187 | +```python |
| 188 | +inputs = ["A", "B", "C"] |
| 189 | + |
| 190 | +for text in inputs: |
| 191 | + embedding = openai.Embedding.create( |
| 192 | + input=text, |
| 193 | + deployment_id="text-embedding-ada-002" |
| 194 | + #engine="text-embedding-ada-002" |
| 195 | + ) |
| 196 | +``` |
| 197 | + |
| 198 | +</td> |
| 199 | +</tr> |
| 200 | +</table> |
| 201 | + |
| 202 | +## Next steps |
| 203 | + |
| 204 | +* Learn more about how to work with ChatGPT and the GPT-4 models with [our how-to guide](../how-to/chatgpt.md). |
| 205 | +* For more examples, check out the [Azure OpenAI Samples GitHub repository](https://aka.ms/AOAICodeSamples) |
0 commit comments