Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
Closed
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
47 changes: 47 additions & 0 deletions examples/google/grounding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\GroundingOutputProcessor;
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 (!$_ENV['GEMINI_API_KEY']) {
echo 'Please set the GEMINI_API_KEY environment variable.'.\PHP_EOL;
exit(1);
}

$platform = PlatformFactory::create($_ENV['GEMINI_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' => ['google_search' => true], 'temperature' => 1.0]);

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

$messages = new MessageBag(Message::ofUser('Summarize what is Symfony framework in 3 sentences'));

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

echo $response->getContent().\PHP_EOL;

$metadata = $response->getMetadata()['grounding'];

printf(
<<<'FORMAT'
Search queries: %s
Sources: %s

FORMAT,
implode(', ', $metadata['webSearchQueries']),
implode(', ', array_unique(array_column(array_column($metadata['groundingChunks'], 'web'), 'title')))
);
41 changes: 41 additions & 0 deletions src/Platform/Bridge/Google/GroundingOutputProcessor.php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's quite straight forward to test, please add one

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Platform\Bridge\Google;

use PhpLlm\LlmChain\Chain\Output;
use PhpLlm\LlmChain\Chain\OutputProcessorInterface;
use PhpLlm\LlmChain\Platform\Response\StreamResponse;
use Symfony\Contracts\HttpClient\ResponseInterface;

/**
* Adds grounding metadata from Google Search / URL context.
*
* @author Valtteri R <[email protected]>
*/
final class GroundingOutputProcessor implements OutputProcessorInterface
{
public function processOutput(Output $output): void
{
if ($output->response instanceof StreamResponse) {
// Streams have to be handled manually as the tokens are part of the streamed chunks
return;
}

$rawResponse = $output->response->getRawResponse()?->getRawObject();
if (!$rawResponse instanceof ResponseInterface) {
return;
}

$metadata = $output->response->getMetadata();

$content = $rawResponse->toArray(false);

if (!$grounding = $content['candidates'][0]['groundingMetadata'] ?? null) {
return;
}

$metadata->add('grounding', $grounding);
}
}