Skip to content

Commit d84a414

Browse files
committed
First draft
0 parents  commit d84a414

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

.env-template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
AZURE_OPENAI_ENDPOINT=https://[RESOURCE].openai.azure.com/
2+
AZURE_OPENAI_KEY=[KEY]
3+
AZURE_OPENAI_DEPLOYMENT_GPT4=gpt-4

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.venv
2+
/.env

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"files.eol": "\n"
3+
}

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Use OpenAI at Azure with Python
2+
3+
## Prerequisites for this Example
4+
5+
- Windows
6+
- One of the latest Python 3.x releases: https://www.python.org/downloads/
7+
- Microsoft C++ Build Tools: https://visualstudio.microsoft.com/visual-cpp-build-tools/
8+
9+
## Run this Example
10+
11+
```bash
12+
# check python version (run once)
13+
python --version
14+
15+
# create new virtual environment (run once)
16+
python -m venv .venv
17+
18+
# activate the virtual environment (run before each session)
19+
.venv\Scripts\activate.ps1 # PowerShell
20+
.venv\Scripts\activate.bat # CMD
21+
22+
# workaround currently necessary for Python 3.12, ignore if you use 3.11 or before (run once)
23+
pip --require-virtualenv install aiohttp==3.9.0b0
24+
25+
# install openai dependency (run once)
26+
pip --require-virtualenv install openai tenacity python-dotenv
27+
28+
# prepare environment variables (run once)
29+
cp .env-template .env
30+
code .env
31+
32+
# run the example script (run whenever you feel like it)
33+
python example.py
34+
35+
# deactivate the virtual environment (run at the end of the session)
36+
deactivate
37+
```

example.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# load the .env file
2+
__import__("dotenv").load_dotenv()
3+
4+
# import required libraries
5+
import openai
6+
from os import environ
7+
from tenacity import (retry, retry_if_exception_type, stop_after_attempt, wait_exponential)
8+
9+
# configure the openai library
10+
openai.api_type = "azure"
11+
openai.api_base = environ["AZURE_OPENAI_ENDPOINT"]
12+
openai.api_key = environ["AZURE_OPENAI_KEY"]
13+
openai.api_version = "2023-05-15"
14+
15+
# openai retry utility to avoid rate limits and network errors
16+
@retry(stop=stop_after_attempt(5),
17+
wait=wait_exponential(multiplier=1, min=2, max=60),
18+
retry=(retry_if_exception_type(openai.error.RateLimitError)
19+
| retry_if_exception_type(openai.error.APIConnectionError)
20+
| retry_if_exception_type(openai.error.Timeout)))
21+
def openai_chat_completion_with_retry(**kwargs):
22+
return openai.ChatCompletion.create(**kwargs)
23+
24+
# example chat completion
25+
result = openai_chat_completion_with_retry(
26+
deployment_id=environ["AZURE_OPENAI_DEPLOYMENT_GPT4"],
27+
model="gpt-4",
28+
messages=[{
29+
"role": "user",
30+
"content": "Hello world",
31+
}],
32+
temperature=0,
33+
)
34+
35+
print(result.choices[0].message.content)

0 commit comments

Comments
 (0)