Building an AI-powered ChatBot using Vercel, OpenAI, and Postgres
A ChatGPT-style documentation assistant (Ask Neon) is built by storing OpenAI text-embedding-ada-002 vectors in Postgres with pgvector, retrieving context by cosine distance, and feeding it to text-davinci-003 from a Vercel Edge Function. A window-function query caps retrieved context at a cumulative 1,500 tokens to fit the 4,096-token model limit, and an ivfflat index (lists ≈ 4·sqrt(rows)) speeds up the similarity search. Neon's WebSocket-based serverless driver works around Edge Functions' lack of TCP, and streaming completions via SSE parsing return answers faster. User feedback is stored for later fine-tuning.
Related posts
- Web-based AI SQL Playground and connecting to Postgres from the browser
Neon mediumAdept
Neon's Postgres AI Playground connects to a Postgres database directly from the browser and uses OpenAI's gpt-3.5-turbo to turn natural-language prompts into SQL. Browsers can't open TCP connections, so the Neon serverless driver tunnels the Postgres wire protocol through a WebSocket-to-TCP proxy. The proxy is open source and can be self-hosted to reach other Postgres providers. A Vercel edge function rate-limits requests with Upstash Redis, injects the user's schema (pulled from information_schema.columns and flattened to a compact text form) into the prompt, and streams the model's response back to the editor.
- Building a RAG application with Llama 3.1 and pgvector
Neon mediumAdept
A small RAG pipeline built on Neon Postgres with pgvector as the vector store, OctoAI's gte-large embedding model producing 1024-dimensional vectors, and Llama 3.1 405B generating answers. The walkthrough enables the vector extension, creates a table with a VECTOR(1024) column, and embeds and inserts a CSV of fake quotes from Python via psycopg2. At query time it embeds the user input, retrieves the two nearest quotes with a cosine-distance ORDER BY ... LIMIT 2 query, and injects them into the system prompt. Embedding dimension directly drives storage cost, e.g. 1024 for gte-large versus up to 3072 for OpenAI's model.
- How to be AI-fancy
Neon mediumAdept
Neon added three LLM-backed features to its SQL editor: natural-language SQL generation, automatic naming of history queries, and one-click error fixing. Generation and fixing send the database schema as context to the LLM. Neon chose inline, context-specific actions over a chat assistant because a useful assistant needs multi-stage context gathering and gives less control over the UX. The implementation lessons are a strategy-pattern interface so providers can be swapped (Claude 3.5 Sonnet on AWS Bedrock was chosen, partly to keep schema data inside AWS without extra user consent), a comparison of cloud-integrated and direct AI providers, and plain HTTP response streaming to the browser via the Fetch API reader instead of WebSockets.
- Semantic search using OpenAI, pg_embedding and Neon
Neon mediumAdept
A YC idea-matcher app implements semantic search by embedding ~4,400 YCombinator company descriptions with OpenAI's text-embedding-ada-002 (1536 dimensions) and storing them as real[] columns in Neon Postgres via the pg_embedding extension. Queries embed the user's idea and order rows by cosine distance (<=>) to return the top five matches from a Next.js edge function, which is colocated with the database region and uses the Neon serverless driver, Upstash rate limiting and Zod validation. The full ingestion script and API route are shown. Sequential scan is fine for this small dataset, but larger tables need an HNSW index. pg_embedding has since been deprecated in favor of pgvector.
- database.build v2: Bring-your-own-LLM
Supabase mediumAdeptdatabase.build, Supabase's in-browser Postgres sandbox built on PGlite, now lets users bring any OpenAI-compatible LLM, with no login and no rate limits. Requests go straight from the browser to the provider. This works because most LLM APIs send CORS headers, and a service worker intercepts the Vercel AI SDK's /api/chat route, reading its BYO-LLM config from IndexedDB. Models need reliable tool calling, SQL knowledge and Chart.js knowledge, so gpt-4o is recommended; local Ollama models still struggle with tool calls, and Ollama buffers tool-call streams. The release also adds one-click deploys to Supabase by piping server-side pg_dump over Live Share into pg_restore, and replaces the tar.gz export with a WASM pg_dump. Serverless PGlite was shelved because of its memory use and single-connection limit in multi-tenant setups and unreliable s3fs storage.