定制 matrixbrains/laravel-ai 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

matrixbrains/laravel-ai

最新稳定版本:1.0.0

Composer 安装命令:

composer require matrixbrains/laravel-ai

包简介

Laravel AI wrapper package with multiple AI provider drivers (OpenAI, Gemini, Claude, Mistral)

README 文档

README

License PHP Laravel

A simple, elegant Laravel package to integrate multiple AI providers with a unified interface. Supports OpenAI, Google Gemini, Anthropic Claude, and Mistral AI.

Features

  • Unified Ai::ask() method for all providers
  • Supports text generation & chat prompts
  • Easily switch between AI providers via .env
  • Extensible: add your own AI driver if needed
  • Free tier support: Google Gemini & Mistral
  • Fully ready for Laravel applications

Installation

Require the package via Composer:

composer require matrixbrains/laravel-ai

Publish the config file:

php artisan vendor:publish --provider="Matrixbrains\LaravelAi\AiServiceProvider" --tag="config"

Configuration

Edit your .env:

AI_DRIVER=gemini   # Options: openai, gemini, claude, mistral

# OpenAI
OPENAI_API_KEY=your_openai_key
OPENAI_MODEL=gpt-4o-mini

# Google Gemini
GEMINI_API_KEY=your_gemini_key
GEMINI_MODEL=gemini-2.0-flash

# Anthropic Claude
CLAUDE_API_KEY=your_claude_key
CLAUDE_MODEL=claude-3-5-sonnet-20240620

# Mistral
MISTRAL_API_KEY=your_mistral_key
MISTRAL_MODEL=mistral-small

Usage

Use the facade to send a prompt:

use Matrixbrains\LaravelAi\Facades\Ai;

$response = Ai::ask("Write a short poem about Laravel.");
echo $response;

The same method works for all drivers. Just switch AI_DRIVER in .env to change the provider.

Example with multiple drivers

// OpenAI
config(['ai.default' => 'openai']);
$response = Ai::ask("Explain AI in simple terms.");

// Google Gemini
config(['ai.default' => 'gemini']);
$response = Ai::ask("Generate a motivational quote.");

// Claude
config(['ai.default' => 'claude']);
$response = Ai::ask("Write a haiku about coding.");

// Mistral
config(['ai.default' => 'mistral']);
$response = Ai::ask("Summarize the latest Laravel release notes.");

Extending with custom drivers

Add a new driver in src/Drivers and register it in AiManager.php. All drivers should implement the AiDriver interface:

class CustomAiDriver
{
    public function ask(string $prompt): string
    {
        // Your AI API integration
    }
}

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/my-feature)
  3. Commit your changes (git commit -m 'Add new feature')
  4. Push to the branch (git push origin feature/my-feature)
  5. Open a Pull Request

License

This package is open-source and licensed under the MIT License.

Supported AI Providers

Provider Free Tier Notes
OpenAI Limited Requires API key
Google Gemini Yes 1,500 requests/day free
Anthropic Claude Limited Free credits for new users
Mistral AI Yes Small models free tier

Matrixbrains Laravel AI makes integrating multiple AI tools seamless for your Laravel apps.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-16