Personality and Mimicry
Relevant source files
The following files were used as context for generating this wiki page:The Personality and Mimicry system in monGARS is a core part of its cognitive architecture, designed to create a more adaptive and personalized conversational experience. It enables the AI to analyze a user's interaction style and adjust its own responses to mirror that style in terms of sentiment, formality, and structure. This is achieved through two main components: the PersonalityEngine for long-term trait evolution and the MimicryModule for short-term stylistic adaptation.
This system is a key feature of the project's self-improvement capabilities, as noted in the project roadmap where "Personality profiles persisted via SQLModel with live adapter updates" is a completed milestone. The overall goal is to move beyond static responses and create a dynamic conversational partner that evolves with the user.
Sources: README.md, ROADMAP.md
System Architecture
The system is composed of two primary classes, PersonalityEngine and MimicryModule, which work in tandem. PersonalityEngine manages the long-term, persistent personality profile of a user, while MimicryModule handles the immediate, turn-by-turn adaptation based on recent conversation history.
The following diagram illustrates the high-level interaction between these components.
This diagram shows the flow of a user message through the cognition pipeline, where the Personality and Mimicry modules are invoked. They interact with persistence and caching layers to ultimately shape the final AI response.
Sources: monGARS/core/personality.py, monGARS/core/mimicry.py
MimicryModule
The MimicryModule is responsible for adapting the AI's responses based on both long-term and short-term user interaction patterns. It extracts stylistic features from user messages, updates a user-specific profile, and then uses this profile to modify the AI's generated response.
Sources: monGARS/core/mimicry.py
Core Concepts
- Feature Extraction: The module analyzes user messages to extract a
FeatureSnapshotof stylistic signals. - Profile Management: It maintains a profile for each user, consisting of a
long_termmoving average of features and ashort_termhistory (a deque) of recent interactions. - Response Adaptation: It modifies generated responses to mirror the user's style, such as matching sentiment, sentence length, or question-asking frequency.
- Persistence & Caching: Profiles are persisted to the database via
PersistenceRepositoryand cached in aTieredCachefor performance.
Class Structure
This diagram shows the main methods and attributes of the MimicryModule and its relationship with the FeatureSnapshot data structure.
Sources: monGARS/core/mimicry.py:18-100
Feature Extraction
The _extract_features method analyzes an interaction (containing a user message and AI response) to quantify its style.
| Feature | Description | Calculation | Source File |
|---|---|---|---|
sentence_length | The number of words in the user's message. | Counts words from _tokenize(message). | monGARS/core/mimicry.py:240-243 |
positive_sentiment | A score from 0.0 (negative) to 1.0 (positive) based on the AI's response. | Uses a lexicon of positive/negative words to score tokens. | monGARS/core/mimicry.py:249-262 |
question_ratio | The ratio of question marks to the number of sentences in the user's message. | Counts ? and divides by the number of sentences. | monGARS/core/mimicry.py:264-275 |
exclamation_ratio | The ratio of exclamation marks to the number of sentences in the user's message. | Counts ! and divides by the number of sentences. | monGARS/core/mimicry.py:264-275 |
Sources: monGARS/core/mimicry.py:235-275
Profile Update Flow
When update_profile is called, it orchestrates feature extraction, profile blending, caching, and persistence.
This sequence diagram details the process of updating a user's mimicry profile, including cache checks, database lookups on cache misses, feature blending, and persisting the updated profile.
Sources: monGARS/core/mimicry.py:199-233, tests/test_mimicry.py:89-102
Response Adaptation
The adapt_response_style method uses the combined long-term and short-term features from a user's profile to modify a response.
This flowchart illustrates the decision logic within adapt_response_style for modifying a response based on the user's calculated profile traits.
Sources: monGARS/core/mimicry.py:277-302
PersonalityEngine
The PersonalityEngine complements the MimicryModule by managing a more stable, long-term model of a user's personality. It uses a StyleFineTuner to analyze interactions and updates a persistent UserPersonality record in the database.
Sources: monGARS/core/personality.py
Core Concepts
- Default Profile: New users start with a default personality profile.
- Style Analysis: It delegates the analysis of user interactions to a
StyleFineTunerto get aStyleAnalysisobject. - Trait Blending: The engine blends the new analysis with the existing profile using a defined learning rate, gradually evolving the user's personality traits over time.
- Persistence: Profiles are loaded from and saved to the
UserPersonalitySQL table.
Data Structures
The engine revolves around the PersonalityProfile dataclass and the UserPersonality SQLModel table.
PersonalityProfile Dataclass
| Field | Type | Description |
|---|---|---|
traits | dict[str, float] | Core personality traits (e.g., openness, neuroticism). |
interaction_style | dict[str, float] | Stylistic preferences (e.g., formality, humor). |
context_preferences | dict[str, float] | Topic preferences (e.g., technical, casual). |
adaptation_rate | float | Rate at which the profile adapts. |
confidence | float | The engine's confidence in the accuracy of the profile. |
Sources: monGARS/core/personality.py:19-26
UserPersonality ER Diagram
This diagram represents the schema for the UserPersonality table used to persist profiles in the database.
Sources: monGARS/core/personality.py:11, 85-94
Personality Analysis Flow
The analyze_personality method is the entry point for updating a user's profile.
This sequence diagram shows how the PersonalityEngine uses the StyleFineTuner to get a personality analysis and then blends it into the user's existing profile before saving it asynchronously.
Sources: monGARS/core/personality.py:102-120
Operational Considerations
The "Persona drift" scenario is a recognized operational issue where adaptive response caches may expire, causing inconsistent persona application. The recommended playbook involves checking cache hit rates, reviewing profile freshness, and potentially warming caches for active users.
Sources: scripts/generate_monGARS_llm_dataset.py:495-515
Summary
The Personality and Mimicry system provides monGARS with a sophisticated mechanism for conversational adaptation. The MimicryModule handles real-time, short-term style matching by analyzing features like sentence length and sentiment. In parallel, the PersonalityEngine builds a stable, long-term understanding of the user's personality, which evolves slowly over many interactions. Together, they allow the AI to be both immediately responsive to the user's current tone and gradually aligned with their underlying personality, creating a more natural and engaging user experience.
Sources: monGARS/core/personality.py, monGARS/core/mimicry.py