File size: 13,047 Bytes
14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd 14a73a7 0bfedbd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
"<a href=\"https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Agents_with_OpenAI_Assistants.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "UPKtF57twVp8",
"outputId": "0af0655d-d8a0-478b-d7f3-8ce55292454a"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m328.5/328.5 kB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m1.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m3.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h"
]
}
],
"source": [
"!pip install -q openai==1.37.0"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "gf1VoYD-Y7TL"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"# Set the \"OPENAI_API_KEY\" in the Python environment. Will be used by OpenAI client later.\n",
"os.environ[\"OPENAI_API_KEY\"] = \"[OPENAI_API_KEY]\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mLTbUTtthHGG"
},
"source": [
"# Math Tutor\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "QxYu2uw9YoG8"
},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"\n",
"client = OpenAI()\n",
"\n",
"assistant = client.beta.assistants.create(\n",
" name=\"Math Tutor\",\n",
" instructions=\"You are a personal math tutor. Write and run code to answer math questions.\",\n",
" model=\"gpt-4o\",\n",
" tools=[{\"type\": \"code_interpreter\"}],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "zdAu65wDY43T"
},
"outputs": [],
"source": [
"thread = client.beta.threads.create()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "AeiK-j7NZIJI"
},
"outputs": [],
"source": [
"message = client.beta.threads.messages.create(\n",
" thread_id=thread.id,\n",
" role=\"user\",\n",
" content=\"I need to solve the equation `3x + 11 = 14`. Can you help me?\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "-PWEekBTZJSR"
},
"outputs": [],
"source": [
"run = client.beta.threads.runs.create_and_poll(\n",
" thread_id=thread.id, assistant_id=assistant.id\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"id": "SKcOwN2XZKTy"
},
"outputs": [],
"source": [
"messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ndRo014JZSLo",
"outputId": "7186ef9a-7fb9-4e4b-c1cf-365f4d0d3bdc"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Of course! To solve the equation \\(3x + 11 = 14\\), follow these steps:\n",
"\n",
"1. **Isolate the variable term**:\n",
" Subtract 11 from both sides of the equation.\n",
" \\[\n",
" 3x + 11 - 11 = 14 - 11\n",
" \\]\n",
" Simplifies to:\n",
" \\[\n",
" 3x = 3\n",
" \\]\n",
"\n",
"2. **Solve for \\( x \\)**:\n",
" Divide both sides of the equation by 3.\n",
" \\[\n",
" x = \\frac{3}{3}\n",
" \\]\n",
" Simplifies to:\n",
" \\[\n",
" x = 1\n",
" \\]\n",
"\n",
"So, the solution to the equation is \\( x = 1 \\).\n",
"\n",
"Let's verify this by substituting \\( x = 1 \\) back into the original equation to confirm that both sides are equal.\n",
"\n",
"\\[\n",
"3(1) + 11 = 14\n",
"\\]\n",
"\\[\n",
"3 + 11 = 14\n",
"\\]\n",
"\\[\n",
"14 = 14\n",
"\\]\n",
"\n",
"The left and right sides are equal, so the solution \\( x = 1 \\) is correct.\n"
]
}
],
"source": [
"print(messages[0].content[0].text.value)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cgE3EEaHhFEh"
},
"source": [
"# Customer Support\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "P-zDilXchGGU",
"outputId": "9e9e306a-61fb-4617-f5a2-99eedd8f6bd2"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--2024-07-23 17:09:57-- https://personales.unican.es/corcuerp/linux/resources/LinuxCommandLineCheatSheet_1.pdf\n",
"Resolving personales.unican.es (personales.unican.es)... 193.144.193.111\n",
"Connecting to personales.unican.es (personales.unican.es)|193.144.193.111|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 267816 (262K) [application/pdf]\n",
"Saving to: βLinuxCommandLineCheatSheet_1.pdfβ\n",
"\n",
"LinuxCommandLineChe 100%[===================>] 261.54K 314KB/s in 0.8s \n",
"\n",
"2024-07-23 17:09:58 (314 KB/s) - βLinuxCommandLineCheatSheet_1.pdfβ saved [267816/267816]\n",
"\n"
]
}
],
"source": [
"!wget https://personales.unican.es/corcuerp/linux/resources/LinuxCommandLineCheatSheet_1.pdf"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "IqLR9ss9lKrz"
},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"\n",
"client = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "VevcGLDCjdUi",
"outputId": "049f6306-84f6-434d-f3c7-dc0741bbbfb6"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"completed\n",
"FileCounts(cancelled=0, completed=1, failed=0, in_progress=0, total=1)\n"
]
}
],
"source": [
"# Create a vector store caled \"Financial Statements\"\n",
"vector_store = client.beta.vector_stores.create(name=\"Tech Support\")\n",
"\n",
"# Ready the files for upload to OpenAI\n",
"file_streams = [open(\"LinuxCommandLineCheatSheet_1.pdf\", \"rb\")]\n",
"\n",
"# Use the upload and poll SDK helper to upload the files, add them to the vector store,\n",
"# and poll the status of the file batch for completion.\n",
"file_batch = client.beta.vector_stores.file_batches.upload_and_poll(\n",
" vector_store_id=vector_store.id, files=file_streams\n",
")\n",
"\n",
"# You can print the status and the file counts of the batch to see the result of this operation.\n",
"print(file_batch.status)\n",
"print(file_batch.file_counts)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "pTzfL1XCjdXT"
},
"outputs": [],
"source": [
"assistant = client.beta.assistants.create(\n",
" name=\"Tech Support\",\n",
" instructions=\"You are a tech support chatbot. Use the product manual to respond accurately to customer inquiries.\",\n",
" model=\"gpt-4o\",\n",
" tools=[{\"type\": \"file_search\"}],\n",
" tool_resources={\"file_search\": {\"vector_store_ids\": [vector_store.id]}},\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "FSTCsotRjdPj"
},
"outputs": [],
"source": [
"# Create a thread and attach the file to the message\n",
"thread = client.beta.threads.create(\n",
" messages=[\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"What 'ls' command do?\",\n",
" }\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"id": "jdD5yJK2jdMu"
},
"outputs": [],
"source": [
"run = client.beta.threads.runs.create_and_poll(\n",
" thread_id=thread.id, assistant_id=assistant.id\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "p0w3ts1DjdKW",
"outputId": "a1720556-12df-42ff-bfd4-a001f3bb2565"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The `ls` command in Linux is used to list the contents of a directory. The common usage of `ls` can be extended with options to display detailed information about files and directories. For example:\n",
"\n",
"- `ls -al` lists all files, including hidden ones, in a long listing format that provides detailed information such as permissions, number of links, owner, group, size, and timestampγ4:0β sourceγγ4:1β sourceγ.\n"
]
}
],
"source": [
"messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))\n",
"\n",
"print(messages[0].content[0].text.value)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "p1KafLldjdFI",
"outputId": "0f1f388f-c04a-4eda-fe6b-9a00d83f0070"
},
"outputs": [
{
"data": {
"text/plain": [
"[FileCitationAnnotation(end_index=394, file_citation=FileCitation(file_id='file-EMNwQYbq7rGni9Ct4V7B8XTR'), start_index=382, text='γ4:0β sourceγ', type='file_citation'),\n",
" FileCitationAnnotation(end_index=406, file_citation=FileCitation(file_id='file-EMNwQYbq7rGni9Ct4V7B8XTR'), start_index=394, text='γ4:1β sourceγ', type='file_citation')]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages[0].content[0].text.annotations"
]
}
],
"metadata": {
"colab": {
"authorship_tag": "ABX9TyOyF/2q5TUS6bkOmPxn67kV",
"include_colab_link": true,
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|