{
"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}
\" f\"**Text:**
\"\n",
" display(Markdown(text_md))\n",
" print(p.get_template())\n",
" display(Markdown(\"
\"))"
]
},
{
"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
}