Spaces:
Sleeping
Sleeping
Created using Colaboratory
Browse files- notebooks/Basic_Tutor.ipynb +175 -0
notebooks/Basic_Tutor.ipynb
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"nbformat": 4,
|
3 |
+
"nbformat_minor": 0,
|
4 |
+
"metadata": {
|
5 |
+
"colab": {
|
6 |
+
"provenance": [],
|
7 |
+
"authorship_tag": "ABX9TyO0gBQAWbHw+/wDj36pMFci",
|
8 |
+
"include_colab_link": true
|
9 |
+
},
|
10 |
+
"kernelspec": {
|
11 |
+
"name": "python3",
|
12 |
+
"display_name": "Python 3"
|
13 |
+
},
|
14 |
+
"language_info": {
|
15 |
+
"name": "python"
|
16 |
+
}
|
17 |
+
},
|
18 |
+
"cells": [
|
19 |
+
{
|
20 |
+
"cell_type": "markdown",
|
21 |
+
"metadata": {
|
22 |
+
"id": "view-in-github",
|
23 |
+
"colab_type": "text"
|
24 |
+
},
|
25 |
+
"source": [
|
26 |
+
"<a href=\"https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Basic_Tutor.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
27 |
+
]
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"cell_type": "code",
|
31 |
+
"execution_count": 4,
|
32 |
+
"metadata": {
|
33 |
+
"id": "o4Q0N2omkAoZ"
|
34 |
+
},
|
35 |
+
"outputs": [],
|
36 |
+
"source": [
|
37 |
+
"!pip install -q openai==1.6.0 cohere==4.39 tiktoken==0.5.2"
|
38 |
+
]
|
39 |
+
},
|
40 |
+
{
|
41 |
+
"cell_type": "code",
|
42 |
+
"source": [
|
43 |
+
"import os\n",
|
44 |
+
"\n",
|
45 |
+
"os.environ[\"OPENAI_API_KEY\"] = \"<YOUR_OPENAI_KEY>\""
|
46 |
+
],
|
47 |
+
"metadata": {
|
48 |
+
"id": "xxK7EAAvr2aT"
|
49 |
+
},
|
50 |
+
"execution_count": 5,
|
51 |
+
"outputs": []
|
52 |
+
},
|
53 |
+
{
|
54 |
+
"cell_type": "code",
|
55 |
+
"source": [
|
56 |
+
"from openai import OpenAI\n",
|
57 |
+
"\n",
|
58 |
+
"client = OpenAI()"
|
59 |
+
],
|
60 |
+
"metadata": {
|
61 |
+
"id": "La8hdWqJkFkh"
|
62 |
+
},
|
63 |
+
"execution_count": 7,
|
64 |
+
"outputs": []
|
65 |
+
},
|
66 |
+
{
|
67 |
+
"cell_type": "code",
|
68 |
+
"source": [
|
69 |
+
"QUESTION_AI = \"List a number of famous artificial intelligence frameworks?\"\n",
|
70 |
+
"QUESTION_NOT_AI = \"What is the name of the highest mountain in the world and its height?\""
|
71 |
+
],
|
72 |
+
"metadata": {
|
73 |
+
"id": "7JRrn0uIsBfg"
|
74 |
+
},
|
75 |
+
"execution_count": 9,
|
76 |
+
"outputs": []
|
77 |
+
},
|
78 |
+
{
|
79 |
+
"cell_type": "code",
|
80 |
+
"source": [
|
81 |
+
"def ask_ai_tutor(question):\n",
|
82 |
+
" try:\n",
|
83 |
+
" # Formulating the system prompt\n",
|
84 |
+
" system_prompt = (\n",
|
85 |
+
" \"You are an AI tutor specialized in answering artificial intelligence-related questions. \"\n",
|
86 |
+
" \"Only answer AI-related question, else say that you cannot answer this question.\"\n",
|
87 |
+
" )\n",
|
88 |
+
"\n",
|
89 |
+
" # Combining the system prompt with the user's question\n",
|
90 |
+
" prompt = f\"Please provide an informative and accurate answer to the following question.\\nQuestion: {question}\\nAnswer:\"\n",
|
91 |
+
"\n",
|
92 |
+
" # Call the OpenAI API\n",
|
93 |
+
" response = client.chat.completions.create(\n",
|
94 |
+
" model='gpt-3.5-turbo-16k',\n",
|
95 |
+
" temperature=0.0,\n",
|
96 |
+
" messages=[\n",
|
97 |
+
" {\"role\": \"system\", \"content\": system_prompt},\n",
|
98 |
+
" {\"role\": \"user\", \"content\": prompt}\n",
|
99 |
+
" ]\n",
|
100 |
+
" )\n",
|
101 |
+
"\n",
|
102 |
+
" # Return the AI's response\n",
|
103 |
+
" return response.choices[0].message.content.strip()\n",
|
104 |
+
"\n",
|
105 |
+
" except Exception as e:\n",
|
106 |
+
" return f\"An error occurred: {e}\""
|
107 |
+
],
|
108 |
+
"metadata": {
|
109 |
+
"id": "CcP26IauuBuV"
|
110 |
+
},
|
111 |
+
"execution_count": 18,
|
112 |
+
"outputs": []
|
113 |
+
},
|
114 |
+
{
|
115 |
+
"cell_type": "code",
|
116 |
+
"source": [
|
117 |
+
"res = ask_ai_tutor( QUESTION_AI )\n",
|
118 |
+
"print( res )"
|
119 |
+
],
|
120 |
+
"metadata": {
|
121 |
+
"colab": {
|
122 |
+
"base_uri": "https://localhost:8080/"
|
123 |
+
},
|
124 |
+
"id": "W_dbwURpufR7",
|
125 |
+
"outputId": "196c4c2f-9b7f-4302-a6e5-3ba2b8cbb37e"
|
126 |
+
},
|
127 |
+
"execution_count": 19,
|
128 |
+
"outputs": [
|
129 |
+
{
|
130 |
+
"output_type": "stream",
|
131 |
+
"name": "stdout",
|
132 |
+
"text": [
|
133 |
+
"Sure! There are several famous artificial intelligence frameworks that are widely used in the field. Some of the popular ones include:\n",
|
134 |
+
"\n",
|
135 |
+
"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",
|
136 |
+
"\n",
|
137 |
+
"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",
|
138 |
+
"\n",
|
139 |
+
"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",
|
140 |
+
"\n",
|
141 |
+
"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",
|
142 |
+
"\n",
|
143 |
+
"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",
|
144 |
+
"\n",
|
145 |
+
"These are just a few examples of famous AI frameworks, and there are many others available depending on specific needs and preferences.\n"
|
146 |
+
]
|
147 |
+
}
|
148 |
+
]
|
149 |
+
},
|
150 |
+
{
|
151 |
+
"cell_type": "code",
|
152 |
+
"source": [
|
153 |
+
"res = ask_ai_tutor( QUESTION_NOT_AI )\n",
|
154 |
+
"print( res )"
|
155 |
+
],
|
156 |
+
"metadata": {
|
157 |
+
"colab": {
|
158 |
+
"base_uri": "https://localhost:8080/"
|
159 |
+
},
|
160 |
+
"id": "37YuVJQquhpN",
|
161 |
+
"outputId": "17c4b543-65fe-4cfb-9020-2d44bfef2eac"
|
162 |
+
},
|
163 |
+
"execution_count": 20,
|
164 |
+
"outputs": [
|
165 |
+
{
|
166 |
+
"output_type": "stream",
|
167 |
+
"name": "stdout",
|
168 |
+
"text": [
|
169 |
+
"I'm sorry, but I cannot answer that question as it is not related to artificial intelligence.\n"
|
170 |
+
]
|
171 |
+
}
|
172 |
+
]
|
173 |
+
}
|
174 |
+
]
|
175 |
+
}
|