File size: 28,961 Bytes
15ad24d |
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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://colab.research.google.com/github/vanderbilt-data-science/lo-achievement/blob/main/instructor_vector_store_creator.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Creating a Shared Vector Store (for Instructors)\n",
"\n",
"This notebook is for instructors to create a *vector store* which contains all of the information necessary for students to generate their own self-study materials using large language models. It is expected that instructors who will use this notebook know how to run and interact with a Jupyter Notebook, specifically on Google Colab.\n",
"\n",
":::{.callout-info}\n",
"On Colab, there may be a pop-up saying 'Warning: This notebook was not authored by Google'. In that case, click 'Run anyways'. If you started this notebook from the Vanderbilt Data Science github, then you can trust the code in this notebook.\n",
":::"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setting Up API Access\n",
"Much of the following code rely on certain *APIs* (application programming interfaces) which have limited access. You will need to get an *API key* for each of those services which will be inserted into the code to let the service know you are an authorized user."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### OpenAI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, you will need an **OpenAI API key**. To do this:\n",
"1. Visit [platform.openai.com/account/api-keys](https://platform.openai.com/account/api-keys) and sign up for an account.\n",
"2. Click 'Create a secret API key', and give it any name you want.\n",
"3. Copy the newly created key, either by right-clicking and pressing 'Copy' or using the keyboard shortcut -- Ctrl+C on Windows, Cmd+C on a Mac.\n",
"\n",
"Run the following code cell. You'll see a blank text box pop up -- paste your API key there (using the shortcut Ctrl+V on Windows, or Cmd+V if you are using a Mac) and press Enter."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"OPENAI_API_KEY = getpass(\"OpenAI API key: \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### DeepLake\n",
"\n",
"Next, you will need to input a **DeepLake API key**, found in the DeepLake dashboard at [app.activeloop.ai](https://app.activeloop.ai).\n",
"\n",
"1. Click the link above and create an account.\n",
"2. After making an account, you will be prompted to set a username. Once you have set your username, copy it, run the code below, paste the username into the text box, and press Enter. (This username will be shared with students.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"DEEPLAKE_USERNAME = input(\"DeepLake username: \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. You should then be on the DeepLake dashboard. At the top, click 'Create API token'. You should see an empty table with the columns 'Name', 'Expiration date', and 'Token'.\n",
"4. Click the 'Create API token' button  at the right of the page, choose a name for the token, then click 'Create API token'. (You do not need to change the expiration date.)\n",
"5. Afterwards, you should see the table look something like this:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"6. Click the two overlaid squares  to copy the API key; then run the code below and paste it into the input text box and press Enter."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"os.environ['ACTIVELOOP_TOKEN'] = getpass(\"DeepLake API key: \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, pick a name for your dataset. It doesn't matter what this is, but keep in mind that it will be shared with the students."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dataset_name = input(\"Enter a name for your dataset: \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Processing The Document(s)\n",
"\n",
"In this part, you will upload the documents you want the students / model to reference; the embeddings will be created from those documents.\n",
"\n",
"**Note: The embeddings of all the documents you share will be publicly available. Do not use this for any documents you want to keep private.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, upload your documents to Google Colab. To do this:\n",
"1. Click on the 'file' icon  at the bottom of the sidebar to the left of these instructions.\n",
"2. Click on the 'upload file' icon  on the left of the Files toolbar.\n",
"3. Select all of the files you want to upload, then click 'Open'.\n",
"4. A warning should pop up. Click 'OK' to continue.\n",
"5. Wait until the spinning circle in the bottom of the 'Files' section disappears. This means that all of the files have been uploaded."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Adding YouTube Videos / Websites\n",
"If you have any websites or YouTube videos which also contain content which you want to put into your data lake, paste those links one at a time into the text box below, pressing 'Enter' after each one. Once you have entered all the links, press 'Enter' without typing anything to finish execution of the code cell.\n",
"\n",
"If you have no URLs to add, just click on the box and press 'Enter' without typing anything."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"url_list = []\n",
"while (url := input(\"Enter a YouTube / website link: \")): url_list.append(url)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Model for embeddings\n",
"\n",
"Below, you can choose a different model which will be used to create the embeddings. At the current time, only OpenAI models are supported. If you're not sure, the following setting should suffice."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model_name = 'text-embedding-ada-002'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Embedding & Database Creation\n",
"\n",
"Now that you've made all of the relevant settings, click the \"Run\" arrow next to this code block, or select this cell and then click \"Run This Cell and All Below\" or \"Run All Below\". This will automatically execute the rest of the code so that your database can be created from your specifications.\n",
"\n",
"You can ignore any warnings that pop up, but if the code stops execution, read the error. If you cannot fix it, please contact the developer."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Library download and installation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# run this code if you're using Google Colab or don't have these packages installed in your computing environment\n",
"#! pip install git+https://<token>@github.com/vanderbilt-data-science/lo-achievement.git\n",
"#! pip install deeplake"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# basic libraries\n",
"import os\n",
"from getpass import getpass\n",
"from IPython.display import display, Markdown\n",
"\n",
"# libraries from our package\n",
"from ai_classroom_suite.PromptInteractionBase import *\n",
"from ai_classroom_suite.MediaVectorStores import *\n",
"\n",
"# from langchain\n",
"import deeplake\n",
"from langchain.vectorstores import DeepLake\n",
"from langchain.embeddings import OpenAIEmbeddings"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#setup OpenAI API key\n",
"os.environ[\"OPENAI_API_KEY\"] = OPENAI_API_KEY\n",
"openai.api_key = OPENAI_API_KEY"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# get transcripts from youtube URLs\n",
"yt_docs, yt_save_path = get_website_youtube_text_file(url_list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, we'll create the embeddings and the vector store from the transcripts of the YouTube videos. Make sure that all your documents are shown in the output from the previous code cell, then continue execution."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create document segments\n",
"doc_segments = rawtext_to_doc_split(yt_docs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make sure that all of your documents are shown in the output from the previous code cell, then continue execution."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create embeddings\n",
"embeddings = OpenAIEmbeddings(model=model_name)\n",
"\n",
"### Dataset Creation ###\n",
"dataset_path = f\"hub://{DEEPLAKE_USERNAME}/{dataset_name}\"\n",
"db = DeepLake.from_documents(all_document_segments, dataset_path=dataset_path,\n",
" embedding=embeddings, public=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sharing With Students"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(Markdown(f'''To let students access the repository, give them the following URL:\n",
"\n",
"`{dataset_path}`'''))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Distribute the URL above to students. They will copy and paste it into the LLM learning application, which then allows their models to use all of the documents you uploaded as reference sources when responding to or creating questions."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "python3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|