File size: 13,632 Bytes
4adb315 c203673 7e92a44 c203673 4adb315 c203673 4adb315 d2e8f84 4adb315 d2e8f84 08b8fbf c203673 d2e8f84 4adb315 7e92a44 4adb315 c203673 4adb315 c203673 4adb315 7e92a44 4adb315 d2e8f84 4adb315 c203673 7e92a44 c203673 4adb315 d2e8f84 08b8fbf c203673 d2e8f84 4adb315 d2e8f84 4adb315 c203673 7e92a44 c203673 4adb315 d2e8f84 08b8fbf c203673 d2e8f84 4adb315 d2e8f84 4adb315 c203673 7e92a44 c203673 4adb315 c203673 4adb315 d2e8f84 4adb315 d2e8f84 4adb315 c203673 4adb315 c203673 7e92a44 c203673 4adb315 c203673 4adb315 c203673 4adb315 7e92a44 4adb315 08b8fbf c203673 4adb315 c203673 4adb315 c203673 4adb315 7e92a44 4adb315 08b8fbf c203673 4adb315 c203673 08b8fbf c203673 08b8fbf c203673 7e92a44 c203673 4adb315 08b8fbf c203673 08b8fbf c203673 4adb315 c203673 |
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 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOUuEM41HPKH6uCJFqocvSD",
"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": [
"<a href=\"https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/01-Basic_Tutor.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Install Packages and Setup Variables"
],
"metadata": {
"id": "DMXyyXD0xix9"
}
},
{
"cell_type": "code",
"execution_count": null,
"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.12.0 cohere==4.47 tiktoken==0.6.0"
]
},
{
"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\"] = \"<YOUR_OPENAI_KEY>\""
],
"metadata": {
"id": "xxK7EAAvr2aT"
},
"execution_count": null,
"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": null,
"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": null,
"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": null,
"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": null,
"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": null,
"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": null,
"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": []
}
]
} |