FortiShield commited on
Commit
7cfa5d1
1 Parent(s): cd5f933

Add files via upload

Browse files
L5_Leveraging_Assistants_API_for_SQL_Databases.ipynb ADDED
@@ -0,0 +1,333 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "a2f88613",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Lesson 5: Leveraging Assistants API for SQL Databases"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "markdown",
13
+ "id": "35d64829",
14
+ "metadata": {},
15
+ "source": [
16
+ "## Setup"
17
+ ]
18
+ },
19
+ {
20
+ "cell_type": "code",
21
+ "execution_count": null,
22
+ "id": "2565494f-532f-4fcb-99b0-e50fe1cb6eef",
23
+ "metadata": {
24
+ "height": 64
25
+ },
26
+ "outputs": [],
27
+ "source": [
28
+ "from openai import AzureOpenAI\n",
29
+ "import json\n",
30
+ "import os"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "markdown",
35
+ "id": "e85ca259",
36
+ "metadata": {},
37
+ "source": [
38
+ "## Import the helper function\n",
39
+ "\n",
40
+ "To access the ``Helper.py`` file, please go to the ``File`` menu and select ``Open...``."
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "execution_count": null,
46
+ "id": "270ea973-e750-48d2-ad96-16c1a0406232",
47
+ "metadata": {
48
+ "height": 64
49
+ },
50
+ "outputs": [],
51
+ "source": [
52
+ "import Helper\n",
53
+ "from Helper import get_positive_cases_for_state_on_date\n",
54
+ "from Helper import get_hospitalized_increase_for_state_on_date"
55
+ ]
56
+ },
57
+ {
58
+ "cell_type": "markdown",
59
+ "id": "2b16b04b",
60
+ "metadata": {},
61
+ "source": [
62
+ "## Launch the Assistant API"
63
+ ]
64
+ },
65
+ {
66
+ "cell_type": "markdown",
67
+ "id": "3d24b35d",
68
+ "metadata": {},
69
+ "source": [
70
+ "**Note**: The pre-configured cloud resource grants you access to the Azure OpenAI GPT model. The key and endpoint provided below are intended for teaching purposes only. Your notebook environment is already set up with the necessary keys, which may differ from those used by the instructor during the filming."
71
+ ]
72
+ },
73
+ {
74
+ "cell_type": "code",
75
+ "execution_count": null,
76
+ "id": "3842d0a1-e2af-4fba-8131-ec5d8ba92927",
77
+ "metadata": {
78
+ "height": 285
79
+ },
80
+ "outputs": [],
81
+ "source": [
82
+ "client = AzureOpenAI(\n",
83
+ " api_key=os.getenv(\"AZURE_OPENAI_KEY\"),\n",
84
+ " api_version=\"2024-02-15-preview\",\n",
85
+ " azure_endpoint = os.getenv(\"AZURE_OPENAI_ENDPOINT\")\n",
86
+ " )\n",
87
+ "\n",
88
+ "# I) Create assistant\n",
89
+ "assistant = client.beta.assistants.create(\n",
90
+ " instructions=\"\"\"You are an assistant answering questions \n",
91
+ " about a Covid dataset.\"\"\",\n",
92
+ " model=\"gpt-4-1106\", \n",
93
+ " tools=Helper.tools_sql)\n",
94
+ "\n",
95
+ "# II) Create thread\n",
96
+ "thread = client.beta.threads.create()\n",
97
+ "print(thread)"
98
+ ]
99
+ },
100
+ {
101
+ "cell_type": "code",
102
+ "execution_count": null,
103
+ "id": "16c4150e-dac3-4f25-9a02-4cbffad8d35b",
104
+ "metadata": {
105
+ "height": 149
106
+ },
107
+ "outputs": [],
108
+ "source": [
109
+ "# III) Add message\n",
110
+ "message = client.beta.threads.messages.create(\n",
111
+ " thread_id=thread.id,\n",
112
+ " role=\"user\",\n",
113
+ " content=\"\"\"how many hospitalized people we had in Alaska\n",
114
+ " the 2021-03-05?\"\"\"\n",
115
+ ")\n",
116
+ "print(message)"
117
+ ]
118
+ },
119
+ {
120
+ "cell_type": "code",
121
+ "execution_count": null,
122
+ "id": "6f89fc9b-cad5-452f-9d9f-d6b48f722fa9",
123
+ "metadata": {
124
+ "height": 98
125
+ },
126
+ "outputs": [],
127
+ "source": [
128
+ "messages = client.beta.threads.messages.list(\n",
129
+ " thread_id=thread.id\n",
130
+ ")\n",
131
+ "\n",
132
+ "print(messages.model_dump_json(indent=2))"
133
+ ]
134
+ },
135
+ {
136
+ "cell_type": "code",
137
+ "execution_count": null,
138
+ "id": "e402a07d-fd1c-454e-b2fb-b2c71fb76a6a",
139
+ "metadata": {
140
+ "height": 115
141
+ },
142
+ "outputs": [],
143
+ "source": [
144
+ "# IV) Run assistant on thread\n",
145
+ "\n",
146
+ "run = client.beta.threads.runs.create(\n",
147
+ " thread_id=thread.id,\n",
148
+ " assistant_id=assistant.id,\n",
149
+ ")"
150
+ ]
151
+ },
152
+ {
153
+ "cell_type": "markdown",
154
+ "id": "025035a0",
155
+ "metadata": {},
156
+ "source": [
157
+ "## Leverage the function calling with Assistants API"
158
+ ]
159
+ },
160
+ {
161
+ "cell_type": "code",
162
+ "execution_count": null,
163
+ "id": "cf1b35b9-f4fa-4b3f-ac38-54366c3230fd",
164
+ "metadata": {
165
+ "height": 914
166
+ },
167
+ "outputs": [],
168
+ "source": [
169
+ "import time\n",
170
+ "from IPython.display import clear_output\n",
171
+ "\n",
172
+ "start_time = time.time()\n",
173
+ "\n",
174
+ "status = run.status\n",
175
+ "\n",
176
+ "while status not in [\"completed\", \"cancelled\", \"expired\", \"failed\"]:\n",
177
+ " time.sleep(5)\n",
178
+ " run = client.beta.threads.runs.retrieve(\n",
179
+ " thread_id=thread.id,run_id=run.id\n",
180
+ " )\n",
181
+ " print(\"Elapsed time: {} minutes {} seconds\".format(\n",
182
+ " int((time.time() - start_time) // 60),\n",
183
+ " int((time.time() - start_time) % 60))\n",
184
+ " )\n",
185
+ " status = run.status\n",
186
+ " print(f'Status: {status}')\n",
187
+ " if (status==\"requires_action\"):\n",
188
+ " available_functions = {\n",
189
+ " \"get_positive_cases_for_state_on_date\": get_positive_cases_for_state_on_date,\n",
190
+ " \"get_hospitalized_increase_for_state_on_date\":get_hospitalized_increase_for_state_on_date\n",
191
+ " }\n",
192
+ "\n",
193
+ " tool_outputs = []\n",
194
+ " for tool_call in run.required_action.submit_tool_outputs.tool_calls:\n",
195
+ " function_name = tool_call.function.name\n",
196
+ " function_to_call = available_functions[function_name]\n",
197
+ " function_args = json.loads(tool_call.function.arguments)\n",
198
+ " function_response = function_to_call(\n",
199
+ " state_abbr=function_args.get(\"state_abbr\"),\n",
200
+ " specific_date=function_args.get(\"specific_date\"),\n",
201
+ " )\n",
202
+ " print(function_response)\n",
203
+ " print(tool_call.id)\n",
204
+ " tool_outputs.append(\n",
205
+ " { \"tool_call_id\": tool_call.id,\n",
206
+ " \"output\": str(function_response)\n",
207
+ " }\n",
208
+ " )\n",
209
+ "\n",
210
+ " run = client.beta.threads.runs.submit_tool_outputs(\n",
211
+ " thread_id=thread.id,\n",
212
+ " run_id=run.id,\n",
213
+ " tool_outputs = tool_outputs\n",
214
+ " )\n",
215
+ "\n",
216
+ "\n",
217
+ "messages = client.beta.threads.messages.list(\n",
218
+ " thread_id=thread.id\n",
219
+ ")\n",
220
+ "\n",
221
+ "print(messages)"
222
+ ]
223
+ },
224
+ {
225
+ "cell_type": "code",
226
+ "execution_count": null,
227
+ "id": "c9bc1379-0ee3-49c3-a954-b026da3aa672",
228
+ "metadata": {
229
+ "height": 30
230
+ },
231
+ "outputs": [],
232
+ "source": [
233
+ "print(messages.model_dump_json(indent=2))"
234
+ ]
235
+ },
236
+ {
237
+ "cell_type": "markdown",
238
+ "id": "65faedb6",
239
+ "metadata": {},
240
+ "source": [
241
+ "## Add the code interpreter"
242
+ ]
243
+ },
244
+ {
245
+ "cell_type": "code",
246
+ "execution_count": null,
247
+ "id": "6768848b-30f1-4cf5-9c60-a84ec5abc97f",
248
+ "metadata": {
249
+ "height": 404
250
+ },
251
+ "outputs": [],
252
+ "source": [
253
+ "file = client.files.create(\n",
254
+ " file=open(\"./data/all-states-history.csv\", \"rb\"),\n",
255
+ " purpose='assistants'\n",
256
+ ")\n",
257
+ "assistant = client.beta.assistants.create(\n",
258
+ " instructions=\"\"\"You are an assitant answering questions about\n",
259
+ " a Covid dataset.\"\"\",\n",
260
+ " model=\"gpt-4-1106\", \n",
261
+ " tools=[{\"type\": \"code_interpreter\"}],\n",
262
+ " file_ids=[file.id])\n",
263
+ "thread = client.beta.threads.create()\n",
264
+ "print(thread)\n",
265
+ "message = client.beta.threads.messages.create(\n",
266
+ " thread_id=thread.id,\n",
267
+ " role=\"user\",\n",
268
+ " content=\"\"\"how many hospitalized people we had in Alaska\n",
269
+ " the 2021-03-05?\"\"\"\n",
270
+ ")\n",
271
+ "print(message)\n",
272
+ "run = client.beta.threads.runs.create(\n",
273
+ " thread_id=thread.id,\n",
274
+ " assistant_id=assistant.id,\n",
275
+ ")"
276
+ ]
277
+ },
278
+ {
279
+ "cell_type": "code",
280
+ "execution_count": null,
281
+ "id": "41d278df-c8c9-4904-8ca5-4767a718d6bd",
282
+ "metadata": {
283
+ "height": 387
284
+ },
285
+ "outputs": [],
286
+ "source": [
287
+ "status = run.status\n",
288
+ "start_time = time.time()\n",
289
+ "while status not in [\"completed\", \"cancelled\", \"expired\", \"failed\"]:\n",
290
+ " time.sleep(5)\n",
291
+ " run = client.beta.threads.runs.retrieve(\n",
292
+ " thread_id=thread.id,\n",
293
+ " run_id=run.id\n",
294
+ " )\n",
295
+ " print(\"Elapsed time: {} minutes {} seconds\".format(\n",
296
+ " int((time.time() - start_time) // 60),\n",
297
+ " int((time.time() - start_time) % 60))\n",
298
+ " )\n",
299
+ " status = run.status\n",
300
+ " print(f'Status: {status}')\n",
301
+ " clear_output(wait=True)\n",
302
+ "\n",
303
+ "\n",
304
+ "messages = client.beta.threads.messages.list(\n",
305
+ " thread_id=thread.id\n",
306
+ ")\n",
307
+ "\n",
308
+ "print(messages.model_dump_json(indent=2))"
309
+ ]
310
+ }
311
+ ],
312
+ "metadata": {
313
+ "kernelspec": {
314
+ "display_name": "Python 3 (ipykernel)",
315
+ "language": "python",
316
+ "name": "python3"
317
+ },
318
+ "language_info": {
319
+ "codemirror_mode": {
320
+ "name": "ipython",
321
+ "version": 3
322
+ },
323
+ "file_extension": ".py",
324
+ "mimetype": "text/x-python",
325
+ "name": "python",
326
+ "nbconvert_exporter": "python",
327
+ "pygments_lexer": "ipython3",
328
+ "version": "3.11.9"
329
+ }
330
+ },
331
+ "nbformat": 4,
332
+ "nbformat_minor": 5
333
+ }