Skip to content

Commit cbfd1af

Browse files
committed
Restored original basic_completions_example_sdk.ipynb
1 parent 00e82b3 commit cbfd1af

File tree

4 files changed

+164
-155
lines changed

4 files changed

+164
-155
lines changed

.DS_Store

6 KB
Binary file not shown.

Basic_Samples/.DS_Store

6 KB
Binary file not shown.
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: 159 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,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": 1,
19-
"id": "a92744f4",
20-
"metadata": {},
21-
"outputs": [
22-
{
23-
"data": {
24-
"text/plain": [
25-
"True"
26-
]
27-
},
28-
"execution_count": 1,
29-
"metadata": {},
30-
"output_type": "execute_result"
31-
}
32-
],
33-
"source": [
34-
"import openai\n",
35-
"import os\n",
36-
"import dotenv\n",
37-
"\n",
38-
"dotenv.load_dotenv()"
39-
]
40-
},
41-
{
42-
"cell_type": "markdown",
43-
"id": "e1966c51",
44-
"metadata": {},
45-
"source": [
46-
"### Setup Parameters\n",
47-
"\n",
48-
"\n",
49-
"Here we will load the configurations from _config.json_ 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": 2,
55-
"id": "19ae1e36",
56-
"metadata": {},
57-
"outputs": [],
58-
"source": [
59-
"# Setting up the client\n",
60-
"client = openai.AzureOpenAI(\n",
61-
" api_key=os.environ['AZURE_OPENAI_API_KEY'],\n",
62-
" api_version=os.environ['OPENAI_API_VERSION'],\n",
63-
" azure_endpoint=os.environ['AZURE_OPENAI_ENDPOINT']\n",
64-
")\n",
65-
"\n",
66-
"# Setting up the deployment name\n",
67-
"deployment_name = os.environ['COMPLETIONS_MODEL']"
68-
]
69-
},
70-
{
71-
"cell_type": "code",
72-
"execution_count": 3,
73-
"id": "b15862a1",
74-
"metadata": {},
75-
"outputs": [
76-
{
77-
"name": "stdout",
78-
"output_type": "stream",
79-
"text": [
80-
"Hello! Welcome to the world!\n"
81-
]
82-
}
83-
],
84-
"source": [
85-
"# Give your prompt here\n",
86-
"prompt = \"Hello world\"\n",
87-
"\n",
88-
"try:\n",
89-
" # Create a completion for the provided prompt and parameters\n",
90-
" # To know more about the parameters, checkout this documentation: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference\n",
91-
" completion = client.completions.create(\n",
92-
" prompt=prompt,\n",
93-
" temperature=0,\n",
94-
" max_tokens=30,\n",
95-
" model=deployment_name)\n",
96-
"\n",
97-
" # print the completion\n",
98-
" print(completion.choices[0].text.strip(\" \\n\"))\n",
99-
" \n",
100-
" # Here indicating if the response is filtered\n",
101-
" if completion.choices[0].finish_reason == \"content_filter\":\n",
102-
" print(\"The generated content is filtered.\")\n",
103-
"\n",
104-
"except openai.APITimeoutError as e:\n",
105-
" # Handle request timeout\n",
106-
" print(f\"Request timed out: {e}\")\n",
107-
"\n",
108-
"except openai.AuthenticationError as e:\n",
109-
" # Handle Authentication error here, e.g. invalid API key\n",
110-
" print(f\"OpenAI API returned an Authentication Error: {e}\")\n",
111-
"\n",
112-
"except openai.APIConnectionError as e:\n",
113-
" # Handle connection error here\n",
114-
" print(f\"Failed to connect to OpenAI API: {e}\")\n",
115-
"\n",
116-
"except openai.BadRequestError as e:\n",
117-
" # Handle connection error here\n",
118-
" print(f\"Invalid Request Error: {e}\")\n",
119-
"\n",
120-
"except openai.RateLimitError as e:\n",
121-
" # Handle rate limit error\n",
122-
" print(f\"OpenAI API request exceeded rate limit: {e}\")\n",
123-
"\n",
124-
"except openai.InternalServerError as e:\n",
125-
" # Handle Service Unavailable error\n",
126-
" print(f\"Service Unavailable: {e}\")\n",
127-
"\n",
128-
"except openai.APIError as e:\n",
129-
" # Handle API error here, e.g. retry or log\n",
130-
" print(f\"OpenAI API returned an API Error: {e}\")\n"
131-
]
132-
}
133-
],
134-
"metadata": {
135-
"kernelspec": {
136-
"display_name": "Python 3 (ipykernel)",
137-
"language": "python",
138-
"name": "python3"
139-
},
140-
"language_info": {
141-
"codemirror_mode": {
142-
"name": "ipython",
143-
"version": 3
144-
},
145-
"file_extension": ".py",
146-
"mimetype": "text/x-python",
147-
"name": "python",
148-
"nbconvert_exporter": "python",
149-
"pygments_lexer": "ipython3",
150-
"version": "3.11.5"
151-
}
152-
},
153-
"nbformat": 4,
154-
"nbformat_minor": 5
155-
}
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+
}

0 commit comments

Comments
 (0)