---
title: "Building an AI-powered ChatBot using Vercel, OpenAI, and Postgres"
description: "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."
url: "https://swe.quest/articles/building-an-ai-powered-chatbot-using-vercel-openai-and-postgres-4fd423473384581f"
canonical: "https://neon.com/blog/building-an-ai-powered-chatbot-using-vercel-openai-and-postgres"
source: "Neon"
published: "2023-03-09"
difficulty: "medium"
kind: "How-to"
reading_minutes: 13
tags: ["rag","postgres","vector-search","llms","serverless"]
---

# Building an AI-powered ChatBot using Vercel, OpenAI, and Postgres

A post on [Neon](https://swe.quest/sources/neon.md), published 2023-03-09, 13 min read, How-to. Original: [https://neon.com/blog/building-an-ai-powered-chatbot-using-vercel-openai-and-postgres](https://neon.com/blog/building-an-ai-powered-chatbot-using-vercel-openai-and-postgres)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [RAG & retrieval](https://swe.quest/tags/rag.md) (AI & ML)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Vector search](https://swe.quest/tags/vector-search.md) (Data & databases)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Serverless](https://swe.quest/tags/serverless.md) (Backend & APIs)

## Prerequisites

- Postgres basics ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- OpenAI API
- JavaScript fetch and streams ([easy posts on JavaScript](https://swe.quest/tags/javascript.md?difficulty=easy))

## Keywords

pgvector, Neon, OpenAI, Vercel Edge Functions, text-embedding-ada-002, text-davinci-003, ivfflat, Next.js

## Related posts

- [Web-based AI SQL Playground and connecting to Postgres from the browser](https://swe.quest/articles/web-based-ai-sql-playground-and-connecting-to-postgres-from-the-browser-fa7f4eccd9643a37.md): Neon · 2023-05-19 · Medium · 5 min read · PostgreSQL, LLMs, Serverless, JavaScript
  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.
- [Building a RAG application with Llama 3.1 and pgvector](https://swe.quest/articles/building-a-rag-application-with-llama-3-1-and-pgvector-aa7660e0eb99da02.md): Neon · 2024-07-30 · Medium · 12 min read · RAG & retrieval, Vector search, PostgreSQL, LLMs, Python
  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.
- [How to be AI-fancy](https://swe.quest/articles/how-to-be-ai-fancy-1617ad0989d06ba6.md): Neon · 2024-11-04 · Medium · 6 min read · LLMs, Product engineering, PostgreSQL, TypeScript
  Neon added three LLM-backed features to its SQL editor: natural-language SQL generation, automatic naming of history queries, and one-click error fixing.
- [Semantic search using OpenAI, pg\_embedding and Neon](https://swe.quest/articles/semantic-search-using-openai-pg-embedding-and-neon-aeafbc27d93ca728.md): Neon · 2023-08-25 · Medium · 9 min read · Vector search, PostgreSQL, LLMs, Serverless, TypeScript
  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.
- [database.build v2: Bring-your-own-LLM](https://swe.quest/articles/database-build-v2-bring-your-own-llm-b2a3b83eee7ef5a0.md): Supabase · 2024-12-06 · Medium · 11 min read · PostgreSQL, LLMs, Browsers, WebAssembly
  database.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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
