{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyNHj7ZktyV9bZgyjC3mEDFq", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# Install Packages and Setup Variables" ], "metadata": { "id": "DMXyyXD0xix9" } }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "o4Q0N2omkAoZ", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "703fe996-2acf-4e90-92c1-252041ba7d7a" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m225.4/225.4 kB\u001b[0m \u001b[31m3.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m51.7/51.7 kB\u001b[0m \u001b[31m1.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m8.5 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[31m2.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.1/3.1 MB\u001b[0m \u001b[31m17.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.8/77.8 kB\u001b[0m \u001b[31m6.5 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[31m5.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h" ] } ], "source": [ "!pip install -q openai==1.6.0 cohere==4.39 tiktoken==0.5.2" ] }, { "cell_type": "code", "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\"] = \"\"" ], "metadata": { "id": "xxK7EAAvr2aT" }, "execution_count": 2, "outputs": [] }, { "cell_type": "markdown", "source": [ "# Load the API client" ], "metadata": { "id": "68RbStS-xpbL" } }, { "cell_type": "code", "source": [ "from openai import OpenAI\n", "\n", "# Defining the \"client\" object that enables\n", "# us to connect to OpenAI API endpoints.\n", "client = OpenAI()" ], "metadata": { "id": "La8hdWqJkFkh" }, "execution_count": 3, "outputs": [] }, { "cell_type": "markdown", "source": [ "# Query the API" ], "metadata": { "id": "CC-sa_uv6J2C" } }, { "cell_type": "code", "source": [ "# Define two questions: 1) Related to AI, 2) Unrelated topic.\n", "# These questions will be used to evaluate model's performance.\n", "QUESTION_AI = \"List a number of famous artificial intelligence frameworks?\"\n", "QUESTION_NOT_AI = \"What is the name of the highest mountain in the world and its height?\"" ], "metadata": { "id": "7JRrn0uIsBfg" }, "execution_count": 4, "outputs": [] }, { "cell_type": "code", "source": [ "# Defining a function to answer a question using \"gpt-3.5-turbo-16k\" model.\n", "def ask_ai_tutor(question):\n", " try:\n", " # Formulating the system prompt and condition the model to answer only AI-related questions.\n", " system_prompt = (\n", " \"You are an AI tutor specialized in answering artificial intelligence-related questions. \"\n", " \"Only answer AI-related question, else say that you cannot answer this question.\"\n", " )\n", "\n", " # Create a user prompt with the user's question\n", " prompt = f\"Please provide an informative and accurate answer to the following question.\\nQuestion: {question}\\nAnswer:\"\n", "\n", " # Call the OpenAI API\n", " response = client.chat.completions.create(\n", " model='gpt-3.5-turbo-16k',\n", " temperature=0.0,\n", " messages=[\n", " {\"role\": \"system\", \"content\": system_prompt},\n", " {\"role\": \"user\", \"content\": prompt}\n", " ]\n", " )\n", "\n", " # Return the AI's response\n", " return response.choices[0].message.content.strip()\n", "\n", " except Exception as e:\n", " return f\"An error occurred: {e}\"" ], "metadata": { "id": "CcP26IauuBuV" }, "execution_count": 5, "outputs": [] }, { "cell_type": "code", "source": [ "# Ask the AI-related question.\n", "RES_AI = ask_ai_tutor( QUESTION_AI )\n", "print( RES_AI )" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "W_dbwURpufR7", "outputId": "3cd84fb9-fe6f-4561-e9ee-ed606a983629" }, "execution_count": 10, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Sure! There are several famous artificial intelligence frameworks that are widely used in the field. Some of the popular ones include:\n", "\n", "1. TensorFlow: Developed by Google, TensorFlow is an open-source framework that is widely used for machine learning and deep learning tasks. It provides a comprehensive ecosystem of tools, libraries, and resources for building and deploying AI models.\n", "\n", "2. PyTorch: Developed by Facebook's AI Research lab, PyTorch is another popular open-source framework for deep learning. It is known for its dynamic computational graph, which allows for more flexibility and ease of use compared to other frameworks.\n", "\n", "3. Keras: Keras is a high-level neural networks API written in Python. It is built on top of TensorFlow and provides a user-friendly interface for building and training deep learning models. Keras is known for its simplicity and ease of use, making it a popular choice for beginners.\n", "\n", "4. Caffe: Caffe is a deep learning framework developed by Berkeley AI Research (BAIR). It is known for its speed and efficiency, particularly for convolutional neural networks (CNNs). Caffe has been widely used in computer vision tasks and has a large community of users and contributors.\n", "\n", "5. Theano: Theano is a Python library that allows for efficient mathematical computations, particularly for deep learning tasks. It provides a high-level interface for defining and optimizing mathematical expressions, making it a popular choice for researchers and developers.\n", "\n", "These are just a few examples of famous AI frameworks, and there are many others available depending on specific needs and preferences.\n" ] } ] }, { "cell_type": "code", "source": [ "# Ask the unrelated question.\n", "RES_NOT_AI = ask_ai_tutor( QUESTION_NOT_AI )\n", "print( RES_NOT_AI )" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "37YuVJQquhpN", "outputId": "4550c44d-2150-4cca-f23e-c89ea43e2040" }, "execution_count": 12, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "I'm sorry, but I cannot answer that question as it is not related to artificial intelligence.\n" ] } ] }, { "cell_type": "markdown", "source": [ "# History" ], "metadata": { "id": "NRBgk6WToIK0" } }, { "cell_type": "code", "source": [ "response = client.chat.completions.create(\n", " model='gpt-3.5-turbo-16k',\n", " temperature=0.0,\n", " messages=[\n", " {\"role\": \"system\", \"content\": \"You are an AI tutor specialized in answering artificial intelligence-related questions. Only answer AI-related question, else say that you cannot answer this question.\"},\n", " {\"role\": \"user\", \"content\": \"Please provide an informative and accurate answer to the following question.\\nQuestion: List a number of famous artificial intelligence frameworks?\\nAnswer:\"},\n", " {\"role\": \"assistant\", \"content\": RES_AI},\n", " {\"role\": \"user\", \"content\": \"Please provide an informative and accurate answer to the following question.\\nQuestion: What is the name of the highest mountain in the world and its height?\\nAnswer:\"},\n", " {\"role\": \"assistant\", \"content\": RES_NOT_AI},\n", " {\"role\": \"user\", \"content\": \"Please provide an informative and accurate answer to the following question.\\nQuestion: Can you write a summary of the first suggested AI framework in the first question?\\nAnswer:\"}\n", " ]\n", " )\n", "\n", "print( response.choices[0].message.content.strip() )" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "0_6GN2XsoEyM", "outputId": "3e66a833-a552-4bcc-9808-7b9f6b539310" }, "execution_count": 15, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Certainly! The first suggested AI framework in the previous question was TensorFlow. TensorFlow is an open-source framework developed by Google that has gained significant popularity in the field of artificial intelligence. It is primarily used for building and training machine learning and deep learning models.\n", "\n", "TensorFlow provides a comprehensive ecosystem of tools, libraries, and resources that make it easier for developers to create and deploy AI models. It offers a flexible architecture that allows for efficient computation on both CPUs and GPUs, enabling faster training and inference.\n", "\n", "One of the key features of TensorFlow is its ability to construct and execute computational graphs. These graphs represent the flow of data through a series of mathematical operations, making it easier to visualize and understand the model's structure. TensorFlow also supports automatic differentiation, which simplifies the process of calculating gradients for training neural networks.\n", "\n", "Moreover, TensorFlow has a vast community of users and contributors, which means there is extensive documentation, tutorials, and pre-trained models available. This makes it easier for developers to get started and leverage the collective knowledge of the community.\n", "\n", "Overall, TensorFlow is a powerful and versatile AI framework that has been widely adopted in various domains, including computer vision, natural language processing, and reinforcement learning. Its flexibility, scalability, and extensive community support make it a popular choice for both researchers and practitioners in the field of artificial intelligence.\n" ] } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "ET_l06LiojaN" }, "execution_count": null, "outputs": [] } ] }