{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyO9EXKHngvJa9fUydE3Tlen",
"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": [
""
]
},
{
"cell_type": "markdown",
"source": [
"# Install Packages and Setup Variables"
],
"metadata": {
"id": "v9bpz99INAc1"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "BeuFJKlj9jKz",
"outputId": "a14a78f4-e43e-4aef-bc69-4ced559df34e"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m226.7/226.7 kB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m52.3/52.3 kB\u001b[0m \u001b[31m3.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.8/1.8 MB\u001b[0m \u001b[31m9.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m15.4/15.4 MB\u001b[0m \u001b[31m26.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m21.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m4.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.1/3.1 MB\u001b[0m \u001b[31m35.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m2.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m136.0/136.0 kB\u001b[0m \u001b[31m10.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.9/3.9 MB\u001b[0m \u001b[31m27.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m290.4/290.4 kB\u001b[0m \u001b[31m22.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m30.8/30.8 MB\u001b[0m \u001b[31m32.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.4/49.4 kB\u001b[0m \u001b[31m2.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h"
]
}
],
"source": [
"!pip install -q llama-index==0.10.30 openai==1.12.0 cohere==4.47 tiktoken==0.6.0"
]
},
{
"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\"] = \"\""
],
"metadata": {
"id": "XuzgSNqcABpV"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Load Dataset"
],
"metadata": {
"id": "f5eV5EnvNCMM"
}
},
{
"cell_type": "markdown",
"source": [
"## Download"
],
"metadata": {
"id": "q-7mRQ-mNJlm"
}
},
{
"cell_type": "markdown",
"source": [
"The dataset includes several articles from the TowardsAI blog, which provide an in-depth explanation of the LLaMA2 model."
],
"metadata": {
"id": "3PsdOdMUNmEi"
}
},
{
"cell_type": "code",
"source": [
"!wget https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/mini-llama-articles.csv"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3ImRCP7pACaI",
"outputId": "c782f06a-5fcb-4134-e197-e2a9c3193ce9"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"--2024-04-09 18:54:34-- https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/mini-llama-articles.csv\n",
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...\n",
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 173646 (170K) [text/plain]\n",
"Saving to: ‘mini-llama-articles.csv’\n",
"\n",
"mini-llama-articles 100%[===================>] 169.58K --.-KB/s in 0.09s \n",
"\n",
"2024-04-09 18:54:35 (1.89 MB/s) - ‘mini-llama-articles.csv’ saved [173646/173646]\n",
"\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Read File"
],
"metadata": {
"id": "bZZLK_wyEc-L"
}
},
{
"cell_type": "code",
"source": [
"import csv\n",
"\n",
"rows = []\n",
"\n",
"# Load the CSV file\n",
"with open(\"./mini-llama-articles.csv\", mode=\"r\", encoding=\"utf-8\") as file:\n",
" csv_reader = csv.reader(file)\n",
"\n",
" for idx, row in enumerate( csv_reader ):\n",
" if idx == 0: continue; # Skip header row\n",
" rows.append( row )\n",
"\n",
"# The number of characters in the dataset.\n",
"print( \"number of articles:\", len( rows ) )"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "miUqycqAEfr7",
"outputId": "911985c6-6884-48ff-fa24-869d44a1a012"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"number of articles: 14\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Generate Embedding"
],
"metadata": {
"id": "f86yksB9K571"
}
},
{
"cell_type": "code",
"source": [
"from llama_index.core import Document\n",
"\n",
"# Convert the texts to Document objects so the LlamaIndex framework can process them.\n",
"documents = [Document(text=row[1]) for row in rows]"
],
"metadata": {
"id": "iXrr5-tnEfm9"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from llama_index.core import VectorStoreIndex\n",
"from llama_index.core.node_parser import SentenceSplitter\n",
"\n",
"# Build index / generate embeddings using OpenAI.\n",
"index = VectorStoreIndex.from_documents(\n",
" documents,\n",
" transformations=[SentenceSplitter(chunk_size=768, chunk_overlap=64)],\n",
")"
],
"metadata": {
"id": "Bsa7Q-DoNWBk"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Save the generated embeddings.\n",
"# index.storage_context.persist(persist_dir=\"indexes\")"
],
"metadata": {
"id": "xxB0A9ZYM-OD"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Query Dataset"
],
"metadata": {
"id": "3DoUxd8KK--Q"
}
},
{
"cell_type": "code",
"source": [
"# Define a query engine that is responsible for retrieving related pieces of text,\n",
"# and using a LLM to formulate the final answer.\n",
"query_engine = index.as_query_engine()"
],
"metadata": {
"id": "bUaNH97dEfh9"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"response = query_engine.query(\n",
" \"How many parameters LLaMA2 model has?\"\n",
")\n",
"print(response)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "KHK4V_GRR6ZG",
"outputId": "8d656836-622a-4261-e24a-9cadf857b376"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"The Llama 2 model has 7 billion, 13 billion, 34 billion, and 70 billion parameters.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"response = query_engine.query(\n",
" \"When will Llama3 will be released?\"\n",
")\n",
"print(response)"
],
"metadata": {
"id": "S-BmyTBbNd9y",
"outputId": "c6a4ec79-7555-4b4d-f212-0b5864c7bded",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"The release date for Llama3 is not provided in the given context information.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Test with smaller chunk size\n",
"# transformations=[SentenceSplitter(chunk_size=512, chunk_overlap=20)]\n",
"\n",
"response = query_engine.query(\n",
" \"How many parameters LLaMA2 model has?\"\n",
")\n",
"print(response)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "tEgFx_aeFS5e",
"outputId": "0353f9e4-0f63-4739-eb5b-717bf19572ef"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"The LLaMA2 model has a range of 7 billion to 65 billion parameters.\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "oZt_sG86RwZ3"
},
"execution_count": null,
"outputs": []
}
]
}