PHP code example of manuelkiessling / ai-tool-bridge
1. Go to this page and download the library: Download manuelkiessling/ai-tool-bridge 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/ */
manuelkiessling / ai-tool-bridge example snippets
declare(strict_types=1);
namespace ManuelKiessling\AiToolBridge\Example;
use ManuelKiessling\AiToolBridge\ToolFunctionCallResult;
use ManuelKiessling\AiToolBridge\ToolFunctionCallResultStatus;
use ManuelKiessling\AiToolBridge\ToolFunction;
class MyProductSearchToolFunction implements ToolFunction
{
public function getName(): string
{
return 'productSearch';
}
public function getDescription(): string
{
return 'allows to search the product catalogue and retrieve information about products';
}
public function getInputJsonSchema(): string
{
return <<<'JSON'
{
"type": "object",
"properties": {
"searchterms": {
"type": "string"
}
},
"
class Example
{
private AiToolBridge $aiToolBridge;
private MyAiService $myAiService;
public function __construct()
{
$this->myAiService = new MyAiService();
$myProductSearchToolFunction = new MyProductSearchToolFunction();
$this->aiToolBridge = new AiToolBridge(
new $this->myAiService,
[$myProductSearchToolFunction],
);
$this->myAiService->setSystemPrompt(
"You are a friendly and helpful shopping assistant that informs the user about our product catalog...
{$this->aiToolBridge->getPrompt()}"
);
}
}
public function invoke(string $json): ToolFunctionCallResult
{
return new ToolFunctionCallResult(
$this,
ToolFunctionCallResultStatus::SUCCESS,
'',
[]
);
}
public function invoke(string $json): ToolFunctionCallResult
{
$jsonAsArray = json_decode($json, true);
// use $jsonAsArray['searchterm'] when talking to a DB or an API...
return new ToolFunctionCallResult(
$this,
ToolFunctionCallResultStatus::SUCCESS,
'Found 2 matching products',
[
[
'id' => 84,
'name' => 'Kawaii Chick Egg Separator',
'price' => 14.99,
'description' => 'Whether you’re a beginner baker or an experienced cook, the Kawaii Chick Egg Separator is a must-have kitchen tool that will make separating eggs a breeze.'
],
[
'id' => 2389,
'name' => 'BlendJet 2',
'price' => 49.99,
'description' => 'Imagine the freedom of being able to go anywhere and blend your favorite smoothies, shakes, margaritas, frappés, or baby food without the limitations of a regular blender.'
]
]
);
}
declare(strict_types=1);
class Example
{
// ...
public function handleAssistantMessage(string $message): void
{
$toolFunctionCallResult = $this->aiToolBridge->handleAssistantMessage($message);
if (is_null($toolFunctionCallResult)) {
// The AI didn't use a tool function, thus its message is meant for the user
$this->sendAssistantMessageToUser($message);
} else {
// The AI used a tool function, we now need to send the result to the AI
$dataAsJson = json_encode($toolFunctionCallResult->data);
$this->sendUserMessageToAssistant($toolFunctionCallResult->message . ' ' . $dataAsJson);
}
}
public function sendAssistantMessageToUser(string $message): void
{
// whatever code is needed to show an AI assistant message to the user
}
public function sendUserMessageToAssistant(string $message): void
{
// whatever code is needed to send a message to the AI assistant
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.