Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"
DATABASE_URL="postgresql://watches:watches@localhost:${FORWARD_DB_PORT:-5432}/watches?schema=public"
EMBEDDING_MODEL=all-minilm
CHAT_MODEL=phi3:mini
41 changes: 31 additions & 10 deletions .upsun/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,46 @@ services:
- vector

applications:
ollama:
stack:
- "python@3.12"
- "ollama"
container_profile: BALANCED
relationships:
db: "postgresql:postgresql"
mounts:
"/.ollama":
source: "storage"
source_path: ".ollama"
web:
commands:
start: "OLLAMA_HOST=:$PORT ollama serve"
hooks:
build: |
set -eux
cd vectorize/
pip install -r requirements.txt
deploy: |
set -eux
set -a
. /app/.env
export OLLAMA_HOST=:$PORT
ollama pull $EMBEDDING_MODEL
ollama pull $CHAT_MODEL

cd vectorize/
python vectorize.py

app:
stack:
- "nodejs@22"
- "python@3.12"
relationships:
db: "postgresql:postgresql"
ollama: "ollama:http"
mounts:
"/.npm":
source: "storage"
source_path: "npm"
"/.cache":
source: "storage"
source_path: "cache"
web:
commands:
start: "npx next start -p $PORT"
Expand All @@ -28,12 +55,6 @@ applications:
npm install
npx prisma generate
npm run build
cd vectorize/
pip install -r requirements.txt
post_deploy: |
set -eux
cd vectorize/
python vectorize.py

routes:
"https://{default}/":
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

The data located in `vectorize/data` is a [dataset from Kaggle](https://www.kaggle.com/datasets/rkiattisak/luxury-watches-price-dataset/data).

This example uses 3 models from HuggingFace:
- `vectorize.py` uses the `sentence-transformers` and run `feature_extractor` with the `all-MiniLM-L6-v2` model locally (CPU). The model is downloaded (88MB) on the first run in the `.cache` folder. The embeddings are then stored in the postgres database. `vectorize.py` also migrates the database schema if it does not exists.
This example uses a local Ollama server:
- `vectorize.py` uses the `all-minilm` model run `feature_extractor` with the `all-MiniLM-L6-v2` model locally (CPU). The model is downloaded (88MB) on the first run in the `~/.cache/huggingface/` folder. The embeddings are then stored in the postgres database. `vectorize.py` also migrates the database schema if it does not exist.
- When the user inputs a prompt, the `recommend` action will first run `featureExtraction` on the serverless HuggingFace inference API with the same model `all-MiniLM-L6-v2` to get the embedding.
- The `recommend` action then query the database using the vector to get 5 similar watches.
- The last step of the action is to query a text generation model on HuggingFace inference API to generate a proper text answer. We are using `mistralai/Mistral-7B-Instruct-v0.2` in this example.
Expand All @@ -22,15 +22,17 @@ The `openai` branch does the same but relies on the OpenAI API for all the LLM a
cd vectorize
python3 -m venv venv
source ./venv/bin/activate
pip install -r requirements.txt (or requirements-mac.txt)
pip install -r requirements.txt
```

### Install dependencies and run services

```
docker-compose up
export FORWARD_DB_PORT=8090
docker compose up -d
cd vectorize/
DB_PATH=watches DB_HOST=127.0.0.1 DB_USERNAME=watches DB_PASSWORD=watches HUGGINGFACE_TOKEN=hf_****** python3 vectorize.py
source ../.env.local
DB_PORT=$FORWARD_DB_PORT DB_PATH=watches DB_HOST=127.0.0.1 DB_USERNAME=watches DB_PASSWORD=watches python3 vectorize.py
cd ../
npm install
npm rum dev
Expand All @@ -40,8 +42,9 @@ npm rum dev

```
upsun project:create
upsun variable:create --name HUGGINGFACE_TOKEN --prefix env: --level project
upsun push
upsun variable:create --name env:EMBEDDING_MODEL --level project --visible-build=true --value=all-minilm
upsun variable:create --name env:CHAT_MODEL --level project --visible-build=true --value=tinyllama:1.1b-chat-v1-q2_K
upsun push --resources-init=manual
```

The `vectorize.py` script is included in the `deploy` hook meaning that it will be triggered on every deploy. This is for demo purposes. You can run it manually instead to avoid delays in deployments.
The `vectorize.py` script is included in the `deploy` hook meaning that it will be triggered on every deploy. This is for demo purposes. You can run it manually instead to avoid delays in deployments.
2 changes: 1 addition & 1 deletion docker/pgsql/postgres.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM postgres:latest
FROM postgres:16

RUN apt-get update && apt-get install -y \
build-essential \
Expand Down
Loading