Skip to content

Commit 672851b

Browse files
committed
Sample Migration
1 parent 11c2091 commit 672851b

File tree

1 file changed

+217
-159
lines changed

1 file changed

+217
-159
lines changed
Lines changed: 217 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,217 @@
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": 1,
19+
"id": "9d9a71de",
20+
"metadata": {},
21+
"outputs": [
22+
{
23+
"name": "stdout",
24+
"output_type": "stream",
25+
"text": [
26+
"Requirement already satisfied: openai in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (1.13.3)\n",
27+
"Requirement already satisfied: python-dotenv in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (1.0.1)\n",
28+
"Requirement already satisfied: anyio<5,>=3.5.0 in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (from openai) (4.3.0)\n",
29+
"Requirement already satisfied: distro<2,>=1.7.0 in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (from openai) (1.9.0)\n",
30+
"Requirement already satisfied: httpx<1,>=0.23.0 in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (from openai) (0.27.0)\n",
31+
"Requirement already satisfied: pydantic<3,>=1.9.0 in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (from openai) (2.6.3)\n",
32+
"Requirement already satisfied: sniffio in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (from openai) (1.3.1)\n",
33+
"Requirement already satisfied: tqdm>4 in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (from openai) (4.66.2)\n",
34+
"Requirement already satisfied: typing-extensions<5,>=4.7 in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (from openai) (4.10.0)\n",
35+
"Requirement already satisfied: idna>=2.8 in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (from anyio<5,>=3.5.0->openai) (3.6)\n",
36+
"Requirement already satisfied: certifi in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (from httpx<1,>=0.23.0->openai) (2024.2.2)\n",
37+
"Requirement already satisfied: httpcore==1.* in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (from httpx<1,>=0.23.0->openai) (1.0.4)\n",
38+
"Requirement already satisfied: h11<0.15,>=0.13 in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.14.0)\n",
39+
"Requirement already satisfied: annotated-types>=0.4.0 in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (from pydantic<3,>=1.9.0->openai) (0.6.0)\n",
40+
"Requirement already satisfied: pydantic-core==2.16.3 in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (from pydantic<3,>=1.9.0->openai) (2.16.3)\n",
41+
"Requirement already satisfied: colorama in c:\\users\\henna\\documents\\github\\openai\\venv312\\lib\\site-packages (from tqdm>4->openai) (0.4.6)\n",
42+
"Note: you may need to restart the kernel to use updated packages.\n"
43+
]
44+
},
45+
{
46+
"name": "stderr",
47+
"output_type": "stream",
48+
"text": [
49+
"\n",
50+
"[notice] A new release of pip is available: 23.2.1 -> 24.0\n",
51+
"[notice] To update, run: python.exe -m pip install --upgrade pip\n"
52+
]
53+
}
54+
],
55+
"source": [
56+
"%pip install --upgrade openai python-dotenv"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 2,
62+
"id": "a92744f4",
63+
"metadata": {},
64+
"outputs": [
65+
{
66+
"data": {
67+
"text/plain": [
68+
"True"
69+
]
70+
},
71+
"execution_count": 2,
72+
"metadata": {},
73+
"output_type": "execute_result"
74+
}
75+
],
76+
"source": [
77+
"import json\n",
78+
"from openai import AzureOpenAI\n",
79+
"import os\n",
80+
"import dotenv\n",
81+
"dotenv.load_dotenv()\n"
82+
]
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"id": "e1966c51",
87+
"metadata": {},
88+
"source": [
89+
"### Setup Parameters\n",
90+
"\n",
91+
"\n",
92+
"Here we will load the configurations from _config.json_ file to setup deployment name, openai api base, openai api key and openai api version."
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": 3,
98+
"id": "19ae1e36",
99+
"metadata": {},
100+
"outputs": [],
101+
"source": [
102+
"# Setting up the deployment name\n",
103+
"deployment_name = os.environ['COMPLETIONS_MODEL']\n",
104+
"\n",
105+
"# The API key for your Azure OpenAI resource.\n",
106+
"api_key = os.environ[\"AZURE_OPENAI_API_KEY\"]\n",
107+
"\n",
108+
"# The base URL for your Azure OpenAI resource. e.g. \"https://<your resource name>.openai.azure.com\"\n",
109+
"azure_endpoint = os.environ['AZURE_OPENAI_ENDPOINT']\n",
110+
"\n",
111+
"# Currently OPENAI API have the following versions available: 2022-12-01\n",
112+
"api_version = os.environ['OPENAI_API_VERSION']\n",
113+
"\n",
114+
"client = AzureOpenAI(\n",
115+
" api_key = api_key, \n",
116+
" azure_endpoint = azure_endpoint,\n",
117+
" api_version = api_version\n",
118+
")"
119+
]
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": 4,
124+
"id": "b15862a1",
125+
"metadata": {},
126+
"outputs": [
127+
{
128+
"name": "stdout",
129+
"output_type": "stream",
130+
"text": [
131+
"!\"\n",
132+
"\n",
133+
"# def outer_func(original_string):\n",
134+
"# def inner_func(string):\n",
135+
"# nonlocal original\n"
136+
]
137+
}
138+
],
139+
"source": [
140+
"# Give your prompt here\n",
141+
"prompt = \"Hello world\"\n",
142+
"\n",
143+
" # Create a completion for the provided prompt and parameters\n",
144+
"try:\n",
145+
" completion = client.completions.create( \n",
146+
" model=deployment_name,\n",
147+
" prompt=prompt,\n",
148+
" max_tokens=20,\n",
149+
" )\n",
150+
"\n",
151+
" # Print the completion\n",
152+
" print(completion.choices[0].text.strip(\" \\n\"))\n",
153+
" \n",
154+
" # Here indicating if the response is filtered\n",
155+
" if completion.choices[0].finish_reason == \"content_filter\":\n",
156+
" print(\"The generated content is filtered.\")\n",
157+
"\n",
158+
"except openai.AuthenticationError as e:\n",
159+
" # Handle Authentication error here, e.g. invalid API key\n",
160+
" print(f\"OpenAI API returned an Authentication Error: {e}\")\n",
161+
"\n",
162+
"except openai.APIConnectionError as e:\n",
163+
" # Handle connection error here\n",
164+
" print(f\"Failed to connect to OpenAI API: {e}\")\n",
165+
"\n",
166+
"except openai.BadRequestError as e:\n",
167+
" # Handle connection error here\n",
168+
" print(f\"Invalid Request Error: {e}\")\n",
169+
"\n",
170+
"except openai.RateLimitError as e:\n",
171+
" # Handle rate limit error\n",
172+
" print(f\"OpenAI API request exceeded rate limit: {e}\")\n",
173+
"\n",
174+
"except openai.InternalServerError as e:\n",
175+
" # Handle Service Unavailable error\n",
176+
" print(f\"Service Unavailable: {e}\")\n",
177+
"\n",
178+
"except openai.APITimeoutError as e:\n",
179+
" # Handle request timeout\n",
180+
" print(f\"Request timed out: {e}\")\n",
181+
"\n",
182+
"except openai.APIError as e:\n",
183+
" # Handle API error here, e.g. retry or log\n",
184+
" print(f\"OpenAI API returned an API Error: {e}\")"
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": null,
190+
"id": "b9be87b2",
191+
"metadata": {},
192+
"outputs": [],
193+
"source": []
194+
}
195+
],
196+
"metadata": {
197+
"kernelspec": {
198+
"display_name": "Python 3 (ipykernel)",
199+
"language": "python",
200+
"name": "python3"
201+
},
202+
"language_info": {
203+
"codemirror_mode": {
204+
"name": "ipython",
205+
"version": 3
206+
},
207+
"file_extension": ".py",
208+
"mimetype": "text/x-python",
209+
"name": "python",
210+
"nbconvert_exporter": "python",
211+
"pygments_lexer": "ipython3",
212+
"version": "3.12.1"
213+
}
214+
},
215+
"nbformat": 4,
216+
"nbformat_minor": 5
217+
}

0 commit comments

Comments
 (0)