A Laravel package for interacting with the ChatGPT API with built-in support for function calling (tools)
allanbernier/laravel-gpt is a Laravel package for a laravel package for interacting with the chatgpt api with built-in support for function calling (tools).
It currently has 1 GitHub stars and 33 downloads on Packagist (latest version v1.0.1).
Install it with composer require allanbernier/laravel-gpt.
Discover more Laravel packages by allanbernier
or browse all Laravel packages to compare alternatives.
Last updated
A powerful Laravel package for interacting with the ChatGPT API. This package provides a fluent, intuitive interface for making requests to OpenAI's ChatGPT API, with built-in support for function calling (tools).
IChatTool interfaceInstall the package via Composer:
composer require allanbernier/laravel-gpt
Publish the configuration file:
php artisan vendor:publish --tag=laravel-gpt-config
Add your OpenAI API key to .env:
OPENAI_API_KEY=your-api-key-here
composer require allanbernier/laravel-gpt
php artisan vendor:publish --tag=laravel-gpt-config
Add your OpenAI API key to .env:
OPENAI_API_KEY=your-api-key-here
use AllanBernier\LaravelGpt\ChatGPT;
$response = ChatGPT::new()
->prompt('Hello, how can I help you?')
->send();
echo $response->content;
Create a tool using the artisan command:
php artisan make:chatTool FindUser
This creates app/ChatTools/FindUser.php. Implement it:
namespace App\ChatTools;
use AllanBernier\LaravelGpt\ChatTool;
use App\Models\User;
class FindUser extends ChatTool
{
/**
* The name of the function.
* If not set, defaults to 'find_user' (class name in snake_case).
*/
public ?string $name = null;
/**
* A description of what the function does.
*/
public ?string $description = 'Finds a user by their name';
/**
* Whether to enable strict schema adherence.
* Defaults to false if not set.
*/
public ?bool $strict = null;
public function parameters(): array
{
return [
'type' => 'object',
'properties' => [
'name' => ['type' => 'string', 'description' => 'The name of the user to find'],
],
'required' => ['name'],
];
}
public function invoke(array $args): mixed
{
return User::where('name', 'like', "%{$args['name']}%")->first();
}
}
use AllanBernier\LaravelGpt\ChatGPT;
use App\ChatTools\FindUser;
$response = ChatGPT::new()
->tool(FindUser::class)
->prompt('Could you find the user with email [email protected]?')
->send();
$user = $response->tool->execute();
echo "Tool: {$response->tool->name}\n"; # find_user
echo "Arguments: " . json_encode($response->tool->args) . "\n"; # {"email": "[email protected]"}
MIT License - see LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.