Skip to content

Commit f2c54e0

Browse files
committed
update OpenAI Java SDK notebook: consolidate examples, introduce new API version, and add function calling feature
1 parent 49965ec commit f2c54e0

File tree

2 files changed

+85
-83
lines changed

2 files changed

+85
-83
lines changed

notebooks/openai/OpenAI Image Recognition.ipynb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
{
99
"metadata": {
1010
"ExecuteTime": {
11-
"end_time": "2025-04-11T16:52:41.140759Z",
12-
"start_time": "2025-04-11T16:52:40.835643Z"
11+
"end_time": "2025-05-26T14:48:57.256733Z",
12+
"start_time": "2025-05-26T14:48:55.931488Z"
1313
}
1414
},
1515
"cell_type": "code",
16-
"source": "@file:DependsOn(\"com.openai:openai-java:1.2.0\")",
16+
"source": "@file:DependsOn(\"com.openai:openai-java:2.2.0\")",
1717
"outputs": [],
1818
"execution_count": 1
1919
},
2020
{
2121
"metadata": {
2222
"ExecuteTime": {
23-
"end_time": "2025-04-11T16:52:41.373172Z",
24-
"start_time": "2025-04-11T16:52:41.174563Z"
23+
"end_time": "2025-05-26T14:48:57.380143Z",
24+
"start_time": "2025-05-26T14:48:57.258416Z"
2525
}
2626
},
2727
"cell_type": "code",
@@ -36,8 +36,8 @@
3636
{
3737
"metadata": {
3838
"ExecuteTime": {
39-
"end_time": "2025-04-11T16:52:41.479406Z",
40-
"start_time": "2025-04-11T16:52:41.397667Z"
39+
"end_time": "2025-05-26T14:48:57.450934Z",
40+
"start_time": "2025-05-26T14:48:57.381249Z"
4141
}
4242
},
4343
"cell_type": "code",
@@ -57,8 +57,8 @@
5757
{
5858
"metadata": {
5959
"ExecuteTime": {
60-
"end_time": "2025-04-11T16:52:41.602453Z",
61-
"start_time": "2025-04-11T16:52:41.513578Z"
60+
"end_time": "2025-05-26T14:48:57.537143Z",
61+
"start_time": "2025-05-26T14:48:57.452529Z"
6262
}
6363
},
6464
"cell_type": "code",
@@ -89,8 +89,8 @@
8989
{
9090
"metadata": {
9191
"ExecuteTime": {
92-
"end_time": "2025-04-11T16:52:41.845482Z",
93-
"start_time": "2025-04-11T16:52:41.682671Z"
92+
"end_time": "2025-05-26T14:48:57.666333Z",
93+
"start_time": "2025-05-26T14:48:57.538342Z"
9494
}
9595
},
9696
"cell_type": "code",
@@ -128,8 +128,8 @@
128128
{
129129
"metadata": {
130130
"ExecuteTime": {
131-
"end_time": "2025-04-11T16:52:52.067224Z",
132-
"start_time": "2025-04-11T16:52:41.872111Z"
131+
"end_time": "2025-05-26T14:49:06.893795Z",
132+
"start_time": "2025-05-26T14:48:57.667447Z"
133133
}
134134
},
135135
"cell_type": "code",
@@ -140,8 +140,8 @@
140140
{
141141
"metadata": {
142142
"ExecuteTime": {
143-
"end_time": "2025-04-11T16:52:52.120414Z",
144-
"start_time": "2025-04-11T16:52:52.070452Z"
143+
"end_time": "2025-05-26T14:49:06.951640Z",
144+
"start_time": "2025-05-26T14:49:06.894791Z"
145145
}
146146
},
147147
"cell_type": "code",
@@ -154,7 +154,7 @@
154154
{
155155
"data": {
156156
"text/plain": [
157-
"The image shows a tabby cat sitting on a windowsill of a wooden building. The window has four glass panes, and the wood has a rustic appearance."
157+
"The image shows a tabby cat sitting on the windowsill of a wooden building. The window frame and walls have a rustic, weathered appearance."
158158
]
159159
},
160160
"execution_count": 7,

notebooks/openai/OpenAI Java SDK Overview.ipynb

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
{
3232
"metadata": {
3333
"ExecuteTime": {
34-
"end_time": "2025-04-01T23:00:42.487230Z",
35-
"start_time": "2025-04-01T23:00:42.187330Z"
34+
"end_time": "2025-05-26T14:49:22.262353Z",
35+
"start_time": "2025-05-26T14:49:21.978733Z"
3636
}
3737
},
3838
"cell_type": "code",
39-
"source": "@file:DependsOn(\"com.openai:openai-java:0.41.1\")",
39+
"source": "@file:DependsOn(\"com.openai:openai-java:2.2.0\")",
4040
"outputs": [],
4141
"execution_count": 1
4242
},
@@ -64,8 +64,8 @@
6464
{
6565
"metadata": {
6666
"ExecuteTime": {
67-
"end_time": "2025-04-01T23:00:42.696974Z",
68-
"start_time": "2025-04-01T23:00:42.492834Z"
67+
"end_time": "2025-05-26T14:49:22.492991Z",
68+
"start_time": "2025-05-26T14:49:22.263692Z"
6969
}
7070
},
7171
"cell_type": "code",
@@ -87,8 +87,8 @@
8787
{
8888
"metadata": {
8989
"ExecuteTime": {
90-
"end_time": "2025-04-01T23:00:49.150957Z",
91-
"start_time": "2025-04-01T23:00:42.706680Z"
90+
"end_time": "2025-05-26T14:49:24.328589Z",
91+
"start_time": "2025-05-26T14:49:22.494979Z"
9292
}
9393
},
9494
"cell_type": "code",
@@ -110,7 +110,7 @@
110110
{
111111
"data": {
112112
"text/plain": [
113-
"Hello! Im an AI assistant designed to help you with information, answer questions, and assist with a variety of topics. How can I help you today?"
113+
"Hello! I'm an AI assistant here to help you with information, answer questions, and assist with a variety of tasks. How can I help you today?"
114114
]
115115
},
116116
"execution_count": 3,
@@ -134,8 +134,8 @@
134134
{
135135
"metadata": {
136136
"ExecuteTime": {
137-
"end_time": "2025-04-01T23:01:01.029018Z",
138-
"start_time": "2025-04-01T23:00:49.156287Z"
137+
"end_time": "2025-05-26T14:49:35.578786Z",
138+
"start_time": "2025-05-26T14:49:24.329484Z"
139139
}
140140
},
141141
"cell_type": "code",
@@ -154,50 +154,55 @@
154154
{
155155
"data": {
156156
"text/plain": [
157-
"Coroutines in Kotlin are a powerful feature that simplifies asynchronous programming and concurrency. They allow you to write non-blocking code in a sequential style, making it easier to work with asynchronous tasks without the complexity of traditional callback methods or thread management.\n",
157+
"Coroutines in Kotlin are a way to handle concurrency and asynchronous programming. They allow you to write asynchronous code that looks synchronous, making it easier to read and maintain. Coroutines can be paused and resumed without blocking a thread, which enables you to perform long-running tasks, such as network requests or I/O operations, without freezing the user interface.\n",
158158
"\n",
159-
"Coroutines are lightweight threads that can be suspended and resumed, enabling you to perform long-running tasks without blocking the main thread. They are particularly useful for tasks like network calls, file I/O, or any operation that might take time and would otherwise cause your application to become unresponsive if handled on the main thread.\n",
159+
"Coroutines are built on top of existing threading models in Kotlin and provide a simple way to work with tasks that can take time to complete. They are lightweight compared to threads, enabling you to have many coroutines running simultaneously without much overhead.\n",
160160
"\n",
161-
"### Key Concepts of Coroutines\n",
162-
"- **Suspend Functions**: Functions marked with the `suspend` keyword can be paused and resumed later. They can call other suspend functions, but they need to be called from within a coroutine or another suspend function.\n",
163-
"- **Coroutine Builders**: Functions that help create coroutines, such as `launch`, `async`, and `runBlocking`.\n",
164-
"- **Scope**: A coroutine scope defines a context for running coroutines. It manages the lifecycle of coroutines and provides a structure for launching them.\n",
161+
"To use coroutines in Kotlin, you typically utilize the `kotlinx.coroutines` library, which provides a range of coroutine builders and tools to assist with asynchronous programming.\n",
165162
"\n",
166-
"### Example:\n",
163+
"### Basic Example of Coroutines\n",
167164
"\n",
168-
"Here's a simple example demonstrating the use of coroutines in Kotlin. In this example, we'll simulate a network call that fetches data and update the UI accordingly.\n",
165+
"Heres a simple example demonstrating how to launch a coroutine in Kotlin:\n",
169166
"\n",
170167
"```kotlin\n",
171168
"import kotlinx.coroutines.*\n",
172169
"\n",
173170
"fun main() = runBlocking {\n",
174-
" // Launching a coroutine\n",
171+
" // This will run in the main thread\n",
172+
" println(\"Starting coroutine...\")\n",
173+
"\n",
175174
" launch {\n",
176-
" // Call a suspend function\n",
177-
" val result = fetchDataFromNetwork()\n",
178-
" println(\"Data fetched: $result\")\n",
175+
" // This will run in a new coroutine\n",
176+
" delay(1000L) // Simulate a long-running task (1 second)\n",
177+
" println(\"Coroutine has finished!\")\n",
179178
" }\n",
180179
"\n",
181-
" println(\"Coroutine launched, continuing with other work.\")\n",
182-
"}\n",
183-
"\n",
184-
"suspend fun fetchDataFromNetwork(): String {\n",
185-
" // Simulating a long-running network call\n",
186-
" delay(2000) // This is a suspend function\n",
187-
" return \"Sample Data\"\n",
180+
" println(\"This will print immediately!\")\n",
181+
" delay(2000L) // Give time for the coroutine to finish\n",
182+
" println(\"End of main.\")\n",
188183
"}\n",
189184
"```\n",
190185
"\n",
191-
"### Explanation of the Example:\n",
192-
"1. **`runBlocking { ... }`**: This is a coroutine builder that blocks the current thread until all the coroutines inside it complete. It's typically used in main functions or tests.\n",
193-
" \n",
194-
"2. **`launch { ... }`**: This launches a new coroutine in the background. It doesn't block the current thread and returns immediately.\n",
186+
"### Explanation\n",
187+
"\n",
188+
"1. **`runBlocking { ... }`**: This is a coroutine builder that blocks the current thread while awaiting the completion of the coroutines within its scope. It is often used in main functions or tests.\n",
189+
"\n",
190+
"2. **`launch { ... }`**: This function starts a new coroutine. The block of code inside `launch` runs concurrently with the code that follows it in the `runBlocking` scope.\n",
191+
"\n",
192+
"3. **`delay(1000L)`**: This function suspends the coroutine for a specified time (1000 milliseconds in this case). It does not block the thread, allowing other coroutines or code to run.\n",
195193
"\n",
196-
"3. **`suspend fun fetchDataFromNetwork()`**: This function simulates fetching data from the network. The `delay` function is used here to mimic the time taken for a network call without blocking the thread.\n",
194+
"4. **Print statements**: The outputs will show the order of execution based on the non-blocking nature of coroutines. \n",
197195
"\n",
198-
"4. **`delay(2000)`**: This is a suspending function that introduces a delay of 2 seconds. During this time, other coroutines or code can run without being blocked.\n",
196+
"When you run this code, you'll see:\n",
199197
"\n",
200-
"When you run this program, it will print \"Coroutine launched, continuing with other work.\" almost immediately, followed by \"Data fetched: Sample Data\" after a 2-second delay, demonstrating the non-blocking behavior of coroutines."
198+
"```\n",
199+
"Starting coroutine...\n",
200+
"This will print immediately!\n",
201+
"Coroutine has finished!\n",
202+
"End of main.\n",
203+
"```\n",
204+
"\n",
205+
"This output demonstrates that the coroutine runs without blocking the main thread, where other code can continue executing."
201206
]
202207
},
203208
"execution_count": 4,
@@ -219,8 +224,8 @@
219224
{
220225
"metadata": {
221226
"ExecuteTime": {
222-
"end_time": "2025-04-01T23:01:06.366674Z",
223-
"start_time": "2025-04-01T23:01:01.046810Z"
227+
"end_time": "2025-05-26T14:49:42.043544Z",
228+
"start_time": "2025-05-26T14:49:35.580430Z"
224229
}
225230
},
226231
"cell_type": "code",
@@ -244,23 +249,19 @@
244249
{
245250
"data": {
246251
"text/plain": [
247-
"**Title: The Last Signal**\n",
252+
"**Title: The Star Weaver**\n",
248253
"\n",
249-
"In the year 2267, humanity had expanded far beyond the cradle of Earth, establishing colonies on distant planets and moons. Among these ventures was the starship *Odyssey*, a sleek vessel tasked with exploring the uncharted reaches of the Andromeda sector. Captain Elara Voss stood on the bridge, her eyes fixed on the swirling cosmos outside.\n",
254+
"Captain Lila Armitage peered through the expansive viewport of the starship *Elysium*. For years, she had dreamt of this moment: a voyage beyond the known galaxies, where stars shimmered like scattered gems on a velvet canvas. The ship hummed softly, its state-of-the-art systems responding to her every command. Around her, the crew busied themselves with preparations for the jump into hyperspace.\n",
250255
"\n",
251-
"“Prepare for jump in thirty seconds,” she commanded, her voice steady despite the excitement that buzzed through her crew. They were about to embark on a voyage that would take them deeper into space than any human had ever ventured before.\n",
256+
"\"All systems go, Captain,\" called out Lieutenant Rhea, her best friend and chief engineer. Her fiery red hair caught the starlight, giving her an ethereal glow.\n",
252257
"\n",
253-
"As they transitioned through hyperspace, time seemed to stretch and warp around them. For a brief moment, Elara found herself lost in thought about what lay ahead: new worlds, new lifeforms—perhaps even ancient civilizations hidden among the stars.\n",
258+
"Lila nodded, her heart racing with excitement and apprehension. \"Prepare for jump in five... four... three...\" As she counted down, memories flooded back—her childhood dreams of exploration fueled by tales of brave explorers charting unknown worlds.\n",
254259
"\n",
255-
"The ship jolted violently as they exited hyperspace. Alarms blared, and lights flickered ominously. “Status report!” Elara shouted as she regained her footing.\n",
260+
"\"Two… One… Engage!\" \n",
256261
"\n",
257-
"“Systems are stable,” replied Keira, the ship’s engineer, her fingers flying over her console. “We’ve emerged in an uncharted system… but there’s interference.” \n",
262+
"The stars outside stretched into glowing streaks as the *Elysium* surged into hyperspace. They were hurtling toward a distant system known only as Gliese 581g—a potentially habitable planet that promised new beginnings for humanity. But it wasn't just a scientific mission; it was personal. Lila had lost her family in a catastrophic event on Earth, and finding hope in the cosmos felt like reclaiming part of herself.\n",
258263
"\n",
259-
"Elara squinted at the screen where swirling clouds of gas obscured their view of nearby planets. Yet one signal pulsed steadily amidst it all—a beacon unlike anything they’d ever encountered before.\n",
260-
"\n",
261-
"“Can you trace it?” she asked.\n",
262-
"\n",
263-
"“It’s faint but"
264+
"Hours turned into days as they navigated through swirling nebulas and shimmering asteroid fields. Then, just as Lila began to settle"
264265
]
265266
},
266267
"execution_count": 5,
@@ -282,8 +283,8 @@
282283
{
283284
"metadata": {
284285
"ExecuteTime": {
285-
"end_time": "2025-04-01T23:01:12.710709Z",
286-
"start_time": "2025-04-01T23:01:06.374896Z"
286+
"end_time": "2025-05-26T14:49:47.826576Z",
287+
"start_time": "2025-05-26T14:49:42.045729Z"
287288
}
288289
},
289290
"cell_type": "code",
@@ -325,14 +326,15 @@
325326
"text": [
326327
"Prompt: Explain machine learning.\n",
327328
"Response:\n",
328-
"Machine learning (ML) is a subset of artificial intelligence (AI) that focuses on the development of algorithms and statistical models that enable computers to perform specific tasks without being explicitly programmed. It allows systems to learn from data, improve their performance over time, and make predictions or decisions based on patterns and insights derived from that data.\n",
329+
"Machine learning is a subset of artificial intelligence (AI) that focuses on the development of algorithms and statistical models that enable computers to perform tasks without being explicitly programmed for each specific task. Instead of following fixed rules, machine learning systems learn from data and improve their performance over time as they are exposed to more information.\n",
330+
"\n",
331+
"### Key Concepts in Machine Learning\n",
329332
"\n",
330-
"### Key Concepts of Machine Learning:\n",
333+
"1. **Data**: At the core of machine learning is data, which includes any information used to train the model. This can come from various sources, such as databases, sensors, images, text, etc.\n",
331334
"\n",
332-
"1. **Data**: The foundation of machine learning is data. This can be structured (like tables) or unstructured (like text, images, or audio). Quality and quantity of data significantly impact the performance of ML models.\n",
335+
"2. **Algorithms**: Machine learning relies on algorithms, which are sets of rules and statistical methods that process and analyze data to identify patterns, make predictions, or classify information.\n",
333336
"\n",
334-
"2. **Algorithms**: Machine learning uses a variety of algorithms to analyze data and identify patterns. Common types include:\n",
335-
" - **Supervised Learning**\n",
337+
"3\n",
336338
"--------------------------------------------------------------------------------\n",
337339
"Prompt: Explain the concept of machine learning to a beginner student.\n",
338340
"Include the following:\n",
@@ -341,13 +343,13 @@
341343
"3) three real-world application examples\n",
342344
"Use analogies to explain complex concepts and keep the explanation within 200 words.\n",
343345
"Response:\n",
344-
"**Definition:** Machine learning is like teaching a computer to learn from experience, just as we do. Instead of programming it with every rule, we give it lots of data, and it learns patterns from that data to make decisions or predictions.\n",
346+
"**Machine Learning Defined:** \n",
347+
"Machine learning is a way for computers to learn from data, similar to how we learn from experiences. Instead of being programmed with specific instructions, machines use data to identify patterns and make decisions.\n",
345348
"\n",
346-
"**Supervised vs. Unsupervised Learning:** \n",
347-
"Think of supervised learning like a teacher guiding a student. The student (computer) learns from labeled examples—like showing a child pictures of cats and dogs and telling them which is which. In contrast, unsupervised learning is like letting a student explore without guidance. The student gets a pile of pictures but must find out patterns on their own, like grouping similar images without knowing what they are.\n",
349+
"**Supervised vs. Unsupervised Learning:** \n",
350+
"Think of supervised learning like training a puppy with treats. You show the puppy what to do (like sit or stay), and when it does well, you reward it. Here, you have labeled data (the correct answers). In contrast, unsupervised learning is like exploring a new city without a map. The computer looks at data without any labels and tries to find patterns or groups on its own, just like you might notice different neighborhoods without instructions.\n",
348351
"\n",
349-
"**Real-World Applications:**\n",
350-
"1. **Spam Filtering\n",
352+
"**Real-World\n",
351353
"--------------------------------------------------------------------------------\n"
352354
]
353355
}
@@ -368,8 +370,8 @@
368370
{
369371
"metadata": {
370372
"ExecuteTime": {
371-
"end_time": "2025-04-01T23:01:14.624335Z",
372-
"start_time": "2025-04-01T23:01:12.717333Z"
373+
"end_time": "2025-05-26T14:49:49.111693Z",
374+
"start_time": "2025-05-26T14:49:47.829017Z"
373375
}
374376
},
375377
"cell_type": "code",
@@ -428,7 +430,7 @@
428430
{
429431
"data": {
430432
"text/plain": [
431-
"Response: ChatCompletionMessage{content=null, refusal=null, role=assistant, annotations=[], audio=, functionCall=, toolCalls=[ChatCompletionMessageToolCall{id=call_OjCBz1vP0J7BdY2ntTF8yILW, function=Function{arguments={\"location\":\"Berlin\"}, name=get_weather, additionalProperties={}}, type=function, additionalProperties={}}], additionalProperties={}}"
433+
"Response: ChatCompletionMessage{content=null, refusal=null, role=assistant, annotations=[], audio=, functionCall=, toolCalls=[ChatCompletionMessageToolCall{id=call_6bOwpB4vx6VSpkonyxUPyeGW, function=Function{arguments={\"location\":\"Berlin\"}, name=get_weather, additionalProperties={}}, type=function, additionalProperties={}}], additionalProperties={}}"
432434
]
433435
},
434436
"execution_count": 7,
@@ -441,8 +443,8 @@
441443
{
442444
"metadata": {
443445
"ExecuteTime": {
444-
"end_time": "2025-04-01T23:01:15.786948Z",
445-
"start_time": "2025-04-01T23:01:14.632713Z"
446+
"end_time": "2025-05-26T14:49:50.630659Z",
447+
"start_time": "2025-05-26T14:49:49.112604Z"
446448
}
447449
},
448450
"cell_type": "code",
@@ -488,7 +490,7 @@
488490
"The model wants to call a function: get_weather\n",
489491
"With arguments: {location=Berlin}\n",
490492
"Final answer:\n",
491-
"The weather in Berlin is currently 13°C and cloudy.\n"
493+
"The current weather in Berlin is 13°C and cloudy.\n"
492494
]
493495
}
494496
],

0 commit comments

Comments
 (0)