Skip to main content
SvaraAI provides AI-generated therapeutic feedback that analyzes both conversation content and detected emotions to offer personalized, empathetic responses. The system uses Google’s Gemini 2.0 Flash model to generate contextually relevant therapeutic insights.

Overview

Therapeutic feedback combines multiple data sources to create meaningful responses:
1

Data collection

Gather conversation transcript and emotion data from the session
2

Emotion prioritization

Identify the top 3 emotions by confidence score
3

Prompt construction

Combine transcript and emotion data into a therapeutic prompt
4

AI generation

Generate personalized feedback using Gemini 2.0 Flash
5

Response delivery

Present feedback to the user with context

Implementation

The therapeutic feedback system is implemented in the Gemini route:
Backend/routes/gemini.ts
import express, { Request, Response } from 'express';

const router = express.Router();

router.post('/', async (req: Request, res: Response): Promise<void> => {
  try {
    const { transcript, emoData } = req.body;

    if (!transcript) {
      res.status(400).json({ error: 'Transcript is required' });
      return;
    }
    
    const emotionData = emoData || {};

    // Format top 3 emotions with scores
    const formattedEmoData = Object.entries(emotionData)
      .sort(([, a], [, b]) => (b as number) - (a as number))
      .slice(0, 3)
      .map(([emotion, score]) => 
        `${emotion}: ${((score as number) * 100).toFixed(1)}%`
      )
      .join('\\n');

    // Load and populate prompt template
    const rawPrompt = process.env.GEMINI_PROMPT;
    const prompt = rawPrompt
      .replace('{{transcript}}', transcript)
      .replace('{{emoData}}', formattedEmoData);

    // Call Gemini API
    const response = await fetch(
      `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${apiKey}`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          contents: [{
            parts: [{ text: prompt }]
          }],
          generationConfig: {
            temperature: 0.3,
            topK: 20,
            topP: 0.8,
            maxOutputTokens: 100,
          }
        })
      }
    );

    const data = await response.json();
    
    res.json({
      response: data.candidates[0].content.parts[0].text,
      emotions: emotionData
    });
  } catch (error) {
    res.status(500).json({
      error: 'Unable to process your request at this time'
    });
  }
});
The system uses a configurable prompt template stored in the GEMINI_PROMPT environment variable, allowing you to customize the therapeutic approach.

Emotion data formatting

Emotion data is processed to focus on the most significant emotional states:
// Extract top 3 emotions by score
const formattedEmoData = Object.entries(emotionData)
  .sort(([, a], [, b]) => (b as number) - (a as number))
  .slice(0, 3)
  .map(([emotion, score]) => 
    `${emotion}: ${((score as number) * 100).toFixed(1)}%`
  )
  .join('\\n');
Converting scores to percentages (0-100 scale) makes them more interpretable for both the AI model and end users.

Generation configuration

The Gemini model is configured for therapeutic contexts:

Temperature: 0.3

Low temperature ensures consistent, reliable responses that stay focused on therapeutic goals rather than creative exploration.

Top-K: 20

Limits token selection to the top 20 most likely options, maintaining coherent and contextually appropriate language.

Top-P: 0.8

Uses nucleus sampling to balance between predictability and natural language variation.

Max tokens: 100

Limits responses to concise, actionable feedback that users can easily digest.
These conservative parameters prioritize safety and reliability over creativity, which is appropriate for therapeutic applications.

Prompt engineering

The system uses a template-based approach for prompt construction:
Prompt Template
You are a compassionate AI therapist analyzing a conversation.

Transcript:
{{transcript}}

Detected emotions:
{{emoData}}

Provide brief, empathetic therapeutic feedback addressing the emotional state and conversation content.
  • Context first: Provide the AI with full conversation context
  • Emotion integration: Explicitly include emotion data for analysis
  • Role definition: Clearly define the AI’s therapeutic role
  • Output constraints: Request brief, actionable feedback
  • Empathy focus: Emphasize compassionate, supportive responses

Response structure

The API returns both the generated feedback and the original emotion data:
Response Type
interface TherapeuticResponse {
  response: string;      // Generated therapeutic feedback
  emotions: Record<string, number>;  // Original emotion scores
}
{
  "response": "I notice you're experiencing significant anxiety alongside sadness. It's completely valid to feel this way. Let's explore what might be contributing to these feelings and identify coping strategies that work for you.",
  "emotions": {
    "anxious": 0.785,
    "sad": 0.652,
    "calm": 0.421,
    "confident": 0.245
  }
}

Caching strategy

The system implements intelligent caching for conversation entries:
Backend/routes/gemini.ts
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes

interface CacheData {
  entries: Entry[];
  lastUpdated: number;
  lastModified: number;
}

let entriesCache: CacheData | null = null;

async function getEntriesWithCache(): Promise<Entry[]> {
  const stats = await fs.stat(ENTRIES_FILE_PATH);
  const fileModified = stats.mtimeMs;

  // Return cached data if valid
  if (entriesCache && 
      Date.now() - entriesCache.lastUpdated < CACHE_DURATION &&
      entriesCache.lastModified === fileModified) {
    return entriesCache.entries;
  }

  // Read fresh data and update cache
  const entriesData = await fs.readFile(ENTRIES_FILE_PATH, 'utf-8');
  const entries = JSON.parse(entriesData);

  entriesCache = {
    entries,
    lastUpdated: Date.now(),
    lastModified: fileModified
  };

  return entries;
}

Performance

Reduces file I/O operations by caching frequently accessed data

Freshness

Invalidates cache when the underlying file changes

Reliability

Falls back to cached data if file reading fails

Efficiency

5-minute cache duration balances freshness and performance

Data structures

The system uses typed interfaces for type safety:
Type Definitions
interface Emotions {
  [key: string]: number;
}

interface Turn {
  speaker: "user" | "assistant";
  text: string;
  timestamp: string;
  emotions: Emotions;
}

interface Entry {
  conversation_id: string;
  timestamp_ms: number;
  turns: Turn[];
}

Error handling

The system implements comprehensive error handling:
if (!transcript) {
  console.error('No transcript provided');
  res.status(400).json({ error: 'Transcript is required' });
  return;
}
Error messages shown to users are intentionally generic to avoid exposing implementation details or sensitive configuration information.

Environment variables

Required configuration for therapeutic feedback:
GEMINI_API_KEY
string
required
Your Google AI API key for accessing Gemini models
GEMINI_PROMPT
string
required
Template for therapeutic prompts with {{transcript}} and {{emoData}} placeholders

API reference

Generate therapeutic feedback

transcript
string
required
Complete conversation transcript to analyze
emoData
object
required
Emotion scores as key-value pairs where keys are emotion names and values are confidence scores (0-1)
curl -X POST https://your-api.com/api/gemini \
  -H "Content-Type: application/json" \
  -d '{
    "transcript": "User: I am feeling overwhelmed lately.\nAssistant: Tell me more about what is overwhelming you.",
    "emoData": {
      "anxious": 0.785,
      "sad": 0.652,
      "calm": 0.421
    }
  }'

Best practices

Tailor the GEMINI_PROMPT template to match your therapeutic approach, whether CBT, DBT, or another modality.
Keep transcripts concise to fit within model context limits while providing sufficient context for meaningful feedback.
Ensure emotion data is from recent analysis to maintain relevance with the conversation context.
Implement additional validation to ensure generated feedback aligns with therapeutic principles.

Limitations

AI-generated therapeutic feedback should complement, not replace, professional mental health care. Always encourage users to seek professional help for serious concerns.
  • Not a replacement for therapy: AI feedback is supportive, not diagnostic or prescriptive
  • Context limitations: 100 token limit may not allow for complex therapeutic explorations
  • Model biases: Gemini may reflect biases present in training data
  • Crisis situations: AI is not equipped to handle mental health emergencies

Next steps

Voice emotion detection

Learn how emotions are detected from voice input

Conversation insights

Explore comprehensive session analytics

Build docs developers (and LLMs) love