Day 7: Personalizing Avatar Responses & User Memory #AIChatbot #PersonalizedAI

On Day 7, we’ll personalize chatbot responses by allowing the avatar to remember user details like their name, mood, or preferences. This creates a more human-like conversation where the assistant feels familiar and attentive.


1. Why Personalize AI Chatbot Responses?

User Engagement: Users enjoy conversations that feel personal.
Memory Retention: Remembering details like names, previous chats, or preferences builds trust.
Contextual Conversations: Allows the assistant to adjust tone and suggestions based on past interactions.

We’ll use: 🔹 AsyncStorage – Store user preferences locally.
🔹 GPT Contextual Memory – Pass chat history + user data to GPT API.
🔹 Emotion History – Track user moods over time.


2. Installing Local Storage for User Data

Step 1: Install AsyncStorage

npm install @react-native-async-storage/async-storage

3. Saving & Retrieving User Preferences

Step 1: Create UserMemory.js Helper

import AsyncStorage from '@react-native-async-storage/async-storage';

export const saveUserName = async (name) => {
    await AsyncStorage.setItem('userName', name);
};

export const getUserName = async () => {
    return await AsyncStorage.getItem('userName');
};

export const saveMood = async (mood) => {
    await AsyncStorage.setItem('lastMood', mood);
};

export const getMood = async () => {
    return await AsyncStorage.getItem('lastMood');
};

Step 2: Use Memory in ChatBot Component

Modify ChatBot.js:

import { saveUserName, getUserName, saveMood, getMood } from '../helpers/UserMemory';

const [userName, setUserName] = useState('');
const [lastMood, setLastMood] = useState('');

useEffect(() => {
    const loadMemory = async () => {
        const storedName = await getUserName();
        const storedMood = await getMood();
        if (storedName) setUserName(storedName);
        if (storedMood) setLastMood(storedMood);
    };
    loadMemory();
}, []);

Step 3: Save User Name (Once Asked)

Modify sendMessage:

if (input.toLowerCase().includes('my name is')) {
    const extractedName = input.split('my name is ')[1].split(' ')[0];
    await saveUserName(extractedName);
    setUserName(extractedName);
}

Step 4: Save Mood Based on Emotions

If you have voice emotion detection integrated from Day 4:

useEffect(() => {
    if (userEmotion) {
        saveMood(userEmotion);
        setLastMood(userEmotion);
    }
}, [userEmotion]);

4. Sending Personalized Context to GPT API

Modify sendMessage:

const sendMessage = async (input) => {
    const context = [
        userName ? `The user's name is ${userName}.` : '',
        lastMood ? `The user's mood was recently ${lastMood}.` : '',
    ].filter(Boolean).join(' ');

    const messages = [{ role: 'user', content: `${context} ${input}` }];

    const response = await axios.post(
        'https://api.openai.com/v1/chat/completions',
        { model: 'gpt-4', messages },
        { headers: { Authorization: `Bearer ${OPENAI_API_KEY}` } }
    );

    const botReply = response.data.choices[0].message.content;
    setMessages([...messages, { text: botReply, sender: 'bot' }]);
};

Example:

  • User: “My name is Sarah.” → Stored as Sarah.
  • User: “How’s the weather?” → Sent as: The user’s name is Sarah. How’s the weather?
See also  Day 3: Mapping Facial Expressions to Chat Responses #FacialExpressions #AIChatbot

5. Personalizing the Avatar’s Greeting

Modify AvatarRenderer.js to show a personal greeting:

<Text style={styles.greeting}>
    {userName ? `Hi, ${userName}! Ready to chat?` : 'Hello! What can I do for you today?'}
</Text>

6. Testing User Personalization

Step 1: Start the App

expo start

Step 2: Simulate a Chat

  • User: “My name is Alex.”
  • Assistant (later): “Good to see you again, Alex!”

Step 3: Close & Reopen App

  • Avatar should greet the user by their name from local memory.

7. Expanding User Memory

Store Favorite TopicsUser likes talking about football.
Track Frequent EmotionsUser is often stressed; offer calming responses.
Remember Task HistoryRemind users of unfinished tasks.

Example:

await AsyncStorage.setItem('favoriteTopic', 'football');
const favoriteTopic = await AsyncStorage.getItem('favoriteTopic');

8. Optimizing Memory Handling

Limit Storage Size – Don’t store large conversations; only keep preferences.
Encrypt Sensitive Data – Use expo-secure-store for storing personal info.

expo install expo-secure-store

9. Key Concepts Covered

✅ Added user memory storage using AsyncStorage.
✅ Personalized GPT chatbot context based on user name and mood.
✅ Improved AI avatar’s greeting & response personalization.


10. Next Steps: Voice Customization & Assistant Personalities

Tomorrow, we’ll:
🔹 Change the chatbot’s voice tone based on mood or personality.
🔹 Let users choose a friendly, professional, or playful assistant.


11. References & Learning Resources


12. SEO Keywords:

Personalized AI chatbot, GPT chatbot with memory, React Native AI assistant, storing user preferences React Native, voice assistant user memory.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.