Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add azure_ad code examples
  • Loading branch information
t-asutedjo committed May 25, 2022
commit e457edc55a6fac37f9c4c0fa51ce0b88862b34f2
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ openai.api_base = "https://example-endpoint.openai.azure.com"
openai.api_version = "2021-11-01-preview"

# create a completion
completion = openai.Completion.create(engine="deployment-namme", prompt="Hello world")
completion = openai.Completion.create(engine="deployment-name", prompt="Hello world")

# print the completion
print(completion.choices[0].text)

# create a search and pass the deployment-name as the engine Id.
search = openai.Engine(id="deployment-namme").search(documents=["White House", "hospital", "school"], query ="the president")
search = openai.Engine(id="deployment-name").search(documents=["White House", "hospital", "school"], query ="the president")

# print the search
print(search)
Expand All @@ -81,6 +81,27 @@ Please note that for the moment, the Microsoft Azure endpoints can only be used
For a detailed example on how to use fine-tuning and other operations using Azure endpoints, please check out the following Jupyter notebook:
[Using Azure fine-tuning](https://github.com/openai/openai-python/blob/main/examples/azure/finetuning.ipynb)

### Microsoft Azure Active Directory Authentication

In order to use Microsoft Active Directory to authenticate to your Azure endpoint, you need to set the api_type to "azure_ad" and pass the acquired credential token to api_key. The rest of the parameters need to be set as specified in the previous section.


```python
from azure.identity import DefaultAzureCredential
import openai

# Request credential
default_credential = DefaultAzureCredential()
token = default_credential.get_token("https://cognitiveservices.azure.com")

# Setup parameters
openai.api_type = "azure_ad"
openai.api_key = token.token
openai.api_base = "https://example-endpoint.openai.azure.com/"
openai.api_version = "2022-03-01-preview"

# ...
```
### Command-line interface

This library additionally provides an `openai` command-line utility
Expand Down
33 changes: 31 additions & 2 deletions examples/azure/finetuning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,35 @@
"openai.api_version = '2022-03-01-preview' # this may change in the future"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Microsoft Active Directory Authentication\n",
"Instead of key based authentication, you can use Active Directory to authenticate using credential tokens. Uncomment the next code section to use credential based authentication:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"from azure.identity import DefaultAzureCredential\n",
"\n",
"default_credential = DefaultAzureCredential()\n",
"token = default_credential.get_token(\"https://cognitiveservices.azure.com\")\n",
"\n",
"openai.api_type = 'azure_ad'\n",
"openai.api_key = token.token\n",
"openai.api_version = '2022-03-01-preview' # this may change in the future\n",
"\n",
"\n",
"openai.api_base = '' # Please add your endpoint here\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -418,10 +447,10 @@
],
"metadata": {
"interpreter": {
"hash": "1efaa68c6557ae864f04a55d1c611eb06843d0ca160c97bf33f135c19475264d"
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
},
"kernelspec": {
"display_name": "Python 3.8.10 ('openai-env')",
"display_name": "Python 3.8.10 64-bit",
"language": "python",
"name": "python3"
},
Expand Down