Skip to content

Commit 3e7e37d

Browse files
committed
fixed rag source format
1 parent 00e956a commit 3e7e37d

File tree

3 files changed

+36
-32
lines changed
  • content/posts/machine learning/deep learning/NLP/Gemma2+RAG
  • public

3 files changed

+36
-32
lines changed

content/posts/machine learning/deep learning/NLP/Gemma2+RAG/index.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ import faiss
8989
```
9090

9191

92-
## Data Loading
92+
## 4. Data Loading
9393
* Use *SimpleDirectoryReader* from llama_index.
9494

9595

@@ -98,7 +98,7 @@ import faiss
9898
documents = SimpleDirectoryReader('/kaggle/input/superconductivity-lectures/').load_data()
9999
```
100100

101-
## Load Embedding Model
101+
## 5. Load Embedding Model
102102
* It uses the "sentence-transformers/all-MiniLM-L6-v2" model to create vector representations of text.
103103
* This model is known for its efficiency in creating semantic embeddings.
104104

@@ -110,7 +110,7 @@ embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-
110110

111111

112112

113-
## 4. Language Model Setup and Loading
113+
## 6. Language Model Setup and Loading
114114
* It uses the "google/gemma-2-9b-it" model, a powerful instruction-tuned language model.
115115
* It configures 8-bit quantization to reduce memory usage
116116
* The tokenizer is set globally for consistency.
@@ -135,7 +135,7 @@ llm_model = HuggingFaceLLM(model_name="google/gemma-2-9b-it",
135135

136136

137137

138-
## 5. Direct LLM Querying
138+
## 7. Direct LLM Querying
139139
This part demonstrates direct querying of the LLM:
140140

141141
* It defines a list of queries about superconductivity.
@@ -326,7 +326,7 @@ Let me know if you'd like me to calculate the numerical value of the magnetic fi
326326

327327

328328

329-
## 6. Vector Store and Index Creation
329+
## 8. Vector Store and Index Creation
330330
This section sets up the vector store and creates the index:
331331

332332
* It initializes a FAISS index with the embedding dimension of 384 (the same as the embedding model)
@@ -353,7 +353,7 @@ index.storage_context.persist()
353353

354354

355355

356-
## 7. RAG Querying
356+
## 9. RAG Querying
357357
* Compare these results with the previous Direct LLM queries
358358
* The default *similarity_top_k* values is 3. However, I set it up to 5 to have more exhaustive answers.
359359
* We expect more accurate and truthful answers. Anyway, when asked about London Equations, they are wrong. Also in the first query, direct LLM provides only few scientists but do not quote "Josephson" in any case (even after multiple generation).
@@ -385,6 +385,7 @@ for i, resp in enumerate(rag_responses):
385385

386386

387387
<span style="font-size:1.5em;font-weight:700"> Which scientists contributed the most to superconductivity? </span>
388+
388389
Sources: _['Lecture1.pdf', 'Lecture1.pdf', 'Lecture1.pdf', 'Lecture1.pdf', 'Lecture1.pdf']_
389390

390391

@@ -411,6 +412,7 @@ The text emphasizes the importance of understanding the microscopic mechanism of
411412

412413

413414
<span style="font-size:1.5em;font-weight:700"> Which are the differences between Type-I and Type-II superconductors? Describe magnetical properties and show formulas. </span>
415+
414416
Sources: _['Lecture2.pdf', 'Lecture2.pdf', 'Lecture1.pdf', 'Lecture3.pdf', 'Lecture1.pdf']_
415417

416418

@@ -459,6 +461,7 @@ Let me know if you have any other questions.
459461

460462

461463
<span style="font-size:1.5em;font-weight:700"> What are the London Equation? Why are they important? </span>
464+
462465
Sources: _['Lecture1.pdf', 'Lecture3.pdf', 'Lecture3.pdf', 'Lecture3.pdf', 'Lecture1.pdf']_
463466

464467

@@ -497,8 +500,8 @@ The provided text highlights the historical development of superconductivity the
497500

498501

499502

500-
501503
<span style="font-size:1.5em;font-weight:700"> Solve this problem: Consider a bulk superconductor containing a cylindrical hole of 0.1 mm diameter. There are 7 magnetic flux quanta trapped in the hole. Find the magnetic field in the hole.</span>
504+
502505
Sources: _['Lecture3.pdf', 'Lecture3.pdf', 'Lecture3.pdf', 'Lecture3.pdf', 'Lecture3.pdf']_
503506

504507

@@ -540,9 +543,9 @@ Substitute the values of Φ0, d, and π into the equation to obtain the numerica
540543

541544
Let me know if you have any further questions.
542545

546+
---
543547

544-
545-
## 8. Conclusion
548+
## 10. Conclusion
546549
This implementation demonstrates the power of RAG in combining the strengths of large language models with the ability to retrieve and utilize specific, relevant information. By using FAISS for efficient similarity search and a state-of-the-art language model like Gemma-2-9b, this system can provide informed, context-aware responses to complex queries about superconductivity.
547550
The comparison between direct LLM responses and RAG responses would likely show the benefits of RAG in providing more detailed, accurate, and source-backed information. This approach is particularly valuable in domains requiring up-to-date or specialized knowledge, where the LLM's pre-trained knowledge might be insufficient or outdated.
548551

public/index.json

+1-1
Large diffs are not rendered by default.

public/posts/machine-learning/deep-learning/nlp/gemma2+rag/index.html

+23-22
Original file line numberDiff line numberDiff line change
@@ -575,20 +575,20 @@ <h2 id="2-setup-and-import">2. Setup and Import</h2>
575575
</span></span><span style="display:flex;"><span>
576576
</span></span><span style="display:flex;"><span><span style="color:#f92672">from</span> llama_index.vector_stores.faiss <span style="color:#f92672">import</span> FaissVectorStore
577577
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> faiss
578-
</span></span></code></pre></div><h2 id="data-loading">Data Loading</h2>
578+
</span></span></code></pre></div><h2 id="4-data-loading">4. Data Loading</h2>
579579
<ul>
580580
<li>Use <em>SimpleDirectoryReader</em> from llama_index.</li>
581581
</ul>
582582
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#75715e"># Load the PDF</span>
583583
</span></span><span style="display:flex;"><span>documents <span style="color:#f92672">=</span> SimpleDirectoryReader(<span style="color:#e6db74">&#39;/kaggle/input/superconductivity-lectures/&#39;</span>)<span style="color:#f92672">.</span>load_data()
584-
</span></span></code></pre></div><h2 id="load-embedding-model">Load Embedding Model</h2>
584+
</span></span></code></pre></div><h2 id="5-load-embedding-model">5. Load Embedding Model</h2>
585585
<ul>
586586
<li>It uses the &ldquo;sentence-transformers/all-MiniLM-L6-v2&rdquo; model to create vector representations of text.</li>
587587
<li>This model is known for its efficiency in creating semantic embeddings.</li>
588588
</ul>
589589
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#75715e"># Load embedding model</span>
590590
</span></span><span style="display:flex;"><span>embed_model <span style="color:#f92672">=</span> HuggingFaceEmbedding(model_name<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;sentence-transformers/all-MiniLM-L6-v2&#34;</span>)
591-
</span></span></code></pre></div><h2 id="4-language-model-setup-and-loading">4. Language Model Setup and Loading</h2>
591+
</span></span></code></pre></div><h2 id="6-language-model-setup-and-loading">6. Language Model Setup and Loading</h2>
592592
<ul>
593593
<li>It uses the &ldquo;google/gemma-2-9b-it&rdquo; model, a powerful instruction-tuned language model.</li>
594594
<li>It configures 8-bit quantization to reduce memory usage</li>
@@ -608,7 +608,7 @@ <h2 id="2-setup-and-import">2. Setup and Import</h2>
608608
</span></span><span style="display:flex;"><span> generate_kwargs<span style="color:#f92672">=</span>{<span style="color:#e6db74">&#34;temperature&#34;</span>: <span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#34;num_return_sequences&#34;</span>:<span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#34;do_sample&#34;</span>: <span style="color:#66d9ef">False</span>},
609609
</span></span><span style="display:flex;"><span> model_kwargs<span style="color:#f92672">=</span>{<span style="color:#e6db74">&#34;quantization_config&#34;</span>: quantization_config},
610610
</span></span><span style="display:flex;"><span> device_map<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;auto&#39;</span>)
611-
</span></span></code></pre></div><h2 id="5-direct-llm-querying">5. Direct LLM Querying</h2>
611+
</span></span></code></pre></div><h2 id="7-direct-llm-querying">7. Direct LLM Querying</h2>
612612
<p>This part demonstrates direct querying of the LLM:</p>
613613
<ul>
614614
<li>It defines a list of queries about superconductivity.</li>
@@ -788,7 +788,7 @@ <h2 id="2-setup-and-import">2. Setup and Import</h2>
788788
</ul>
789789
<p>Calculate the magnetic field (B) using these values.</p>
790790
<p>Let me know if you&rsquo;d like me to calculate the numerical value of the magnetic field.</p>
791-
<h2 id="6-vector-store-and-index-creation">6. Vector Store and Index Creation</h2>
791+
<h2 id="8-vector-store-and-index-creation">8. Vector Store and Index Creation</h2>
792792
<p>This section sets up the vector store and creates the index:</p>
793793
<ul>
794794
<li>It initializes a FAISS index with the embedding dimension of 384 (the same as the embedding model)</li>
@@ -807,7 +807,7 @@ <h2 id="6-vector-store-and-index-creation">6. Vector Store and Index Creation</h
807807
</span></span><span style="display:flex;"><span>
808808
</span></span><span style="display:flex;"><span><span style="color:#75715e"># save the vector store locally</span>
809809
</span></span><span style="display:flex;"><span>index<span style="color:#f92672">.</span>storage_context<span style="color:#f92672">.</span>persist()
810-
</span></span></code></pre></div><h2 id="7-rag-querying">7. RAG Querying</h2>
810+
</span></span></code></pre></div><h2 id="9-rag-querying">9. RAG Querying</h2>
811811
<ul>
812812
<li>Compare these results with the previous Direct LLM queries</li>
813813
<li>The default <em>similarity_top_k</em> values is 3. However, I set it up to 5 to have more exhaustive answers.</li>
@@ -825,8 +825,8 @@ <h2 id="6-vector-store-and-index-creation">6. Vector Store and Index Creation</h
825825
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">for</span> node <span style="color:#f92672">in</span> resp<span style="color:#f92672">.</span>source_nodes:
826826
</span></span><span style="display:flex;"><span> sources<span style="color:#f92672">.</span>append(node<span style="color:#f92672">.</span>metadata[<span style="color:#e6db74">&#39;file_name&#39;</span>])
827827
</span></span><span style="display:flex;"><span> display(Markdown(<span style="color:#e6db74">&#34;## &#34;</span> <span style="color:#f92672">+</span> queries[i] <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34;</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span> <span style="color:#f92672">+</span> <span style="color:#e6db74">f</span><span style="color:#e6db74">&#34;Sources: _</span><span style="color:#e6db74">{</span>sources<span style="color:#e6db74">}</span><span style="color:#e6db74">_</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span> <span style="color:#f92672">+</span> resp<span style="color:#f92672">.</span>response))
828-
</span></span></code></pre></div><p><span style="font-size:1.5em;font-weight:700"> Which scientists contributed the most to superconductivity? </span>
829-
Sources: <em>[&lsquo;Lecture1.pdf&rsquo;, &lsquo;Lecture1.pdf&rsquo;, &lsquo;Lecture1.pdf&rsquo;, &lsquo;Lecture1.pdf&rsquo;, &lsquo;Lecture1.pdf&rsquo;]</em></p>
828+
</span></span></code></pre></div><p><span style="font-size:1.5em;font-weight:700"> Which scientists contributed the most to superconductivity? </span></p>
829+
<p>Sources: <em>[&lsquo;Lecture1.pdf&rsquo;, &lsquo;Lecture1.pdf&rsquo;, &lsquo;Lecture1.pdf&rsquo;, &lsquo;Lecture1.pdf&rsquo;, &lsquo;Lecture1.pdf&rsquo;]</em></p>
830830
<p>Based on the provided text, the scientists who contributed the most to superconductivity are:</p>
831831
<ul>
832832
<li><strong>Heike Kamerlingh Onnes:</strong> Discovered superconductivity.</li>
@@ -841,8 +841,8 @@ <h2 id="6-vector-store-and-index-creation">6. Vector Store and Index Creation</h
841841
<li><strong>John Cooper:</strong> His work on electron pairing in superconductors was crucial for the development of the BCS theory.</li>
842842
</ul>
843843
<p>The text emphasizes the importance of understanding the microscopic mechanism of superconductivity, highlighting the contributions of Cooper and the development of the BCS theory. It also provides some insights into why certain materials, like noble metals, do not become superconductors.</p>
844-
<p><span style="font-size:1.5em;font-weight:700"> Which are the differences between Type-I and Type-II superconductors? Describe magnetical properties and show formulas. </span>
845-
Sources: <em>[&lsquo;Lecture2.pdf&rsquo;, &lsquo;Lecture2.pdf&rsquo;, &lsquo;Lecture1.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture1.pdf&rsquo;]</em></p>
844+
<p><span style="font-size:1.5em;font-weight:700"> Which are the differences between Type-I and Type-II superconductors? Describe magnetical properties and show formulas. </span></p>
845+
<p>Sources: <em>[&lsquo;Lecture2.pdf&rsquo;, &lsquo;Lecture2.pdf&rsquo;, &lsquo;Lecture1.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture1.pdf&rsquo;]</em></p>
846846
<p>Superconductors can be divided into two groups, Type-I and Type-II, characterized by their different responses to external magnetic fields. This classification is crucial in understanding the behavior of superconductors in various applications.</p>
847847
<p><strong>Type-I Superconductors:</strong></p>
848848
<ul>
@@ -893,8 +893,8 @@ <h2 id="6-vector-store-and-index-creation">6. Vector Store and Index Creation</h
893893
</li>
894894
</ul>
895895
<p>Let me know if you have any other questions.</p>
896-
<p><span style="font-size:1.5em;font-weight:700"> What are the London Equation? Why are they important? </span>
897-
Sources: <em>[&lsquo;Lecture1.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture1.pdf&rsquo;]</em></p>
896+
<p><span style="font-size:1.5em;font-weight:700"> What are the London Equation? Why are they important? </span></p>
897+
<p>Sources: <em>[&lsquo;Lecture1.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture1.pdf&rsquo;]</em></p>
898898
<p>The London equations are a set of two fundamental equations that describe the behavior of superconductors in electromagnetic fields. They are:</p>
899899
<ul>
900900
<li><strong>Equation (3.6):</strong> ∇²H = - (4π/λ²)J<sub>s</sub></li>
@@ -930,8 +930,8 @@ <h2 id="6-vector-store-and-index-creation">6. Vector Store and Index Creation</h
930930
</ul>
931931
<p><strong>Contextual Connection:</strong></p>
932932
<p>The provided text highlights the historical development of superconductivity theory, culminating in the BCS theory. The London equations, while a simplified model, played a crucial role in laying the groundwork for these later, more sophisticated theories. They provided the first concrete explanation for the phenomenon of perfect diamagnetism and zero resistance, paving the way for a deeper understanding of superconductivity.</p>
933-
<p><span style="font-size:1.5em;font-weight:700"> Solve this problem: Consider a bulk superconductor containing a cylindrical hole of 0.1 mm diameter. There are 7 magnetic flux quanta trapped in the hole. Find the magnetic field in the hole.</span>
934-
Sources: <em>[&lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;]</em></p>
933+
<p><span style="font-size:1.5em;font-weight:700"> Solve this problem: Consider a bulk superconductor containing a cylindrical hole of 0.1 mm diameter. There are 7 magnetic flux quanta trapped in the hole. Find the magnetic field in the hole.</span></p>
934+
<p>Sources: <em>[&lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;, &lsquo;Lecture3.pdf&rsquo;]</em></p>
935935
<p>To solve this problem, we can use the concept of magnetic flux quantization in superconductors.</p>
936936
<p><strong>1. Magnetic Flux Quantization:</strong></p>
937937
<p>Each flux quantum (Φ0) is given by:</p>
@@ -954,7 +954,8 @@ <h2 id="6-vector-store-and-index-creation">6. Vector Store and Index Creation</h
954954
<p><strong>3. Numerical Calculation:</strong></p>
955955
<p>Substitute the values of Φ0, d, and π into the equation to obtain the numerical value of the magnetic field in the hole.</p>
956956
<p>Let me know if you have any further questions.</p>
957-
<h2 id="8-conclusion">8. Conclusion</h2>
957+
<hr>
958+
<h2 id="10-conclusion">10. Conclusion</h2>
958959
<p>This implementation demonstrates the power of RAG in combining the strengths of large language models with the ability to retrieve and utilize specific, relevant information. By using FAISS for efficient similarity search and a state-of-the-art language model like Gemma-2-9b, this system can provide informed, context-aware responses to complex queries about superconductivity.
959960
The comparison between direct LLM responses and RAG responses would likely show the benefits of RAG in providing more detailed, accurate, and source-backed information. This approach is particularly valuable in domains requiring up-to-date or specialized knowledge, where the LLM&rsquo;s pre-trained knowledge might be insufficient or outdated.</p>
960961

@@ -1121,13 +1122,13 @@ <h5 class="text-center ps-3">Table of Contents</h5>
11211122
<li><a href="#1-introduction">1. Introduction</a></li>
11221123
<li><a href="#2-setup-and-import">2. Setup and Import</a></li>
11231124
<li><a href="#3-model-and-vectordb-imports">3. Model and VectorDB imports</a></li>
1124-
<li><a href="#data-loading">Data Loading</a></li>
1125-
<li><a href="#load-embedding-model">Load Embedding Model</a></li>
1126-
<li><a href="#4-language-model-setup-and-loading">4. Language Model Setup and Loading</a></li>
1127-
<li><a href="#5-direct-llm-querying">5. Direct LLM Querying</a></li>
1128-
<li><a href="#6-vector-store-and-index-creation">6. Vector Store and Index Creation</a></li>
1129-
<li><a href="#7-rag-querying">7. RAG Querying</a></li>
1130-
<li><a href="#8-conclusion">8. Conclusion</a></li>
1125+
<li><a href="#4-data-loading">4. Data Loading</a></li>
1126+
<li><a href="#5-load-embedding-model">5. Load Embedding Model</a></li>
1127+
<li><a href="#6-language-model-setup-and-loading">6. Language Model Setup and Loading</a></li>
1128+
<li><a href="#7-direct-llm-querying">7. Direct LLM Querying</a></li>
1129+
<li><a href="#8-vector-store-and-index-creation">8. Vector Store and Index Creation</a></li>
1130+
<li><a href="#9-rag-querying">9. RAG Querying</a></li>
1131+
<li><a href="#10-conclusion">10. Conclusion</a></li>
11311132
</ul>
11321133
</nav>
11331134
</div>

0 commit comments

Comments
 (0)