Multilingual Chat Support

Status: Planned — research phase complete, implementation pending.


Problem Statement

Echo of Dreams currently assumes English for all text processing:

  1. Full-text search (FTS) is hardcoded to PostgreSQL's 'english' text search configuration. Non-English lorebook content and memories are indexed with English stemming rules, producing incorrect tokens and poor recall.
  2. Cross-language search is unsupported. A German lorebook queried in English returns no FTS matches — the stems are incompatible across languages.
  3. LLM response language is unguided. When a character's system definition is in English, models tend to respond in English even when the user writes in another language. No language instruction exists anywhere in the pipeline.

Vector embeddings (openai/text-embedding-3-large) already handle multilingual semantics well — "wizard" and "Zauberer" are close in embedding space. The gaps are in FTS and prompt-level behavior.


Current State

All FTS uses 'english' configuration:

Location Usage
FTS triggers (33_vector_and_fts_support.sql) to_tsvector('english', ...)
Lorebook search (fn_search_lorebook) plainto_tsquery('english', ...)
Memory search (fn_search_memories) plainto_tsquery('english', ...)
Entry expansion (fn_expand_lorebook_entries) plainto_tsquery('english', ...)

PostgreSQL ships with text search configurations for ~30 languages (german, french, spanish, italian, japanese, etc.). Each provides language-appropriate stemming and stop-word removal.

Design: Per-Entity Language Configuration

Lorebooks and conversations get a user-set language that controls FTS indexing.

Schema Changes

Add a text_search_config column to entities that have tsvector columns:

Default value: 'simple' (no stemming, works for any language).

Trigger Changes

FTS triggers switch from hardcoded 'english' to the row's config:

-- Before
NEW.content_tsv := to_tsvector('english', content_text);

-- After
NEW.content_tsv := to_tsvector(COALESCE(NEW.text_search_config, 'simple'), content_text);

Query Changes

Search functions accept a p_text_search_config parameter for the query side:

-- Query uses the searcher's language config
v_fts_query := plainto_tsquery(p_text_search_config, v_query_text);

The Cross-Language Problem

When the content language and query language differ (German lorebook, English user), FTS fundamentally cannot bridge the gap — stems from different languages don't overlap.

Resolution: Accept this as a known limitation. The hybrid search system already handles it:

Search Method Cross-Language? Notes
Vector similarity Yes Embedding model handles 100+ languages natively
FTS No Same-language only; scores ~0 when languages differ
Keyword substring Partial Matches proper nouns, shared terms across languages
Regex triggers Partial Pattern-based, language-independent

When languages match, FTS provides its full stemming benefit. When they don't, the score naturally drops to ~0 and the other three signals compensate. No special fallback logic needed — the scoring combination already handles it gracefully.

Optional Enhancement: Dual tsvector columns

If cross-language FTS recall proves insufficient in practice, a second content_tsv_simple column (using 'simple' config) can be added alongside the language-specific one. This provides exact-token matching across languages at the cost of a second GIN index per partition.

This is not planned for initial implementation — it should be driven by measured recall gaps rather than preemptive optimization.

CJK Consideration

Chinese, Japanese, and Korean are not well-served by PostgreSQL's built-in text search configurations, which rely on whitespace tokenization. Options for future consideration:

For the initial implementation, CJK content should use the 'simple' config and rely primarily on vector search for recall. CJK-specific tokenization can be added as a follow-up if the user base warrants it.


Layer 2: LLM Response Language

Current State

The chat pipeline has zero language handling:

The system prompt is assembled from the character's SystemPrompt field, mode sections, lorebook context, memory, and persona — all in whatever language the creator wrote them in.

When a character definition is entirely in English, the LLM receives an English-dominated context and naturally anchors to English responses. Behavior varies by model — Claude and GPT-4 will often follow the user's language if the user is consistent, but smaller models may not.

Design: Language Preference with Prompt Injection

New Settings

Add language configuration at two levels:

  1. Character-level language — The language the character's content is written in. Set by the creator.
  2. User-level preferred language — The language the user wants responses in. Set by the user per-conversation or globally.

When the user's preferred language differs from the character's content language, a prompt instruction is injected to guide the LLM.

Prompt Injection

A new LanguageContentProvider in the pipeline injects a language instruction into the system prompt when needed:

[Always respond in {user_language}. The character's personality and world information
may be written in {character_language} — use it as reference but write your responses
in {user_language}.]

Placement: Near the end of the system prompt, after character definition and context but before security directives. Late placement gives it higher influence over the LLM's behavior (recency bias).

Conditional injection: Only injected when:

When languages match or no preference is set, nothing is injected — no unnecessary prompt bloat.

Enhancement Prompt Reinforcement

For longer conversations where models drift back to the content language, the enhancement prompt system (already supporting periodic mid-conversation reminders) can reinforce the language preference:

[Remember: respond in {user_language}.]

This uses the existing EnhancePrompt / EnhanceInterval mechanism on modes — no new infrastructure needed.

Model-Specific Behavior

Language adherence varies significantly by model:

The enhancement prompt reinforcement addresses the middle tier. For weak models, the only reliable solution is translating the character content itself, which is outside the scope of this feature.


Implementation Order

Phase 1: FTS Language Support (Database)

  1. Add text_search_config column to lorebook_entry_contents and web_summaries
  2. Add language column to lorebooks and web_conversations
  3. Update FTS triggers to use per-row config
  4. Update search functions to accept query-side config parameter
  5. Backfill existing rows with 'simple' default
  6. Add language selector UI to lorebook and conversation settings

Phase 2: LLM Language Guidance (Pipeline)

  1. Add SettingsKeys.Chat.ResponseLanguage setting
  2. Add language property to character and conversation models
  3. Create LanguageContentProvider for system prompt injection
  4. Add language preference UI to conversation settings
  5. Optional: Add language reinforcement to enhancement prompts

Phase 3: Observation and Tuning

  1. Monitor FTS recall for non-English content vs English baseline
  2. Evaluate whether dual tsvector columns are needed
  3. Assess CJK user needs and tokenization requirements
  4. Tune prompt injection wording based on model behavior across languages

What This Does NOT Cover