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
Favorites
0
Views
10
Forks
0
Issues
0
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:
- Node.js (v18 or higher)
- pnpm: Install pnpm
- OpenAI API Key: Get one here
- Vectorize.io Account: Sign up here
π§ Installation
Install dependencies
pnpm installSet up environment variables
Create a
.env.localfile in the root directory of your project:# Create the file (from project root) touch .env.localOpen 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_hereImportant: The
.env.localfile is automatically ignored by git, keeping your API keys secure.
π Environment Variables Setup
OpenAI API Key
- Visit OpenAI Platform
- Sign in or create an account
- Click "Create new secret key"
- Give your key a name (e.g., "rag-next-app")
- Copy the generated key immediately (you won't see it again!)
- In your
.env.localfile, replaceyour_openai_api_key_herewith your actual key:OPENAI_API_KEY=sk-proj-xxxxxxxxxxxxxxxxxxxxx
Vectorize.io Configuration
- Sign up at Vectorize.io
- Create a new organization
- Navigate to your organization settings
- Create a new pipeline:
- Choose "Document Retrieval" as the pipeline type
- Configure your pipeline settings
- Save the pipeline
- Generate an access token:
- Go to "API Tokens" in your organization settings
- Create a new token with "Retrieval Access" permissions
- Copy the token
- 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
Start the development server
pnpm devOpen your browser
Navigate to http://localhost:3000
Test the application
- Visit the main page to see the Next.js welcome screen
- Go to
/vectorizeto 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:

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
- User Input: User types a question in the chat interface
- Document Retrieval: The system queries Vectorize.io to find relevant documents
- Context Formation: Retrieved documents are formatted as context
- AI Generation: OpenAI GPT-4o-mini generates a response using the context
- Response Display: The answer is shown with source documents for transparency
π― Usage
Chat Interface
- Navigate to
/vectorizefor 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 Turbopackpnpm build- Build the application for productionpnpm start- Start the production serverpnpm lint- Run ESLint
π Troubleshooting
Common Issues
Missing Environment Variables
- Ensure all required environment variables are set in
.env.local - Check that your API keys are valid and have proper permissions
- Ensure all required environment variables are set in
Vectorize Connection Issues
- Verify your Vectorize.io credentials
- Ensure your pipeline is properly configured and has documents
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 configurationFailed to process chat- Usually indicates OpenAI API issues
π Learn More
Next.js Resources
AI & RAG Resources
π Deployment
Vercel (Recommended)
- Push your code to a Git repository
- Connect your repository to Vercel
- Add your environment variables in the Vercel dashboard
- Deploy automatically on every push
π€ Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting
- Submit a pull request