Skip to main content
Glama
noualit

llama-memory

by noualit

llama-memory

用于 llama-server 的 MCP 记忆服务,通过 Postgres + PGVector 提供持久化历史和语义记忆。

注意:本项目仅用于本地/演示用途。未经额外加固(HTTPS、适当认证、备份),请勿直接暴露到互联网。

功能

  • 语义记忆:按含义而非仅关键词保存和检索记忆。

  • 对话桥接:LLM 自动创建对话,记忆相互关联。

  • 跨会话回忆:询问“我们之前聊过什么?”即可获得准确答案。

  • MCP 协议:直接与 llama-server 内置的 MCP 支持配合使用。

Related MCP server: engram

要求

  • Python 3.11(推荐使用 Miniconda)

  • PostgreSQL 16+ 并带有 PGVector 扩展

  • 带 --jinja 标志的 llama-server(工具调用必需)

  • 在 llama-server 上运行 nomic-embed-text(默认端口 8081)

安装

# Clone the repo
git clone https://github.com/noualit/llama-memory-local.git
cd llama-memory-local

# Create environment
conda create -n llama-memory python=3.11
conda activate llama-memory

# Install dependencies
pip install -e .

配置

将 .env.example 复制为 .env 并进行编辑:

cp .env.example .env

示例:

# Database
DATABASE_URL="postgresql://postgres:yourpassword@localhost:5432/llamamem"

# Llama-server (LLM)
LLAMA_SERVER_BASE_URL="http://localhost:8080"

# Embedding model (nomic-embed-text via llama-server)
EMBEDDING_MODEL_URL="http://localhost:8081"

# Embedding model name (default: nomic-embed-text)
EMBEDDING_MODEL_NAME="nomic-embed-text"

# Service port
SERVICE_PORT=9001

设置数据库

创建数据库并运行迁移:

psql -U postgres -c "CREATE DATABASE llamamem;"
alembic upgrade head

应用还会在启动时自动确保基本模式,以方便使用。

运行服务

# Using the script
.\scripts\run_server.ps1

# Or directly
python -m uvicorn app.main:app --host 0.0.0.0 --port 9001

连接到 llama-server

添加到您的 llama-server MCP 配置中:

{
  "mcpServers": {
    "llama-memory": {
      "url": "http://YOUR_SERVER_IP:9001/mcp"
    }
  }
}

服务必须可从 llama-server 访问。如果它们运行在不同的机器上,请使用实际 IP,而不是 localhost。

MCP 工具

工具

描述

create_conversation

创建新的对话会话

list_conversations

列出对话及其记忆数量

get_conversation_history

获取对话中的所有记忆

search_memories

跨所有记忆进行语义搜索

save_memory

存储重要事实或决策

系统提示

您可以:

  • 从服务获取推荐的系统提示:

    • GET /system-prompt → 返回纯文本。

  • 或者将以下最小版本粘贴到 llama-server 中:

MEMORY WORKFLOW:
- At the start of each new conversation, call create_conversation with a short title.
- Use the conversation_id from create_conversation when calling save_memory.
- Before answering questions about past topics, call search_memories FIRST.
- When the user shares important information, save it with save_memory.
- If list_conversations has previous chats, check get_conversation_history for context.

健康检查

curl http://localhost:9001/health

返回数据库状态、嵌入服务状态和工具数量。

架构

高层结构:

  • app/main.py — FastAPI 应用、生命周期、/system-prompt

  • app/settings.py — 从 .env 读取的 Pydantic 设置

  • app/clients/embeddings.py — 调用 nomic-embed-text 生成向量

  • app/db/engine.py — asyncpg 连接池(单例)

  • app/db/schema.py — 启动时自动创建表

  • app/mcp/endpoint.py — MCP 协议处理器、速率限制器

  • app/mcp/tools/ — 各个工具的实现

  • migrations/ — Alembic 数据库迁移

开发

# Run tests
pytest tests/ -v

# Run with auto-reload
python -m uvicorn app.main:app --host 0.0.0.0 --port 9001 --reload

有关贡献指南,请参阅 CONTRIBUTING.md。

许可证

MIT(参见 LICENSE 文件)。

Maintenance

ActivityStale
ResponsivenessNo issues

Related MCP Connectors

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    Persistent semantic memory server for AI assistants via MCP, enabling long-term context retention and semantic search across conversations.
    12
    MIT
  • A
    license
    C
    quality
    A
    maintenance
    Provides persistent, local-first AI memory across sessions via MCP tools for storing, searching, and retrieving context from past interactions.
    31
    139 PyPI
    1
    MIT
  • A
    license
    A
    quality
    D
    maintenance
    Provides persistent memory with semantic search for MCP-based AI agents, enabling them to store and recall information across sessions using vector embeddings.
    4
    1
    MIT
  • F
    license
    Not graded
    quality
    C
    maintenance
    Persistent memory MCP server for AI agents that stores, recalls, and searches conversation history, key-value context, and long-term entries across sessions with semantic search and FIFO queues.
    526 npm
    1
    -