0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【入門から実践】Letta(旧 MemGPT)の使い方を体系的に解説

Posted at

🚀 本記事では、Letta(旧 MemGPT) を導入して「Hello, World!」を動かすところから、会話メモリを活用した実用コードまでを体系的にまとめます。
初学者でも動かせる最小構成と、応用につながる拡張例を両方提示します。


Letta とは?

  • Letta は、LLM のコンテキスト制約を乗り越えるために「メモリ階層(Core/External)」と「自己編集型メモリ」を実装した エージェント基盤/サーバー です。
  • 開発者は 状態を持つエージェントを API/SDK で扱えます。
  • もともと「MemGPT」と呼ばれていた OSS が進化し、製品名として Letta に統合されました。

特徴

  • 会話の記憶管理
    Core Memory(人格・ユーザー情報)と External/Archival Memory(ベクタDBや長文ナレッジ)を分離。
  • 運用向け設計
    REST API、Python/TS SDK、UI(ADE: Agent Development Environment)で可視化・管理可能。
  • マルチモデル対応
    OpenAI / Anthropic / Ollama / vLLM など多数のプロバイダに接続可能。
  • セキュリティ
    パスワード保護、ツール実行のサンドボックス機構(E2B)。

インストール

前提環境

  • Python 3.9+ または Docker
  • LLM プロバイダの API キー(例:OpenAI の OPENAI_API_KEY
  • ローカル推論(Ollama 等)を使う場合は事前インストールと起動

1. Letta Cloud + SDK(最短ルート)

pip install -U letta-client

2. Docker で自己ホスト(推奨)

docker run \
  -v ~/.letta/.persist/pgdata:/var/lib/postgresql/data \
  -p 8283:8283 \
  -e OPENAI_API_KEY="your_openai_api_key" \
  letta/letta:latest

Ollama など複数プロバイダを使う場合:

docker run \
  -v ~/.letta/.persist/pgdata:/var/lib/postgresql/data \
  -p 8283:8283 \
  -e OPENAI_API_KEY="your_openai_api_key" \
  -e OLLAMA_BASE_URL="http://host.docker.internal:11434" \
  letta/letta:latest

※Linux の場合は --network host を付けて http://localhost:11434 を利用します。


3. pip でサーバー起動

pip install -U letta
export OPENAI_API_KEY=sk-...
letta server  # ポート :8283 で起動

SDK 利用時は letta-client を追加インストールしてください。


設定

.env の例

OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=...
OLLAMA_BASE_URL=http://host.docker.internal:11434

SECURE=true
LETTA_SERVER_PASSWORD=change_me_strongly

Python SDK 接続例

from letta_client import Letta

# 自己ホストに接続
client = Letta(base_url="http://localhost:8283", token="change_me_strongly")

# Letta Cloud に接続
cloud = Letta(token="LETTA_API_KEY", project="default-project")

実行例

Hello World 的な最小コード

from letta_client import Letta

client = Letta(token="LETTA_API_KEY")  # Cloud利用

agent = client.agents.create(
    model="openai/gpt-4.1",
    embedding="openai/text-embedding-3-small",
    memory_blocks=[
        {"label": "persona", "value": "You are a concise assistant."},
        {"label": "human",   "value": "The user's name is Alex."}
    ]
)

resp = client.agents.messages.create(
    agent_id=agent.id,
    messages=[{"role": "user", "content": "Hello! What's my name?"}]
)

for m in resp.messages:
    if m.get("message_type") == "assistant_message":
        print("ASSISTANT:", m["content"])

実用例:会話とメモリ編集

from letta_client import Letta

client = Letta(base_url="http://localhost:8283", token="change_me_strongly")

agent = client.agents.create(
    model="openai/gpt-4.1",
    embedding="openai/text-embedding-3-small",
    memory_blocks=[
        {"label": "persona", "value": "You are a helpful travel planner."},
        {"label": "human", "value": "The human likes ramen and snowboarding."},
    ]
)

def ask(text: str):
    r = client.agents.messages.create(agent_id=agent.id, messages=[{"role": "user", "content": text}])
    for m in r.messages:
        if m.get("message_type") == "assistant_message":
            return m["content"]

print(ask("Hi, my name is Sato."))

# Core Memory を追加
client.core_memory.blocks.create(
    agent_id=agent.id,
    label="user_profile",
    value="The user's name is Sato. They live in Tokyo."
)

print(ask("Where do I live? And what foods do I like?"))

出力イメージ

ASSISTANT: Nice to meet you, Sato!
ASSISTANT: You live in Tokyo and you like ramen and snowboarding.

ベストプラクティス

  1. まずは Cloud + SDK → ADE UI で観察すると理解が速い

  2. 本番運用は Docker → APIキーは .env で管理

  3. セキュリティSECURE=true + 強固な LETTA_SERVER_PASSWORD

  4. メモリ設計を分離

    • Core Memory = パーソナリティ・ユーザー情報
    • External Memory = ナレッジベース / 会話履歴
  5. 小さく始めて大きくする

    • まず OpenAI で検証 → その後 Ollama/vLLM へ展開

参考リンク


まとめ

  • Letta は「LLM × 長期記憶」を扱える新しい基盤
  • Cloud / Docker / pip の 3 スタイルで導入可能
  • Hello World → 会話 + メモリ活用 → External/RAG と段階的に拡張するのがベスト
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?