Skip to content

Commit 3bcf707

Browse files
Merge pull request Azure-Samples#104 from hennachng/main
Sample Migration to the v1.X version of Python OpenAI
2 parents 615ef2a + 7a8c0cf commit 3bcf707

File tree

1 file changed

+164
-159
lines changed

1 file changed

+164
-159
lines changed
Lines changed: 164 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,164 @@
1-
{
2-
"cells": [
3-
{
4-
"cell_type": "markdown",
5-
"id": "011db2ec",
6-
"metadata": {},
7-
"source": [
8-
"<h1 align =\"center\"> Python SDK Samples</h1>\n",
9-
"<hr>\n",
10-
"\n",
11-
"# Create a Completion\n",
12-
"\n",
13-
"Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position."
14-
]
15-
},
16-
{
17-
"cell_type": "code",
18-
"execution_count": 1,
19-
"id": "a92744f4",
20-
"metadata": {},
21-
"outputs": [],
22-
"source": [
23-
"import json\n",
24-
"import openai\n",
25-
"import os"
26-
]
27-
},
28-
{
29-
"cell_type": "markdown",
30-
"id": "e1966c51",
31-
"metadata": {},
32-
"source": [
33-
"### Setup Parameters\n",
34-
"\n",
35-
"\n",
36-
"Here we will load the configurations from _config.json_ file to setup deployment name, openai api base, openai api key and openai api version."
37-
]
38-
},
39-
{
40-
"cell_type": "code",
41-
"execution_count": 2,
42-
"id": "19ae1e36",
43-
"metadata": {},
44-
"outputs": [],
45-
"source": [
46-
"# Load config values\n",
47-
"with open(r'config.json') as config_file:\n",
48-
" config_details = json.load(config_file)\n",
49-
"\n",
50-
"# Setting up the deployment name\n",
51-
"deployment_name = config_details['COMPLETIONS_MODEL']\n",
52-
"\n",
53-
"# This is set to `azure`\n",
54-
"openai.api_type = \"azure\"\n",
55-
"\n",
56-
"# The API key for your Azure OpenAI resource.\n",
57-
"openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
58-
"\n",
59-
"# The base URL for your Azure OpenAI resource. e.g. \"https://<your resource name>.openai.azure.com\"\n",
60-
"openai.api_base = config_details['OPENAI_API_BASE']\n",
61-
"\n",
62-
"# Currently OPENAI API have the following versions available: 2022-12-01\n",
63-
"openai.api_version = config_details['OPENAI_API_VERSION']"
64-
]
65-
},
66-
{
67-
"cell_type": "code",
68-
"execution_count": 3,
69-
"id": "b15862a1",
70-
"metadata": {},
71-
"outputs": [
72-
{
73-
"name": "stdout",
74-
"output_type": "stream",
75-
"text": [
76-
"Hello! Welcome to the world!\n"
77-
]
78-
}
79-
],
80-
"source": [
81-
"# Give your prompt here\n",
82-
"prompt = \"Hello world\"\n",
83-
"\n",
84-
"try:\n",
85-
" # Create a completion for the provided prompt and parameters\n",
86-
" # To know more about the parameters, checkout this documentation: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference\n",
87-
" completion = openai.Completion.create(\n",
88-
" prompt=prompt,\n",
89-
" temperature=0,\n",
90-
" max_tokens=30,\n",
91-
" engine=deployment_name)\n",
92-
"\n",
93-
" # print the completion\n",
94-
" print(completion.choices[0].text.strip(\" \\n\"))\n",
95-
" \n",
96-
" # Here indicating if the response is filtered\n",
97-
" if completion.choices[0].finish_reason == \"content_filter\":\n",
98-
" print(\"The generated content is filtered.\")\n",
99-
" \n",
100-
"except openai.error.APIError as e:\n",
101-
" # Handle API error here, e.g. retry or log\n",
102-
" print(f\"OpenAI API returned an API Error: {e}\")\n",
103-
"\n",
104-
"except openai.error.AuthenticationError as e:\n",
105-
" # Handle Authentication error here, e.g. invalid API key\n",
106-
" print(f\"OpenAI API returned an Authentication Error: {e}\")\n",
107-
"\n",
108-
"except openai.error.APIConnectionError as e:\n",
109-
" # Handle connection error here\n",
110-
" print(f\"Failed to connect to OpenAI API: {e}\")\n",
111-
"\n",
112-
"except openai.error.InvalidRequestError as e:\n",
113-
" # Handle connection error here\n",
114-
" print(f\"Invalid Request Error: {e}\")\n",
115-
"\n",
116-
"except openai.error.RateLimitError as e:\n",
117-
" # Handle rate limit error\n",
118-
" print(f\"OpenAI API request exceeded rate limit: {e}\")\n",
119-
"\n",
120-
"except openai.error.ServiceUnavailableError as e:\n",
121-
" # Handle Service Unavailable error\n",
122-
" print(f\"Service Unavailable: {e}\")\n",
123-
"\n",
124-
"except openai.error.Timeout as e:\n",
125-
" # Handle request timeout\n",
126-
" print(f\"Request timed out: {e}\")"
127-
]
128-
},
129-
{
130-
"cell_type": "code",
131-
"execution_count": null,
132-
"id": "b9be87b2",
133-
"metadata": {},
134-
"outputs": [],
135-
"source": []
136-
}
137-
],
138-
"metadata": {
139-
"kernelspec": {
140-
"display_name": "Python 3 (ipykernel)",
141-
"language": "python",
142-
"name": "python3"
143-
},
144-
"language_info": {
145-
"codemirror_mode": {
146-
"name": "ipython",
147-
"version": 3
148-
},
149-
"file_extension": ".py",
150-
"mimetype": "text/x-python",
151-
"name": "python",
152-
"nbconvert_exporter": "python",
153-
"pygments_lexer": "ipython3",
154-
"version": "3.11.1"
155-
}
156-
},
157-
"nbformat": 4,
158-
"nbformat_minor": 5
159-
}
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "011db2ec",
6+
"metadata": {},
7+
"source": [
8+
"<h1 align =\"center\"> Python SDK Samples</h1>\n",
9+
"<hr>\n",
10+
"\n",
11+
"# Create a Completion\n",
12+
"\n",
13+
"Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position."
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"id": "9d9a71de",
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"%pip install --upgrade openai python-dotenv"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 11,
29+
"id": "a92744f4",
30+
"metadata": {},
31+
"outputs": [
32+
{
33+
"data": {
34+
"text/plain": [
35+
"True"
36+
]
37+
},
38+
"execution_count": 11,
39+
"metadata": {},
40+
"output_type": "execute_result"
41+
}
42+
],
43+
"source": [
44+
"import json\n",
45+
"from openai import AzureOpenAI\n",
46+
"import os\n",
47+
"import dotenv\n",
48+
"dotenv.load_dotenv()\n"
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"id": "e1966c51",
54+
"metadata": {},
55+
"source": [
56+
"### Setup Parameters\n",
57+
"\n",
58+
"\n",
59+
"Here we will read the environment variables from dotenv file to setup deployment name, openai api base, openai api key and openai api version."
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": 12,
65+
"id": "19ae1e36",
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"# Setting up the deployment name\n",
70+
"deployment_name = os.environ['COMPLETIONS_MODEL']\n",
71+
"\n",
72+
"# The API key for your Azure OpenAI resource.\n",
73+
"api_key = os.environ[\"AZURE_OPENAI_API_KEY\"]\n",
74+
"\n",
75+
"# The base URL for your Azure OpenAI resource. e.g. \"https://<your resource name>.openai.azure.com\"\n",
76+
"azure_endpoint = os.environ['AZURE_OPENAI_ENDPOINT']\n",
77+
"\n",
78+
"# Currently OPENAI API have the following versions available: 2022-12-01\n",
79+
"api_version = os.environ['OPENAI_API_VERSION']\n",
80+
"\n",
81+
"client = AzureOpenAI(\n",
82+
" api_key=api_key, \n",
83+
" azure_endpoint=azure_endpoint,\n",
84+
" api_version=api_version\n",
85+
")"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"id": "b15862a1",
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"# Give your prompt here\n",
96+
"prompt = \"Hello world\"\n",
97+
"\n",
98+
" # Create a completion for the provided prompt and parameters\n",
99+
"try:\n",
100+
" completion = client.completions.create( \n",
101+
" model=deployment_name,\n",
102+
" prompt=prompt,\n",
103+
" max_tokens=20,\n",
104+
" )\n",
105+
"\n",
106+
" # Print the completion\n",
107+
" print(completion.choices[0].text.strip(\" \\n\"))\n",
108+
" \n",
109+
" # Here indicating if the response is filtered\n",
110+
" if completion.choices[0].finish_reason == \"content_filter\":\n",
111+
" print(\"The generated content is filtered.\")\n",
112+
"\n",
113+
"except openai.AuthenticationError as e:\n",
114+
" # Handle Authentication error here, e.g. invalid API key\n",
115+
" print(f\"OpenAI API returned an Authentication Error: {e}\")\n",
116+
"\n",
117+
"except openai.APIConnectionError as e:\n",
118+
" # Handle connection error here\n",
119+
" print(f\"Failed to connect to OpenAI API: {e}\")\n",
120+
"\n",
121+
"except openai.BadRequestError as e:\n",
122+
" # Handle connection error here\n",
123+
" print(f\"Invalid Request Error: {e}\")\n",
124+
"\n",
125+
"except openai.RateLimitError as e:\n",
126+
" # Handle rate limit error\n",
127+
" print(f\"OpenAI API request exceeded rate limit: {e}\")\n",
128+
"\n",
129+
"except openai.InternalServerError as e:\n",
130+
" # Handle Service Unavailable error\n",
131+
" print(f\"Service Unavailable: {e}\")\n",
132+
"\n",
133+
"except openai.APITimeoutError as e:\n",
134+
" # Handle request timeout\n",
135+
" print(f\"Request timed out: {e}\")\n",
136+
"\n",
137+
"except openai.APIError as e:\n",
138+
" # Handle API error here, e.g. retry or log\n",
139+
" print(f\"OpenAI API returned an API Error: {e}\")"
140+
]
141+
}
142+
],
143+
"metadata": {
144+
"kernelspec": {
145+
"display_name": "Python 3 (ipykernel)",
146+
"language": "python",
147+
"name": "python3"
148+
},
149+
"language_info": {
150+
"codemirror_mode": {
151+
"name": "ipython",
152+
"version": 3
153+
},
154+
"file_extension": ".py",
155+
"mimetype": "text/x-python",
156+
"name": "python",
157+
"nbconvert_exporter": "python",
158+
"pygments_lexer": "ipython3",
159+
"version": "3.12.1"
160+
}
161+
},
162+
"nbformat": 4,
163+
"nbformat_minor": 5
164+
}

0 commit comments

Comments
 (0)