google-gemini-php / symfony
Gemini 的 Symfony 扩展包
Requires
- php: ^8.1.0
- google-gemini-php/client: ^1.0.0
- nyholm/psr7: ^1.8.1
- psr/http-client: ^1.0.3
- psr/http-factory: ^1.0.2
- symfony/config: ^5.4|^6.3|^7.0
- symfony/dependency-injection: ^5.4|^6.3|^7.0.1
- symfony/http-client: ^5.4|^6.3|^7.0
- symfony/http-kernel: ^5.4|^6.3|^7.0.1
Requires (Dev)
- laravel/pint: ^1.13.6
- phpstan/phpstan: ^1.10.47
- symfony/phpunit-bridge: ^5.4|^6.3|^7.0.1
This package is not auto-updated.
Last update: 2024-09-13 15:45:48 UTC
README
为 Symfony 的 Gemini PHP 是一个社区维护的 PHP API 客户端,允许您与 Gemini AI API 进行交互。
- Fatih AYDIN github.com/aydinfatih
有关更多信息,请参阅google-gemini-php/client 存储库。
目录
先决条件
为了完成此快速入门,请确保您的开发环境满足以下要求
- 需要 PHP 8.1+
- 需要 Symfony 5,6,7
设置
安装
首先,通过Composer 包管理器安装 Gemini
composer require google-gemini-php/symfony
接下来,在您的 config/bundles.php 中注册该扩展包
return [ // ... Gemini\Symfony\GeminiBundle::class => ['all' => true], ]
这将为您项目创建一个 .env 配置文件,您可以使用环境变量对其进行修改以满足需求
GEMINI_API_KEY=
您还可以定义以下环境变量。
GEMINI_BASE_URL=
设置您的 API 密钥
要使用 Gemini API,您需要一个 API 密钥。如果您还没有,请先在 Google AI Studio 中创建一个密钥。
用法
与 Gemini 的 API 交互
$result = $container->get('gemini')->geminiPro()->generateContent('Hello'); $result->text(); // Hello! How can I assist you today?
聊天资源
纯文本输入
给定一个输入消息,从模型生成响应。如果输入仅包含文本,请使用 gemini-pro
模型。
$result = $container->get('gemini')->geminiPro()->generateContent('Hello'); $result->text(); // Hello! How can I assist you today?
文本和图像输入
如果输入包含文本和图像,请使用 gemini-pro-vision
模型。
$result = $container->get('gemini')->geminiProVision() ->generateContent([ 'What is this picture?', new Blob( mimeType: MimeType::IMAGE_JPEG, data: base64_encode( file_get_contents('https://storage.googleapis.com/generativeai-downloads/images/scones.jpg') ) ) ]); $result->text(); // The picture shows a table with a white tablecloth. On the table are two cups of coffee, a bowl of blueberries, a silver spoon, and some flowers. There are also some blueberry scones on the table.
多轮对话(聊天)
使用 Gemini,您可以构建跨多个轮次的自由对话。
$chat = $container->get('gemini')->chat() ->startChat(history: [ Content::parse(part: 'The stories you write about what I have to say should be one line. Is that clear?'), Content::parse(part: 'Yes, I understand. The stories I write about your input should be one line long.', role: Role::MODEL) ]); $response = $chat->sendMessage('Create a story set in a quiet village in 1600s France'); echo $response->text(); // Amidst rolling hills and winding cobblestone streets, the tranquil village of Beausoleil whispered tales of love, intrigue, and the magic of everyday life in 17th century France. $response = $chat->sendMessage('Rewrite the same story in 1600s England'); echo $response->text(); // In the heart of England's lush countryside, amidst emerald fields and thatched-roof cottages, the village of Willowbrook unfolded a tapestry of love, mystery, and the enchantment of ordinary days in the 17th century.
对于文本和图像输入的
gemini-pro-vision
模型(多轮对话尚未优化。确保在聊天用例中使用 gemini-pro 和纯文本输入。
流式生成内容
默认情况下,模型在完成整个生成过程后返回响应。您可以通过不等待整个结果,而是使用流式处理来处理部分结果来实现更快的交互。
$stream = $container->get('gemini')->geminiPro() ->streamGenerateContent('Write long a story about a magic backpack.'); foreach ($stream as $response) { echo $response->text(); }
计数标记
当使用长提示时,在向模型发送任何内容之前计数标记可能很有用。
$response = $container->get('gemini')->geminiPro() ->countTokens('Write a story about a magic backpack.'); echo $response->totalTokens; // 9
配置
您发送到模型的每个提示都包括控制模型如何生成响应的参数值。模型可以根据不同的参数值生成不同的结果。有关更多信息,请参阅 模型参数。
此外,您可以使用安全设置来调整可能被认为有害的响应的可能性。默认情况下,安全设置阻止所有维度中具有中等和/或高度概率的不安全内容的响应。有关更多信息,请参阅 安全设置。
use Gemini\Data\GenerationConfig; use Gemini\Enums\HarmBlockThreshold; use Gemini\Data\SafetySetting; use Gemini\Enums\HarmCategory; $safetySettingDangerousContent = new SafetySetting( category: HarmCategory::HARM_CATEGORY_DANGEROUS_CONTENT, threshold: HarmBlockThreshold::BLOCK_ONLY_HIGH ); $safetySettingHateSpeech = new SafetySetting( category: HarmCategory::HARM_CATEGORY_HATE_SPEECH, threshold: HarmBlockThreshold::BLOCK_ONLY_HIGH ); $generationConfig = new GenerationConfig( stopSequences: [ 'Title', ], maxOutputTokens: 800, temperature: 1, topP: 0.8, topK: 10 ); $generativeModel = $container->get('gemini')->geminiPro() ->withSafetySetting($safetySettingDangerousContent) ->withSafetySetting($safetySettingHateSpeech) ->withGenerationConfig($generationConfig) ->generateContent("Write a story about a magic backpack.");
嵌入资源
嵌入是一种将信息表示为数组中浮点数列表的技术。使用Gemini,您可以以向量化的形式表示文本(单词、句子和文本块),使其更容易比较和对比嵌入。例如,两个主题或情感相似的文字应该具有相似的嵌入,这可以通过余弦相似度等数学比较技术来识别。
使用embedding-001
模型与embedContents
或batchEmbedContents
。
$response = $container->get('gemini')->embeddingModel() ->embedContent("Write a story about a magic backpack."); print_r($response->embedding->values); //[ // [0] => 0.008624583 // [1] => -0.030451821 // [2] => -0.042496547 // [3] => -0.029230341 // [4] => 0.05486475 // [5] => 0.006694871 // [6] => 0.004025645 // [7] => -0.007294857 // [8] => 0.0057651913 // ... //]
模型
列出模型
使用列表模型查看可用的Gemini模型。
$response = $container->get('gemini')->models()->list(); $response->models; //[ // [0] => Gemini\Data\Model Object // ( // [name] => models/gemini-pro // [version] => 001 // [displayName] => Gemini Pro // [description] => The best model for scaling across a wide range of tasks // ... // ) // [1] => Gemini\Data\Model Object // ( // [name] => models/gemini-pro-vision // [version] => 001 // [displayName] => Gemini Pro Vision // [description] => The best image understanding model to handle a broad range of applications // ... // ) // [2] => Gemini\Data\Model Object // ( // [name] => models/embedding-001 // [version] => 001 // [displayName] => Embedding 001 // [description] => Obtain a distributed representation of a text. // ... // ) //]
获取模型
获取有关模型的信息,例如版本、显示名称、输入令牌限制等。
$response = $container->get('gemini')->models()->retrieve(ModelType::GEMINI_PRO); $response->model; //Gemini\Data\Model Object //( // [name] => models/gemini-pro // [version] => 001 // [displayName] => Gemini Pro // [description] => The best model for scaling across a wide range of tasks // ... //)
测试
该包提供了一个假的Gemini\Client
类实现,允许您模拟API响应。
为了测试您的代码,确保在测试用例中将Gemini\Client
类与Gemini\Testing\ClientFake
类进行交换。
模拟的响应以创建模拟客户端时提供的顺序返回。
所有响应都有一个fake()
方法,允许您通过仅提供与您的测试用例相关的参数来轻松创建响应对象。
use Gemini\Testing\ClientFake; use Gemini\Responses\GenerativeModel\GenerateContentResponse; $container->get('gemini')->fake([ GenerateContentResponse::fake([ 'candidates' => [ [ 'content' => [ 'parts' => [ [ 'text' => 'success', ], ], ], ], ], ]), ]); $result = $container->get('gemini')->geminiPro()->generateContent('test'); expect($result->text())->toBe('success');
在流式响应的情况下,您可以可选地提供一个包含模拟响应数据的资源。
use Gemini\Testing\ClientFake; use Gemini\Responses\GenerativeModel\GenerateContentResponse; $container->get('gemini')->fake([ GenerateContentResponse::fakeStream(), ]); $result = $container->get('gemini')->geminiPro()->streamGenerateContent('Hello'); expect($response->getIterator()->current()) ->text()->toBe('In the bustling city of Aethelwood, where the cobblestone streets whispered');
在请求发送后,有各种方法可以确保发送了预期的请求。
// assert list models request was sent $container->get('gemini')->models()->assertSent(callback: function ($method) { return $method === 'list'; }); // or $container->get('gemini')->assertSent(resource: Models::class, callback: function ($method) { return $method === 'list'; }); $container->get('gemini')->geminiPro()->assertSent(function (string $method, array $parameters) { return $method === 'generateContent' && $parameters[0] === 'Hello'; }); // or $container->get('gemini')->assertSent(resource: GenerativeModel::class, model: ModelType::GEMINI_PRO, callback: function (string $method, array $parameters) { return $method === 'generateContent' && $parameters[0] === 'Hello'; }); // assert 2 generative model requests were sent $container->get('gemini')->assertSent(resource: GenerativeModel::class, model: ModelType::GEMINI_PRO, callback: 2); // or $container->get('gemini')->geminiPro()->assertSent(2); // assert no generative model requests were sent $container->get('gemini')->assertNotSent(resource: GenerativeModel::class, model: ModelType::GEMINI_PRO); // or $container->get('gemini')->geminiPro()->assertNotSent(); // assert no requests were sent $container->get('gemini')->assertNothingSent();
要编写期望API请求失败的测试,您可以提供Throwable
对象作为响应。
$container->get('gemini')->fake([ new ErrorException([ 'message' => 'The model `gemini-basic` does not exist', 'status' => 'INVALID_ARGUMENT', 'code' => 400, ]), ]); // the `ErrorException` will be thrown $container->get('gemini')->geminiPro()->generateContent('test');