hw description update
Browse files- rag-homework.ipynb +38 -54
rag-homework.ipynb
CHANGED
@@ -62,20 +62,17 @@
|
|
62 |
"Your task is to implement and compare two chunking strategies: fixed-sized chunking and content-aware chunking. For content-aware you could split by sentences, paragraphs or in some other way that makes sence.\n",
|
63 |
"\n",
|
64 |
"The deliverables are:\n",
|
65 |
-
"
|
66 |
-
"1. The code for chunk splitting\n",
|
67 |
-
"2. The illustrative examples of how retrived chunks differ depending on the chunking strategy.\n",
|
68 |
-
"3. The analysis of pros and cons of each strategy\n"
|
69 |
]
|
70 |
},
|
71 |
{
|
72 |
"cell_type": "code",
|
73 |
-
"execution_count":
|
74 |
"id": "f7bad8c8",
|
75 |
"metadata": {},
|
76 |
"outputs": [],
|
77 |
"source": [
|
78 |
-
"#
|
79 |
]
|
80 |
},
|
81 |
{
|
@@ -87,24 +84,28 @@
|
|
87 |
"\n",
|
88 |
"Chunks need to be vectorized and made accessible to an LLM to enable semantic search with embedding models. A current industry standard is to use a vector database to store and retrieve texts both conveniently and efficiently. There are many products out there, we'll be using [LanceDB](https://lancedb.github.io/lancedb/). LanceDB is a young product, one way it stands out is that it's embedded - it's designed not to be a standalone service but rather a part of an application, more on this [here](https://lancedb.github.io/lancedb/basic/).\n",
|
89 |
"\n",
|
90 |
-
"
|
|
|
|
|
|
|
|
|
91 |
"\n",
|
92 |
"- `sentence-transformers/all-MiniLM-L6-v2`: a light model, produces vectors of length 384\n",
|
93 |
"- `BAAI/bge-large-en-v1.5`: a much heavier model, embedding vector length is 1024\n",
|
94 |
"\n",
|
95 |
"Feel free to explore other embedding models and justify your choice.\n",
|
96 |
-
"For different embedding
|
97 |
"\n",
|
98 |
-
"Run the embedding+ingestion script as follows, make sure to look into the script and
|
99 |
"\n",
|
100 |
"```\n",
|
101 |
-
"python prep_scrips/lancedb_setup.py --emb-model <model name> --table <db table name> --input-dir <folder with docs> --num-sub-vectors <a number which is a divisor of the embedding dim>\n",
|
102 |
"```\n",
|
103 |
"\n",
|
104 |
"Before committing to your space set up environment variables on the settings tab of your space, use `.env` as a ference list of all the things you can customize. Make sure to add HF_TOKEN and OPENAI_API_KEY as secrets.\n",
|
105 |
"Not all the parameters are required to set via environment variables, most have default values.\n",
|
106 |
"\n",
|
107 |
-
"
|
108 |
"\n",
|
109 |
"To commit large files to Github use `git lfs`:\n",
|
110 |
"```\n",
|
@@ -115,11 +116,13 @@
|
|
115 |
"```\n",
|
116 |
"Then proceed as usual.\n",
|
117 |
"\n",
|
118 |
-
"For experimenting you can easily switch between embedding models/tables by changing the values of the corresponding env variables in your space (EMB_MODEL
|
119 |
"\n",
|
120 |
"The deliverables are:\n",
|
121 |
-
"1. The illustration of how retrieved documents differ depending on the embedding model
|
122 |
-
"2. The analysis of
|
|
|
|
|
123 |
]
|
124 |
},
|
125 |
{
|
@@ -129,33 +132,7 @@
|
|
129 |
"metadata": {},
|
130 |
"outputs": [],
|
131 |
"source": [
|
132 |
-
"# Embed documents and ingest into the database "
|
133 |
-
]
|
134 |
-
},
|
135 |
-
{
|
136 |
-
"cell_type": "markdown",
|
137 |
-
"id": "c929fe99-7f3f-49cf-af9f-07bac9668c2f",
|
138 |
-
"metadata": {},
|
139 |
-
"source": [
|
140 |
-
"### Step 3: DB retrieval\n",
|
141 |
-
"\n",
|
142 |
-
"To augment an LLM with a knowledge base the context provided in a prompt is enriched with relevant pieces from a DB. An incoming query is vectorized with an embedding model and top-k relevant pieces are retrieved based on similarity to a query vector. Your task is to implement database retrieval and adjust the prompt to include the documents from the DB.\n",
|
143 |
-
"\n",
|
144 |
-
"The deliverables are:\n",
|
145 |
-
"\n",
|
146 |
-
"1. The new code that enables RAG. Paste the code added and some surrounding context; indicate which file it comes from.\n",
|
147 |
-
"2. A new prompt template\n",
|
148 |
-
"3. Examples of how the output changes\n"
|
149 |
-
]
|
150 |
-
},
|
151 |
-
{
|
152 |
-
"cell_type": "code",
|
153 |
-
"execution_count": null,
|
154 |
-
"id": "5bc1399d-1c3b-48b7-a9fb-ed48a0dfaa6a",
|
155 |
-
"metadata": {},
|
156 |
-
"outputs": [],
|
157 |
-
"source": [
|
158 |
-
"# DB retrieval implementation"
|
159 |
]
|
160 |
},
|
161 |
{
|
@@ -163,9 +140,9 @@
|
|
163 |
"id": "7d818b4f-ba5a-4c81-b6d7-f3474c398d9c",
|
164 |
"metadata": {},
|
165 |
"source": [
|
166 |
-
"### Step
|
167 |
"\n",
|
168 |
-
"A reranker is a second-level model which produces similarity scores for pairs of (input query + retrieved document). Cross-encoders are conventionally used for reranking, their architecture is slightly different from retrieval models (more on it [here]). Cross-encoders are much more costly to run, therefore a retrieval model is used to get a few (dozens) highest-scoring items, and a reranker picks the best among these. The overall pipeline is similar to the recommender system indudustry standard: a light model retrieves top-n, a precise and heavy model reranks n to get top k, n is orders of magnitude larger than k.\n",
|
169 |
"\n",
|
170 |
"Cross-encoders are optional because of the overhead their usage implies. Your task is to implement a reranker using a cross-encoder and assess pros and cons of having it. Do not forget that the process of pulling the most relevant documents becomes two-staged: retrieve a larger number of items first, than rerank and keep the best top-k for context.\n",
|
171 |
"\n",
|
@@ -178,8 +155,8 @@
|
|
178 |
"The deliverables are:\n",
|
179 |
"\n",
|
180 |
"1. The code that enables a reranker.\n",
|
181 |
-
"
|
182 |
-
"
|
183 |
]
|
184 |
},
|
185 |
{
|
@@ -189,7 +166,7 @@
|
|
189 |
"metadata": {},
|
190 |
"outputs": [],
|
191 |
"source": [
|
192 |
-
"# Implement code for selecting the final documents using a cross-encoder"
|
193 |
]
|
194 |
},
|
195 |
{
|
@@ -197,18 +174,23 @@
|
|
197 |
"id": "f5816c54-a290-4cb0-b7db-3b8901998cb0",
|
198 |
"metadata": {},
|
199 |
"source": [
|
200 |
-
"### Step
|
201 |
"\n",
|
202 |
-
"The suggested Mistral-7b is a great but small model for an LLM. A larger model can be applied to a wider range of problems and do more complex reasoning. Within the scope of this project a larger model may not be beneficial but for more complex cases the difference would become apparent.
|
203 |
"\n",
|
204 |
-
"
|
205 |
"\n",
|
206 |
-
"The
|
|
|
|
|
|
|
|
|
207 |
"\n",
|
208 |
"The deliverables are:\n",
|
209 |
"\n",
|
210 |
-
"1. The comparison between outputs
|
211 |
-
"2. The difference in response times
|
|
|
212 |
]
|
213 |
},
|
214 |
{
|
@@ -226,7 +208,7 @@
|
|
226 |
"id": "70c16440",
|
227 |
"metadata": {},
|
228 |
"source": [
|
229 |
-
"### Step
|
230 |
"\n",
|
231 |
"Use a powerful LLM (e.g. GPT-4) to quantitatively evaluate outputs of two alternative setups (different embedding models, different LLMs, both etc.). For inspiration and for prompts refer to [1](https://arxiv.org/pdf/2306.05685.pdf), [2](https://arxiv.org/pdf/2401.10020.pdf), [3](https://www.airtrain.ai/blog/the-comprehensive-guide-to-llm-evaluation#high-level-approach)\n",
|
232 |
"\n",
|
@@ -243,7 +225,9 @@
|
|
243 |
"id": "39c18ba0-e54a-478f-9e60-0ea65c29238a",
|
244 |
"metadata": {},
|
245 |
"outputs": [],
|
246 |
-
"source": [
|
|
|
|
|
247 |
}
|
248 |
],
|
249 |
"metadata": {
|
|
|
62 |
"Your task is to implement and compare two chunking strategies: fixed-sized chunking and content-aware chunking. For content-aware you could split by sentences, paragraphs or in some other way that makes sence.\n",
|
63 |
"\n",
|
64 |
"The deliverables are:\n",
|
65 |
+
"- The code for chunk splitting"
|
|
|
|
|
|
|
66 |
]
|
67 |
},
|
68 |
{
|
69 |
"cell_type": "code",
|
70 |
+
"execution_count": 1,
|
71 |
"id": "f7bad8c8",
|
72 |
"metadata": {},
|
73 |
"outputs": [],
|
74 |
"source": [
|
75 |
+
"# Chunk splitting deliverables"
|
76 |
]
|
77 |
},
|
78 |
{
|
|
|
84 |
"\n",
|
85 |
"Chunks need to be vectorized and made accessible to an LLM to enable semantic search with embedding models. A current industry standard is to use a vector database to store and retrieve texts both conveniently and efficiently. There are many products out there, we'll be using [LanceDB](https://lancedb.github.io/lancedb/). LanceDB is a young product, one way it stands out is that it's embedded - it's designed not to be a standalone service but rather a part of an application, more on this [here](https://lancedb.github.io/lancedb/basic/).\n",
|
86 |
"\n",
|
87 |
+
"Find more details on how different databases compare in [this](https://thedataquarry.com/tags/vector-db/) series of posts. \n",
|
88 |
+
"\n",
|
89 |
+
"Your task is to vectorize and ingest chunked documents into the database. \n",
|
90 |
+
"**For each chunking strategy from the previous step create a separate table with one of the embedding models. Compare the chunking strategies and choose one. Perform vectorization+ingestion with the second model only with one chunking strategy of your choice**.\n",
|
91 |
+
"Use prep_scrips/lancedb_setup.py to vectorize chunks and store vector representations along with raw text in a Lancedb instance. The script also creates an index for fast ANN retrieval (not really needed for this exercise but necessary at scale). Try different embedding models and see how results differ. The options are:\n",
|
92 |
"\n",
|
93 |
"- `sentence-transformers/all-MiniLM-L6-v2`: a light model, produces vectors of length 384\n",
|
94 |
"- `BAAI/bge-large-en-v1.5`: a much heavier model, embedding vector length is 1024\n",
|
95 |
"\n",
|
96 |
"Feel free to explore other embedding models and justify your choice.\n",
|
97 |
+
"For different embedding models and different chunking strategies create different tables in the database so you can easily switch between them and compare.\n",
|
98 |
"\n",
|
99 |
+
"Run the embedding+ingestion script as follows, make sure to look into the script and go over the arguments. Note that the number of sub-vectors for indexing must be a divisor of the model embedding size.\n",
|
100 |
"\n",
|
101 |
"```\n",
|
102 |
+
"python prep_scrips/lancedb_setup.py --emb-model <model name> --table <db table name> --input-dir <folder with chunked docs> --num-sub-vectors <a number which is a divisor of the embedding dim>\n",
|
103 |
"```\n",
|
104 |
"\n",
|
105 |
"Before committing to your space set up environment variables on the settings tab of your space, use `.env` as a ference list of all the things you can customize. Make sure to add HF_TOKEN and OPENAI_API_KEY as secrets.\n",
|
106 |
"Not all the parameters are required to set via environment variables, most have default values.\n",
|
107 |
"\n",
|
108 |
+
"*The database is expected to be in the `gradio_app` folder under `.lancedb`, make sure to move it there if was initialized elsewhere.* It can be parametrized but it's unnecessary here.\n",
|
109 |
"\n",
|
110 |
"To commit large files to Github use `git lfs`:\n",
|
111 |
"```\n",
|
|
|
116 |
"```\n",
|
117 |
"Then proceed as usual.\n",
|
118 |
"\n",
|
119 |
+
"For experimenting you can easily switch between embedding models/tables by changing the values of the corresponding env variables in your space (`EMB_MODEL`, `TABLE_NAME`). Overall, every time you change the value of an environment variable a space gets automatically rebuilt.\n",
|
120 |
"\n",
|
121 |
"The deliverables are:\n",
|
122 |
+
"1. The illustration of how retrieved documents differ depending on the embedding model and the chunking strategy. You should create at least 3 tables: model_1 + chunking_strategy_1, model_1 + chunking_strategy_2, model_2 + chunking_strategy_<1 or 2>\n",
|
123 |
+
"2. The analysis of pros and cons of chunking strategies\n",
|
124 |
+
"3. The analysis of how retrieved document differ between embedding models (is one better than the other?)\n",
|
125 |
+
"4. The analysis of how the embedding time differs between models"
|
126 |
]
|
127 |
},
|
128 |
{
|
|
|
132 |
"metadata": {},
|
133 |
"outputs": [],
|
134 |
"source": [
|
135 |
+
"# Embed documents with different chunking strategies and ingest into the database "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
]
|
137 |
},
|
138 |
{
|
|
|
140 |
"id": "7d818b4f-ba5a-4c81-b6d7-f3474c398d9c",
|
141 |
"metadata": {},
|
142 |
"source": [
|
143 |
+
"### Step 3: Add a reranker\n",
|
144 |
"\n",
|
145 |
+
"A reranker is a second-level model which produces similarity scores for pairs of (input query + retrieved document). Cross-encoders are conventionally used for reranking, their architecture is slightly different from retrieval models (more on it [here] and [here](https://www.sbert.net/examples/applications/retrieve_rerank/README.html)). Cross-encoders are much more costly to run, therefore a retrieval model is used to get a few (dozens) highest-scoring items, and a reranker picks the best among these. The overall pipeline is similar to the recommender system indudustry standard: a light model retrieves top-n, a precise and heavy model reranks n to get top k, n is orders of magnitude larger than k.\n",
|
146 |
"\n",
|
147 |
"Cross-encoders are optional because of the overhead their usage implies. Your task is to implement a reranker using a cross-encoder and assess pros and cons of having it. Do not forget that the process of pulling the most relevant documents becomes two-staged: retrieve a larger number of items first, than rerank and keep the best top-k for context.\n",
|
148 |
"\n",
|
|
|
155 |
"The deliverables are:\n",
|
156 |
"\n",
|
157 |
"1. The code that enables a reranker.\n",
|
158 |
+
"3. A comparison of how the prompt and the model output change after adding a reranker\n",
|
159 |
+
"4. The analysis of pros and cons. The evaluation aspects should include the relevance of the top-k documents, the response time.\n"
|
160 |
]
|
161 |
},
|
162 |
{
|
|
|
166 |
"metadata": {},
|
167 |
"outputs": [],
|
168 |
"source": [
|
169 |
+
"# Implement code for selecting the final documents using a cross-encoder and compare with and without"
|
170 |
]
|
171 |
},
|
172 |
{
|
|
|
174 |
"id": "f5816c54-a290-4cb0-b7db-3b8901998cb0",
|
175 |
"metadata": {},
|
176 |
"source": [
|
177 |
+
"### Step 4: Try a different LLM\n",
|
178 |
"\n",
|
179 |
+
"The suggested `Mistral-7b-instruct` is a great but small model for an LLM. A larger model can be applied to a wider range of problems and do more complex reasoning. Within the scope of this project a larger model may not be beneficial but for more complex cases the difference would become apparent. Another dimension to explore is a base model which was not instruction fine-tuned - it won't respond to your queries the way you'd expect. It may be a great exercise to see the value of fine-tuning.\n",
|
180 |
"\n",
|
181 |
+
"The task here is to try out an alternative LLM to explore the differencies.\n",
|
182 |
"\n",
|
183 |
+
"The options are:\n",
|
184 |
+
"1. mistralai/Mistral-7B-v0.1\n",
|
185 |
+
"2. mistralai/Mixtral-8x7B-Instruct-v0.1\n",
|
186 |
+
"\n",
|
187 |
+
"Of couse, feel free to choose another one and give some details on how different it is from the initial model.\n",
|
188 |
"\n",
|
189 |
"The deliverables are:\n",
|
190 |
"\n",
|
191 |
+
"1. The comparison between outputs of the Mistral-7b-instuct and a different model of your choice.\n",
|
192 |
+
"2. The difference in response times if a larger model was chosen. Make sure to make multiple queries to make the comparison meaningful.\n",
|
193 |
+
"3. Analyse the differencies between outputs and share the conclusions.\n"
|
194 |
]
|
195 |
},
|
196 |
{
|
|
|
208 |
"id": "70c16440",
|
209 |
"metadata": {},
|
210 |
"source": [
|
211 |
+
"### Step 5 (Bonus): Use an LLM to quantitatively compare outputs of different variants of the system (LLM as a Judge)\n",
|
212 |
"\n",
|
213 |
"Use a powerful LLM (e.g. GPT-4) to quantitatively evaluate outputs of two alternative setups (different embedding models, different LLMs, both etc.). For inspiration and for prompts refer to [1](https://arxiv.org/pdf/2306.05685.pdf), [2](https://arxiv.org/pdf/2401.10020.pdf), [3](https://www.airtrain.ai/blog/the-comprehensive-guide-to-llm-evaluation#high-level-approach)\n",
|
214 |
"\n",
|
|
|
225 |
"id": "39c18ba0-e54a-478f-9e60-0ea65c29238a",
|
226 |
"metadata": {},
|
227 |
"outputs": [],
|
228 |
+
"source": [
|
229 |
+
"# The code implementing LLM-as-a-Judge and "
|
230 |
+
]
|
231 |
}
|
232 |
],
|
233 |
"metadata": {
|