FunctionCall helper class
Instructor offers FunctionCall class to extract arguments of a function
or method from content.
This is useful when you want to build tool use capability, e.g. for AI
chatbots or agents.
<?php
use Cognesy\Instructor\Extras\FunctionCall\FunctionCall;
use Cognesy\Instructor\Instructor;
function saveUser(string $name, int $age, string $country) {
}
$text = "His name is Jason, he is 28 years old and he lives in Germany.";
$args = (new Instructor)->respond(
messages: $text,
responseModel: FunctionCall::fromFunctionName('saveUser'),
);
saveUser(...$args);
?>
<?php
use Cognesy\Instructor\Extras\FunctionCall\FunctionCall;
use Cognesy\Instructor\Instructor;
class DataStore {
public function saveUser(string $name, int $age, string $country) {
}
}
$text = "His name is Jason, he is 28 years old and he lives in Germany.";
$args = (new Instructor)->respond(
messages: $text,
responseModel: FunctionCall::fromMethodName(Datastore::class, 'saveUser'),
);
(new DataStore)->saveUser(...$args);
?>
<?php
use Cognesy\Instructor\Extras\FunctionCall\FunctionCall;
use Cognesy\Instructor\Instructor;
$callable = function saveUser(string $name, int $age, string $country) {
}
$text = "His name is Jason, he is 28 years old and he lives in Germany.";
$args = (new Instructor)->respond(
messages: $text,
responseModel: FunctionCall::fromCallable($callable),
);
$callable(...$args);
?>