AlaFalaki commited on
Commit
db48752
Β·
1 Parent(s): e55b1d2

Created using Colab

Browse files
notebooks/17-Using_LLMs_to_rank_chunks_as_the_Judge.ipynb ADDED
@@ -0,0 +1,830 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "authorship_tag": "ABX9TyMAX07BuQFwcBDYOOj62vXm",
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/17-Using_LLMs_to_rank_chunks_as_the_Judge.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": "markdown",
31
+ "source": [
32
+ "# Install Packages and Setup Variables"
33
+ ],
34
+ "metadata": {
35
+ "id": "0FbELaf7TrW7"
36
+ }
37
+ },
38
+ {
39
+ "cell_type": "code",
40
+ "execution_count": 1,
41
+ "metadata": {
42
+ "id": "Yubz8AanRRSW",
43
+ "colab": {
44
+ "base_uri": "https://localhost:8080/"
45
+ },
46
+ "outputId": "2487c4fd-0fb5-4894-ffe6-c747f4adb952"
47
+ },
48
+ "outputs": [
49
+ {
50
+ "output_type": "stream",
51
+ "name": "stdout",
52
+ "text": [
53
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m226.7/226.7 kB\u001b[0m \u001b[31m4.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
54
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.8/1.8 MB\u001b[0m \u001b[31m11.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
55
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m211.1/211.1 kB\u001b[0m \u001b[31m6.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
56
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m508.6/508.6 kB\u001b[0m \u001b[31m7.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
57
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m15.4/15.4 MB\u001b[0m \u001b[31m15.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
58
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m23.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
59
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m2.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
60
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m81.3/81.3 kB\u001b[0m \u001b[31m6.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
61
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m97.6/97.6 kB\u001b[0m \u001b[31m5.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
62
+ "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
63
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m7.4/7.4 MB\u001b[0m \u001b[31m28.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
64
+ "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
65
+ " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
66
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.4/2.4 MB\u001b[0m \u001b[31m55.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
67
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m91.8/91.8 kB\u001b[0m \u001b[31m7.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
68
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m60.8/60.8 kB\u001b[0m \u001b[31m5.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
69
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m41.3/41.3 kB\u001b[0m \u001b[31m3.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
70
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.4/5.4 MB\u001b[0m \u001b[31m56.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
71
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.8/6.8 MB\u001b[0m \u001b[31m47.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
72
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m60.1/60.1 kB\u001b[0m \u001b[31m4.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
73
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m106.1/106.1 kB\u001b[0m \u001b[31m1.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
74
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m67.3/67.3 kB\u001b[0m \u001b[31m4.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
75
+ "\u001b[?25h Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n",
76
+ " Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n",
77
+ " Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n",
78
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m698.9/698.9 kB\u001b[0m \u001b[31m55.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
79
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.6/1.6 MB\u001b[0m \u001b[31m80.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
80
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m67.6/67.6 kB\u001b[0m \u001b[31m9.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
81
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m71.9/71.9 kB\u001b[0m \u001b[31m8.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
82
+ "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
83
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m10.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
84
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m6.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
85
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m141.9/141.9 kB\u001b[0m \u001b[31m16.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
86
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m290.4/290.4 kB\u001b[0m \u001b[31m26.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
87
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m46.0/46.0 kB\u001b[0m \u001b[31m5.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
88
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.8/50.8 kB\u001b[0m \u001b[31m6.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
89
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m341.4/341.4 kB\u001b[0m \u001b[31m35.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
90
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.4/3.4 MB\u001b[0m \u001b[31m92.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
91
+ "\u001b[2K \u001b[90m━━━���━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m78.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
92
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m130.2/130.2 kB\u001b[0m \u001b[31m16.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
93
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m86.8/86.8 kB\u001b[0m \u001b[31m11.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
94
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.4/49.4 kB\u001b[0m \u001b[31m5.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
95
+ "\u001b[?25h Building wheel for tinysegmenter (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
96
+ " Building wheel for feedfinder2 (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
97
+ " Building wheel for jieba3k (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
98
+ " Building wheel for pypika (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n",
99
+ " Building wheel for sgmllib3k (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
100
+ ]
101
+ }
102
+ ],
103
+ "source": [
104
+ "!pip install -q llama-index==0.10.30 openai==1.12.0 tiktoken==0.6.0 newspaper3k==0.2.8 chromadb==0.4.21 llama-index-vector-stores-chroma==0.1.7"
105
+ ]
106
+ },
107
+ {
108
+ "cell_type": "code",
109
+ "source": [
110
+ "import os\n",
111
+ "\n",
112
+ "# Set the \"OPENAI_API_KEY\" in the Python environment. Will be used by OpenAI client later.\n",
113
+ "os.environ[\"OPENAI_API_KEY\"] = \"[OPENAI_API_KEY]\""
114
+ ],
115
+ "metadata": {
116
+ "id": "xLXFuRW-TpUu"
117
+ },
118
+ "execution_count": 2,
119
+ "outputs": []
120
+ },
121
+ {
122
+ "cell_type": "markdown",
123
+ "source": [
124
+ "# Load a Model"
125
+ ],
126
+ "metadata": {
127
+ "id": "r6GCYYqqTuMc"
128
+ }
129
+ },
130
+ {
131
+ "cell_type": "code",
132
+ "source": [
133
+ "from llama_index.llms.openai import OpenAI\n",
134
+ "\n",
135
+ "llm = OpenAI(temperature=0.9, model=\"gpt-3.5-turbo\", max_tokens=512)"
136
+ ],
137
+ "metadata": {
138
+ "id": "pupJpdZaTu5m"
139
+ },
140
+ "execution_count": 3,
141
+ "outputs": []
142
+ },
143
+ {
144
+ "cell_type": "markdown",
145
+ "source": [
146
+ "# Create a Vector Store"
147
+ ],
148
+ "metadata": {
149
+ "id": "gaKYO-KrTwsn"
150
+ }
151
+ },
152
+ {
153
+ "cell_type": "code",
154
+ "source": [
155
+ "import chromadb\n",
156
+ "\n",
157
+ "# create client and a new collection\n",
158
+ "# chromadb.EphemeralClient saves data in-memory.\n",
159
+ "chroma_client = chromadb.PersistentClient(path=\"./mini-llama-articles\")\n",
160
+ "chroma_collection = chroma_client.create_collection(\"mini-llama-articles\")"
161
+ ],
162
+ "metadata": {
163
+ "id": "npCqCZSPZKR0"
164
+ },
165
+ "execution_count": 4,
166
+ "outputs": []
167
+ },
168
+ {
169
+ "cell_type": "code",
170
+ "source": [
171
+ "from llama_index.vector_stores.chroma import ChromaVectorStore\n",
172
+ "\n",
173
+ "# Define a storage context object using the created vector database.\n",
174
+ "vector_store = ChromaVectorStore(chroma_collection=chroma_collection)"
175
+ ],
176
+ "metadata": {
177
+ "id": "dG9eKSVrZMs1"
178
+ },
179
+ "execution_count": 5,
180
+ "outputs": []
181
+ },
182
+ {
183
+ "cell_type": "markdown",
184
+ "source": [
185
+ "# Load the Dataset (CSV)"
186
+ ],
187
+ "metadata": {
188
+ "id": "HmiFENBdZMAk"
189
+ }
190
+ },
191
+ {
192
+ "cell_type": "markdown",
193
+ "source": [
194
+ "## Download"
195
+ ],
196
+ "metadata": {
197
+ "id": "X-20isiTZRIa"
198
+ }
199
+ },
200
+ {
201
+ "cell_type": "markdown",
202
+ "source": [
203
+ "The dataset includes several articles from the TowardsAI blog, which provide an in-depth explanation of the LLaMA2 model. Read the dataset as a long string."
204
+ ],
205
+ "metadata": {
206
+ "id": "-lWKX814ZURc"
207
+ }
208
+ },
209
+ {
210
+ "cell_type": "code",
211
+ "source": [
212
+ "!wget https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/mini-llama-articles.csv"
213
+ ],
214
+ "metadata": {
215
+ "id": "fmlEL849ZPrH",
216
+ "colab": {
217
+ "base_uri": "https://localhost:8080/"
218
+ },
219
+ "outputId": "63039988-ab7a-4ecf-deb0-d9510628ecb8"
220
+ },
221
+ "execution_count": 6,
222
+ "outputs": [
223
+ {
224
+ "output_type": "stream",
225
+ "name": "stdout",
226
+ "text": [
227
+ "--2024-04-30 18:37:36-- https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/mini-llama-articles.csv\n",
228
+ "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.110.133, 185.199.109.133, 185.199.111.133, ...\n",
229
+ "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443... connected.\n",
230
+ "HTTP request sent, awaiting response... 200 OK\n",
231
+ "Length: 173646 (170K) [text/plain]\n",
232
+ "Saving to: β€˜mini-llama-articles.csv’\n",
233
+ "\n",
234
+ "mini-llama-articles 100%[===================>] 169.58K --.-KB/s in 0.01s \n",
235
+ "\n",
236
+ "2024-04-30 18:37:37 (11.3 MB/s) - β€˜mini-llama-articles.csv’ saved [173646/173646]\n",
237
+ "\n"
238
+ ]
239
+ }
240
+ ]
241
+ },
242
+ {
243
+ "cell_type": "markdown",
244
+ "source": [
245
+ "# Read File"
246
+ ],
247
+ "metadata": {
248
+ "id": "r9PL_eiTZW7y"
249
+ }
250
+ },
251
+ {
252
+ "cell_type": "code",
253
+ "source": [
254
+ "import csv\n",
255
+ "\n",
256
+ "rows = []\n",
257
+ "\n",
258
+ "# Load the file as a JSON\n",
259
+ "with open(\"./mini-llama-articles.csv\", mode=\"r\", encoding=\"utf-8\") as file:\n",
260
+ " csv_reader = csv.reader(file)\n",
261
+ "\n",
262
+ " for idx, row in enumerate( csv_reader ):\n",
263
+ " if idx == 0: continue; # Skip header row\n",
264
+ " rows.append( row )\n",
265
+ "\n",
266
+ "# The number of characters in the dataset.\n",
267
+ "len( rows )"
268
+ ],
269
+ "metadata": {
270
+ "id": "x5IwXJi8ZQGh"
271
+ },
272
+ "execution_count": null,
273
+ "outputs": []
274
+ },
275
+ {
276
+ "cell_type": "markdown",
277
+ "source": [
278
+ "# Convert to Document obj"
279
+ ],
280
+ "metadata": {
281
+ "id": "ktYUZzzSZaDW"
282
+ }
283
+ },
284
+ {
285
+ "cell_type": "code",
286
+ "source": [
287
+ "from llama_index.core.schema import Document\n",
288
+ "\n",
289
+ "# Convert the chunks to Document objects so the LlamaIndex framework can process them.\n",
290
+ "documents = [Document(text=row[1], metadata={\"title\": row[0], \"url\": row[2], \"source_name\": row[3]}) for row in rows]"
291
+ ],
292
+ "metadata": {
293
+ "id": "oO10Q-UyZQEB"
294
+ },
295
+ "execution_count": null,
296
+ "outputs": []
297
+ },
298
+ {
299
+ "cell_type": "markdown",
300
+ "source": [
301
+ "# Transforming"
302
+ ],
303
+ "metadata": {
304
+ "id": "0PnovZ0tZdAT"
305
+ }
306
+ },
307
+ {
308
+ "cell_type": "code",
309
+ "source": [
310
+ "from llama_index.core.node_parser import TokenTextSplitter\n",
311
+ "\n",
312
+ "# Define the splitter object that split the text into segments with 512 tokens,\n",
313
+ "# with a 128 overlap between the segments.\n",
314
+ "text_splitter = TokenTextSplitter(\n",
315
+ " separator=\" \", chunk_size=512, chunk_overlap=128\n",
316
+ ")"
317
+ ],
318
+ "metadata": {
319
+ "id": "wzOQZH6VZQBm"
320
+ },
321
+ "execution_count": null,
322
+ "outputs": []
323
+ },
324
+ {
325
+ "cell_type": "code",
326
+ "source": [
327
+ "from llama_index.core.extractors import (\n",
328
+ " SummaryExtractor,\n",
329
+ " QuestionsAnsweredExtractor,\n",
330
+ " KeywordExtractor,\n",
331
+ ")\n",
332
+ "from llama_index.embeddings.openai import OpenAIEmbedding\n",
333
+ "from llama_index.core.ingestion import IngestionPipeline\n",
334
+ "\n",
335
+ "# Create the pipeline to apply the transformation on each chunk,\n",
336
+ "# and store the transformed text in the chroma vector store.\n",
337
+ "pipeline = IngestionPipeline(\n",
338
+ " transformations=[\n",
339
+ " text_splitter,\n",
340
+ " QuestionsAnsweredExtractor(questions=3, llm=llm),\n",
341
+ " SummaryExtractor(summaries=[\"prev\", \"self\"], llm=llm),\n",
342
+ " KeywordExtractor(keywords=10, llm=llm),\n",
343
+ " OpenAIEmbedding(),\n",
344
+ " ],\n",
345
+ " vector_store=vector_store\n",
346
+ ")\n",
347
+ "\n",
348
+ "# Run the transformation pipeline.\n",
349
+ "nodes = pipeline.run(documents=documents, show_progress=True);"
350
+ ],
351
+ "metadata": {
352
+ "id": "l6UP7M_rZeXS"
353
+ },
354
+ "execution_count": null,
355
+ "outputs": []
356
+ },
357
+ {
358
+ "cell_type": "code",
359
+ "source": [
360
+ "len( nodes )"
361
+ ],
362
+ "metadata": {
363
+ "id": "GcUUhs88ZeUs"
364
+ },
365
+ "execution_count": null,
366
+ "outputs": []
367
+ },
368
+ {
369
+ "cell_type": "code",
370
+ "source": [
371
+ "# Compress the vector store directory to a zip file to be able to download and use later.\n",
372
+ "!zip -r vectorstore.zip mini-llama-articles"
373
+ ],
374
+ "metadata": {
375
+ "id": "B_P8Cil-ZeQM"
376
+ },
377
+ "execution_count": null,
378
+ "outputs": []
379
+ },
380
+ {
381
+ "cell_type": "markdown",
382
+ "source": [
383
+ "# Load Indexes"
384
+ ],
385
+ "metadata": {
386
+ "id": "YSGHsZMMZj4E"
387
+ }
388
+ },
389
+ {
390
+ "cell_type": "markdown",
391
+ "source": [
392
+ "If you have already uploaded the zip file for the vector store checkpoint, please uncomment the code in the following cell block to extract its contents. After doing so, you will be able to load the dataset from local storage."
393
+ ],
394
+ "metadata": {
395
+ "id": "J81Yvj0AZlvK"
396
+ }
397
+ },
398
+ {
399
+ "cell_type": "code",
400
+ "source": [
401
+ "# !unzip vectorstore.zip"
402
+ ],
403
+ "metadata": {
404
+ "id": "M8iaOOGyZeNp",
405
+ "colab": {
406
+ "base_uri": "https://localhost:8080/"
407
+ },
408
+ "outputId": "6a117a0b-161a-4889-daf6-baf94ae00d2a"
409
+ },
410
+ "execution_count": 7,
411
+ "outputs": [
412
+ {
413
+ "output_type": "stream",
414
+ "name": "stdout",
415
+ "text": [
416
+ "Archive: vectorstore.zip\n",
417
+ " creating: mini-llama-articles/\n",
418
+ " creating: mini-llama-articles/a361e92f-9895-41b6-ba72-4ad38e9875bd/\n",
419
+ " inflating: mini-llama-articles/a361e92f-9895-41b6-ba72-4ad38e9875bd/data_level0.bin \n",
420
+ " inflating: mini-llama-articles/a361e92f-9895-41b6-ba72-4ad38e9875bd/header.bin \n",
421
+ " extracting: mini-llama-articles/a361e92f-9895-41b6-ba72-4ad38e9875bd/link_lists.bin \n",
422
+ " inflating: mini-llama-articles/a361e92f-9895-41b6-ba72-4ad38e9875bd/length.bin \n",
423
+ " inflating: mini-llama-articles/chroma.sqlite3 \n"
424
+ ]
425
+ }
426
+ ]
427
+ },
428
+ {
429
+ "cell_type": "code",
430
+ "source": [
431
+ "# Load the vector store from the local storage.\n",
432
+ "db = chromadb.PersistentClient(path=\"./mini-llama-articles\")\n",
433
+ "chroma_collection = db.get_or_create_collection(\"mini-llama-articles\")\n",
434
+ "vector_store = ChromaVectorStore(chroma_collection=chroma_collection)"
435
+ ],
436
+ "metadata": {
437
+ "id": "6tzS_EKPZeLS"
438
+ },
439
+ "execution_count": 8,
440
+ "outputs": []
441
+ },
442
+ {
443
+ "cell_type": "code",
444
+ "source": [
445
+ "from llama_index.core import VectorStoreIndex\n",
446
+ "\n",
447
+ "# Create the index based on the vector store.\n",
448
+ "index = VectorStoreIndex.from_vector_store(vector_store)"
449
+ ],
450
+ "metadata": {
451
+ "id": "0T6FL7J3ZrNK"
452
+ },
453
+ "execution_count": 9,
454
+ "outputs": []
455
+ },
456
+ {
457
+ "cell_type": "markdown",
458
+ "source": [
459
+ "# RankGPT"
460
+ ],
461
+ "metadata": {
462
+ "id": "w2XBkzNwLle5"
463
+ }
464
+ },
465
+ {
466
+ "cell_type": "code",
467
+ "source": [
468
+ "from llama_index.core.postprocessor.rankGPT_rerank import RankGPTRerank\n",
469
+ "\n",
470
+ "rankGPT = RankGPTRerank(top_n=3, llm=OpenAI(model=\"gpt-3.5-turbo\"))"
471
+ ],
472
+ "metadata": {
473
+ "id": "_it2CxTtLmHT"
474
+ },
475
+ "execution_count": 10,
476
+ "outputs": []
477
+ },
478
+ {
479
+ "cell_type": "code",
480
+ "source": [
481
+ "# Define a query engine that is responsible for retrieving related pieces of text,\n",
482
+ "# and using a LLM to formulate the final answer.\n",
483
+ "# The `node_postprocessors` function will be applied to the retrieved nodes.\n",
484
+ "query_engine = index.as_query_engine(\n",
485
+ " similarity_top_k=10,\n",
486
+ " node_postprocessors=[rankGPT]\n",
487
+ ")\n",
488
+ "\n",
489
+ "res = query_engine.query(\"How many parameters LLaMA2 model has?\")"
490
+ ],
491
+ "metadata": {
492
+ "id": "YA3M9m9CL6AJ"
493
+ },
494
+ "execution_count": 11,
495
+ "outputs": []
496
+ },
497
+ {
498
+ "cell_type": "code",
499
+ "source": [
500
+ "res.response"
501
+ ],
502
+ "metadata": {
503
+ "colab": {
504
+ "base_uri": "https://localhost:8080/",
505
+ "height": 53
506
+ },
507
+ "id": "wgyjv9e6MCVm",
508
+ "outputId": "70723d5e-9d16-4123-884b-0d65cd91a405"
509
+ },
510
+ "execution_count": 12,
511
+ "outputs": [
512
+ {
513
+ "output_type": "execute_result",
514
+ "data": {
515
+ "text/plain": [
516
+ "'The Llama 2 model has four different parameter sizes: 7 billion, 13 billion, 34 billion, and 70 billion.'"
517
+ ],
518
+ "application/vnd.google.colaboratory.intrinsic+json": {
519
+ "type": "string"
520
+ }
521
+ },
522
+ "metadata": {},
523
+ "execution_count": 12
524
+ }
525
+ ]
526
+ },
527
+ {
528
+ "cell_type": "code",
529
+ "source": [
530
+ "# Show the retrieved nodes\n",
531
+ "for src in res.source_nodes:\n",
532
+ " print(\"Node ID\\t\", src.node_id)\n",
533
+ " print(\"Title\\t\", src.metadata['title'])\n",
534
+ " print(\"Text\\t\", src.text)\n",
535
+ " print(\"Score\\t\", src.score)\n",
536
+ " print(\"-_\"*20)"
537
+ ],
538
+ "metadata": {
539
+ "colab": {
540
+ "base_uri": "https://localhost:8080/"
541
+ },
542
+ "id": "wUhOlwWcMEUT",
543
+ "outputId": "eae3754b-5cb8-4c5d-d739-c42c9686006d"
544
+ },
545
+ "execution_count": 13,
546
+ "outputs": [
547
+ {
548
+ "output_type": "stream",
549
+ "name": "stdout",
550
+ "text": [
551
+ "Node ID\t d6f533e5-fef8-469c-a313-def19fd38efe\n",
552
+ "Title\t Meta's Llama 2: Revolutionizing Open Source Language Models for Commercial Use\n",
553
+ "Text\t I. Llama 2: Revolutionizing Commercial Use Unlike its predecessor Llama 1, which was limited to research use, Llama 2 represents a major advancement as an open-source commercial model. Businesses can now integrate Llama 2 into products to create AI-powered applications. Availability on Azure and AWS facilitates fine-tuning and adoption. However, restrictions apply to prevent exploitation. Companies with over 700 million active daily users cannot use Llama 2. Additionally, its output cannot be used to improve other language models. 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. III. Safety Considerations: A Top Priority for Meta Meta's commitment to safety and alignment shines through in Llama 2's design. The model demonstrates exceptionally low AI safety violation percentages, surpassing even ChatGPT in safety benchmarks. Finding the right balance between helpfulness and safety when optimizing a model poses significant challenges. While a highly helpful model may be capable of answering any question, including sensitive ones like \"How do I build a bomb?\", it also raises concerns about potential misuse. Thus, striking the perfect equilibrium between providing useful information and ensuring safety is paramount. However, prioritizing safety to an extreme extent can lead to a model that struggles to effectively address a diverse range of questions. This limitation could hinder the model's practical applicability and user experience. Thus, achieving\n",
554
+ "Score\t 0.7077337819711658\n",
555
+ "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n",
556
+ "Node ID\t 2f3b7c34-8fd0-4134-af38-ef1b77e32cd8\n",
557
+ "Title\t Meta's Llama 2: Revolutionizing Open Source Language Models for Commercial Use\n",
558
+ "Text\t The model demonstrates exceptionally low AI safety violation percentages, surpassing even ChatGPT in safety benchmarks. Finding the right balance between helpfulness and safety when optimizing a model poses significant challenges. While a highly helpful model may be capable of answering any question, including sensitive ones like \"How do I build a bomb?\", it also raises concerns about potential misuse. Thus, striking the perfect equilibrium between providing useful information and ensuring safety is paramount. However, prioritizing safety to an extreme extent can lead to a model that struggles to effectively address a diverse range of questions. This limitation could hinder the model's practical applicability and user experience. Thus, achieving an optimum balance that allows the model to be both helpful and safe is of utmost importance. To strike the right balance between helpfulness and safety, Meta employed two reward models - one for helpfulness and another for safety - to optimize the model's responses. The 34B parameter model has reported higher safety violations than other variants, possibly contributing to the delay in its release. IV. Helpfulness Comparison: Llama 2 Outperforms Competitors Llama 2 emerges as a strong contender in the open-source language model arena, outperforming its competitors in most categories. The 70B parameter model outperforms all other open-source models, while the 7B and 34B models outshine Falcon in all categories and MPT in all categories except coding. Despite being smaller, Llam a2's performance rivals that of Chat GPT 3.5, a significantly larger closed-source model. While GPT 4 and PalM-2-L, with their larger size, outperform Llama 2, this is expected due to their capacity for handling complex language tasks. Llama 2's impressive ability to compete with larger models highlights its efficiency and potential in the market. However, Llama 2 does face challenges in coding and math problems, where models like Chat GPT 4 excel, given their significantly larger size. Chat GPT 4 performed significantly better than Llama 2 for coding (HumanEval benchmark)and math problem tasks (GSM8k benchmark). Open-source AI technologies, like Llama 2, continue to advance, offering\n",
559
+ "Score\t 0.7025566634608498\n",
560
+ "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n",
561
+ "Node ID\t 1c7a8637-6f65-401e-be33-26886c828a34\n",
562
+ "Title\t Inside Code Llama: Meta AI's Entrance in the Code LLM Space\n",
563
+ "Text\t Inside Code Llama The release of Code Llama does not include a single model but three different variants, characterized by their parameter sizes of 7B, 13B, and 34B. Each of these models has been trained on an extensive pool of 500B tokens encompassing code and code-related information. Notably, the 7B and 13B base and instruct models have been endowed with fill-in-the-middle (FIM) competence, empowering them to seamlessly insert code into existing code structures. This attribute equips them to handle tasks like code completion right from the outset.The trio of models caters to distinct requisites concerning serving and latency. For instance, the 7B model boasts the ability to operate on a single GPU. While the 34B model stands out for yielding optimal outcomes and elevating coding assistance, the smaller 7B and 13B versions excel in speed, making them fitting for low-latency tasks such as real-time code completion. Meta AI's innovations further extend to two nuanced adaptations of Code Llama: Code Llama - Python and Code Llama - Instruct. Code Llama - Python is a specialized derivation, meticulously honed on a substantial volume of Python code spanning 100B tokens. Given Python's central role in code generation benchmarks and its significance within the AI community, this focused model augments utility.Code Llama - Instruct represents an alignment and refinement of Code Llama through instructional fine-tuning. This novel training approach entails furnishing the model with \"natural language instruction\" inputs paired with anticipated outputs. This strategic methodology enhances the model's capacity to grasp human expectations in prompts. For endeavors involving code generation, it is advised to opt for Code Llama - Instruct versions, as they have been calibrated to yield useful and secure natural language responses. Deep diving into the Code Llama training and fine-tuning, there are a few aspects that are worth highlighting 1) DatasetLlama's training rests on a meticulously curated dataset enriched with publicly available code, offering a near-duplicate-free landscape. The dataset consists of 500B tokens during the initial phase, starting from the 7B, 13B, and 34B\n",
564
+ "Score\t 0.6889534709415898\n",
565
+ "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n"
566
+ ]
567
+ }
568
+ ]
569
+ },
570
+ {
571
+ "cell_type": "markdown",
572
+ "source": [
573
+ "# Custom Postprocessor"
574
+ ],
575
+ "metadata": {
576
+ "id": "5mcAcZqhQluE"
577
+ }
578
+ },
579
+ {
580
+ "cell_type": "markdown",
581
+ "source": [
582
+ "## The `Judger` Function"
583
+ ],
584
+ "metadata": {
585
+ "id": "7v7vmJblQrN6"
586
+ }
587
+ },
588
+ {
589
+ "cell_type": "markdown",
590
+ "source": [
591
+ "The following function will query GPT-4 to retrieve the top three nodes that has highest similarity to the asked question."
592
+ ],
593
+ "metadata": {
594
+ "id": "6k8IKlN9QvU7"
595
+ }
596
+ },
597
+ {
598
+ "cell_type": "code",
599
+ "source": [
600
+ "from pydantic import BaseModel\n",
601
+ "from llama_index.llms.openai import OpenAI\n",
602
+ "from llama_index.core.prompts import PromptTemplate\n",
603
+ "\n",
604
+ "\n",
605
+ "def judger(nodes, query):\n",
606
+ "\n",
607
+ " # The model's output template\n",
608
+ " class OrderedNodes(BaseModel):\n",
609
+ " \"\"\"A node with the id and assigned score.\"\"\"\n",
610
+ " node_id: list\n",
611
+ " score: list\n",
612
+ "\n",
613
+ " # Prepare the nodes and wrap them in <NODE></NODE> identifier, as well as the query\n",
614
+ " the_nodes=\"\"\n",
615
+ " for idx, item in enumerate(nodes):\n",
616
+ " the_nodes += f\"<NODE{idx+1}>\\nNode ID: {item.node_id}\\nText: {item.text}\\n</NODE{idx+1}>\\n\"\n",
617
+ "\n",
618
+ " query = \"<QUERY>\\n{}\\n</QUERY>\".format(query)\n",
619
+ "\n",
620
+ " # Define the prompt template\n",
621
+ " prompt_tmpl = PromptTemplate(\n",
622
+ " \"\"\"\n",
623
+ " You receive a qurey along with a list of nodes' text and their ids. Your task is to assign score\n",
624
+ " to each node based on its contextually closeness to the given query. The final output is each\n",
625
+ " node id along with its proximity score.\n",
626
+ " Here is the list of nodes:\n",
627
+ " {nodes_list}\n",
628
+ "\n",
629
+ " And the following is the query:\n",
630
+ " {user_query}\n",
631
+ "\n",
632
+ " Score each of the nodes based on their text and their relevancy to the provided query.\n",
633
+ " The score must be a decimal number between 0 an 1 so we can rank them.\"\"\"\n",
634
+ " )\n",
635
+ "\n",
636
+ " # Define the an instance of GPT-4 and send the request\n",
637
+ " llm = OpenAI(model=\"gpt-4\")\n",
638
+ " ordered_nodes = llm.structured_predict(\n",
639
+ " OrderedNodes, prompt_tmpl, nodes_list=the_nodes, user_query=query\n",
640
+ " )\n",
641
+ "\n",
642
+ " return ordered_nodes"
643
+ ],
644
+ "metadata": {
645
+ "id": "WhtJ1OeF9L3G"
646
+ },
647
+ "execution_count": 25,
648
+ "outputs": []
649
+ },
650
+ {
651
+ "cell_type": "markdown",
652
+ "source": [
653
+ "## Define Postprocessor"
654
+ ],
655
+ "metadata": {
656
+ "id": "Q5f1GrBKZprO"
657
+ }
658
+ },
659
+ {
660
+ "cell_type": "markdown",
661
+ "source": [
662
+ "The following class will use the `judger` function to rank the nodes, and filter them based on the ranks."
663
+ ],
664
+ "metadata": {
665
+ "id": "yZujUJTvQ6Yu"
666
+ }
667
+ },
668
+ {
669
+ "cell_type": "code",
670
+ "source": [
671
+ "from typing import (\n",
672
+ " List,\n",
673
+ " Optional\n",
674
+ ")\n",
675
+ "from llama_index.core import QueryBundle\n",
676
+ "from llama_index.core.postprocessor.types import BaseNodePostprocessor\n",
677
+ "from llama_index.core.schema import NodeWithScore\n",
678
+ "\n",
679
+ "\n",
680
+ "class OpenaiAsJudgePostprocessor(BaseNodePostprocessor):\n",
681
+ " def _postprocess_nodes(\n",
682
+ " self, nodes: List[NodeWithScore], query_bundle: Optional[QueryBundle]\n",
683
+ " ) -> List[NodeWithScore]:\n",
684
+ "\n",
685
+ " r = judger(nodes, query_bundle)\n",
686
+ "\n",
687
+ " node_ids = r.node_id\n",
688
+ " scores = r.score\n",
689
+ "\n",
690
+ " sorted_scores = sorted(enumerate(scores), key=lambda x: x[1], reverse=True)\n",
691
+ " top_three_nodes = [sorted_scores[i][0] for i in range(3)]\n",
692
+ "\n",
693
+ " selected_nodes_id = [node_ids[item] for item in top_three_nodes]\n",
694
+ "\n",
695
+ " final_nodes = []\n",
696
+ " for item in nodes:\n",
697
+ " if item.node_id in selected_nodes_id:\n",
698
+ " final_nodes.append( item )\n",
699
+ "\n",
700
+ " return final_nodes"
701
+ ],
702
+ "metadata": {
703
+ "id": "-QtyuC8fZun0"
704
+ },
705
+ "execution_count": 26,
706
+ "outputs": []
707
+ },
708
+ {
709
+ "cell_type": "code",
710
+ "source": [
711
+ "judge = OpenaiAsJudgePostprocessor()"
712
+ ],
713
+ "metadata": {
714
+ "id": "jk-lqYlYLipi"
715
+ },
716
+ "execution_count": 27,
717
+ "outputs": []
718
+ },
719
+ {
720
+ "cell_type": "markdown",
721
+ "source": [
722
+ "## Query Engine with Postprocessor"
723
+ ],
724
+ "metadata": {
725
+ "id": "cgtsvxR7SflP"
726
+ }
727
+ },
728
+ {
729
+ "cell_type": "code",
730
+ "source": [
731
+ "# Define a query engine that is responsible for retrieving related pieces of text,\n",
732
+ "# and using a LLM to formulate the final answer.\n",
733
+ "# The `node_postprocessors` function will be applied to the retrieved nodes.\n",
734
+ "query_engine = index.as_query_engine(\n",
735
+ " similarity_top_k=10,\n",
736
+ " node_postprocessors=[judge]\n",
737
+ ")\n",
738
+ "\n",
739
+ "res = query_engine.query(\"How many parameters LLaMA2 model has?\")"
740
+ ],
741
+ "metadata": {
742
+ "id": "1Hh3RLCeLfXZ"
743
+ },
744
+ "execution_count": 28,
745
+ "outputs": []
746
+ },
747
+ {
748
+ "cell_type": "code",
749
+ "source": [
750
+ "res.response"
751
+ ],
752
+ "metadata": {
753
+ "colab": {
754
+ "base_uri": "https://localhost:8080/",
755
+ "height": 53
756
+ },
757
+ "id": "zmZv0EIyF0wG",
758
+ "outputId": "7ff1b3bf-1b5f-4985-ea0d-3048d94c8da1"
759
+ },
760
+ "execution_count": 29,
761
+ "outputs": [
762
+ {
763
+ "output_type": "execute_result",
764
+ "data": {
765
+ "text/plain": [
766
+ "'The Llama 2 model is available in four different sizes: 7 billion, 13 billion, 34 billion, and 70 billion parameters.'"
767
+ ],
768
+ "application/vnd.google.colaboratory.intrinsic+json": {
769
+ "type": "string"
770
+ }
771
+ },
772
+ "metadata": {},
773
+ "execution_count": 29
774
+ }
775
+ ]
776
+ },
777
+ {
778
+ "cell_type": "code",
779
+ "source": [
780
+ "# Show the retrieved nodes\n",
781
+ "for src in res.source_nodes:\n",
782
+ " print(\"Node ID\\t\", src.node_id)\n",
783
+ " print(\"Title\\t\", src.metadata['title'])\n",
784
+ " print(\"Text\\t\", src.text)\n",
785
+ " print(\"Score\\t\", src.score)\n",
786
+ " print(\"-_\"*20)"
787
+ ],
788
+ "metadata": {
789
+ "id": "bBMaG6yaZzjA",
790
+ "colab": {
791
+ "base_uri": "https://localhost:8080/"
792
+ },
793
+ "outputId": "8a173ef7-e66f-4f9b-a979-c88a17028ef0"
794
+ },
795
+ "execution_count": 30,
796
+ "outputs": [
797
+ {
798
+ "output_type": "stream",
799
+ "name": "stdout",
800
+ "text": [
801
+ "Node ID\t d6f533e5-fef8-469c-a313-def19fd38efe\n",
802
+ "Title\t Meta's Llama 2: Revolutionizing Open Source Language Models for Commercial Use\n",
803
+ "Text\t I. Llama 2: Revolutionizing Commercial Use Unlike its predecessor Llama 1, which was limited to research use, Llama 2 represents a major advancement as an open-source commercial model. Businesses can now integrate Llama 2 into products to create AI-powered applications. Availability on Azure and AWS facilitates fine-tuning and adoption. However, restrictions apply to prevent exploitation. Companies with over 700 million active daily users cannot use Llama 2. Additionally, its output cannot be used to improve other language models. 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. III. Safety Considerations: A Top Priority for Meta Meta's commitment to safety and alignment shines through in Llama 2's design. The model demonstrates exceptionally low AI safety violation percentages, surpassing even ChatGPT in safety benchmarks. Finding the right balance between helpfulness and safety when optimizing a model poses significant challenges. While a highly helpful model may be capable of answering any question, including sensitive ones like \"How do I build a bomb?\", it also raises concerns about potential misuse. Thus, striking the perfect equilibrium between providing useful information and ensuring safety is paramount. However, prioritizing safety to an extreme extent can lead to a model that struggles to effectively address a diverse range of questions. This limitation could hinder the model's practical applicability and user experience. Thus, achieving\n",
804
+ "Score\t 0.7077337819711658\n",
805
+ "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n",
806
+ "Node ID\t 2f3b7c34-8fd0-4134-af38-ef1b77e32cd8\n",
807
+ "Title\t Meta's Llama 2: Revolutionizing Open Source Language Models for Commercial Use\n",
808
+ "Text\t The model demonstrates exceptionally low AI safety violation percentages, surpassing even ChatGPT in safety benchmarks. Finding the right balance between helpfulness and safety when optimizing a model poses significant challenges. While a highly helpful model may be capable of answering any question, including sensitive ones like \"How do I build a bomb?\", it also raises concerns about potential misuse. Thus, striking the perfect equilibrium between providing useful information and ensuring safety is paramount. However, prioritizing safety to an extreme extent can lead to a model that struggles to effectively address a diverse range of questions. This limitation could hinder the model's practical applicability and user experience. Thus, achieving an optimum balance that allows the model to be both helpful and safe is of utmost importance. To strike the right balance between helpfulness and safety, Meta employed two reward models - one for helpfulness and another for safety - to optimize the model's responses. The 34B parameter model has reported higher safety violations than other variants, possibly contributing to the delay in its release. IV. Helpfulness Comparison: Llama 2 Outperforms Competitors Llama 2 emerges as a strong contender in the open-source language model arena, outperforming its competitors in most categories. The 70B parameter model outperforms all other open-source models, while the 7B and 34B models outshine Falcon in all categories and MPT in all categories except coding. Despite being smaller, Llam a2's performance rivals that of Chat GPT 3.5, a significantly larger closed-source model. While GPT 4 and PalM-2-L, with their larger size, outperform Llama 2, this is expected due to their capacity for handling complex language tasks. Llama 2's impressive ability to compete with larger models highlights its efficiency and potential in the market. However, Llama 2 does face challenges in coding and math problems, where models like Chat GPT 4 excel, given their significantly larger size. Chat GPT 4 performed significantly better than Llama 2 for coding (HumanEval benchmark)and math problem tasks (GSM8k benchmark). Open-source AI technologies, like Llama 2, continue to advance, offering\n",
809
+ "Score\t 0.7025566634608498\n",
810
+ "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n",
811
+ "Node ID\t 021c859e-809b-49b8-8d0d-38cc326c1203\n",
812
+ "Title\t Meta's Llama 2: Revolutionizing Open Source Language Models for Commercial Use\n",
813
+ "Text\t with their larger size, outperform Llama 2, this is expected due to their capacity for handling complex language tasks. Llama 2's impressive ability to compete with larger models highlights its efficiency and potential in the market. However, Llama 2 does face challenges in coding and math problems, where models like Chat GPT 4 excel, given their significantly larger size. Chat GPT 4 performed significantly better than Llama 2 for coding (HumanEval benchmark)and math problem tasks (GSM8k benchmark). Open-source AI technologies, like Llama 2, continue to advance, offering strong competition to closed-source models. V. Ghost Attention: Enhancing Conversational Continuity One unique feature in Llama 2 is Ghost Attention, which ensures continuity in conversations. This means that even after multiple interactions, the model remembers its initial instructions, ensuring more coherent and consistent responses throughout the conversation. This feature significantly enhances the user experience and makes Llama 2 a more reliable language model for interactive applications. In the example below, on the left, it forgets to use an emoji after a few conversations. On the right, with Ghost Attention, even after having many conversations, it will remember the context and continue to use emojis in its response. VI. Temporal Capability: A Leap in Information Organization Meta reported a groundbreaking temporal capability, where the model organizes information based on time relevance. Each question posed to the model is associated with a date, and it responds accordingly by considering the event date before which the question becomes irrelevant. For example, if you ask the question, \"How long ago did Barack Obama become president?\", its only relevant after 2008. This temporal awareness allows Llama 2 to deliver more contextually accurate responses, enriching the user experience further. VII. Open Questions and Future Outlook Meta's open-sourcing of Llama 2 represents a seismic shift, now offering developers and researchers commercial access to a leading language model. With Llama 2 outperforming MosaicML's current MPT models, all eyes are on how Databricks will respond. Can MosaicML's next MPT iteration beat Llama 2? Is it worthwhile to compete\n",
814
+ "Score\t 0.691486848320407\n",
815
+ "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n"
816
+ ]
817
+ }
818
+ ]
819
+ },
820
+ {
821
+ "cell_type": "code",
822
+ "source": [],
823
+ "metadata": {
824
+ "id": "J7sIPpFFTep3"
825
+ },
826
+ "execution_count": null,
827
+ "outputs": []
828
+ }
829
+ ]
830
+ }