PHP code example of assistant-engine / open-functions-memory

1. Go to this page and download the library: Download assistant-engine/open-functions-memory library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

assistant-engine / open-functions-memory example snippets



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;
}

    // 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);
            }
        ]
    ]
    // ...

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;
}

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();