{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "view-in-github" }, "source": [ "\"Open\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 }