File size: 7,519 Bytes
4adb315
 
 
 
 
 
d2e8f84
4adb315
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2e8f84
4adb315
 
d2e8f84
 
 
 
 
 
 
 
 
4adb315
 
d2e8f84
4adb315
 
 
 
 
 
 
 
 
 
 
 
 
d2e8f84
4adb315
 
 
 
 
d2e8f84
4adb315
 
d2e8f84
 
 
 
 
 
 
 
 
4adb315
 
 
 
 
d2e8f84
 
4adb315
 
 
 
 
d2e8f84
4adb315
 
d2e8f84
 
 
 
 
 
 
 
 
4adb315
 
 
d2e8f84
 
4adb315
 
 
 
 
 
d2e8f84
4adb315
 
 
 
 
d2e8f84
4adb315
 
d2e8f84
4adb315
 
 
 
 
d2e8f84
4adb315
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2e8f84
4adb315
 
 
 
 
d2e8f84
4adb315
 
 
 
 
 
 
 
 
 
d2e8f84
4adb315
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2e8f84
4adb315
 
 
 
 
 
 
 
 
 
d2e8f84
4adb315
 
 
 
 
 
 
 
 
 
 
d2e8f84
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
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyMWsO+T66P1RZEeDMmjXjcm",
      "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"
      },
      "outputs": [],
      "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\"] = \"<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 = ask_ai_tutor( QUESTION_AI )\n",
        "print( res )"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "W_dbwURpufR7",
        "outputId": "196c4c2f-9b7f-4302-a6e5-3ba2b8cbb37e"
      },
      "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 = ask_ai_tutor( QUESTION_NOT_AI )\n",
        "print( res )"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "37YuVJQquhpN",
        "outputId": "17c4b543-65fe-4cfb-9020-2d44bfef2eac"
      },
      "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"
          ]
        }
      ]
    }
  ]
}