Laravel 的 Gemini API 客户端

v0.3.0 2023-12-27 03:38 UTC

This package is auto-updated.

Last update: 2024-08-31 11:00:45 UTC


README

Gemini API Client for Laravel - Examples

Total Downloads Latest Version License

Laravel 的 Gemini API 客户端

Laravel 的 Gemini API 客户端允许您在 Laravel 应用程序中使用 Google 的生成式 AI 模型,如 Gemini Pro 和 Gemini Pro Vision。

支持 PHP 8.1 和 Laravel v9、v10。

本库不是由 Google 开发或支持的。

目录

安装

您需要一个 API 密钥才能访问 Google 的 Gemini API。访问 Google AI Studio 获取 API 密钥。

第一步是使用 Composer 安装 Gemini API 客户端 for Laravel。

composer require gemini-api-php/laravel

配置

配置客户端有两种方式。

环境变量

您可以使用从 Google AI Studio 获取的 API 密钥设置 GEMINI_API_KEY 环境变量。

将以下行添加到您的 .env 文件中。

GEMINI_API_KEY='YOUR_GEMINI_API_KEY'

配置文件

您还可以运行以下命令在应用程序的配置文件夹中创建配置文件。

php artisan vendor:publish --provider=GeminiAPI\Laravel\ServiceProvider

现在您可以编辑 config/gemini.php 文件以配置 Gemini API 客户端。

如何使用

文本生成

use GeminiAPI\Laravel\Facades\Gemini;

print Gemini::generateText('PHP in less than 100 chars');
// PHP: A server-side scripting language used to create dynamic web applications.
// Easy to learn, widely used, and open-source.

使用图像文件进行文本生成

use GeminiAPI\Laravel\Facades\Gemini;

print Gemini::generateTextUsingImageFile(
    'image/jpeg',
    'elephpant.jpg',
    'Explain what is in the image',
);
// The image shows an elephant standing on the Earth.
// The elephant is made of metal and has a glowing symbol on its forehead.
// The Earth is surrounded by a network of glowing lines.
// The image is set against a starry background.

使用图像数据进行文本生成

use GeminiAPI\Laravel\Facades\Gemini;

print Gemini::generateTextUsingImage(
    'image/jpeg',
    base64_encode(file_get_contents('elephpant.jpg')),
    'Explain what is in the image',
);
// The image shows an elephant standing on the Earth.
// The elephant is made of metal and has a glowing symbol on its forehead.
// The Earth is surrounded by a network of glowing lines.
// The image is set against a starry background.

聊天会话(多轮对话)

use GeminiAPI\Laravel\Facades\Gemini;

$chat = Gemini::startChat();

print $chat->sendMessage('Hello World in PHP');
// echo "Hello World!";
// This code will print "Hello World!" to the standard output.

print $chat->sendMessage('in Go');
// fmt.Println("Hello World!")
// This code will print "Hello World!" to the standard output.

带有历史记录的聊天会话

use GeminiAPI\Laravel\Facades\Gemini;

$history = [
    [
        'message' => 'Hello World in PHP',
        'role' => 'user',
    ],
    [
        'message' => <<<MESSAGE
            echo "Hello World!";

            This code will print "Hello World!" to the standard output.
            MESSAGE,
        'role' => 'model',
    ],
];
$chat = Gemini::startChat($history);

print $chat->sendMessage('in Go');
// fmt.Println("Hello World!")
// This code will print "Hello World!" to the standard output.

文本嵌入

use GeminiAPI\Laravel\Facades\Gemini;

print_r(Gemini::embedText('PHP in less than 100 chars'));
// [
//    [0] => 0.041395925
//    [1] => -0.017692696
//    ...
// ]

令牌计数

use GeminiAPI\Laravel\Facades\Gemini;

print Gemini::countTokens('PHP in less than 100 chars');
// 10

列出模型

use GeminiAPI\Laravel\Facades\Gemini;

print_r(Gemini::listModels());
//[
//  [0] => GeminiAPI\Resources\Model Object
//    (
//      [name] => models/gemini-pro
//      [displayName] => Gemini Pro
//      [description] => The best model for scaling across a wide range of tasks
//      ...
//    )
//  [1] => GeminiAPI\Resources\Model Object
//    (
//      [name] => models/gemini-pro-vision
//      [displayName] => Gemini Pro Vision
//      [description] => The best image understanding model to handle a broad range of applications
//      ...
//    )
//]

访问底层 Gemini API 客户端

use GeminiAPI\Laravel\Facades\Gemini;

$client = Gemini::client();

致谢

本项目受到 OpenAI PHP for LaravelOpenAI PHP client 优秀工作的启发。

我们衷心感谢 OpenAI PHP 及其团队的贡献。