Skip to content
All work

AI Chat Assistant

ChatGPT Clone

Full-stack AI chat assistant with streamed replies, saved conversation history, image understanding and question answering over uploaded PDFs.

Overview

What the product does

A rebuild of a modern AI assistant's core experience. Users sign in, start a conversation and get an answer that streams in token by token and is saved to their account. Each reply can draw on images they attach or PDFs they upload, so the assistant can answer about a screenshot or from the contents of a document.

Problem

The problem it solves

A useful assistant needs more than a chat box in front of a model: history that lives on the server rather than in the browser, answers grounded in the user's own documents instead of general knowledge, and uploads that can't be used to reach another user's files or slip a non-image past the model.

My role

What I contributed

Designed and built both applications: the Next.js chat interface and the Express API behind it, covering streaming, the PDF retrieval pipeline, file uploads, authentication and persistence.

Solution

How the system was designed

A Next.js frontend proxies every /api request to an Express and TypeScript API on Bun, keeping auth cookies first-party. The Vercel AI SDK streams OpenAI replies, PostgreSQL through Prisma stores users, conversations, messages and images, and a LangChain pipeline indexes PDFs into Qdrant for retrieval-augmented answers. The server owns the conversation history, rebuilding the context from the database on every request.

Architecture

System architecture

ClientApplicationServicesDataExternalHTTPSRewriteVerifyStreamContextQueriesSearchGenerateEmbedBrowserChat UINext.jsUI · /api proxyExpress APIBun · TypeScriptBetter AuthSessions · OAuthChat streamVercel AI SDKRAG pipelineLangChainPostgreSQLPrisma ORMQdrantPDF embeddingsOpenAIChat · Vision · Embeddings

Key features

What it does in practice

Challenges

The interesting engineering problems

  • Keeping the history trustworthy

    Problem
    If the client sends the whole conversation with each request, it can inject or alter earlier turns, and requests grow with every message.
    Approach
    The client sends only the newest message. The server confirms the user owns the conversation and rebuilds the context from PostgreSQL, so earlier turns can't be tampered with and requests stay small.
  • Accepting untrusted uploads

    Problem
    A browser-supplied MIME type proves nothing, and attachments referenced by id could be used to reach another user's files.
    Approach
    Images are validated by their magic bytes rather than the claimed type. Attachments travel as ids that the server resolves for the current user only, and RAG searches are filtered by both user and document.
  • Streaming through a proxy

    Problem
    With the API behind the Next.js rewrite proxy, responses can be buffered, so a streamed reply arrives in one block instead of token by token.
    Approach
    Set response headers and socket options that disable compression and proxy buffering, so each chunk is flushed to the browser as soon as the model produces it.
  • Saving replies without slowing them down

    Problem
    Writing to the database before streaming delays the first token, and saving too early leaves empty or half-written replies when a generation fails.
    Approach
    Messages are saved in one Prisma transaction after the stream finishes. Generation continues if the client disconnects so the saved reply is complete, and failed or empty replies are never stored.

Technology

The stack behind it

  • Next.jsReact Framework
  • Express.jsHTTP Framework
  • OpenAILLMs & Embeddings
  • Vercel AI SDKLLM Streaming
  • LangChainRetrieval Pipeline
  • QdrantVector Database
  • PostgreSQLRelational Database
  • Prisma ORMType-Safe ORM
  • Better AuthAuthentication
  • BunRuntime & Package Manager
  • TypeScriptType Safety
  • ReactFrontend UI
  • Tailwind CSSStyling System
  • TanStack QueryServer State
  • ZodSchema Validation
  • DockerContainerization