Elffuss BraidHub

kikocis/lancedb public

main 路 2.4 KiBRenderedRawPermalink
1**Vanilla RAG 馃尡**
2====================================================================
3
4RAG(Retrieval-Augmented Generation) works by finding documents related to the user's question, combining them with a prompt for a large language model (LLM), and then using the LLM to create more accurate and relevant answers.
5
6Here鈥檚 a simple guide to building a RAG pipeline from scratch:
7
81. **Data Loading**: Gather and load the documents you want to use for answering questions.
9
102. **Chunking and Embedding**: Split the documents into smaller chunks and convert them into numerical vectors (embeddings) that capture their meaning.
11
123. **Vector Store**: Create a LanceDB table to store and manage these vectors for quick access during retrieval.
13
144. **Retrieval & Prompt Preparation**: When a question is asked, find the most relevant document chunks from the table and prepare a prompt combining these chunks with the question.
15
165. **Answer Generation**: Send the prepared prompt to a LLM to generate a detailed and accurate answer.
17
18<figure markdown="span">
19 ![agent-based-rag](https://raw.githubusercontent.com/lancedb/assets/main/docs/assets/rag/rag_from_scratch.png)
20 <figcaption>Vanilla RAG
21 </figcaption>
22</figure>
23
24[![Open In Colab](../assets/colab.svg)](https://colab.research.google.com/github/lancedb/vectordb-recipes/blob/main/tutorials/RAG-from-Scratch/RAG_from_Scratch.ipynb)
25
26Here鈥檚 a code snippet for defining a table with the [Embedding API](https://lancedb.github.io/lancedb/embeddings/embedding_functions/), which simplifies the process by handling embedding extraction and querying in one step.
27
28```python
29import pandas as pd
30import lancedb
31from lancedb.pydantic import LanceModel, Vector
32from lancedb.embeddings import get_registry
33
34db = lancedb.connect("/tmp/db")
35model = get_registry().get("sentence-transformers").create(name="BAAI/bge-small-en-v1.5", device="cpu")
36
37class Docs(LanceModel):
38 text: str = model.SourceField()
39 vector: Vector(model.ndims()) = model.VectorField()
40
41table = db.create_table("docs", schema=Docs)
42
43# considering chunks are in list format
44df = pd.DataFrame({'text':chunks})
45table.add(data=df)
46
47query = "What is issue date of lease?"
48actual = table.search(query).limit(1).to_list()[0]
49print(actual.text)
50```
51
52Check Colab for the complete code
53
54[![Open In Colab](../assets/colab.svg)](https://colab.research.google.com/github/lancedb/vectordb-recipes/blob/main/tutorials/RAG-from-Scratch/RAG_from_Scratch.ipynb)