PHP code example of assistant-engine / open-functions-core
1. Go to this page and download the library: Download assistant-engine/open-functions-core 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-core example snippets
use AssistantEngine\OpenFunctions\Core\Contracts\AbstractOpenFunction;
use AssistantEngine\OpenFunctions\Core\Models\Responses\TextResponseItem;
use AssistantEngine\OpenFunctions\Core\Helpers\FunctionDefinition;
use AssistantEngine\OpenFunctions\Core\Helpers\Parameter;
class HelloWorldOpenFunction extends AbstractOpenFunction
{
/**
* Generate function definitions.
*
* This method returns a schema that defines the "helloWorld" function.
*/
public function generateFunctionDefinitions(): array
{
// Create a new function definition for helloWorld.
$functionDef = new FunctionDefinition(
'helloWorld',
'Returns a friendly greeting.'
);
// In this simple example, no parameters are = $helloFunction->generateFunctionDefinitions();
// Example: Using the tools in an OpenAI API call (pseudo-code)
// Assume you have an OpenAI client that accepts a "tools" parameter.
$client = new OpenAIClient('YOUR_API_KEY');
$response = $client->chat()->create([
'model' => 'gpt-4o',
'messages' => [
['role' => 'user', 'content' => 'Greet me please.']
],
'tools' => $tools,
]);
// Output the response content.
print_r($response);
use AssistantEngine\OpenFunctions\Core\Helpers\FunctionDefinition;
use AssistantEngine\OpenFunctions\Core\Helpers\Parameter;
// Create a function definition with name "myFunction" and a short description
$funcDef = new FunctionDefinition('myFunction', 'A function that does something');
// Add a
use AssistantEngine\OpenFunctions\Core\Examples\DeliveryOpenFunction;
use AssistantEngine\OpenFunctions\Core\Examples\WeatherOpenFunction;
use AssistantEngine\OpenFunctions\Core\Services\OpenFunctionRegistry;
$deliveryFunction = new DeliveryOpenFunction(['Pizza', 'Burger', 'Sushi']);
$weatherFunction = new WeatherOpenFunction();
$registry = new OpenFunctionRegistry();
// Register DeliveryOpenFunction under the "delivery" namespace
$registry->registerOpenFunction(
'delivery',
'Handles product delivery, orders, and shipping details.',
$deliveryFunction
);
// Register WeatherOpenFunction under the "weather" namespace
$registry->registerOpenFunction(
'weather',
'Handles weather and forecasting services.',
$weatherFunction
);
// Access all function definitions (namespaced)
$allDefinitions = $registry->getFunctionDefinitions();
print_r($allDefinitions);
// Execute a sample function call (simulate an LLM request to "delivery_orderProduct")
$response = $registry->executeFunctionCall('delivery_orderProduct', [
'productName' => 'Pizza',
'quantity' => 2
]);
// The response is a standard Response object
if ($response->isError) {
echo "Error: " . print_r($response->toArray(), true);
} else {
echo "Success: " . print_r($response->toArray(), true);
}
use OpenAI;
// 1. Prepare your messages
$messageList = new MessageList();
// Add a user message
$messageList->addMessage(new UserMessage("I want to order 2 Burgers"));
// 2. Convert messages to array
$messagesArray = $messageList->toArray();
// 3. Prepare the function definitions from your registry
$functionDefinitions = $registry->getFunctionDefinitions();
// 4. Call the Chat API
$client = OpenAI::client('YOUR_OPENAI_API_KEY');
$response = $client->chat()->create([
'model' => 'gpt-4o',
'messages' => $messagesArray,
'tools' => $functionDefinitions, // Provide all your known function definitions
]);
// 5. Parse the response
// The model might either return text or a function call (tool call).
if (isset($response->choices[0]->message->toolCalls)) {
foreach ($response->choices[0]->message->toolCalls as $toolCall) {
// Extract the function name (already namespaced) and arguments
$functionName = $toolCall->function->name;
$functionArgs = json_decode($toolCall->function->arguments, true);
// 6. Execute the function via the registry
$toolResponse = $registry->executeFunctionCall($functionName, $functionArgs);
// 7. Return the tool's response back to the conversation (or handle it as you wish)
print_r($toolResponse->toArray());
}
} else {
// Regular text response from the model
echo $response->choices[0]->message->content;
}
$messageList = new MessageList();
$messageList->addExtension($registry); // <-- This will prepend a DeveloperMessage automatically
$messageList->addMessage(new UserMessage("What's the weather in Berlin for the next 3 days?"));
// Now when you do $messageList->toArray(),
// it has a developer message listing the registered namespaces
// plus your user message.
$messagesArray = $messageList->toArray();
$functionDefinitions = $registry->getFunctionDefinitions();
// Use $messagesArray and $functionDefinitions in an OpenAI chat call as before...