Skip to content

Commit a9bbffd

Browse files
committed
OpenAI SDK fixes
1 parent a8fb712 commit a9bbffd

File tree

5 files changed

+127
-28
lines changed

5 files changed

+127
-28
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
azure-ai-inference~=1.0.0b4
22
azure-ai-evaluation~=1.1.0
3-
openai~=1.37.1
3+
openai~=1.72.0
44
mistralai~=0.4.2
55
python-dotenv~=1.0.1
66
ipykernel~=6.29.5

samples/python/openai/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ To run these notebooks, click on a link below to open it in Codespaces and selec
1414
To run these scripts, open your terminal and run a command like:
1515

1616
```shell
17-
python3 samples/python/openai/basic.py
17+
python samples/python/openai/basic.py
1818
```
1919

2020
* [basic.py](basic.py): basic call to the gpt-4o-mini chat completion API

samples/python/openai/embeddings_getting_started.ipynb

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,18 @@
2020
},
2121
{
2222
"cell_type": "code",
23-
"execution_count": null,
23+
"execution_count": 1,
2424
"metadata": {},
25-
"outputs": [],
25+
"outputs": [
26+
{
27+
"name": "stdout",
28+
"output_type": "stream",
29+
"text": [
30+
"Note: you may need to restart the kernel to use updated packages.\n",
31+
"Note: you may need to restart the kernel to use updated packages.\n"
32+
]
33+
}
34+
],
2635
"source": [
2736
"%pip install openai --quiet\n",
2837
"%pip install python-dotenv --quiet"
@@ -41,7 +50,7 @@
4150
},
4251
{
4352
"cell_type": "code",
44-
"execution_count": null,
53+
"execution_count": 2,
4554
"metadata": {},
4655
"outputs": [],
4756
"source": [
@@ -73,9 +82,18 @@
7382
},
7483
{
7584
"cell_type": "code",
76-
"execution_count": null,
85+
"execution_count": 3,
7786
"metadata": {},
78-
"outputs": [],
87+
"outputs": [
88+
{
89+
"name": "stdout",
90+
"output_type": "stream",
91+
"text": [
92+
"1536\n",
93+
"[-0.019143931567668915, -0.025292053818702698, -0.0017211713129654527, 0.01883450709283352, -0.03382139280438423, -0.019682060927152634, -0.02102738246321678, 0.05160655081272125, -0.03218010067939758, -0.03043118305504322]\n"
94+
]
95+
}
96+
],
7997
"source": [
8098
"model_name = \"text-embedding-3-small\" \n",
8199
"\n",
@@ -101,9 +119,19 @@
101119
},
102120
{
103121
"cell_type": "code",
104-
"execution_count": null,
122+
"execution_count": 4,
105123
"metadata": {},
106-
"outputs": [],
124+
"outputs": [
125+
{
126+
"name": "stdout",
127+
"output_type": "stream",
128+
"text": [
129+
"[-0.019143931567668915, -0.025292053818702698, -0.0017211713129654527, 0.01883450709283352, -0.03382139280438423, -0.019682060927152634, -0.02102738246321678, 0.05160655081272125, -0.03218010067939758, -0.03043118305504322]\n",
130+
"[0.027567066252231598, -0.026544300839304924, -0.027091361582279205, 0.057798076421022415, -0.0034756148234009743, -0.0028126018587499857, 0.03184840455651283, 0.010120609775185585, -0.0341080017387867, -0.04464485123753548]\n",
131+
"[-0.030637985095381737, -0.0062282998114824295, -0.038596536964178085, -0.008159957826137543, 0.004671987611800432, 0.018016600981354713, 0.02050670050084591, -0.05268268659710884, 0.0091486731544137, 0.015965931117534637]\n"
132+
]
133+
}
134+
],
107135
"source": [
108136
"model_name = \"text-embedding-3-small\" \n",
109137
"inputs = [\"Hello, world!\", \"How are you?\", \"What's the weather like?\"]\n",
@@ -132,7 +160,7 @@
132160
],
133161
"metadata": {
134162
"kernelspec": {
135-
"display_name": "gh-cookbook",
163+
"display_name": "Python 3",
136164
"language": "python",
137165
"name": "python3"
138166
},

samples/python/openai/getting_started.ipynb

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,18 @@
2020
},
2121
{
2222
"cell_type": "code",
23-
"execution_count": null,
23+
"execution_count": 1,
2424
"metadata": {},
25-
"outputs": [],
25+
"outputs": [
26+
{
27+
"name": "stdout",
28+
"output_type": "stream",
29+
"text": [
30+
"Note: you may need to restart the kernel to use updated packages.\n",
31+
"Note: you may need to restart the kernel to use updated packages.\n"
32+
]
33+
}
34+
],
2635
"source": [
2736
"%pip install openai --quiet\n",
2837
"%pip install python-dotenv --quiet"
@@ -41,7 +50,7 @@
4150
},
4251
{
4352
"cell_type": "code",
44-
"execution_count": null,
53+
"execution_count": 2,
4554
"metadata": {},
4655
"outputs": [],
4756
"source": [
@@ -75,9 +84,17 @@
7584
},
7685
{
7786
"cell_type": "code",
78-
"execution_count": null,
87+
"execution_count": 3,
7988
"metadata": {},
80-
"outputs": [],
89+
"outputs": [
90+
{
91+
"name": "stdout",
92+
"output_type": "stream",
93+
"text": [
94+
"The capital of France is Paris.\n"
95+
]
96+
}
97+
],
8198
"source": [
8299
"\n",
83100
"response = client.chat.completions.create(\n",
@@ -115,9 +132,17 @@
115132
},
116133
{
117134
"cell_type": "code",
118-
"execution_count": null,
135+
"execution_count": 4,
119136
"metadata": {},
120-
"outputs": [],
137+
"outputs": [
138+
{
139+
"name": "stdout",
140+
"output_type": "stream",
141+
"text": [
142+
"The capital of Spain is Madrid.\n"
143+
]
144+
}
145+
],
121146
"source": [
122147
"# Call the chat completion API\n",
123148
"response = client.chat.completions.create(\n",
@@ -162,9 +187,17 @@
162187
},
163188
{
164189
"cell_type": "code",
165-
"execution_count": null,
190+
"execution_count": 5,
166191
"metadata": {},
167-
"outputs": [],
192+
"outputs": [
193+
{
194+
"name": "stdout",
195+
"output_type": "stream",
196+
"text": [
197+
"The image features a cute, fluffy puppy with a golden coat seated on a textured mat. In front of the puppy is a black and gray food bowl, which reflects the surroundings. The floor appears to be wooden, and there are elements of a cozy environment visible in the background, including a brick wall and wooden furniture. The overall atmosphere of the image is warm and inviting.\n"
198+
]
199+
}
200+
],
168201
"source": [
169202
"import base64\n",
170203
"model_name = \"gpt-4o-mini\"\n",
@@ -237,9 +270,29 @@
237270
},
238271
{
239272
"cell_type": "code",
240-
"execution_count": null,
273+
"execution_count": 7,
241274
"metadata": {},
242-
"outputs": [],
275+
"outputs": [
276+
{
277+
"name": "stdout",
278+
"output_type": "stream",
279+
"text": [
280+
"Exercising every day offers numerous benefits for both your physical and mental well-being. Here are five compelling reasons to make daily exercise a part of your routine:\n",
281+
"\n",
282+
"1. **Improved Physical Health**: Regular exercise strengthens your heart, improves circulation, boosts your immune system, and helps maintain a healthy weight. It can lower the risk of chronic diseases such as heart disease, type 2 diabetes, and certain cancers.\n",
283+
"\n",
284+
"2. **Enhanced Mental Well-being**: Physical activity releases endorphins, often referred to as \"feel-good\" hormones. This can reduce feelings of stress, anxiety, and depression while improving your mood and overall emotional health.\n",
285+
"\n",
286+
"3. **Increased Energy Levels**: Contrary to what one might expect, regular exercise can lead to increased energy levels. It improves your stamina and reduces fatigue, making daily tasks easier and more enjoyable.\n",
287+
"\n",
288+
"4. **Better Sleep Quality**: Engaging in physical activity can help you fall asleep faster and deepen your sleep. Exercise helps regulate your sleep patterns and can alleviate issues like insomnia.\n",
289+
"\n",
290+
"5. **Social Connections and Routine**: Exercising daily can provide opportunities for social interaction, whether through group classes, team sports, or simply exercising with friends. Additionally, establishing a regular exercise routine can enhance your sense of discipline and structure in your daily life.\n",
291+
"\n",
292+
"Incorporating daily exercise can have a profound impact on multiple dimensions of your health and well-being."
293+
]
294+
}
295+
],
243296
"source": [
244297
"# Call the chat completion API\n",
245298
"response = client.chat.completions.create(\n",
@@ -259,8 +312,10 @@
259312
"\n",
260313
"# Print the streamed response\n",
261314
"for update in response:\n",
262-
" if update.choices[0].delta.content:\n",
263-
" print(update.choices[0].delta.content, end=\"\")\n"
315+
" if update.choices:\n",
316+
" content = update.choices[0].delta.content\n",
317+
" if content:\n",
318+
" print(content, end=\"\")\n"
264319
]
265320
},
266321
{
@@ -276,9 +331,23 @@
276331
},
277332
{
278333
"cell_type": "code",
279-
"execution_count": null,
334+
"execution_count": 8,
280335
"metadata": {},
281-
"outputs": [],
336+
"outputs": [
337+
{
338+
"name": "stdout",
339+
"output_type": "stream",
340+
"text": [
341+
"Calling function `get_flight_info` with arguments {'origin_city': 'Seattle', 'destination_city': 'Miami'}\n",
342+
"Function returned = {\"airline\": \"Delta\", \"flight_number\": \"DL123\", \"flight_date\": \"May 7th, 2024\", \"flight_time\": \"10:00AM\"}\n",
343+
"Model response = The next flight from Seattle to Miami is with Delta Airlines. Here are the details:\n",
344+
"\n",
345+
"- **Flight Number**: DL123\n",
346+
"- **Date**: May 7th, 2024\n",
347+
"- **Time**: 10:00 AM\n"
348+
]
349+
}
350+
],
282351
"source": [
283352
"import json\n",
284353
"\n",
@@ -404,7 +473,7 @@
404473
],
405474
"metadata": {
406475
"kernelspec": {
407-
"display_name": "gh-cookbook",
476+
"display_name": "Python 3",
408477
"language": "python",
409478
"name": "python3"
410479
},

samples/python/openai/streaming.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434

3535
# Print the streamed response
3636
for update in response:
37-
if update.choices[0].delta.content:
38-
print(update.choices[0].delta.content, end="")
37+
if update.choices:
38+
content = update.choices[0].delta.content
39+
if content:
40+
print(content, end="")
3941

4042
print()

0 commit comments

Comments
 (0)