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

1. Go to this page and download the library: Download assistant-engine/open-functions-notion 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-notion example snippets




use AssistantEngine\OpenFunctions\Notion\NotionOpenFunction;
use OpenAI;

// Set up your Notion integration API key
$apiKey = env('NOTION_API_KEY');  // Your Notion integration token

// Initialize the Notion Open Function
$notionFunction = new NotionOpenFunction($apiKey);

// Generate function definitions (for tool integration with an LLM)
$functionDefinitions = $notionFunction->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, $notionFunction);
} 

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

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

        $response = $notionFunction->callMethod($functionName, $functionArgs);
        $result[] = $response;
    }

    return $result;
}

// inside config/filament-assistant.php

'tools' => [
    'notion' => [
        'namespace'   => 'notion',
        'description' => 'This tool allows you to interact with your Notion workspace to manage databases, pages, and content blocks.',
        'tool'        => function () {
            $apiKey = env('NOTION_API_KEY');
            return new \AssistantEngine\OpenFunctions\Notion\NotionOpenFunction($apiKey);
        },
    ]
]