File size: 17,485 Bytes
a2a9a44 84f8c13 a2a9a44 9adb76c a2a9a44 9adb76c a2a9a44 84f8c13 a2a9a44 84f8c13 a2a9a44 84f8c13 a2a9a44 9adb76c 84f8c13 a2a9a44 84f8c13 a2a9a44 84f8c13 a2a9a44 84f8c13 a2a9a44 84f8c13 a2a9a44 84f8c13 a2a9a44 9adb76c 84f8c13 a2a9a44 84f8c13 a2a9a44 3e7bb9e a2a9a44 84f8c13 a2a9a44 84f8c13 a2a9a44 84f8c13 a2a9a44 84f8c13 a2a9a44 84f8c13 a2a9a44 84f8c13 a4ba306 84f8c13 a2a9a44 84f8c13 a2a9a44 9adb76c a2a9a44 84f8c13 a2a9a44 84f8c13 a2a9a44 84f8c13 a2a9a44 84f8c13 a2a9a44 84f8c13 a2a9a44 9adb76c a2a9a44 9adb76c a2a9a44 9adb76c a2a9a44 84f8c13 a2a9a44 9adb76c a2a9a44 9adb76c a2a9a44 84f8c13 e0aadb4 84f8c13 a4ba306 84f8c13 e0aadb4 a2a9a44 9adb76c a2a9a44 9adb76c a2a9a44 e0aadb4 a2a9a44 9adb76c a2a9a44 9adb76c a2a9a44 9adb76c a2a9a44 9adb76c a2a9a44 e0aadb4 a2a9a44 84f8c13 a2a9a44 |
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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Create AI-Tutor vector database"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"\n",
"load_dotenv(\"../.env\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import chromadb\n",
"\n",
"# create client and a new collection\n",
"# chromadb.EphemeralClient saves data in-memory.\n",
"chroma_client = chromadb.PersistentClient(path=\"./ai-tutor-db\")\n",
"chroma_collection = chroma_client.create_collection(\"ai-tutor-db\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.vector_stores.chroma import ChromaVectorStore\n",
"from llama_index.core import StorageContext\n",
"\n",
"vector_store = ChromaVectorStore(chroma_collection=chroma_collection)\n",
"\n",
"# Define a storage context object using the created vector store.\n",
"storage_context = StorageContext.from_defaults(vector_store=vector_store)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"from llama_index.core.schema import TextNode\n",
"\n",
"def load_jsonl_create_nodes(filepath):\n",
" nodes = [] # List to hold the created node objects\n",
" with open(filepath, \"r\") as file:\n",
" for line in file:\n",
" # Load each line as a JSON object\n",
" json_obj = json.loads(line)\n",
" # Extract required information\n",
" title = json_obj.get(\"title\")\n",
" url = json_obj.get(\"url\")\n",
" content = json_obj.get(\"content\")\n",
" source = json_obj.get(\"source\")\n",
" # Create a TextNode object and append to the list\n",
" node = TextNode(\n",
" text=content,\n",
" metadata={\"title\": title, \"url\": url, \"source\": source},\n",
" excluded_embed_metadata_keys=[\"title\", \"url\", \"source\"],\n",
" excluded_llm_metadata_keys=[\"title\", \"url\", \"source\"],\n",
" )\n",
" nodes.append(node)\n",
" return nodes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"filepath = \"../combined_data.jsonl\"\n",
"nodes = load_jsonl_create_nodes(filepath)\n",
"\n",
"print(f\"Loaded {len(nodes)} nodes/chunks from the JSONL file\\n \")\n",
"\n",
"node = nodes[0]\n",
"print(f\"ID: {node.id_} \\nText: {node.text}, \\nMetadata: {node.metadata}\")\n",
"\n",
"print(\"\\n\")\n",
"\n",
"node = nodes[-10000]\n",
"print(f\"ID: {node.id_} \\nText: {node.text}, \\nMetadata: {node.metadata}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# # Create the pipeline to apply the transformation on each chunk,\n",
"# # and store the transformed text in the chroma vector store.\n",
"# pipeline = IngestionPipeline(\n",
"# transformations=[\n",
"# text_splitter,\n",
"# QuestionsAnsweredExtractor(questions=3, llm=llm),\n",
"# SummaryExtractor(summaries=[\"prev\", \"self\"], llm=llm),\n",
"# KeywordExtractor(keywords=10, llm=llm),\n",
"# OpenAIEmbedding(),\n",
"# ],\n",
"# vector_store=vector_store\n",
"# )\n",
"\n",
"# nodes = pipeline.run(documents=documents, show_progress=True);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.embeddings.openai import OpenAIEmbedding\n",
"from llama_index.core import VectorStoreIndex\n",
"\n",
"# embeds = OpenAIEmbedding(model=\"text-embedding-3-small\", mode=\"similarity\")\n",
"# embeds = OpenAIEmbedding(model=\"text-embedding-3-large\", mode=\"similarity\")\n",
"embeds = OpenAIEmbedding(model=\"text-embedding-3-large\", mode=\"text_search\")\n",
"# embeds = OpenAIEmbedding(model=\"text-embedding-ada-002\", mode=\"similarity\")\n",
"\n",
"# Build index / generate embeddings using OpenAI.\n",
"index = VectorStoreIndex(nodes=nodes, show_progress=True, use_async=True, storage_context=storage_context, embed_model=embeds, insert_batch_size=3000,)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.llms.openai import OpenAI\n",
"\n",
"llm = OpenAI(temperature=0, model=\"gpt-3.5-turbo\", max_tokens=None)\n",
"query_engine = index.as_query_engine(llm=llm, similarity_top_k=5, embed_model=embeds)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"res = query_engine.query(\"What is the LLaMa model?\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"res.response"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for src in res.source_nodes:\n",
" print(\"Node ID\\t\", src.node_id)\n",
" print(\"Title\\t\", src.metadata['title'])\n",
" print(\"Text\\t\", src.text)\n",
" print(\"Score\\t\", src.score)\n",
" print(\"Metadata\\t\", src.metadata) \n",
" print(\"-_\"*20)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Load DB from disk"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:chromadb.telemetry.product.posthog:Anonymized telemetry enabled. See https://docs.trychroma.com/telemetry for more information.\n",
"INFO:chromadb.api.segment:Collection ai-tutor-db is not created.\n"
]
}
],
"source": [
"import logging\n",
"\n",
"logger = logging.getLogger(__name__)\n",
"logging.basicConfig(level=logging.INFO)\n",
"\n",
"\n",
"import chromadb\n",
"from llama_index.vector_stores.chroma import ChromaVectorStore\n",
"# Create your index\n",
"db2 = chromadb.PersistentClient(path=\"./ai-tutor-db\")\n",
"chroma_collection = db2.get_or_create_collection(\"ai-tutor-db\")\n",
"vector_store = ChromaVectorStore(chroma_collection=chroma_collection)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Create your index\n",
"from llama_index.core import VectorStoreIndex\n",
"index = VectorStoreIndex.from_vector_store(vector_store=vector_store)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.embeddings.openai import OpenAIEmbedding\n",
"from llama_index.llms.openai import OpenAI\n",
"from llama_index.core.vector_stores import (\n",
" ExactMatchFilter,\n",
" MetadataFilters,\n",
" MetadataFilter,\n",
" FilterOperator,\n",
" FilterCondition,\n",
")\n",
"\n",
"filters = MetadataFilters(\n",
" filters=[\n",
" MetadataFilter(key=\"source\", value=\"lanchain_course\"),\n",
" MetadataFilter(key=\"source\", value=\"langchain_docs\"),\n",
" ],\n",
" condition=FilterCondition.OR,\n",
")\n",
"\n",
"llm = OpenAI(temperature=0, model=\"gpt-3.5-turbo\", max_tokens=None)\n",
"embeds = OpenAIEmbedding(model=\"text-embedding-3-large\", mode=\"text_search\")\n",
"# query_engine = index.as_query_engine(\n",
"# llm=llm, similarity_top_k=5, embed_model=embeds, verbose=True, streaming=True, filters=filters\n",
"# )\n",
"query_engine = index.as_query_engine(\n",
" llm=llm, similarity_top_k=5, embed_model=embeds, verbose=True,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
]
}
],
"source": [
"res = query_engine.query(\"What is the LLama model?\")\n",
"\n",
"# history = \"\" \n",
"# for token in res.response_gen:\n",
"# history += token\n",
"# print(history)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'The LLama model is a family of large language models (LLMs) released by Meta AI, with different model sizes ranging from 7 billion to 65 billion parameters in the first version. The developers reported that the 13 billion parameter model outperformed larger models like GPT-3 and was competitive with state-of-the-art models like PaLM and Chinchilla. The model weights were released to the research community under a noncommercial license, but they were leaked to the public shortly after.'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res.response"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Node ID\t 6f8c13d8-1458-444c-857b-c1dc11d7f134\n",
"Source\t wikipedia\n",
"Title\t LLaMA\n",
"Text\t LLaMA LLaMA (Large Language Model Meta AI) is a family of large language models (LLMs), released by Meta AI starting in February 2023. For the first version of LLaMa, four model sizes were trained: 7, 13, 33 and 65 billion parameters. LLaMA's developers reported that the 13B parameter model's performance on most NLP benchmarks exceeded that of the much larger GPT-3 (with 175B parameters) and that the largest model was competitive with state of the art models such as PaLM and Chinchilla. Whereas the most powerful LLMs have generally been accessible only through limited APIs (if at all), Meta released LLaMA's model weights to the research community under a noncommercial license. Within a week of LLaMA's release, its weights were leaked to the public on 4chan via BitTorrent. In July 2023, Meta released several models as Llama 2, using 7, 13 and 70 billion parameters.\n",
"Score\t 0.5133216393307418\n",
"-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n",
"Node ID\t 7226ba39-fd90-4790-879f-9f6af7b4ac6d\n",
"Source\t towards_ai\n",
"Title\t Fine-Tuning a Llama-2 7B Model for Python Code Generation\n",
"Text\t New Llama-2 model In mid-July, Meta released its new family of pre-trained and finetuned models called Llama-2, with an open source and commercial character to facilitate its use and expansion. The base model was released with a chat version and sizes 7B, 13B, and 70B. Together with the models, the corresponding papers were published describing their characteristics and relevant points of the learning process, which provide very interesting information on the subject. For pre-training, 40% more tokens were used, reaching 2T, the context length was doubled and the grouped-query attention (GQA) technique was applied to speed up inference on the heavier 70B model. On the standard transformer architecture, RMSNorm normalization, SwiGLU activation, and rotatory positional embedding are used, the context length reaches 4096 tokens, and an Adam optimizer is applied with a cosine learning rate schedule, a weight decay of 0.1 and gradient clipping. \n",
"Score\t 0.49851718223076963\n",
"-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n",
"Node ID\t a654d9c1-fa81-4ad8-b16a-06a49f145636\n",
"Source\t hf_transformers\n",
"Title\t Overview\n",
"Text\t The Open-Llama model was proposed in Open-Llama project by community developer s-JoL.\n",
"The model is mainly based on LLaMA with some modifications, incorporating memory-efficient attention from Xformers, stable embedding from Bloom, and shared input-output embedding from PaLM.\n",
"And the model is pre-trained on both Chinese and English, which gives it better performance on Chinese language tasks.\n",
"This model was contributed by s-JoL.\n",
"The original code can be found Open-Llama.\n",
"Checkpoint and usage can be found at s-JoL/Open-Llama-V1.\n",
"Score\t 0.4888355341806359\n",
"-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n",
"Node ID\t 846101f6-d9c5-4356-b569-c4383fe9b481\n",
"Source\t towards_ai\n",
"Title\t Meta's Llama 2: Revolutionizing Open Source Language Models for Commercial Use\n",
"Text\t II. Llama 2 Model Flavors Llama 2 is available in four different model sizes: 7 billion, 13 billion, 34 billion, and 70 billion parameters. While 7B, 13B, and 70B have already been released, the 34B model is still awaited. The pretrained variant, trained on a whopping 2 trillion tokens, boasts a context window of 4096 tokens, twice the size of its predecessor Llama 1. Meta also released a Llama 2 fine-tuned model for chat applications that was trained on over 1 million human annotations. Such extensive training comes at a cost, with the 70B model taking a staggering 1720320 GPU hours to train. The context window's length determines the amount of content the model can process at once, making Llama 2 a powerful language model in terms of scale and efficiency. \n",
"Score\t 0.4818213806198265\n",
"-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n",
"Node ID\t bfffeff1-76d5-4ad4-9988-38be59ad9562\n",
"Source\t hf_transformers\n",
"Title\t Overview\n",
"Text\t d 34B parameters each. All models are trained on sequences of 16k tokens and show improvements on inputs with up to 100k tokens. 7B and 13B Code Llama and Code Llama - Instruct variants support infilling based on surrounding content. Code Llama reaches state-of-the-art performance among open models on several code benchmarks, with scores of up to 53% and 55% on HumanEval and MBPP, respectively. Notably, Code Llama - Python 7B outperforms Llama 2 70B on HumanEval and MBPP, and all our models outperform every other publicly available model on MultiPL-E. We release Code Llama under a permissive license that allows for both research and commercial use.\n",
"Check out all Code Llama models here and the officially released ones in the codellama org.\n",
"The Llama2 family models, on which Code Llama is based, were trained using bfloat16, but the original inference uses float16. Let’s look at the different precisions: float32: PyTorch convention on model initialization is to load models in float32, no\n",
"Score\t 0.4799444818466049\n",
"-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n"
]
}
],
"source": [
"for src in res.source_nodes:\n",
" print(\"Node ID\\t\", src.node_id)\n",
" print(\"Source\\t\", src.metadata['source'])\n",
" print(\"Title\\t\", src.metadata['title'])\n",
" print(\"Text\\t\", src.text)\n",
" print(\"Score\\t\", src.score)\n",
" print(\"-_\"*20)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import Markdown, display\n",
"# define prompt viewing function\n",
"def display_prompt_dict(prompts_dict):\n",
" for k, p in prompts_dict.items():\n",
" text_md = f\"**Prompt Key**: {k}<br>\" f\"**Text:** <br>\"\n",
" display(Markdown(text_md))\n",
" print(p.get_template())\n",
" display(Markdown(\"<br><br>\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"prompts_dict = query_engine.get_prompts()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display_prompt_dict(prompts_dict)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "env",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|