This repository was archived by the owner on Jul 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
feat: expose Gemini grounding metadata #386
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'))) | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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