Skip to content

Commit 140eab0

Browse files
Merge pull request Azure-Samples#100 from solbell2/main
Migration to the v1.X version of the Python openai library
2 parents 11c2091 + 2dd28ec commit 140eab0

File tree

4 files changed

+159
-150
lines changed

4 files changed

+159
-150
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore .env files
2+
.env
3+
4+
# Ignore config.json file
5+
config.json
Lines changed: 153 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,153 @@
1-
{
2-
"cells": [
3-
{
4-
"cell_type": "markdown",
5-
"id": "759f9ec0",
6-
"metadata": {},
7-
"source": [
8-
"<h1 align =\"center\"> REST API Reference 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": "f4b3d21a",
20-
"metadata": {},
21-
"outputs": [],
22-
"source": [
23-
"import json\n",
24-
"import os\n",
25-
"import openai\n",
26-
"import requests"
27-
]
28-
},
29-
{
30-
"cell_type": "markdown",
31-
"id": "5b2d4a0f",
32-
"metadata": {},
33-
"source": [
34-
"### Setup Parameters\n",
35-
"\n",
36-
"\n",
37-
"Here we will load the configurations from _config.json_ file to setup deployment_name, openai_api_base, openai_api_key and openai_api_version."
38-
]
39-
},
40-
{
41-
"cell_type": "code",
42-
"execution_count": 2,
43-
"id": "fd85fb30",
44-
"metadata": {},
45-
"outputs": [],
46-
"source": [
47-
"# Load config values\n",
48-
"with open(r'config.json') as config_file:\n",
49-
" config_details = json.load(config_file)\n",
50-
" \n",
51-
"# Setting up the deployment name\n",
52-
"deployment_name = config_details['COMPLETIONS_MODEL']\n",
53-
"\n",
54-
"# The base URL for your Azure OpenAI resource. e.g. \"https://<your resource name>.openai.azure.com\"\n",
55-
"openai_api_base = config_details['OPENAI_API_BASE']\n",
56-
"\n",
57-
"# The API key for your Azure OpenAI resource.\n",
58-
"openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
59-
"\n",
60-
"# Currently OPENAI API have the following versions available: 2022-12-01. All versions follow the YYYY-MM-DD date structure.\n",
61-
"openai_api_version = config_details['OPENAI_API_VERSION']"
62-
]
63-
},
64-
{
65-
"cell_type": "code",
66-
"execution_count": 3,
67-
"id": "aef62557",
68-
"metadata": {},
69-
"outputs": [
70-
{
71-
"name": "stdout",
72-
"output_type": "stream",
73-
"text": [
74-
"\n",
75-
"\n",
76-
"Hello! Welcome to the world!\n"
77-
]
78-
}
79-
],
80-
"source": [
81-
"# Request URL\n",
82-
"api_url = f\"{openai_api_base}/openai/deployments/{deployment_name}/completions?api-version={openai_api_version}\"\n",
83-
"\n",
84-
"# Example prompt for request payload\n",
85-
"prompt = \"Hello world\"\n",
86-
"\n",
87-
"# Json payload\n",
88-
"# To know more about the parameters, checkout this documentation: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference\n",
89-
"json_data = {\n",
90-
" \"prompt\": prompt,\n",
91-
" \"temperature\":0,\n",
92-
" \"max_tokens\": 30\n",
93-
"}\n",
94-
"\n",
95-
"# Including the api-key in HTTP headers\n",
96-
"headers = {\"api-key\": openai_api_key}\n",
97-
"\n",
98-
"try:\n",
99-
" # Request for creating a completion for the provided prompt and parameters\n",
100-
" response = requests.post(api_url, json=json_data, headers=headers)\n",
101-
" completion = response.json()\n",
102-
" \n",
103-
" # print the completion\n",
104-
" print(completion['choices'][0]['text'])\n",
105-
" \n",
106-
" # Here indicating if the response is filtered\n",
107-
" if completion['choices'][0]['finish_reason'] == \"content_filter\":\n",
108-
" print(\"The generated content is filtered.\")\n",
109-
"except:\n",
110-
" print(\"An exception has occurred. \\n\")\n",
111-
" print(\"Error Message:\", completion['error']['message'])"
112-
]
113-
},
114-
{
115-
"cell_type": "code",
116-
"execution_count": null,
117-
"id": "b6165c63",
118-
"metadata": {},
119-
"outputs": [],
120-
"source": []
121-
}
122-
],
123-
"metadata": {
124-
"kernelspec": {
125-
"display_name": "Python 3 (ipykernel)",
126-
"language": "python",
127-
"name": "python3"
128-
},
129-
"language_info": {
130-
"codemirror_mode": {
131-
"name": "ipython",
132-
"version": 3
133-
},
134-
"file_extension": ".py",
135-
"mimetype": "text/x-python",
136-
"name": "python",
137-
"nbconvert_exporter": "python",
138-
"pygments_lexer": "ipython3",
139-
"version": "3.11.1"
140-
}
141-
},
142-
"nbformat": 4,
143-
"nbformat_minor": 5
144-
}
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "759f9ec0",
6+
"metadata": {},
7+
"source": [
8+
"<h1 align =\"center\"> REST API Reference 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": 13,
19+
"id": "f4b3d21a",
20+
"metadata": {},
21+
"outputs": [
22+
{
23+
"data": {
24+
"text/plain": [
25+
"True"
26+
]
27+
},
28+
"execution_count": 13,
29+
"metadata": {},
30+
"output_type": "execute_result"
31+
}
32+
],
33+
"source": [
34+
"import os\n",
35+
"import requests\n",
36+
"import dotenv\n",
37+
"\n",
38+
"dotenv.load_dotenv()\n"
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"id": "5b2d4a0f",
44+
"metadata": {},
45+
"source": [
46+
"### Setup Parameters\n",
47+
"\n",
48+
"\n",
49+
"Here we will load the configurations from an .env file to setup deployment_name, openai_api_base, openai_api_key and openai_api_version."
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 14,
55+
"id": "fd85fb30",
56+
"metadata": {},
57+
"outputs": [],
58+
"source": [
59+
"# Setting up the deployment name\n",
60+
"deployment_name = os.environ['COMPLETIONS_MODEL']\n",
61+
"\n",
62+
"# The base URL for your Azure OpenAI resource. e.g. \"https://<your resource name>.openai.azure.com\"\n",
63+
"openai_api_base = os.environ['AZURE_OPENAI_ENDPOINT']\n",
64+
"\n",
65+
"# The API key for your Azure OpenAI resource.\n",
66+
"openai_api_key = os.environ['AZURE_OPENAI_API_KEY']\n",
67+
"\n",
68+
"# Currently OPENAI API have the following versions available: https://learn.microsoft.com/azure/ai-services/openai/reference\n",
69+
"openai_api_version = os.environ['OPENAI_API_VERSION']"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 19,
75+
"id": "aef62557",
76+
"metadata": {},
77+
"outputs": [
78+
{
79+
"name": "stdout",
80+
"output_type": "stream",
81+
"text": [
82+
"\n",
83+
"\n",
84+
"Hello! Welcome to the world!\n"
85+
]
86+
}
87+
],
88+
"source": [
89+
"# Request URL\n",
90+
"api_url = f\"{openai_api_base}/openai/deployments/{deployment_name}/completions?api-version={openai_api_version}\"\n",
91+
"\n",
92+
"# Example prompt for request payload\n",
93+
"prompt = \"Hello world\"\n",
94+
"\n",
95+
"# Json payload\n",
96+
"# To know more about the parameters, checkout this documentation: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference\n",
97+
"json_data = {\n",
98+
" \"prompt\": prompt,\n",
99+
" \"temperature\": 0,\n",
100+
" \"max_tokens\": 30\n",
101+
"}\n",
102+
"\n",
103+
"# Including the api-key in HTTP headers\n",
104+
"headers = {\"api-key\": openai_api_key}\n",
105+
"\n",
106+
"try: \n",
107+
" # Request for creating a completion for the provided prompt and parameters\n",
108+
" response = requests.post(api_url, json=json_data, headers=headers)\n",
109+
"\n",
110+
" completion = response.json()\n",
111+
" \n",
112+
" # print the completion\n",
113+
" print(completion['choices'][0]['text'])\n",
114+
" \n",
115+
" # Here indicating if the response is filtered\n",
116+
" if completion['choices'][0]['finish_reason'] == \"content_filter\":\n",
117+
" print(\"The generated content is filtered.\")\n",
118+
"\n",
119+
"except:\n",
120+
" print(\"An exception has occurred. \\n\")\n"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": null,
126+
"id": "b6165c63",
127+
"metadata": {},
128+
"outputs": [],
129+
"source": []
130+
}
131+
],
132+
"metadata": {
133+
"kernelspec": {
134+
"display_name": "Python 3 (ipykernel)",
135+
"language": "python",
136+
"name": "python3"
137+
},
138+
"language_info": {
139+
"codemirror_mode": {
140+
"name": "ipython",
141+
"version": 3
142+
},
143+
"file_extension": ".py",
144+
"mimetype": "text/x-python",
145+
"name": "python",
146+
"nbconvert_exporter": "python",
147+
"pygments_lexer": "ipython3",
148+
"version": "3.11.5"
149+
}
150+
},
151+
"nbformat": 4,
152+
"nbformat_minor": 5
153+
}

Basic_Samples/Completions/config.json

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
jsonpointer==2.3
22
jsonschema==4.17.3
3-
openai==0.27.2
3+
openai==1.11.1
44
tiktoken==0.3.1

0 commit comments

Comments
 (0)