Spaces:
Sleeping
Sleeping
Created using Colab
Browse files
notebooks/Larger_Context_Larger_N.ipynb
ADDED
@@ -0,0 +1,402 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"nbformat": 4,
|
3 |
+
"nbformat_minor": 0,
|
4 |
+
"metadata": {
|
5 |
+
"colab": {
|
6 |
+
"provenance": [],
|
7 |
+
"authorship_tag": "ABX9TyOywIfWGtoPk+CbHWvxkiSH",
|
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/Larger_Context_Larger_N.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": "qtOtOvibOBfW"
|
36 |
+
}
|
37 |
+
},
|
38 |
+
{
|
39 |
+
"cell_type": "code",
|
40 |
+
"execution_count": null,
|
41 |
+
"metadata": {
|
42 |
+
"id": "I-hKKV6GEkro"
|
43 |
+
},
|
44 |
+
"outputs": [],
|
45 |
+
"source": [
|
46 |
+
"!pip install -q llama-index==0.10.37 openai==1.30.1 tiktoken==0.7.0 chromadb==0.5.0 llama-index-llms-gemini==0.1.10 llama-index-vector-stores-chroma==0.1.7"
|
47 |
+
]
|
48 |
+
},
|
49 |
+
{
|
50 |
+
"cell_type": "code",
|
51 |
+
"source": [
|
52 |
+
"import os\n",
|
53 |
+
"\n",
|
54 |
+
"# Set the \"OPENAI_API_KEY\" in the Python environment. Will be used by OpenAI client later.\n",
|
55 |
+
"os.environ[\"OPENAI_API_KEY\"] = \"[OPENAI_API_KEY]\"\n",
|
56 |
+
"os.environ[\"GOOGLE_API_KEY\"] = \"[GOOGLE_API_KEY]\""
|
57 |
+
],
|
58 |
+
"metadata": {
|
59 |
+
"id": "5UZDtKWJWZ3c"
|
60 |
+
},
|
61 |
+
"execution_count": null,
|
62 |
+
"outputs": []
|
63 |
+
},
|
64 |
+
{
|
65 |
+
"cell_type": "markdown",
|
66 |
+
"source": [
|
67 |
+
"# Load Gemini Model"
|
68 |
+
],
|
69 |
+
"metadata": {
|
70 |
+
"id": "P8un03bdrwIn"
|
71 |
+
}
|
72 |
+
},
|
73 |
+
{
|
74 |
+
"cell_type": "code",
|
75 |
+
"source": [
|
76 |
+
"from llama_index.llms.gemini import Gemini\n",
|
77 |
+
"\n",
|
78 |
+
"llm = Gemini(model=\"models/gemini-pro\")"
|
79 |
+
],
|
80 |
+
"metadata": {
|
81 |
+
"id": "dFvjEffurv6T"
|
82 |
+
},
|
83 |
+
"execution_count": null,
|
84 |
+
"outputs": []
|
85 |
+
},
|
86 |
+
{
|
87 |
+
"cell_type": "markdown",
|
88 |
+
"source": [
|
89 |
+
"# Download the Vector Store"
|
90 |
+
],
|
91 |
+
"metadata": {
|
92 |
+
"id": "fcX9C-AThh15"
|
93 |
+
}
|
94 |
+
},
|
95 |
+
{
|
96 |
+
"cell_type": "code",
|
97 |
+
"source": [
|
98 |
+
"!wget https://github.com/AlaFalaki/tutorial_notebooks/raw/main/data/vectorstore.zip"
|
99 |
+
],
|
100 |
+
"metadata": {
|
101 |
+
"colab": {
|
102 |
+
"base_uri": "https://localhost:8080/"
|
103 |
+
},
|
104 |
+
"id": "_oi1avNUhhYd",
|
105 |
+
"outputId": "f1c3c907-d4fe-47a6-bcc8-b2d09b54317c"
|
106 |
+
},
|
107 |
+
"execution_count": null,
|
108 |
+
"outputs": [
|
109 |
+
{
|
110 |
+
"output_type": "stream",
|
111 |
+
"name": "stdout",
|
112 |
+
"text": [
|
113 |
+
"--2024-06-05 18:22:24-- https://github.com/AlaFalaki/tutorial_notebooks/raw/main/data/vectorstore.zip\n",
|
114 |
+
"Resolving github.com (github.com)... 140.82.121.3\n",
|
115 |
+
"Connecting to github.com (github.com)|140.82.121.3|:443... connected.\n",
|
116 |
+
"HTTP request sent, awaiting response... 302 Found\n",
|
117 |
+
"Location: https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/vectorstore.zip [following]\n",
|
118 |
+
"--2024-06-05 18:22:24-- https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/vectorstore.zip\n",
|
119 |
+
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.109.133, 185.199.110.133, ...\n",
|
120 |
+
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected.\n",
|
121 |
+
"HTTP request sent, awaiting response... 200 OK\n",
|
122 |
+
"Length: 1479982 (1.4M) [application/zip]\n",
|
123 |
+
"Saving to: ‘vectorstore.zip’\n",
|
124 |
+
"\n",
|
125 |
+
"vectorstore.zip 100%[===================>] 1.41M --.-KB/s in 0.03s \n",
|
126 |
+
"\n",
|
127 |
+
"2024-06-05 18:22:24 (54.1 MB/s) - ‘vectorstore.zip’ saved [1479982/1479982]\n",
|
128 |
+
"\n"
|
129 |
+
]
|
130 |
+
}
|
131 |
+
]
|
132 |
+
},
|
133 |
+
{
|
134 |
+
"cell_type": "code",
|
135 |
+
"source": [
|
136 |
+
"!unzip vectorstore.zip"
|
137 |
+
],
|
138 |
+
"metadata": {
|
139 |
+
"colab": {
|
140 |
+
"base_uri": "https://localhost:8080/"
|
141 |
+
},
|
142 |
+
"id": "8BM4sU-bWZ0l",
|
143 |
+
"outputId": "6aa613b0-9f4f-4873-eb02-f1d9566ebeb8"
|
144 |
+
},
|
145 |
+
"execution_count": null,
|
146 |
+
"outputs": [
|
147 |
+
{
|
148 |
+
"output_type": "stream",
|
149 |
+
"name": "stdout",
|
150 |
+
"text": [
|
151 |
+
"Archive: vectorstore.zip\n",
|
152 |
+
" creating: mini-llama-articles/\n",
|
153 |
+
" creating: mini-llama-articles/a361e92f-9895-41b6-ba72-4ad38e9875bd/\n",
|
154 |
+
" inflating: mini-llama-articles/a361e92f-9895-41b6-ba72-4ad38e9875bd/data_level0.bin \n",
|
155 |
+
" inflating: mini-llama-articles/a361e92f-9895-41b6-ba72-4ad38e9875bd/header.bin \n",
|
156 |
+
" extracting: mini-llama-articles/a361e92f-9895-41b6-ba72-4ad38e9875bd/link_lists.bin \n",
|
157 |
+
" inflating: mini-llama-articles/a361e92f-9895-41b6-ba72-4ad38e9875bd/length.bin \n",
|
158 |
+
" inflating: mini-llama-articles/chroma.sqlite3 \n"
|
159 |
+
]
|
160 |
+
}
|
161 |
+
]
|
162 |
+
},
|
163 |
+
{
|
164 |
+
"cell_type": "code",
|
165 |
+
"source": [
|
166 |
+
"import chromadb\n",
|
167 |
+
"from llama_index.vector_stores.chroma import ChromaVectorStore\n",
|
168 |
+
"\n",
|
169 |
+
"# Load the vector store from the local storage.\n",
|
170 |
+
"db = chromadb.PersistentClient(path=\"./mini-llama-articles\")\n",
|
171 |
+
"chroma_collection = db.get_or_create_collection(\"mini-llama-articles\")\n",
|
172 |
+
"vector_store = ChromaVectorStore(chroma_collection=chroma_collection)"
|
173 |
+
],
|
174 |
+
"metadata": {
|
175 |
+
"id": "VikY0MnrWZyC"
|
176 |
+
},
|
177 |
+
"execution_count": null,
|
178 |
+
"outputs": []
|
179 |
+
},
|
180 |
+
{
|
181 |
+
"cell_type": "code",
|
182 |
+
"source": [
|
183 |
+
"from llama_index.core import VectorStoreIndex\n",
|
184 |
+
"\n",
|
185 |
+
"# Create the index based on the vector store.\n",
|
186 |
+
"index = VectorStoreIndex.from_vector_store(vector_store, llm=llm)"
|
187 |
+
],
|
188 |
+
"metadata": {
|
189 |
+
"id": "o87JiKrUWZvG"
|
190 |
+
},
|
191 |
+
"execution_count": null,
|
192 |
+
"outputs": []
|
193 |
+
},
|
194 |
+
{
|
195 |
+
"cell_type": "code",
|
196 |
+
"source": [
|
197 |
+
"query_engine = index.as_query_engine()\n",
|
198 |
+
"\n",
|
199 |
+
"res = query_engine.query(\"How many parameters LLaMA2 model has?\")"
|
200 |
+
],
|
201 |
+
"metadata": {
|
202 |
+
"id": "-H8c-pUpqu7W"
|
203 |
+
},
|
204 |
+
"execution_count": null,
|
205 |
+
"outputs": []
|
206 |
+
},
|
207 |
+
{
|
208 |
+
"cell_type": "code",
|
209 |
+
"source": [
|
210 |
+
"res.response"
|
211 |
+
],
|
212 |
+
"metadata": {
|
213 |
+
"colab": {
|
214 |
+
"base_uri": "https://localhost:8080/",
|
215 |
+
"height": 35
|
216 |
+
},
|
217 |
+
"id": "Cg36kDnZvuX0",
|
218 |
+
"outputId": "7c36f9a1-fbb8-4531-c976-75de9ff89ec6"
|
219 |
+
},
|
220 |
+
"execution_count": null,
|
221 |
+
"outputs": [
|
222 |
+
{
|
223 |
+
"output_type": "execute_result",
|
224 |
+
"data": {
|
225 |
+
"text/plain": [
|
226 |
+
"'The Llama 2 model has four different model sizes: 7 billion, 13 billion, 34 billion, and 70 billion parameters.'"
|
227 |
+
],
|
228 |
+
"application/vnd.google.colaboratory.intrinsic+json": {
|
229 |
+
"type": "string"
|
230 |
+
}
|
231 |
+
},
|
232 |
+
"metadata": {},
|
233 |
+
"execution_count": 66
|
234 |
+
}
|
235 |
+
]
|
236 |
+
},
|
237 |
+
{
|
238 |
+
"cell_type": "code",
|
239 |
+
"source": [
|
240 |
+
"# Show the retrieved nodes\n",
|
241 |
+
"for src in res.source_nodes:\n",
|
242 |
+
" print(\"Node ID\\t\", src.node_id)\n",
|
243 |
+
" print(\"Title\\t\", src.metadata['title'])\n",
|
244 |
+
" print(\"Text\\t\", src.text)\n",
|
245 |
+
" print(\"Score\\t\", src.score)\n",
|
246 |
+
" print(\"-_\"*20)"
|
247 |
+
],
|
248 |
+
"metadata": {
|
249 |
+
"colab": {
|
250 |
+
"base_uri": "https://localhost:8080/"
|
251 |
+
},
|
252 |
+
"id": "-b1dQAQkrL4X",
|
253 |
+
"outputId": "7a2c99f1-3b0a-4635-e866-842f4ebc7b35"
|
254 |
+
},
|
255 |
+
"execution_count": null,
|
256 |
+
"outputs": [
|
257 |
+
{
|
258 |
+
"output_type": "stream",
|
259 |
+
"name": "stdout",
|
260 |
+
"text": [
|
261 |
+
"Node ID\t d6f533e5-fef8-469c-a313-def19fd38efe\n",
|
262 |
+
"Title\t Meta's Llama 2: Revolutionizing Open Source Language Models for Commercial Use\n",
|
263 |
+
"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",
|
264 |
+
"Score\t 0.7078549032318474\n",
|
265 |
+
"-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n",
|
266 |
+
"Node ID\t 2f3b7c34-8fd0-4134-af38-ef1b77e32cd8\n",
|
267 |
+
"Title\t Meta's Llama 2: Revolutionizing Open Source Language Models for Commercial Use\n",
|
268 |
+
"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",
|
269 |
+
"Score\t 0.7026792232112851\n",
|
270 |
+
"-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n"
|
271 |
+
]
|
272 |
+
}
|
273 |
+
]
|
274 |
+
},
|
275 |
+
{
|
276 |
+
"cell_type": "markdown",
|
277 |
+
"source": [
|
278 |
+
"# Evaluate"
|
279 |
+
],
|
280 |
+
"metadata": {
|
281 |
+
"id": "eB83yG_o0cjO"
|
282 |
+
}
|
283 |
+
},
|
284 |
+
{
|
285 |
+
"cell_type": "code",
|
286 |
+
"source": [
|
287 |
+
"!wget https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/rag_eval_dataset.json"
|
288 |
+
],
|
289 |
+
"metadata": {
|
290 |
+
"colab": {
|
291 |
+
"base_uri": "https://localhost:8080/"
|
292 |
+
},
|
293 |
+
"id": "TblvUrZ97TV6",
|
294 |
+
"outputId": "8d4bf9ce-7309-41c8-9705-9e02f7de5203"
|
295 |
+
},
|
296 |
+
"execution_count": null,
|
297 |
+
"outputs": [
|
298 |
+
{
|
299 |
+
"output_type": "stream",
|
300 |
+
"name": "stdout",
|
301 |
+
"text": [
|
302 |
+
"--2024-06-05 19:43:23-- https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/rag_eval_dataset.json\n",
|
303 |
+
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...\n",
|
304 |
+
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.\n",
|
305 |
+
"HTTP request sent, awaiting response... 200 OK\n",
|
306 |
+
"Length: 476714 (466K) [text/plain]\n",
|
307 |
+
"Saving to: ‘rag_eval_dataset.json’\n",
|
308 |
+
"\n",
|
309 |
+
"\rrag_eval_dataset.js 0%[ ] 0 --.-KB/s \rrag_eval_dataset.js 100%[===================>] 465.54K --.-KB/s in 0.02s \n",
|
310 |
+
"\n",
|
311 |
+
"2024-06-05 19:43:24 (25.0 MB/s) - ‘rag_eval_dataset.json’ saved [476714/476714]\n",
|
312 |
+
"\n"
|
313 |
+
]
|
314 |
+
}
|
315 |
+
]
|
316 |
+
},
|
317 |
+
{
|
318 |
+
"cell_type": "code",
|
319 |
+
"source": [
|
320 |
+
"# We can also load the dataset from a previously saved json file.\n",
|
321 |
+
"from llama_index.core.evaluation import EmbeddingQAFinetuneDataset\n",
|
322 |
+
"\n",
|
323 |
+
"rag_eval_dataset = EmbeddingQAFinetuneDataset.from_json(\n",
|
324 |
+
" \"./rag_eval_dataset.json\"\n",
|
325 |
+
")"
|
326 |
+
],
|
327 |
+
"metadata": {
|
328 |
+
"id": "fqRm2AMPrNE7"
|
329 |
+
},
|
330 |
+
"execution_count": null,
|
331 |
+
"outputs": []
|
332 |
+
},
|
333 |
+
{
|
334 |
+
"cell_type": "code",
|
335 |
+
"source": [
|
336 |
+
"from llama_index.core.evaluation import RelevancyEvaluator, FaithfulnessEvaluator, BatchEvalRunner\n",
|
337 |
+
"from llama_index.llms.openai import OpenAI\n",
|
338 |
+
"\n",
|
339 |
+
"llm_gpt4 = OpenAI(temperature=0, model=\"gpt-4o\")\n",
|
340 |
+
"\n",
|
341 |
+
"faithfulness_evaluator = FaithfulnessEvaluator(llm=llm_gpt4)\n",
|
342 |
+
"relevancy_evaluator = RelevancyEvaluator(llm=llm_gpt4)\n",
|
343 |
+
"\n",
|
344 |
+
"# Run evaluation\n",
|
345 |
+
"queries = list(rag_eval_dataset.queries.values())\n",
|
346 |
+
"batch_eval_queries = queries[:20]\n",
|
347 |
+
"\n",
|
348 |
+
"runner = BatchEvalRunner(\n",
|
349 |
+
"{\"faithfulness\": faithfulness_evaluator, \"relevancy\": relevancy_evaluator},\n",
|
350 |
+
"workers=32,\n",
|
351 |
+
")\n",
|
352 |
+
"\n",
|
353 |
+
"for i in [2, 4, 6, 8, 10, 15, 20, 25, 30]:\n",
|
354 |
+
" # Set Faithfulness and Relevancy evaluators\n",
|
355 |
+
" query_engine = index.as_query_engine(similarity_top_k=i)\n",
|
356 |
+
"\n",
|
357 |
+
" eval_results = await runner.aevaluate_queries(\n",
|
358 |
+
" query_engine, queries=batch_eval_queries\n",
|
359 |
+
" )\n",
|
360 |
+
" faithfulness_score = sum(result.passing for result in eval_results['faithfulness']) / len(eval_results['faithfulness'])\n",
|
361 |
+
" print(f\"top_{i} faithfulness_score: {faithfulness_score}\")\n",
|
362 |
+
"\n",
|
363 |
+
" relevancy_score = sum(result.passing for result in eval_results['relevancy']) / len(eval_results['relevancy'])\n",
|
364 |
+
" print(f\"top_{i} relevancy_score: {relevancy_score}\")"
|
365 |
+
],
|
366 |
+
"metadata": {
|
367 |
+
"colab": {
|
368 |
+
"base_uri": "https://localhost:8080/"
|
369 |
+
},
|
370 |
+
"id": "1GagTcRz7XkU",
|
371 |
+
"outputId": "2c03eebc-2362-4934-fb19-8bdcb6ceb44d"
|
372 |
+
},
|
373 |
+
"execution_count": null,
|
374 |
+
"outputs": [
|
375 |
+
{
|
376 |
+
"output_type": "stream",
|
377 |
+
"name": "stdout",
|
378 |
+
"text": [
|
379 |
+
"top_2 faithfulness_score: 1.0\n",
|
380 |
+
"top_2 relevancy_score: 1.0\n",
|
381 |
+
"top_4 faithfulness_score: 1.0\n",
|
382 |
+
"top_4 relevancy_score: 0.95\n",
|
383 |
+
"top_6 faithfulness_score: 1.0\n",
|
384 |
+
"top_6 relevancy_score: 0.95\n",
|
385 |
+
"top_8 faithfulness_score: 1.0\n",
|
386 |
+
"top_8 relevancy_score: 1.0\n",
|
387 |
+
"top_10 faithfulness_score: 1.0\n",
|
388 |
+
"top_10 relevancy_score: 1.0\n",
|
389 |
+
"top_15 faithfulness_score: 0.95\n",
|
390 |
+
"top_15 relevancy_score: 0.95\n",
|
391 |
+
"top_20 faithfulness_score: 1.0\n",
|
392 |
+
"top_20 relevancy_score: 0.95\n",
|
393 |
+
"top_25 faithfulness_score: 0.95\n",
|
394 |
+
"top_25 relevancy_score: 1.0\n",
|
395 |
+
"top_30 faithfulness_score: 0.95\n",
|
396 |
+
"top_30 relevancy_score: 0.95\n"
|
397 |
+
]
|
398 |
+
}
|
399 |
+
]
|
400 |
+
}
|
401 |
+
]
|
402 |
+
}
|