Skip to content

Commit 355a908

Browse files
Merge pull request Azure-Samples#114 from hennachng/main
Migration to the v1.X version of Python OpenAI
2 parents a911fb5 + 455cbfb commit 355a908

File tree

1 file changed

+165
-170
lines changed

1 file changed

+165
-170
lines changed
Lines changed: 165 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1,170 +1,165 @@
1-
{
2-
"cells": [
3-
{
4-
"cell_type": "markdown",
5-
"id": "278e7451",
6-
"metadata": {},
7-
"source": [
8-
"<h1 align =\"center\"> Python SDK Sample</h1>\n",
9-
"<hr>\n",
10-
"\n",
11-
"# Chat Completions\n",
12-
"\n",
13-
"Chat models take a series of messages as input, and return a model-generated message as output.\n",
14-
"The main input is the messages parameter. Messages must be an array of message objects, where each object has a role (either \"system\", \"user\", or \"assistant\") and content (the content of the message). "
15-
]
16-
},
17-
{
18-
"cell_type": "code",
19-
"execution_count": null,
20-
"id": "fb97123e",
21-
"metadata": {},
22-
"outputs": [],
23-
"source": [
24-
"# if needed, install and/or upgrade to the latest version of the OpenAI Python library\n",
25-
"%pip install --upgrade openai"
26-
]
27-
},
28-
{
29-
"cell_type": "code",
30-
"execution_count": 1,
31-
"id": "ccbb9a99",
32-
"metadata": {},
33-
"outputs": [],
34-
"source": [
35-
"# import os module & the OpenAI Python library for calling the OpenAI API\n",
36-
"import os\n",
37-
"import openai\n",
38-
"import json"
39-
]
40-
},
41-
{
42-
"cell_type": "markdown",
43-
"id": "6d33f92a",
44-
"metadata": {},
45-
"source": [
46-
"### Setup Parameters"
47-
]
48-
},
49-
{
50-
"cell_type": "code",
51-
"execution_count": 2,
52-
"id": "1d67d3b6",
53-
"metadata": {},
54-
"outputs": [],
55-
"source": [
56-
"# Load config values\n",
57-
"with open(r'config.json') as config_file:\n",
58-
" config_details = json.load(config_file)\n",
59-
" \n",
60-
"# Setting up the deployment name\n",
61-
"chatgpt_model_name = config_details['CHATGPT_MODEL']\n",
62-
"\n",
63-
"# This is set to `azure`\n",
64-
"openai.api_type = \"azure\"\n",
65-
"\n",
66-
"# The API key for your Azure OpenAI resource.\n",
67-
"openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
68-
"\n",
69-
"# The base URL for your Azure OpenAI resource. e.g. \"https://<your resource name>.openai.azure.com\"\n",
70-
"openai.api_base = config_details['OPENAI_API_BASE']\n",
71-
"\n",
72-
"# Currently Chat Completion API have the following versions available: 2023-03-15-preview\n",
73-
"openai.api_version = config_details['OPENAI_API_VERSION']"
74-
]
75-
},
76-
{
77-
"cell_type": "code",
78-
"execution_count": 3,
79-
"id": "150a3db0",
80-
"metadata": {},
81-
"outputs": [
82-
{
83-
"name": "stdout",
84-
"output_type": "stream",
85-
"text": [
86-
"The Los Angeles Dodgers won the World Series in 2020.\n"
87-
]
88-
}
89-
],
90-
"source": [
91-
"# A sample API call for chat completions looks as follows:\n",
92-
"# Messages must be an array of message objects, where each object has a role (either \"system\", \"user\", or \"assistant\") and content (the content of the message).\n",
93-
"# For more info: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#chat-completions\n",
94-
"\n",
95-
"try:\n",
96-
" response = openai.ChatCompletion.create(\n",
97-
" engine=chatgpt_model_name,\n",
98-
" messages=[\n",
99-
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
100-
" {\"role\": \"user\", \"content\": \"Who won the world series in 2020?\"}\n",
101-
" ]\n",
102-
" )\n",
103-
"\n",
104-
" # print the response\n",
105-
" print(response['choices'][0]['message']['content'])\n",
106-
" \n",
107-
"except openai.error.APIError as e:\n",
108-
" # Handle API error here, e.g. retry or log\n",
109-
" print(f\"OpenAI API returned an API Error: {e}\")\n",
110-
"\n",
111-
"except openai.error.AuthenticationError as e:\n",
112-
" # Handle Authentication error here, e.g. invalid API key\n",
113-
" print(f\"OpenAI API returned an Authentication Error: {e}\")\n",
114-
"\n",
115-
"except openai.error.APIConnectionError as e:\n",
116-
" # Handle connection error here\n",
117-
" print(f\"Failed to connect to OpenAI API: {e}\")\n",
118-
"\n",
119-
"except openai.error.InvalidRequestError as e:\n",
120-
" # Handle connection error here\n",
121-
" print(f\"Invalid Request Error: {e}\")\n",
122-
"\n",
123-
"except openai.error.RateLimitError as e:\n",
124-
" # Handle rate limit error\n",
125-
" print(f\"OpenAI API request exceeded rate limit: {e}\")\n",
126-
"\n",
127-
"except openai.error.ServiceUnavailableError as e:\n",
128-
" # Handle Service Unavailable error\n",
129-
" print(f\"Service Unavailable: {e}\")\n",
130-
"\n",
131-
"except openai.error.Timeout as e:\n",
132-
" # Handle request timeout\n",
133-
" print(f\"Request timed out: {e}\")\n",
134-
" \n",
135-
"except:\n",
136-
" # Handles all other exceptions\n",
137-
" print(\"An exception has occured.\")"
138-
]
139-
},
140-
{
141-
"cell_type": "code",
142-
"execution_count": null,
143-
"id": "cc92fe64",
144-
"metadata": {},
145-
"outputs": [],
146-
"source": []
147-
}
148-
],
149-
"metadata": {
150-
"kernelspec": {
151-
"display_name": "Python 3 (ipykernel)",
152-
"language": "python",
153-
"name": "python3"
154-
},
155-
"language_info": {
156-
"codemirror_mode": {
157-
"name": "ipython",
158-
"version": 3
159-
},
160-
"file_extension": ".py",
161-
"mimetype": "text/x-python",
162-
"name": "python",
163-
"nbconvert_exporter": "python",
164-
"pygments_lexer": "ipython3",
165-
"version": "3.11.1"
166-
}
167-
},
168-
"nbformat": 4,
169-
"nbformat_minor": 5
170-
}
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "278e7451",
6+
"metadata": {},
7+
"source": [
8+
"<h1 align =\"center\"> Python SDK Sample</h1>\n",
9+
"<hr>\n",
10+
"\n",
11+
"# Chat Completions\n",
12+
"\n",
13+
"Chat models take a series of messages as input, and return a model-generated message as output.\n",
14+
"The main input is the messages parameter. Messages must be an array of message objects, where each object has a role (either \"system\", \"user\", or \"assistant\") and content (the content of the message). "
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": null,
20+
"id": "fb97123e",
21+
"metadata": {},
22+
"outputs": [],
23+
"source": [
24+
"# if needed, install and/or upgrade to the latest version of the OpenAI Python library\n",
25+
"%pip install --upgrade openai"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 9,
31+
"id": "ccbb9a99",
32+
"metadata": {},
33+
"outputs": [
34+
{
35+
"data": {
36+
"text/plain": [
37+
"True"
38+
]
39+
},
40+
"execution_count": 9,
41+
"metadata": {},
42+
"output_type": "execute_result"
43+
}
44+
],
45+
"source": [
46+
"# import os module & the OpenAI Python library for calling the OpenAI API\n",
47+
"import os\n",
48+
"from openai import AzureOpenAI\n",
49+
"import dotenv\n",
50+
"dotenv.load_dotenv()\n"
51+
]
52+
},
53+
{
54+
"cell_type": "markdown",
55+
"id": "6d33f92a",
56+
"metadata": {},
57+
"source": [
58+
"### Setup Parameters"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 10,
64+
"id": "1d67d3b6",
65+
"metadata": {},
66+
"outputs": [],
67+
"source": [
68+
"# Setting up the deployment name\n",
69+
"deployment_name = os.environ['COMPLETIONS_MODEL']\n",
70+
"\n",
71+
"# The API key for your Azure OpenAI resource.\n",
72+
"api_key = os.environ[\"AZURE_OPENAI_API_KEY\"]\n",
73+
"\n",
74+
"# The base URL for your Azure OpenAI resource. e.g. \"https://<your resource name>.openai.azure.com\"\n",
75+
"azure_endpoint = os.environ['AZURE_OPENAI_ENDPOINT']\n",
76+
"\n",
77+
"# Currently Chat Completion API have the following versions available: 2023-03-15-preview\n",
78+
"api_version = os.environ['OPENAI_API_VERSION']\n",
79+
"\n",
80+
"client = AzureOpenAI(\n",
81+
" api_key=api_key, \n",
82+
" azure_endpoint=azure_endpoint,\n",
83+
" api_version=api_version\n",
84+
")"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": null,
90+
"id": "150a3db0",
91+
"metadata": {},
92+
"outputs": [],
93+
"source": [
94+
"# A sample API call for chat completions looks as follows:\n",
95+
"# Messages must be an array of message objects, where each object has a role (either \"system\", \"user\", or \"assistant\") and content (the content of the message).\n",
96+
"# For more info: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#chat-completions\n",
97+
"\n",
98+
"try:\n",
99+
" response = client.chat.completions.create(\n",
100+
" model=deployment_name,\n",
101+
" messages=[\n",
102+
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
103+
" {\"role\": \"user\", \"content\": \"Who won the world series in 2020?\"}\n",
104+
" ]\n",
105+
" )\n",
106+
"\n",
107+
" # print the response\n",
108+
" print(response.choices[0].message.content)\n",
109+
"\n",
110+
"except openai.AuthenticationError as e:\n",
111+
" # Handle Authentication error here, e.g. invalid API key\n",
112+
" print(f\"OpenAI API returned an Authentication Error: {e}\")\n",
113+
"\n",
114+
"except openai.APIConnectionError as e:\n",
115+
" # Handle connection error here\n",
116+
" print(f\"Failed to connect to OpenAI API: {e}\")\n",
117+
"\n",
118+
"except openai.BadRequestError as e:\n",
119+
" # Handle connection error here\n",
120+
" print(f\"Invalid Request Error: {e}\")\n",
121+
"\n",
122+
"except openai.RateLimitError as e:\n",
123+
" # Handle rate limit error\n",
124+
" print(f\"OpenAI API request exceeded rate limit: {e}\")\n",
125+
"\n",
126+
"except openai.InternalServerError as e:\n",
127+
" # Handle Service Unavailable error\n",
128+
" print(f\"Service Unavailable: {e}\")\n",
129+
"\n",
130+
"except openai.APITimeoutError as e:\n",
131+
" # Handle request timeout\n",
132+
" print(f\"Request timed out: {e}\")\n",
133+
" \n",
134+
"except openai.APIError as e:\n",
135+
" # Handle API error here, e.g. retry or log\n",
136+
" print(f\"OpenAI API returned an API Error: {e}\")\n",
137+
"\n",
138+
"except:\n",
139+
" # Handles all other exceptions\n",
140+
" print(\"An exception has occured.\")"
141+
]
142+
}
143+
],
144+
"metadata": {
145+
"kernelspec": {
146+
"display_name": "Python 3 (ipykernel)",
147+
"language": "python",
148+
"name": "python3"
149+
},
150+
"language_info": {
151+
"codemirror_mode": {
152+
"name": "ipython",
153+
"version": 3
154+
},
155+
"file_extension": ".py",
156+
"mimetype": "text/x-python",
157+
"name": "python",
158+
"nbconvert_exporter": "python",
159+
"pygments_lexer": "ipython3",
160+
"version": "3.12.1"
161+
}
162+
},
163+
"nbformat": 4,
164+
"nbformat_minor": 5
165+
}

0 commit comments

Comments
 (0)