|
| 1 | +This is a demo to show how you can use Feast to do RAG |
| 2 | + |
| 3 | +## Installation via PyEnv and Poetry |
| 4 | + |
| 5 | +This demo assumes you have Pyenv (2.3.10) and Poetry (1.4.1) installed on your machine as well as Python 3.9. |
| 6 | + |
| 7 | +```bash |
| 8 | +pyenv local 3.9 |
| 9 | +poetry shell |
| 10 | +poetry install |
| 11 | +``` |
| 12 | +## Setting up the data and Feast |
| 13 | + |
| 14 | +To fetch the data simply run |
| 15 | +```bash |
| 16 | +python pull_states.py |
| 17 | +``` |
| 18 | +Which will output a file called `city_wikipedia_summaries.csv`. |
| 19 | + |
| 20 | +Then run |
| 21 | +```bash |
| 22 | +python batch_score_documents.py |
| 23 | +``` |
| 24 | +Which will output data to `data/city_wikipedia_summaries_with_embeddings.parquet` |
| 25 | + |
| 26 | +Next we'll need to do some Feast work and move the data into a repo created by |
| 27 | +Feast. |
| 28 | + |
| 29 | +## Feast |
| 30 | + |
| 31 | +To get started, make sure to have Feast installed and PostGreSQL. |
| 32 | + |
| 33 | +First run |
| 34 | +```bash |
| 35 | +cp ./data feature_repo/ |
| 36 | +``` |
| 37 | + |
| 38 | +And then open the `module_4.ipynb` notebook and follow those instructions. |
| 39 | + |
| 40 | +It will walk you through a trivial tutorial to retrieve the top `k` most similar |
| 41 | +documents using PGVector. |
| 42 | + |
| 43 | +# Overview |
| 44 | + |
| 45 | +The overview is relatively simple, the goal is to define an architecture |
| 46 | +to support the following: |
| 47 | + |
| 48 | +```mermaid |
| 49 | +flowchart TD; |
| 50 | + A[Pull Data] --> B[Batch Score Embeddings]; |
| 51 | + B[Batch Score Embeddings] --> C[Materialize Online]; |
| 52 | + C[Materialize Online] --> D[Retrieval Augmented Generation]; |
| 53 | +``` |
| 54 | + |
| 55 | +# Results |
| 56 | + |
| 57 | +The simple demo shows the code below with the retrieved data shown. |
| 58 | + |
| 59 | +```python |
| 60 | +import pandas as pd |
| 61 | + |
| 62 | +from feast import FeatureStore |
| 63 | +from batch_score_documents import run_model, TOKENIZER, MODEL |
| 64 | +from transformers import AutoTokenizer, AutoModel |
| 65 | + |
| 66 | +df = pd.read_parquet("./feature_repo/data/city_wikipedia_summaries_with_embeddings.parquet") |
| 67 | + |
| 68 | +store = FeatureStore(repo_path=".") |
| 69 | + |
| 70 | +# Prepare a query vector |
| 71 | +question = "the most populous city in the U.S. state of Texas?" |
| 72 | + |
| 73 | +tokenizer = AutoTokenizer.from_pretrained(TOKENIZER) |
| 74 | +model = AutoModel.from_pretrained(MODEL) |
| 75 | +query_embedding = run_model(question, tokenizer, model) |
| 76 | +query = query_embedding.detach().cpu().numpy().tolist()[0] |
| 77 | + |
| 78 | +# Retrieve top k documents |
| 79 | +features = store.retrieve_online_documents( |
| 80 | + feature="city_embeddings:Embeddings", |
| 81 | + query=query, |
| 82 | + top_k=3 |
| 83 | +) |
| 84 | +``` |
| 85 | +And running `features_df.head()` will show: |
| 86 | + |
| 87 | +``` |
| 88 | +features_df.head() |
| 89 | + Embeddings distance |
| 90 | +0 [0.11749928444623947, -0.04684492573142052, 0.... 0.935567 |
| 91 | +1 [0.10329511761665344, -0.07897591590881348, 0.... 0.939936 |
| 92 | +2 [0.11634305864572525, -0.10321836173534393, -0... 0.983343 |
| 93 | +``` |
0 commit comments