LaravelPackages.net
Acme Inc.
Toggle sidebar
allanbernier/laravel-gpt

A Laravel package for interacting with the ChatGPT API with built-in support for function calling (tools)

33
1
v1.0.1
About allanbernier/laravel-gpt

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

Laravel GPT

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).

Features

  • πŸš€ Simple and fluent API for ChatGPT requests
  • πŸ› οΈ Built-in support for JSON Schema tools (function calling)
  • πŸ“¦ Easy tool creation with artisan command
  • πŸ”§ Type-safe tool implementation with IChatTool interface
  • ⚑ Chainable methods for elegant code
  • 🎯 Automatic tool execution based on ChatGPT's choices
  • πŸ”„ Automatic retry logic for failed requests
  • πŸ“Š Usage tracking and token monitoring

Requirements

  • PHP >= 8.1
  • Laravel >= 9.0 (supports Laravel 9, 10, 11, and 12)

Installation

Install 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

Quick Start

1. Installation

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

2. First Use

use AllanBernier\LaravelGpt\ChatGPT;

$response = ChatGPT::new()
    ->prompt('Hello, how can I help you?')
    ->send();

echo $response->content;

3. Create a Tool

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();
    }
}

3. Use the Tool

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]"}

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Comments