|
| 1 | +# Retrieval Augmented Generation (RAG) |
| 2 | + |
| 3 | +Feast provides built-in support for Retrieval Augmented Generation (RAG) through its `FeastRAGRetriever` class, which integrates with HuggingFace's transformers library. This functionality allows you to use your feature store as a knowledge base for LLM applications. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To use the RAG functionality, install Feast with the RAG extras: |
| 8 | + |
| 9 | +```bash |
| 10 | +pip install feast[rag] |
| 11 | +``` |
| 12 | + |
| 13 | +This will install the necessary dependencies including `transformers`, `sentence-transformers`, and `torch`. |
| 14 | + |
| 15 | +## Overview |
| 16 | + |
| 17 | +The RAG implementation in Feast consists of two main components: |
| 18 | + |
| 19 | +1. **Vector Store**: Abstract interface and Feast implementation for storing and querying vector embeddings |
| 20 | +2. **RAG Retriever**: Implementation that extends HuggingFace's `RagRetriever` class |
| 21 | + |
| 22 | +## Components |
| 23 | + |
| 24 | +### Vector Store |
| 25 | + |
| 26 | +The `VectorStore` abstract base class defines the interface for vector storage and retrieval: |
| 27 | + |
| 28 | +```python |
| 29 | +from feast import VectorStore |
| 30 | + |
| 31 | +class CustomVectorStore(VectorStore): |
| 32 | + def query( |
| 33 | + self, |
| 34 | + query_vector: Optional[np.ndarray] = None, |
| 35 | + query_string: Optional[str] = None, |
| 36 | + top_k: int = 10, |
| 37 | + ): |
| 38 | + # Implement vector/text search logic here |
| 39 | + pass |
| 40 | +``` |
| 41 | + |
| 42 | +Feast provides a built-in implementation `FeastVectorStore` that uses Feast's feature store capabilities: |
| 43 | + |
| 44 | +```python |
| 45 | +from feast import FeastVectorStore |
| 46 | + |
| 47 | +vector_store = FeastVectorStore( |
| 48 | + store=feature_store, |
| 49 | + rag_view=document_feature_view, |
| 50 | + features=["embedding", "text"] |
| 51 | +) |
| 52 | +``` |
| 53 | + |
| 54 | +### RAG Retriever |
| 55 | + |
| 56 | +The `FeastRAGRetriever` class extends HuggingFace's `RagRetriever` to provide seamless integration with Feast: |
| 57 | + |
| 58 | +```python |
| 59 | +from feast import FeastRAGRetriever, FeastIndex |
| 60 | + |
| 61 | +retriever = FeastRAGRetriever( |
| 62 | + question_encoder_tokenizer=tokenizer, |
| 63 | + question_encoder=encoder, |
| 64 | + generator_tokenizer=generator_tokenizer, |
| 65 | + generator_model=generator_model, |
| 66 | + feast_repo_path="./feature_repo", |
| 67 | + vector_store=vector_store, |
| 68 | + search_type="hybrid", # Can be "text", "vector", or "hybrid" |
| 69 | + config=config, |
| 70 | + index=FeastIndex(vector_store) |
| 71 | +) |
| 72 | +``` |
| 73 | + |
| 74 | +## Usage Example |
| 75 | + |
| 76 | +Here's a complete example of setting up and using RAG with Feast: |
| 77 | + |
| 78 | +```python |
| 79 | +from feast import FeatureStore, FeatureView, Field, FeastVectorStore, FeastRAGRetriever, FeastIndex |
| 80 | +from feast.types import Array, Float32, String |
| 81 | +from transformers import T5Tokenizer, T5ForConditionalGeneration |
| 82 | +from sentence_transformers import SentenceTransformer |
| 83 | + |
| 84 | +# 1. Set up your feature view for document storage |
| 85 | +document_view = FeatureView( |
| 86 | + name="document_store", |
| 87 | + schema=[ |
| 88 | + Field(name="text", dtype=String), |
| 89 | + Field(name="embedding", dtype=Array(Float32, (384,))) |
| 90 | + ], |
| 91 | + # ... other feature view configuration |
| 92 | +) |
| 93 | + |
| 94 | +# 2. Initialize the vector store |
| 95 | +store = FeatureStore(repo_path="./feature_repo") |
| 96 | +vector_store = FeastVectorStore( |
| 97 | + store=store, |
| 98 | + rag_view=document_view, |
| 99 | + features=["embedding", "text"] |
| 100 | +) |
| 101 | + |
| 102 | +# 3. Initialize models |
| 103 | +tokenizer = T5Tokenizer.from_pretrained("t5-small") |
| 104 | +model = T5ForConditionalGeneration.from_pretrained("t5-small") |
| 105 | +query_encoder = SentenceTransformer("all-MiniLM-L6-v2") |
| 106 | + |
| 107 | +# 4. Create the RAG retriever |
| 108 | +retriever = FeastRAGRetriever( |
| 109 | + question_encoder_tokenizer=tokenizer, |
| 110 | + question_encoder=model, |
| 111 | + generator_tokenizer=tokenizer, |
| 112 | + generator_model=model, |
| 113 | + feast_repo_path="./feature_repo", |
| 114 | + vector_store=vector_store, |
| 115 | + search_type="hybrid", |
| 116 | + config={"index_name": "docs"}, |
| 117 | + index=FeastIndex(vector_store), |
| 118 | + query_encoder_model=query_encoder |
| 119 | +) |
| 120 | + |
| 121 | +# 5. Use the retriever |
| 122 | +# For retrieval only |
| 123 | +doc_scores, doc_dicts = retriever.retrieve( |
| 124 | + question_hidden_states, |
| 125 | + n_docs=5 |
| 126 | +) |
| 127 | + |
| 128 | +# For generation with retrieved context |
| 129 | +answer = retriever.generate_answer( |
| 130 | + "What is machine learning?", |
| 131 | + top_k=3, |
| 132 | + max_new_tokens=100 |
| 133 | +) |
| 134 | +``` |
| 135 | + |
| 136 | +## Search Types |
| 137 | + |
| 138 | +The `FeastRAGRetriever` supports three types of search: |
| 139 | + |
| 140 | +1. **text**: Pure text-based search using the query string |
| 141 | +2. **vector**: Pure vector similarity search using encoded query embeddings |
| 142 | +3. **hybrid**: Combination of text and vector search |
| 143 | + |
| 144 | +## Configuration |
| 145 | + |
| 146 | +Key configuration options for `FeastRAGRetriever`: |
| 147 | + |
| 148 | +- `search_type`: The type of search to perform ("text", "vector", or "hybrid") |
| 149 | +- `query_encoder_model`: Model to use for encoding queries (string path or SentenceTransformer instance) |
| 150 | +- `format_document`: Optional function to customize document formatting (defaults to key-value format) |
| 151 | +- `id_field`: Field to use as document ID |
| 152 | + |
| 153 | +## Performance Considerations |
| 154 | + |
| 155 | +1. For optimal performance, ensure your feature view containing document embeddings has appropriate indexing |
| 156 | +2. Consider using a more powerful model than T5-small for production use cases |
| 157 | +3. The quality of retrieval depends heavily on the choice of embedding model |
| 158 | + |
| 159 | +## Limitations |
| 160 | + |
| 161 | +1. The current implementation requires the entire document corpus to be stored in Feast |
| 162 | +2. Vector similarity search performance depends on the underlying feature store implementation |
| 163 | +3. Document retrieval is optimized for cosine similarity scoring |
| 164 | + |
| 165 | +## See Also |
| 166 | + |
| 167 | +- [Feature Views Documentation](feature-repository.md) |
| 168 | +- [Online Store Documentation](online-stores/) |
0 commit comments