Download the PHP package assistant-engine/open-functions-core without Composer
On this page you can find all versions of the php package assistant-engine/open-functions-core. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download assistant-engine/open-functions-core
More information about assistant-engine/open-functions-core
Files in assistant-engine/open-functions-core
Package open-functions-core
Short Description A core library that provides a framework for building and registering Open Functions for AI-powered applications. It simplifies integration with large language models by standardizing function definitions, message handling, and response formatting, reducing boilerplate for advanced assistant capabilities.
License MIT
Homepage https://github.com/AssistantEngine/open-functions-core
Informations about the package open-functions-core
Open Functions Core
Open Functions provide a standardized way to implement and invoke functions for tool calling with large language models (LLMs). They encapsulate both the generation of structured function definitions and the actual function execution within a unified interface. At the heart of this framework is the AbstractOpenFunction class, which standardizes:
- Function Definition Generation: Using built-in helpers like FunctionDefinition and Parameter, developers can define function schemas that are fully compliant with LLM tool-calling requirements. This ensures that every function exposes its parameters, types, and descriptions in a consistent format.
- Function Invocation: The AbstractOpenFunction class includes a standardized method (callMethod) to invoke the actual implementation of the function. This method guarantees that results are wrapped into a Response object, handling single responses, arrays of response items, and even exceptions in a unified manner.
- Message Handling: Additional helper classes are provided for working with messages (UserMessage, AssistantMessage, DeveloperMessage, and ToolMessagecomposr) and managing message lists. This makes it easier to integrate function calls into conversation flows with LLMs.
Together, these components simplify the process of integrating tool calling capabilities into your application, reducing boilerplate and ensuring that both the function definitions and their invocations follow a clear, predictable standard.
Table of Contents
- Features
- Installation
- Basic Concepts
- AbstractOpenFunction
- FunctionDefinition & Parameter
- OpenFunctionRegistry
- Messages & MessageList
- Usage Examples
- Registering and Calling Functions
- Using OpenAI PHP SDK to Call a Function
- Utilizing the Function Registry with OpenAI
- Working with Responses
- Examples
- DeliveryOpenFunction
- WeatherOpenFunction
- Contributing
- License
Features
- AbstractOpenFunction: Base class for creating custom Open Functions.
- FunctionDefinition and Parameter: Helpers to generate valid function definitions for LLM function calling.
- OpenFunctionRegistry: Namespace and manage multiple Open Functions.
- Message classes: Build conversation history in an object-oriented way (UserMessage, AssistantMessage, DeveloperMessage, etc.).
- MessageList and Extensions: Manage message lists and hook in additional messages (e.g., developer instructions about available namespaces).
- Response Objects: Standardized function call return data (success/failure, multiple items, binary/text, etc.).
Installation
Install the package via Composer:
Basic Concepts
AbstractOpenFunction
The AbstractOpenFunction
class is the foundation for all Open Functions. Extend this class to implement your functions. It:
- Provides a
callMethod(...)
handler that wraps output into a Response. - Declares an abstract
generateFunctionDefinitions()
method for function definitions. - Ensures consistent error handling in case of invalid function calls or exceptions.
Example: HelloWorld Open Function Using FunctionDefinition and Parameter
In this example:
- HelloWorldOpenFunction extends the abstract open function class.
- The generateFunctionDefinitions method uses the FunctionDefinition helper to create the function schema. Although no parameters are required here, the code shows how to add one using the Parameter helper if needed.
- The helloWorld method implements the function to return a “Hello, world!” greeting.
- Finally, the tool definitions are extracted and passed as the tools parameter when calling the OpenAI API.
FunctionDefinition & Parameter
These classes help build a JSON schema definition for your function:
OpenFunctionRegistry
The OpenFunctionRegistry is a centralized place to:
- Register one or more AbstractOpenFunction instances under a specific namespace (e.g., delivery, weather).
- Retrieve all aggregated function definitions.
- Execute a namespaced function call by name (e.g., delivery_orderProduct).
The registry also implements MessageListExtensionInterface, meaning it can automatically prepend a developer message about the registered namespaces if you add it as an extension in your MessageList.
Messages & MessageList
All conversation elements (user input, system instructions, developer instructions, tool/assistant responses) are represented by:
- UserMessage: The user’s input.
- AssistantMessage: The model’s response.
- SystemMessage: Traditional system-level instructions for older OpenAI models.
- DeveloperMessage: Developer instructions for the newer o1 models (replacement for SystemMessage).
- ToolMessage: Responses from a tool to the LLM.
These are stored and managed in a MessageList, which can be converted to an array suitable for the OpenAI Chat API.
Usage Examples
Registering and Calling Functions
Below is a minimal example using the DeliveryOpenFunction and WeatherOpenFunction from the repository’s Examples folder. We will:
- Instantiate our Open Functions.
- Register them with a namespace in the OpenFunctionRegistry.
- Fetch their combined function definitions.
- Execute a test call locally using the executeFunctionCall method.
Using OpenAI PHP SDK to Call a Function
Suppose you have a conversation with a user, and you suspect that the user’s prompt will trigger a function call. With the OpenAI PHP SDK (or a similar library), you can provide function definitions and messages.
Below is an example (pseudo-code) of how you might call the OpenAI Chat Completions endpoint and let it auto-call a function:
Utilizing the Function Registry with OpenAI
In the above example, we manually provided the function definitions. Another approach is to let the OpenFunctionRegistry automatically insert a developer message about the namespaces. You do this by adding the registry as a MessageList extension:
When the toArray() method is called, the registry’s extension will run and prepend a developer message enumerating the available tool namespaces. That helps the model know which functions exist and are available.
Working with Responses
All function calls return a Response object, which contains:
- isError (boolean) indicating if the function call succeeded or failed.
- content (an array of ResponseItem objects).
A ResponseItem can be:
- TextResponseItem: contains plain text.
- BinaryResponseItem: contains base64 or binary data.
Example
If you call:
You might get:
Example Open Functions
DeliveryOpenFunction
See DeliveryOpenFunction.php for a real-world example. It demonstrates how to:
- Implement multiple methods (listProducts, orderProduct, etc.).
- Return different text responses based on the function call.
- Provide function definitions for each method using FunctionDefinition and Parameter.
WeatherOpenFunction
See WeatherOpenFunction.php for an example that:
- Fetches (or simulates) current weather conditions.
- Simulates multi-day forecasts.
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.