Mem0

Mem0 會從代理對話中擷取持久記憶,並在後續執行時取回相關記憶。 當記憶體應該跨會話可用時,設定儲存與檢索範圍。

此整合採用記憶模式:提取並回憶選定且持久的資訊,而非重播完整對話逐字稿。

這很重要

Mem0 是一個第三方系統。 在寄送申請資料前,請檢視其資料處理、保存、區域邊界及服務條款。

備註

目前 Agent Framework .NET 還沒有 Mem0 整合。

安裝套件

pip install agent-framework-mem0 --pre

直接設定 MEM0_API_KEY 或傳遞 API 金鑰。 分別設定儲存與檢索:

  • user_id、agent_id 和 application_id 是儲存範圍。 Mem0 會將它們標註在每一筆已儲存的記憶上。
  • search_user_id、search_agent_id 和 search_application_id 是檢索範圍。 Mem0 會用這些記憶來選擇要搜尋的記憶。

應用程式範圍需要 Mem0 Platform 用戶端(AsyncMemoryClient)。 OSS AsyncMemory 用戶端僅支援使用者與代理範圍。

檢索範圍不會繼承自儲存範圍。 如果你未設定任何 search_* 範圍,提供者會儲存記憶,但不會提取這些記憶。

針對每位使用者的記憶體,請將 search_user_id 和 user_id 設為相同的穩定識別碼:

async def main() -> None:
    """Example of memory usage with Mem0 context provider."""
    print("=== Mem0 Context Provider Example ===")
    # Each record in Mem0 should be associated with agent_id or user_id or application_id.
    # In this example, we associate Mem0 records with user_id.
    user_id = str(uuid.uuid4())
    # For Azure authentication, run `az login` command in terminal or replace AzureCliCredential with preferred
    # authentication option.
    # For Mem0 authentication, set Mem0 API key via "api_key" parameter or MEM0_API_KEY environment variable.
    async with (
        AzureCliCredential() as credential,
        Agent(
            client=FoundryChatClient(credential=credential),
            name="FriendlyAssistant",
            instructions="You are a friendly assistant.",
            tools=retrieve_company_report,
            context_providers=[Mem0ContextProvider(source_id="mem0", user_id=user_id, search_user_id=user_id)],
        ) as agent,
    ):

Mem0 以非同步方式處理記憶。 在生產環境中,應使用重試或服務感知的一致性處理機制,而非依賴固定的延遲。

備註

目前 Agent Framework Go 還沒有 Mem0 整合功能。 最新狀態請參閱 Agent Framework Go 倉庫 。

下一步

深入探討: