ai-agent-personal-finance-assistant

This project aims to develop an AI agent that assists with personal finance management. It analyzes users' income and expenses, providing budget suggestions. Implemented in TypeScript, it features a highly extensible design.

GitHub Stars

0

User Rating

Not Rated

Forks

0

Issues

0

Views

2

Favorites

0

README
RAG Next.js TypeScript Application

A modern Retrieval-Augmented Generation (RAG) chat application built with Next.js, TypeScript, and powered by OpenAI's GPT models with vector-based document retrieval using Vectorize.io.

๐Ÿš€ Features
  • AI-Powered Chat: Interactive chat interface with GPT-4o-mini
  • Document Retrieval: RAG system that retrieves relevant context from vectorized documents
  • Real-time Sources: View document sources that inform AI responses
  • Modern UI: Clean, responsive interface built with Tailwind CSS
  • Type Safety: Full TypeScript implementation
๐Ÿ› ๏ธ Tech Stack
  • Framework: Next.js 15 with App Router
  • Language: TypeScript
  • AI/ML: OpenAI GPT-4o-mini, AI SDK
  • Vector Database: Vectorize.io
  • Styling: Tailwind CSS
  • Icons: Lucide React
๐Ÿ“‹ Prerequisites

Before setting up this project, you'll need:

  1. Node.js (v18 or higher)
  2. pnpm: Install pnpm
  3. OpenAI API Key: Get one here
  4. Vectorize.io Account: Sign up here
๐Ÿ”ง Installation
  1. Install dependencies

    pnpm install
    
  2. Set up environment variables

    Create a .env.local file in the root directory of your project:

    # Create the file (from project root)
    touch .env.local
    

    Open the file in your editor and add the following variables:

    # OpenAI Configuration
    OPENAI_API_KEY=your_openai_api_key_here
    
    # Vectorize.io Configuration
    VECTORIZE_PIPELINE_ACCESS_TOKEN=your_vectorize_access_token_here
    VECTORIZE_ORGANIZATION_ID=your_vectorize_organization_id_here
    VECTORIZE_PIPELINE_ID=your_vectorize_pipeline_id_here
    

    Important: The .env.local file is automatically ignored by git, keeping your API keys secure.

๐Ÿ”‘ Environment Variables Setup
OpenAI API Key
  1. Visit OpenAI Platform
  2. Sign in or create an account
  3. Click "Create new secret key"
  4. Give your key a name (e.g., "rag-next-app")
  5. Copy the generated key immediately (you won't see it again!)
  6. In your .env.local file, replace your_openai_api_key_here with your actual key:
    OPENAI_API_KEY=sk-proj-xxxxxxxxxxxxxxxxxxxxx
    
Vectorize.io Configuration
  1. Sign up at Vectorize.io
  2. Create a new organization
  3. Navigate to your organization settings
  4. Create a new pipeline:
    • Choose "Document Retrieval" as the pipeline type
    • Configure your pipeline settings
    • Save the pipeline
  5. Generate an access token:
    • Go to "API Tokens" in your organization settings
    • Create a new token with "Retrieval Access" permissions
    • Copy the token
  6. From your Vectorize dashboard, copy these values to your .env.local:
    VECTORIZE_PIPELINE_ACCESS_TOKEN=eyJhbGciOi... (your full token)
    VECTORIZE_ORGANIZATION_ID=527d9a27-c34a-4d0a-8fde-... (your org ID)
    VECTORIZE_PIPELINE_ID=aip0c318-344a-4721-a9e7-... (your pipeline ID)
    
Verifying Your Setup

After adding all environment variables, your .env.local file should look similar to this:

# OpenAI Configuration
OPENAI_API_KEY=sk-proj-xxxxxxxxxxxxxxxxxxxxx

# Vectorize.io Configuration
VECTORIZE_PIPELINE_ACCESS_TOKEN=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...
VECTORIZE_ORGANIZATION_ID=527d9a27-c34a-4d0a-8fde-1129a57eb5b8
VECTORIZE_PIPELINE_ID=aip0c318-344a-4721-a9e7-5526c96d9b49

Note: Never commit your .env.local file to version control!

๐Ÿš€ Getting Started
  1. Start the development server

    pnpm dev
    
  2. Open your browser

    Navigate to http://localhost:3000

  3. Test the application

    • Visit the main page to see the Next.js welcome screen
    • Go to /vectorize to access the RAG chat interface
    • Start asking questions about your vectorized documents
๐Ÿ—๏ธ Application Architecture
Sequence Diagram

The following sequence diagram illustrates the complete interaction flow for both RAG Chat and Agent Chat functionalities:

Sequence Diagram

This diagram shows the step-by-step interactions between users, frontend components, API endpoints, services, and external APIs for both RAG and Agent chat flows.

Architecture Overview
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                              FRONTEND (Next.js)                            โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”         โ”‚
โ”‚  โ”‚   / (Home)      โ”‚    โ”‚  /vectorize     โ”‚    โ”‚    /agent       โ”‚         โ”‚
โ”‚  โ”‚   page.tsx      โ”‚    โ”‚   page.tsx      โ”‚    โ”‚   page.tsx      โ”‚         โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜         โ”‚
โ”‚                          โ”‚                     โ”‚                           โ”‚
โ”‚                          โ–ผ                     โ–ผ                           โ”‚
โ”‚                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”               โ”‚
โ”‚                    โ”‚   chat.tsx      โ”‚    โ”‚ agent-chat.tsx  โ”‚               โ”‚
โ”‚                    โ”‚ (RAG Chat UI)   โ”‚    โ”‚ (Agent Chat UI) โ”‚               โ”‚
โ”‚                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜               โ”‚
โ”‚                          โ”‚                     โ”‚                           โ”‚
โ”‚                          โ”‚                     โ”‚                           โ”‚
โ”‚                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”          โ”‚                           โ”‚
โ”‚                    โ”‚sources-display  โ”‚          โ”‚                           โ”‚
โ”‚                    โ”‚     .tsx        โ”‚          โ”‚                           โ”‚
โ”‚                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜          โ”‚                           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ”‚                     โ”‚
                             โ–ผ                     โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                              API LAYER                                     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”           โ”‚
โ”‚          โ”‚  /api/chat      โ”‚                  โ”‚  /api/agent     โ”‚           โ”‚
โ”‚          โ”‚   route.ts      โ”‚                  โ”‚   route.ts      โ”‚           โ”‚
โ”‚          โ”‚ (RAG Endpoint)  โ”‚                  โ”‚(Agent Endpoint) โ”‚           โ”‚
โ”‚          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜           โ”‚
โ”‚                    โ”‚                                   โ”‚                    โ”‚
โ”‚                    โ”‚                                   โ”‚                    โ”‚
โ”‚                    โ–ผ                                   โ–ผ                    โ”‚
โ”‚          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”           โ”‚
โ”‚          โ”‚ generateText()  โ”‚                  โ”‚ streamText()    โ”‚           โ”‚
โ”‚          โ”‚ (Single Call)   โ”‚                  โ”‚ (Multi-Step)    โ”‚           โ”‚
โ”‚          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜           โ”‚
โ”‚                                                         โ”‚                    โ”‚
โ”‚                                               โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”         โ”‚
โ”‚                                               โ”‚     AGENT TOOLS    โ”‚         โ”‚
โ”‚                                               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค         โ”‚
โ”‚                                               โ”‚ โ€ข getLocation()   โ”‚         โ”‚
โ”‚                                               โ”‚ โ€ข getWeather()    โ”‚         โ”‚
โ”‚                                               โ”‚ โ€ข searchDocuments โ”‚         โ”‚
โ”‚                                               โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ”‚                           โ”‚
                             โ–ผ                           โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                           SERVICE LAYER                                    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                     โ”‚
โ”‚                    โ”‚ RetrievalServiceโ”‚ โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚                    โ”‚ (/lib/retrieval)โ”‚                                   โ”‚ โ”‚
โ”‚                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                   โ”‚ โ”‚
โ”‚                             โ”‚                                           โ”‚ โ”‚
โ”‚                             โ–ผ                                           โ”‚ โ”‚
โ”‚                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                   โ”‚ โ”‚
โ”‚                    โ”‚ VectorizeServiceโ”‚                                   โ”‚ โ”‚
โ”‚                    โ”‚ (/lib/vectorize)โ”‚                                   โ”‚ โ”‚
โ”‚                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                   โ”‚ โ”‚
โ”‚                                                                          โ”‚ โ”‚
โ”‚         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                       โ”‚ โ”‚
โ”‚         โ”‚ /lib/utils.ts   โ”‚    โ”‚ /lib/consts.ts  โ”‚                       โ”‚ โ”‚
โ”‚         โ”‚ (Utilities)     โ”‚    โ”‚ (Constants)     โ”‚                       โ”‚ โ”‚
โ”‚         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                       โ”‚ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ”‚                                           โ”‚
                             โ–ผ                                           โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚                          EXTERNAL APIs                                 โ”‚ โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚
โ”‚    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                 โ”‚ โ”‚
โ”‚    โ”‚   OpenAI API    โ”‚              โ”‚  Vectorize.io   โ”‚ โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚    โ”‚                 โ”‚              โ”‚                 โ”‚                   โ”‚
โ”‚    โ”‚ โ€ข GPT-4o        โ”‚              โ”‚ โ€ข Document      โ”‚                   โ”‚
โ”‚    โ”‚ โ€ข GPT-4o-mini   โ”‚              โ”‚   Retrieval     โ”‚                   โ”‚
โ”‚    โ”‚ โ€ข Text          โ”‚              โ”‚ โ€ข Vector Search โ”‚                   โ”‚
โ”‚    โ”‚   Generation    โ”‚              โ”‚ โ€ข Embeddings    โ”‚                   โ”‚
โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                   โ”‚
โ”‚             โ–ฒ                                 โ–ฒ                           โ”‚
โ”‚             โ”‚                                 โ”‚                           โ”‚
โ”‚    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                   โ”‚
โ”‚    โ”‚ OPENAI_API_KEY  โ”‚              โ”‚ VECTORIZE_*     โ”‚                   โ”‚
โ”‚    โ”‚                 โ”‚              โ”‚ ENV VARIABLES   โ”‚                   โ”‚
โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

 DATA FLOW:
 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
 โ”‚ RAG CHAT FLOW:                                                             โ”‚
 โ”‚ User Input โ†’ /api/chat โ†’ RetrievalService โ†’ VectorizeService โ†’ Documents    โ”‚
 โ”‚           โ†“                                                                โ”‚
 โ”‚ OpenAI API โ† Context + Messages โ† Formatted Documents โ† Vectorize.io       โ”‚
 โ”‚           โ†“                                                                โ”‚
 โ”‚ Response + Sources โ†’ Chat UI                                               โ”‚
 โ”‚                                                                            โ”‚
 โ”‚ AGENT FLOW:                                                                โ”‚
 โ”‚ User Input โ†’ /api/agent โ†’ AI decides tools โ†’ Multi-step execution          โ”‚
 โ”‚           โ†“                                                                โ”‚
 โ”‚ Tools: getLocation() + getWeather() + searchDocuments()                    โ”‚
 โ”‚           โ†“                                                                โ”‚
 โ”‚ Streaming Response โ†’ Agent Chat UI                                         โ”‚
 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
๐Ÿ“ Project Structure
rag-next-typescript/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ agent/             # AI Agent interface
โ”‚   โ”‚   โ””โ”€โ”€ page.tsx       # Agent page (server-rendered)
โ”‚   โ”œโ”€โ”€ api/
โ”‚   โ”‚   โ”œโ”€โ”€ agent/         # Agent API with multi-step tools
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ route.ts   # Streaming agent endpoint
โ”‚   โ”‚   โ””โ”€โ”€ chat/          # Traditional RAG chat API
โ”‚   โ”‚       โ””โ”€โ”€ route.ts   # Single-turn RAG endpoint
โ”‚   โ”œโ”€โ”€ vectorize/         # RAG chat interface
โ”‚   โ”‚   โ””โ”€โ”€ page.tsx       # Vectorize chat page
โ”‚   โ”œโ”€โ”€ globals.css        # Global styles
โ”‚   โ”œโ”€โ”€ layout.tsx         # Root layout
โ”‚   โ””โ”€โ”€ page.tsx          # Home page
โ”œโ”€โ”€ components/
โ”‚   โ”œโ”€โ”€ agent-chat.tsx    # Agent chat component (client-side)
โ”‚   โ”œโ”€โ”€ chat.tsx          # RAG chat component
โ”‚   โ””โ”€โ”€ sources-display.tsx # Document sources display
โ”œโ”€โ”€ lib/
โ”‚   โ”œโ”€โ”€ consts.ts         # Constants and loading messages
โ”‚   โ”œโ”€โ”€ retrieval.ts      # Document retrieval service
โ”‚   โ”œโ”€โ”€ utils.ts          # Utility functions
โ”‚   โ””โ”€โ”€ vectorize.ts      # Vectorize.io API integration
โ”œโ”€โ”€ types/
โ”‚   โ”œโ”€โ”€ chat.ts           # Chat-related types
โ”‚   โ””โ”€โ”€ vectorize.ts      # Vectorize API types
โ””โ”€โ”€ .env.local           # Environment variables
๐Ÿ”„ How It Works
  1. User Input: User types a question in the chat interface
  2. Document Retrieval: The system queries Vectorize.io to find relevant documents
  3. Context Formation: Retrieved documents are formatted as context
  4. AI Generation: OpenAI GPT-4o-mini generates a response using the context
  5. Response Display: The answer is shown with source documents for transparency
๐ŸŽฏ Usage
Chat Interface
  • Navigate to /vectorize for the main chat interface
  • Type questions related to your vectorized documents
  • View source documents that informed each AI response
  • Enjoy real-time loading animations and smooth interactions
Adding Documents

To add documents to your vector database, you'll need to use the Vectorize.io platform or API to upload and process your documents before they can be retrieved by this application.

๐Ÿ› ๏ธ Available Scripts
  • pnpm dev - Start development server with Turbopack
  • pnpm build - Build the application for production
  • pnpm start - Start the production server
  • pnpm lint - Run ESLint
๐Ÿ” Troubleshooting
Common Issues
  1. Missing Environment Variables

    • Ensure all required environment variables are set in .env.local
    • Check that your API keys are valid and have proper permissions
  2. Vectorize Connection Issues

    • Verify your Vectorize.io credentials
    • Ensure your pipeline is properly configured and has documents
  3. OpenAI API Errors

    • Check your OpenAI API key validity
    • Ensure you have sufficient credits/quota
Error Messages
  • Failed to retrieve documents from Vectorize - Check Vectorize.io configuration
  • Failed to process chat - Usually indicates OpenAI API issues
๐Ÿ“– Learn More
Next.js Resources
AI & RAG Resources
๐Ÿš€ Deployment
Vercel (Recommended)
  1. Push your code to a Git repository
  2. Connect your repository to Vercel
  3. Add your environment variables in the Vercel dashboard
  4. Deploy automatically on every push
๐Ÿค Contributing
  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run tests and linting
  5. Submit a pull request
Author Information

0

Followers

0

Repositories

0

Gists

0

Total Contributions

Threads