Track AI Character Secrets with Local Embeddings and llamadart
Job to be done: Implement local embeddings to track information sharing in AI character dialogue
🇳🇬 Ways to use this in Nigeria
Ideas to get you started, adapt to your situation.
- Student
Build a JAMB prep app that checks if your practice answers match correct facts using local AI embeddings.
- 9-5 employee
Create a tool to track if your team members are sharing project secrets correctly in internal chats.
- Entrepreneur
Develop a game where NPCs remember and share specific plot details based on player conversations.
What this is, in plain English
This workflow explores how to use “local embeddings” to manage what AI characters in a game know and learn from each other. Local embeddings mean that the AI model runs directly on your device, without needing to send data to an external service. This approach helps a game track specific pieces of information, like a secret, and determine if one character has successfully shared it with another during a conversation.
The core idea is to compare the meaning of what a character says with known facts in the game world. The llamadart library, written in the Dart programming language, helps integrate AI models like EmbeddingGemma to perform this comparison.
Because this involves setting up a coding environment and working with specific programming libraries, it is considered an advanced concept. There isn’t a simple copy-paste recipe for beginners, as the exact instructions would live within a larger code project that changes as the tools update.
What you can use it for
- Tracking character knowledge: Create game worlds where AI characters have individual knowledge and secrets, and track when information is genuinely shared.
- Building dynamic dialogue systems: Develop AI agents that can learn new facts or update their understanding based on conversations.
- Comparing text meaning: Build systems that can understand if two different sentences convey similar information, even if they use different words.
- Creating interactive narratives: Design stories where the flow of information between characters directly impacts the game’s progression.
Tools you need
- llamadart (free): A Dart library for working with local AI models, including embedding models, within Dart applications.
- EmbeddingGemma (free): A small, efficient AI model from Google designed to create numerical representations (embeddings) of text. It can be run locally or accessed via free APIs.
How it actually works
The process begins with the game storing specific pieces of information as “facts,” for example, “Pip’s red scarf is lying in the pond reeds.” When two AI characters have a conversation, the system first identifies facts that the speaker knows but the listener does not.
Next, an “embedding model” (like EmbeddingGemma) is used through the llamadart library. This model converts both the speaker’s lines of dialogue and the stored facts into “numeric vectors” (lists of numbers). These vectors are mathematical representations of the text’s meaning.
The system then compares these numeric vectors using a technique called “cosine similarity.” This calculation measures how similar the meanings of the dialogue and the facts are. If the similarity score is high enough, the game records that the listener has acquired that specific piece of information and notes who shared it.
The author illustrates the core operations with these code snippets, which are part of a larger Dart application:
// The game stores facts as text
final facts = [
"Pip's red scarf is lying in the pond reeds.",
];
// Use llamadart to turn facts into numeric vectors (embeddings)
final factVectors = await engine.embedBatch(
facts.map((text) => 'title: none | text: $text').toList(),
normalize: true,
);
// Turn a character's spoken line into a numeric vector
final lineVector = await engine.embed(
"title: none | text: I saw Pip's scarf tangled in the reeds by the pond.",
normalize: true,
);
It’s important to note that the embedding model itself does not decide if information was learned. Instead, it provides a similarity score, and the game’s rules then use this score to make the final decision about whether a fact has been successfully transferred. For accurate results, the same embedding model and consistent text formatting (like the title: none | text: prefix shown above) must be used for both the facts and the dialogue lines.
Words you’ll see, explained
- Embeddings: Numerical representations of text that capture its meaning. Words or sentences with similar meanings will have similar embeddings, making them easier to compare.
- Numeric vector: A list of numbers that represents a piece of text (its embedding). These vectors allow computers to understand and compare the meaning of words and sentences.
- Cosine similarity: A mathematical technique used to measure how similar two numeric vectors are. In this context, it indicates how close in meaning two pieces of text (like a dialogue line and a stored fact) are.
Original source
This concept was introduced by leehack in a blog post titled ‘Local embeddings with llamadart: how llamas share secrets’ on the DEV Community platform. The article explores how to implement this system within a game called Llama Village.
Notes & variations
- Do you even need this?: For simpler applications or games, you might achieve similar results with less complexity by using keyword matching or rule-based systems to track information. Embeddings are powerful for understanding nuanced meaning but add a layer of technical complexity.
- Free-tier limits: Since
llamadartandEmbeddingGemmaare designed for local use, there are generally no traditional “free-tier limits” or costs associated with their usage. The primary limitation will be the processing power and memory of the device running the application. - Common pitfall: A common mistake is using different embedding models or inconsistent text formatting (such as adding prefixes) when generating embeddings for facts versus dialogue lines. This can lead to inaccurate similarity scores and unreliable tracking of information. Always ensure consistency for the best results.