AI Module
Develop AI Chat and Image Generation applications with a "learning portal" approach.
The AI module comes pre-configured with Vercel AI SDK, tailored for building ChatGPT-like interfaces and Image Generators. In this guide, you'll learn how to customize the system and leverage Empire features.
Prompt Template Manager
Managing system prompts from a central location ensures consistency and ease of maintenance. The RapidCore AI module recommends organizing your prompts in the apps/ai-starter/src/lib/prompts.ts file.
Creating a Sample Template
You can structure your system prompts as follows:
// apps/ai-starter/src/lib/prompts.ts
export const SYSTEM_PROMPTS = {
CONSULTANT: "You are an expert SaaS consultant for RapidCore. You guide users on technical architecture.",
BLOG_WRITER: "You are a creative content writer. You prepare SEO-friendly and engaging blog posts.",
};
You can easily import and use these templates in your API routes (api/ai/chat/route.ts).
Storing your prompts as constant variables ensures that the same instructions are preserved when switching between different models (GPT-4, Claude, etc.) and reduces the risk of "prompt drift."
Vector DB & RAG Technical Guide
To enable AI models to talk not just with general information, but with your private data (documents, product lists, etc.), Retrieval-Augmented Generation (RAG) is used.
Supabase pgvector Setup
-
Go to the Supabase panel and activate the
pgvectorextension via the SQL Editor:create extension vector; -
Create a table to store your documents:
create table documents ( id bigserial primary key, content text, embedding vector(1536) -- 1536 dimensions for OpenAI embeddings );
Document Embedding
When a new document is added, you can convert its content into a vector using the OpenAI text-embedding-3-small model and save it. RapidCore includes vector-utils.ts helpers to facilitate this process.
Why RAG?
AI models are limited by the data they were trained on. RAG allows the model to perform a "search" in your database before responding and use the found information as context. This way, AI speaks with up-to-date and completely private data.
Token and Credit Tracking Mechanism
RapidCore offers an advanced credit system to keep high-cost AI operations under control.
Setting Token Limits
User-based limits are managed in apps/ai-starter/src/actions/ai-credits.ts. You can set daily or monthly token limits based on the user's subscription plan (Starter, Pro, Empire):
const PLAN_LIMITS = {
STARTER: 100,
PRO: 500,
EMPIRE: "unlimited" // Unlimited access
};
Credit Purchase Cycle
When a user runs out of credits, they can purchase new credit packages via Stripe Checkout. After a successful payment, the Stripe Webhook (api/webhooks/stripe/route.ts) is triggered, and the credits field in the user's database is updated instantly.
AI Agents & Fine-tuning (Empire Exclusive)
The Empire package includes an AI Agent architecture that automates complex workflows.
Multi-Model Chaining
For example, a "Blog Writer Agent" goes through these stages:
- Researcher Model: Collects keywords and sources about the topic.
- Writer Model: Creates the first draft with the collected data.
- Editor Model: Checks language, spelling, and SEO.
Fine-tuning Processes
To make models more capable with your custom datasets:
- Data Collection: Accumulate user feedback and successful outputs in
jsonlformat. - Export: Export data to the OpenAI or Anthropic panel in the appropriate format with one click.
Troubleshooting (FAQ)
1. "Invalid API Key" Error
Ensure that the OPENAI_API_KEY or ANTHROPIC_API_KEY variables in your .env file are set correctly. Check that you haven't left any spaces.
2. Vercel Timeout Issues
In the Vercel Hobby plan, the function duration is limited to 10 seconds. For long-running AI responses, ensure you are using streaming: true mode or upgrade to Vercel Pro.
3. Rate Limit Exceeded (429)
You may receive "429 Too Many Requests" when requests are sent too frequently. In this case, you should increase your limits from the API provider's panel or add queuing between requests.
In local development, you can see all prompt/response traffic in the browser console by using the NEXT_PUBLIC_DEBUG_AI=true flag.