Skip to content

Commit fc99dd0

Browse files
committed
Add feast rag retriver functionality
Signed-off-by: Fiona Waters <[email protected]>
1 parent b89fadd commit fc99dd0

File tree

7 files changed

+543
-1
lines changed

7 files changed

+543
-1
lines changed

docs/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
* [Feast CLI reference](reference/feast-cli-commands.md)
157157
* [Python API reference](http://rtd.feast.dev)
158158
* [Usage](reference/usage.md)
159+
* [RAG (Retrieval Augmented Generation)](reference/rag.md)
159160

160161
## Project
161162

docs/reference/rag.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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/)

pyproject.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ snowflake = [
116116
"snowflake-connector-python[pandas]>=3.7,<4",
117117
]
118118
sqlite_vec = ["sqlite-vec==v0.1.6"]
119+
rag = [
120+
"transformers>=4.36.0",
121+
"sentence-transformers>=2.5.0",
122+
"torch>=2.2.0",
123+
]
119124

120125
ci = [
121126
"build",
@@ -162,7 +167,7 @@ ci = [
162167
"types-setuptools",
163168
"types-tabulate",
164169
"virtualenv<20.24.2",
165-
"feast[aws, azure, cassandra, clickhouse, couchbase, delta, docling, duckdb, elasticsearch, faiss, gcp, ge, go, grpcio, hazelcast, hbase, ibis, ikv, k8s, milvus, mssql, mysql, opentelemetry, spark, trino, postgres, pytorch, qdrant, redis, singlestore, snowflake, sqlite_vec]"
170+
"feast[aws, azure, cassandra, clickhouse, couchbase, delta, docling, duckdb, elasticsearch, faiss, gcp, ge, go, grpcio, hazelcast, hbase, ibis, ikv, k8s, milvus, mssql, mysql, opentelemetry, spark, trino, postgres, pytorch, qdrant, rag, redis, singlestore, snowflake, sqlite_vec]"
166171
]
167172
nlp = ["feast[docling, milvus, pytorch]"]
168173
dev = ["feast[ci]"]

sdk/python/feast/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from .repo_config import RepoConfig
2323
from .stream_feature_view import StreamFeatureView
2424
from .value_type import ValueType
25+
from .rag_retriever import FeastRAGRetriever, FeastIndex
26+
from .vector_store import VectorStore, FeastVectorStore
2527

2628
try:
2729
__version__ = _version("feast")
@@ -51,4 +53,8 @@
5153
"RequestSource",
5254
"AthenaSource",
5355
"Project",
56+
"FeastIndex",
57+
"FeastRAGRetriever",
58+
"VectorStore",
59+
"FeastVectorStore",
5460
]

0 commit comments

Comments
 (0)