Skip to main content

Gemini API Integration

UniPulse uses Google Gemini 2.5 Flash as its primary AI model via the @google/genai SDK for all text generation, and text-embedding-004 for vector embeddings.


SDK Setup

import { GoogleGenAI } from '@google/genai';

const genai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
ModelPurposeConfiguration
gemini-2.5-flashText generation (captions, replies, analysis)Temperature varies by task
text-embedding-004Vector embeddings (similarity, classification)Fixed dimensions

Core AI Functions

All AI functions are in apps/api/src/services/ai.service.ts:

FunctionPurposeInputOutput
callGemini()Base function for all Gemini API callsPrompt, system prompt, temperatureGenerated text
generateCaption()Generate post captionsTopic, platform, brand voice, language, countArray of caption options
rewriteCaption()Rewrite an existing captionCaption, instruction, platformRewritten caption
generateHashtags()Generate relevant hashtagsCaption, platform, countArray of hashtags
generateCTA()Generate call-to-action textContext, goal, platformCTA options
translateCaption()Translate caption to another languageCaption, target languageTranslated text
generateImage()Generate image from promptDescription, style, dimensionsImage URL

API Endpoints

All AI routes are under /api/v1/ai:

EndpointMethodFunction CalledRate Limited
/api/v1/ai/caption/generatePOSTgenerateCaption()aiLimiter + requireQuota
/api/v1/ai/caption/rewritePOSTrewriteCaption()aiLimiter + requireQuota
/api/v1/ai/hashtags/generatePOSTgenerateHashtags()aiLimiter + requireQuota
/api/v1/ai/cta/generatePOSTgenerateCTA()aiLimiter + requireQuota
/api/v1/ai/translatePOSTtranslateCaption()aiLimiter + requireQuota
/api/v1/ai/image/generatePOSTgenerateImage()aiLimiter + requireQuota
/api/v1/ai/chatPOSTAI chataiLimiter
/api/v1/ai/suggestionsGETBackground suggestionsrequireFeature

Where AI Is Used Across the Platform

FeatureServiceDescriptionTemperature
Caption generationai.serviceGenerate social media captions0.8 (creative)
Caption rewritingai.serviceRewrite with specific instructions0.7
Hashtag generationai.servicePlatform-optimized hashtags0.5
CTA generationai.serviceCall-to-action text0.7
Translationai.serviceMulti-language caption translation0.3 (precise)
Brand voice applicationbrand-voice.serviceApply trained brand voice style0.6
Content calendarcontent-calendar.servicePlan content schedules with AI0.8
Content repurposingcontent-repurpose.serviceTransform content across formats0.7
Trend analysistrend-scanner.serviceDetect and analyze trending topics0.5
Performance advisorai-advisor.serviceGenerate performance recommendations0.4
Conversation repliesconversation-brain.serviceGenerate context-aware replies0.6
Intent classificationintent-classifier.serviceClassify message intent and sentiment0.2 (deterministic)
Post classificationclassification.serviceCategorize post content0.2
A/B test evaluationab-test.serviceAnalyze test results with AI0.3
Gap analysiscompetitor.serviceCompetitive content gap analysis0.5
Predictionsprediction.serviceEngagement and revenue forecasting0.3

Usage Tracking

All AI calls are tracked per workspace via incrementUsage():

// After each successful AI call
await incrementUsage(workspaceId, 'ai_generations');

Usage is stored in the WorkspaceUsage model and checked by requireQuota middleware before each AI request. The AiPromptLog model records individual API calls for debugging and cost analysis:

FieldPurpose
workspaceIdWorkspace that made the call
promptTypeType of prompt (caption, hashtag, reply, etc.)
tokensToken count (input + output)
modelModel used (gemini-2.5-flash)
latencyMsResponse time
createdAtTimestamp

Prompt Type Index (PROMPT_CONFIGS)

All prompt types are indexed in PROMPT_CONFIGS for consistent configuration:

const PROMPT_CONFIGS = {
caption_generate: { model: 'gemini-2.5-flash', temperature: 0.8, maxTokens: 2000 },
caption_rewrite: { model: 'gemini-2.5-flash', temperature: 0.7, maxTokens: 2000 },
hashtag_generate: { model: 'gemini-2.5-flash', temperature: 0.5, maxTokens: 500 },
cta_generate: { model: 'gemini-2.5-flash', temperature: 0.7, maxTokens: 500 },
translate: { model: 'gemini-2.5-flash', temperature: 0.3, maxTokens: 2000 },
intent_classify: { model: 'gemini-2.5-flash', temperature: 0.2, maxTokens: 200 },
conversation: { model: 'gemini-2.5-flash', temperature: 0.6, maxTokens: 1000 },
// ... more prompt types
};

Rate Limiting & Cost Control

ControlImplementation
Per-IP rate limitingaiLimiter in rateLimiter.ts
Per-workspace quotasrequireQuota('ai_generations') middleware
Plan-based limitsPlanFeature defines max AI calls per billing period
Usage dashboardAdmin panel shows AI consumption per workspace
Cost trackingAiPromptLog records tokens for cost analysis

Environment Variables

VariableRequiredDescription
GEMINI_API_KEYYes (for AI features)Google AI API key from ai.google.dev
API Key Security

The GEMINI_API_KEY should never be exposed to the frontend. All AI calls go through the backend API, which adds authentication, rate limiting, and usage tracking before calling Gemini.


Cross-Reference