An AI-powered semantic memory system built with Rust and Google's Gemini API. Store memories with semantic embeddings and retrieve them contextually when asking questions.
- Semantic Memory: Store text memories with AI-generated embeddings for semantic search
- Contextual Retrieval: Ask questions and get relevant memories injected into AI responses
- Vector Similarity Search: Uses cosine similarity to find semantically related memories
- Persistent Storage: Memories stored in JSON format on disk
- CLI Interface: Simple command-line interface for storing and querying memories
- Rust (latest stable version)
- Google Gemini API key (get one free at aistudio.google.com)
- Clone this repository:
git clone <your-repo-url>
cd memory-system- Create a
.envfile with your API key:
echo "GEMINI_API_KEY=your_api_key_here" > .env- Build and run:
cargo run -- remember "Hello, I'm building a memory system!"
cargo run -- ask "What am I building?"cargo run -- remember "Your memory text here"cargo run -- ask "Your question here"The system will:
- Embed your question
- Find the top 3 most semantically similar memories
- Inject them into the AI's context
- Return an answer based on your stored memories
cargo run -- listcargo run -- clear# Store some memories
$ cargo run -- remember "My name is Alice and I love programming"
$ cargo run -- remember "I work as a software engineer at TechCorp"
$ cargo run -- remember "My favorite programming language is Rust"
# Ask questions
$ cargo run -- ask "What's my name and job?"
# Output: Based on your memories, your name is Alice and you work as a software engineer at TechCorp.
$ cargo run -- ask "What do I like?"
# Output: According to your stored memories, you love programming and your favorite language is Rust.βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β User Input βββββΆβ Agent Layer βββββΆβ Gemini API β
β β β β β β
β β’ Remember text β β β’ Embed text β β β’ Embeddings β
β β’ Ask questions β β β’ Search memory β β β’ Chat β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Memory Store β
β β
β β’ JSON storage β
β β’ Vector search β
β β’ Cosine sim. β
ββββββββββββββββββββ
Memories are stored in data/memories.json:
{
"memories": [
{
"id": 0,
"text": "My name is Alice",
"embedding": [0.123, -0.456, 0.789, ...]
}
]
}Each memory contains:
- ID: Unique identifier
- Text: The original text
- Embedding: 768-dimensional vector representing semantic meaning
When you ask a question:
- Embed Question: Convert your question to a vector using
gemini-embedding-2 - Vector Search: Compare question vector to all stored memory vectors using cosine similarity
- Retrieve Top-K: Return the 3 most similar memories
- Context Injection: Include retrieved memories in the AI prompt
- Generate Response: Use
gemini-2.0-flashto answer with memory context
The system uses cosine similarity to measure semantic relatedness:
similarity = (A β’ B) / (||A|| Γ ||B||)
Where:
A β’ Bis the dot product||A||and||B||are vector magnitudes
GEMINI_API_KEY: Your Google Gemini API key (required)
| Purpose | Model | API Endpoint |
|---|---|---|
| Embeddings | gemini-embedding-2 |
embedContent |
| Chat | gemini-2.0-flash |
generateContent |
tokio: Async runtimereqwest: HTTP client for API callsserde: JSON serialization/deserializationclap: Command-line argument parsingcolored: Terminal output coloringdotenv: Environment variable loadinganyhow: Error handling
cargo buildcargo testsrc/
βββ main.rs # CLI entry point and command routing
βββ agent.rs # Core logic for remembering and asking
βββ memory.rs # Memory storage and vector search
βββ embeddings.rs # Google Gemini embedding API client
βββ llm.rs # Google Gemini chat API client
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is open source. Feel free to use and modify.
- Built with Google Gemini API
- Inspired by semantic memory systems and vector databases
- Thanks to the Rust community for excellent crates