assistant-engine/open-functions-memory

Memory OpenFunction provides a standardized API for storing, updating, retrieving, and removing conversational memories - empowering AI-powered applications with enhanced context management.

v1.0.0 2025-03-08 14:16 UTC

This package is auto-updated.

Last update: 2025-03-08 14:23:05 UTC


README

Demo Video

Memory OpenFunction

The Memory OpenFunction is an implementation of the OpenFunctions architecture designed to provide a standardized interface for managing memories within your AI-powered applications. This repository allows language models (LLMs) to store, update, remove, and list memories easily.

Key Features:

  • ✅ Standardized Memory Interface - Provides a consistent API to store, update, remove, and list memories.
  • ✅ CRUD Operations - Built-in methods to add new memories, update existing entries, delete memories, and retrieve all stored memories.
  • ✅ Seamless Integration - Easily integrates with the OpenAI PHP SDK and can be added as a tool within the Filament Assistant Plugin.
  • ✅ Flexible Storage Options - Comes with a demo JSON memory driver, with the ability to swap in custom drivers like MySQL, Redis, or others.
  • ✅ Memory Presenter - Enhance your LLM’s context by preloading user memories directly into the conversation history.
  • ✅ Open Source & Extensible - Fully compliant with the OpenFunctions architecture and ready for customization to suit your application’s needs.

Installation

Install the Memory OpenFunction via Composer:

composer require assistant-engine/open-functions-memory

Usage

Using the OpenAI PHP SDK

Below is a basic example of how to use the Memory OpenFunction directly with the OpenAI PHP SDK:

<?php
use AssistantEngine\OpenFunctions\Memory\MemoryOpenFunction;
use AssistantEngine\OpenFunctions\Memory\Driver\JsonMemoryDriver;
use OpenAI;

// Initialize the JSON memory driver with a storage path just for demonstration
$driver = new JsonMemoryDriver(__DIR__ . '/storage/memory.json');

// Create an instance of MemoryOpenFunction
$memoryFunction = new MemoryOpenFunction($driver);

// Generate function definitions (for tool integration with an LLM)
$functionDefinitions = $memoryFunction->generateFunctionDefinitions();

$client = OpenAI::client(env('OPENAI_TOKEN'));

$result = $client->chat()->create([
    'model' => 'gpt-4o',
    'messages' => [],
    'tools' => $functionDefinitions
]);

$choice = $result->choices[0];

if ($choice->finishReason === 'tool_calls') {
    $toolCalls = processToolCalls($choice->message->toolCalls, $githubFunction);
} 

function processToolCalls($toolCalls, AbstractOpenFunction $memoryFunction)
{
    $result = [];

    foreach ($toolCalls as $toolCall) {
        // Extract the function name (already namespaced) and arguments
        $functionName = $toolCall->function->name;
        $functionArgs = json_decode($toolCall->function->arguments, true);

        $response = $memoryFunction->callMethod($functionName, $functionArgs);
    }

    return $result;
}

Using the Filament Assistant Plugin

The Memory OpenFunction can also be integrated as a tool within the Filament Assistant Plugin. To add it, include the following configuration in your config/filament-assistant.php file:

    // inside config/filament-assistant.php
    
    // Tools configuration: each tool is identified by a key.
    'tools' => [
        'memory' => [
            'namespace'   => 'memory',
            'description' => 'This is your memory. Store all relevant facts about the user.',
            'tool'        => function () {
                // Initialize the JSON memory driver with a storage path just for demonstration
                $driver = new JsonMemoryDriver(__DIR__ . '/storage/memory.json');

                return new MemoryOpenFunction($driver);
            }
        ]
    ]
    // ...

With this configuration in place, your assistant can directly utilize the Memory tool. You can add multiple tools to enhance your assistant’s capabilities.

Demo Assistant Example

Memory Driver

The Memory OpenFunction interacts with a driver to handle storing, retrieving, and managing memory data. The driver must implement the MemoryDriverInterface, which defines a standardized set of methods.

namespace AssistantEngine\OpenFunctions\Memory\Contracts;

interface MemoryDriverInterface
{
    public function listMemories(): array;
    public function getMemory(string $memoryId): ?string;
    public function addMemory(string $content): string;
    public function updateMemory(string $memoryId, string $newContent): void;
    public function removeMemory(string $memoryId): void;
}

For your convenience, there is already a JSON memory driver provided. This is just a demonstration driver. Feel free to implement the driver yourself using other storage options like MySQL, Redis, or any other preferred storage system. The JSON memory driver stores a data object like this:

{
    "memories": {
        "mem_64b7c9389c001": "User prefers thin crust pizza with extra cheese.",
        "mem_64b7c9389c002": "User loves margherita pizza topped with fresh basil.",
        "mem_64b7c9389c003": "User avoids pineapple on pizza."
    }
}

Using the Memory Presenter

The MemoryPresenter implements the MessageListExtensionInterface from the OpenFunctions architecture. This interface allows the presenter to extend or modify a MessageList before it’s sent to the language model. The idea is to provide the LLM with more context by preloading user memories into the conversation history.

use AssistantEngine\OpenFunctions\Memory\Driver\JsonMemoryDriver;
use AssistantEngine\OpenFunctions\Memory\MemoryPresenter;
use AssistantEngine\OpenFunctions\Core\Models\Messages\MessageList;

// Initialize the memory driver (for example, using the JSON driver)
$driver = new JsonMemoryDriver(__DIR__ . '/storage/memory.json');

// Create the MemoryPresenter instance with the driver
$presenter = new MemoryPresenter($driver);

// Create a new MessageList
$messageList = new MessageList();

// Register the MemoryPresenter as an extension to the MessageList
$messageList->addExtension($presenter);

// Now, when converting the message list to an array,
// the presenter's extend() method will be invoked automatically,
// adding the preloaded memories as a developer message.
$messagesArray = $messageList->toArray();

Feel free to adjust the MemoryPresenter implementation or integrate it with your custom memory drivers as needed!

Methods

Method Description Parameters
addMemory Adds a new memory entry. content: string (required)
updateMemory Updates an existing memory by its ID. memoryId: string (required);
newContent: string (required)
removeMemory Removes a memory entry by its ID. memoryId: string (required)
listMemories Lists all stored memories. None

Available Open Function Implementations

In addition to creating your own OpenFunction, there are several ready-to-use implementations available like Slack, Trello, Notion, Jira Service Desk, Bitbucket, and GitHub.

You can find a list of all OpenFunctions inside the OpenFunctions Core repository.

Consultancy & Support

Do you need assistance integrating OpenFunctions into your application, or help setting it up?
We offer consultancy services to help you get the most out of our package, whether you’re just getting started or looking to optimize an existing setup.

Reach out to us at contact@assistant-engine.com.

Contributing

We welcome contributions from the community! Feel free to submit pull requests, open issues, and help us improve the package.

License

This project is licensed under the MIT License. Please see License File for more information.