Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions examples/google/server-tools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use PhpLlm\LlmChain\Chain\Chain;
use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor;
use PhpLlm\LlmChain\Chain\Toolbox\Tool\Clock;
use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;
use PhpLlm\LlmChain\Platform\Bridge\Google\Gemini;
use PhpLlm\LlmChain\Platform\Bridge\Google\PlatformFactory;
use PhpLlm\LlmChain\Platform\Message\Message;
use PhpLlm\LlmChain\Platform\Message\MessageBag;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__, 2).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');

if (empty($_ENV['GOOGLE_API_KEY'])) {
echo 'Please set the GOOGLE_API_KEY environment variable.'.\PHP_EOL;
exit(1);
}

$platform = PlatformFactory::create($_ENV['GOOGLE_API_KEY']);

// Available server-side tools as of 2025-06-28: url_context, google_search, code_execution
$llm = new Gemini('gemini-2.5-pro-preview-03-25', ['server_tools' => ['url_context' => true], 'temperature' => 1.0]);

$toolbox = Toolbox::create(new Clock());
$processor = new ChainProcessor($toolbox);
$chain = new Chain($platform, $llm);

$messages = new MessageBag(
Message::ofUser(
<<<'PROMPT'
What was the 12 month Euribor rate a week ago based on https://www.euribor-rates.eu/en/current-euribor-rates/4/euribor-rate-12-months/
PROMPT,
),
);

$response = $chain->call($messages);

echo $response->getContent().\PHP_EOL;
9 changes: 9 additions & 0 deletions src/Platform/Bridge/Google/ModelHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,21 @@ public function request(Model $model, array|string $payload, array $options = []
$generationConfig = ['generationConfig' => $options];
unset($generationConfig['generationConfig']['stream']);
unset($generationConfig['generationConfig']['tools']);
unset($generationConfig['generationConfig']['server_tools']);

if (isset($options['tools'])) {
$generationConfig['tools'] = $options['tools'];
unset($options['tools']);
}

foreach ($options['server_tools'] ?? [] as $tool => $params) {
if (!$params) {
continue;
}

$generationConfig['tools'][] = [$tool => true === $params ? new \ArrayObject() : $params];
}

return $this->httpClient->request('POST', $url, [
'headers' => [
'x-goog-api-key' => $this->apiKey,
Expand Down
3 changes: 2 additions & 1 deletion src/Platform/Contract.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpLlm\LlmChain\Platform\Contract\Normalizer\Response\ToolCallNormalizer;
use PhpLlm\LlmChain\Platform\Contract\Normalizer\ToolNormalizer;
use PhpLlm\LlmChain\Platform\Tool\Tool;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer;

Expand Down Expand Up @@ -74,6 +75,6 @@ public function createRequestPayload(Model $model, object|array|string $input):
*/
public function createToolOption(array $tools, Model $model): array
{
return $this->normalizer->normalize($tools, context: [self::CONTEXT_MODEL => $model]);
return $this->normalizer->normalize($tools, context: [self::CONTEXT_MODEL => $model, AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true]);
}
}