A PostgreSQL + pgvector system to store journal entries and answer natural language questions via vector similarity search.
npm installRun the Docker container with pgvector support:
docker run -d \
--name journal-db \
-p 5432:5432 \
-e POSTGRES_PASSWORD=postgres \
ankane/pgvectorConnect to the PostgreSQL database and run the schema:
psql -U postgres -h localhost -d postgres -f schema.sqlEdit .env and add your OpenAI API key:
OPENAI_API_KEY=sk-...
npm startThe server will start on http://localhost:3000
POST /journal
Request:
{
"entry": "played tennis with nicey after work"
}Response:
{
"status": "saved"
}POST /ask
Request:
{
"question": "what racket sports did i play in the last 2 weeks?"
}Response:
{
"answer": "You played tennis, badminton, and squash in the last two weeks.",
"matches": [
{
"id": 1,
"entry": "played tennis with nicey after work",
"created_at": "2026-03-08T06:34:01.517Z"
},
{
"id": 2,
"entry": "tried badminton with coworkers",
"created_at": "2026-02-20T06:34:02.000Z"
},
{
"id": 4,
"entry": "played squash at the gym",
"created_at": "2026-02-28T06:34:03.000Z"
}
]
}- Question is converted to an embedding using OpenAI's text-embedding-3-small model
- PostgreSQL pgvector performs similarity search using the
<->operator (L2 distance) - Top 5 matching entries are retrieved from the database
- GPT-4o-mini generates a natural language answer using the retrieved entries as context
# Seed the database
npm run seed
# Or add entries manually
curl -X POST http://localhost:3000/journal \
-H "Content-Type: application/json" \
-d '{"entry": "played tennis with sam after work"}'
# Ask a question
curl -X POST http://localhost:3000/ask \
-H "Content-Type: application/json" \
-d '{"question": "what racket sports did i play in the last 2 months?"}'