Visitar URL original
uploading rag demo by franciscojavierarceo · Pull Request #20 · feast-dev/feast-workshop · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ terraform.tfstate.backup
.vscode/*
**/derby.log
**/metastore_db/*
.env
.env
.idea
1 change: 1 addition & 0 deletions module_4_rag/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data/*
1 change: 1 addition & 0 deletions module_4_rag/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.9
37 changes: 37 additions & 0 deletions module_4_rag/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM python:3.9

# Set environment varibles
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code


# Install dependencies
RUN LIBMEMCACHED=/opt/local
RUN apt-get update && apt-get install -y \
libmemcached11 \
libmemcachedutil2 \
libmemcached-dev \
libz-dev \
curl \
gettext

ENV PYTHONHASHSEED=random \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
# Poetry's configuration: \
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_CREATE=false \
POETRY_CACHE_DIR='/var/cache/pypoetry' \
POETRY_HOME='/usr/local' \
POETRY_VERSION=1.4.1

RUN curl -sSL https://install.python-poetry.org | python3 - --version $POETRY_VERSION

COPY pyproject.toml poetry.lock /code/
RUN poetry install --no-interaction --no-ansi --no-root

COPY . ./code/
64 changes: 64 additions & 0 deletions module_4_rag/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
This is a demo to show how you can use Feast to do RAG

## Installation via PyEnv and Poetry

This demo assumes you have Pyenv (2.3.10) and Poetry (1.4.1) installed on your machine as well as Python 3.9.

```bash
pyenv local 3.9
poetry shell
poetry install
```
## Setting up the data and Feast

To fetch the data simply run
```bash
python pull_states.py
```
Which will output a file called `city_wikipedia_summaries.csv`.

Then run
```bash
python batch_score_documents.py
```

# Overview

The overview is relatively simple, the goal is to define an architecture
to support the following:

```mermaid
flowchart TD;
A[Pull Data] --> B[Batch Score Embeddings];
B[Batch Score Embeddings] --> C[Materialize Online];
C[Materialize Online] --> D[Retrieval Augmented Generation];
D[Retrieval Augmented Generation] --> E[Store User Interaction];
E[Store User Interaction] --> F[Update Training Labels];
F[Update Training Labels] --> H[Fine Tuning];
H[Fine Tuning] -. Backpropagate .-> B[Batch Score Embeddings];
```


A simple example of the user experience:

```
Q: Can you tell me about Chicago?
A: Here's some wikipedia facts about Chicago...
```

# Limitations
A common issue with RAG and LLMs is hallucination. There are two common
approaches:

1. Prompt engineering
- This approach is the most obvious but is susceptible to prompt injection

2. Build a Classifier to return the "I don't know" response
- This approach is less obvious, requires another model, more training data,
and fine tuning

We can, in fact, use both approaches to further attempt to minimize the
likelihood of prompt injection.

This demo will display both.

99 changes: 99 additions & 0 deletions module_4_rag/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from flask import (
Flask,
jsonify,
request,
render_template,
)
from flasgger import Swagger
from datetime import datetime
from get_features import (
get_onboarding_features,
get_onboarding_score,
get_daily_features,
get_daily_score,
)
from ml import make_risk_decision

app = Flask(__name__)
swagger = Swagger(app)


@app.route("/")
def onboarding_page():
return render_template("index.html")


@app.route("/home")
def home_page():
return render_template("home.html")


@app.route("/onboarding-risk-features/", methods=["POST"])
def onboarding_features():
"""Example endpoint returning features by id
This is using docstrings for specifications.
---
parameters:
- name: state
type: string
in: query
required: true
default: NJ

- name: ssn
type: string
in: query
required: true
default: 123-45-6789

- name: dl
type: string
in: query
required: true
default: some-dl-number

- name: dob
type: string
in: query
required: true
default: 12-23-2000
responses:
200:
description: A JSON of features
schema:
id: OnboardingFeatures
properties:
is_gt_18_years_old:
type: array
items:
schema:
id: value
type: number
is_valid_state:
type: array
items:
schema:
id: value
type: number
is_previously_seen_ssn:
type: array
items:
schema:
id: value
type: number
is_previously_seen_dl:
type: array
items:
schema:
id: value
type: number
"""
r = request.args
feature_vector = get_onboarding_features(
r.get("state"), r.get("ssn"), r.get("dl"), r.get("dob")
)
return jsonify(feature_vector)


if __name__ == "__main__":
app.run(debug=True)
47 changes: 47 additions & 0 deletions module_4_rag/batch_score_documents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os
import pandas as pd
from transformers import AutoTokenizer, AutoModel
import torch
import torch.nn.functional as F

INPUT_FILENAME = "./data/city_wikipedia_summaries.csv"
EXPORT_FILENAME = "./data/city_wikipedia_summaries_with_embeddings.parquet"
TOKENIZER = 'sentence-transformers/all-MiniLM-L6-v2'
MODEL = 'sentence-transformers/all-MiniLM-L6-v2'

def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)

def run_model(sentences, tokenizer, model):
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
# Compute token embeddings
with torch.no_grad():
model_output = model(**encoded_input)

sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
return sentence_embeddings

def score_data() -> None:
if EXPORT_FILENAME not in os.listdir():
print("scored data not found...generating embeddings...")
df = pd.read_csv(INPUT_FILENAME)
tokenizer = AutoTokenizer.from_pretrained(TOKENIZER)
model = AutoModel.from_pretrained(MODEL)
embeddings = run_model(df['Wiki Summary'].tolist(), tokenizer, model)
print(embeddings)
print('shape = ', df.shape)
df['Embeddings'] = list(embeddings.detach().cpu().numpy())
print("embeddings generated...")
df['event_timestamp'] = pd.to_datetime('today')
df["item_id"] = df.index
print(df.head())
df.to_parquet(EXPORT_FILENAME, index=False)
print("...data exported. job complete")
else:
print("scored data found...skipping generating embeddings.")

if __name__ == '__main__':
score_data()
14 changes: 14 additions & 0 deletions module_4_rag/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3.9'

services:
web:
env_file:
- .env
build: .
command:
- /bin/bash
- -c
- python3 /code/run.py

volumes:
- .:/code
3 changes: 3 additions & 0 deletions module_4_rag/feature_repo/entities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from feast import Entity

item = Entity(name="item_id")
20 changes: 20 additions & 0 deletions module_4_rag/feature_repo/feature_store.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
project: feast_demo_local
provider: local
registry:
registry_type: sql
path: postgresql://@localhost:5432/feast
online_store:
type: postgres
pgvector_enabled: true
vector_len: 384
host: 127.0.0.1
port: 5432
database: feast
user: ""
password: ""


offline_store:
type: file
entity_key_serialization_version: 2

28 changes: 28 additions & 0 deletions module_4_rag/feature_repo/features.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from datetime import timedelta

from feast import (
FeatureView,
Field, FileSource,
)
from feast.data_format import ParquetFormat
from feast.types import Float32, Array
from entities import item


parquet_file_path = "../data/city_wikipedia_summaries_with_embeddings.parquet"

source = FileSource(
file_format=ParquetFormat(),
path=parquet_file_path,
timestamp_field="event_timestamp",
)

city_embeddings_feature_view = FeatureView(
name="city_embeddings",
entities=[item],
schema=[
Field(name="Embeddings", dtype=Array(Float32)),
],
source=source,
ttl=timedelta(hours=2),
)
Loading