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
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 install
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
- 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.local
file, replaceyour_openai_api_key_here
with 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 dev
Open your browser
Navigate to http://localhost:3000
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:
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
/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 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
0
Followers
0
Repositories
0
Gists
0
Total Contributions