allanbernier/laravel-gpt 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

allanbernier/laravel-gpt

Composer 安装命令:

composer require allanbernier/laravel-gpt

包简介

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

README 文档

README

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 john@example.com?')
    ->send();

$user = $response->tool->execute();
echo "Tool: {$response->tool->name}\n"; # find_user
echo "Arguments: " . json_encode($response->tool->args) . "\n"; # {"email": "john@example.com"}

License

MIT License - see LICENSE file for details.

Contributing

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

allanbernier/laravel-gpt 适用场景与选型建议

allanbernier/laravel-gpt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「tools」 「chat」 「laravel」 「assistant」 「ai」 「openai」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 allanbernier/laravel-gpt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 allanbernier/laravel-gpt 我们能提供哪些服务?
定制开发 / 二次开发

基于 allanbernier/laravel-gpt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 33
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-18