Skip to content

Commit 4b1e6d1

Browse files
authored
docs(gemini): add caching docs (#435)
1 parent 3485f6d commit 4b1e6d1

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/providers/gemini.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,48 @@ foreach ($response->additionalContent['groundingSupports'] as $part) {
7777
// Pass $text and $footnotes to your frontend.
7878
```
7979

80+
## Caching
81+
82+
Prism supports Gemini prompt caching, though due to Gemini requiring you first upload the cached content, it works a little differently to other providers.
83+
84+
To store content in the cache, use the Gemini provider cache method as follows:
85+
86+
```php
87+
88+
use Prism\Prism\Enums\Provider;
89+
use Prism\Prism\Prism;
90+
use Prism\Prism\Providers\Gemini\Gemini;
91+
use Prism\Prism\ValueObjects\Messages\Support\Document;
92+
use Prism\Prism\ValueObjects\Messages\SystemMessage;
93+
use Prism\Prism\ValueObjects\Messages\UserMessage;
94+
95+
/** @var Gemini */
96+
$provider = Prism::provider(Provider::Gemini);
97+
98+
$object = $provider->cache(
99+
model: 'gemini-1.5-flash-002',
100+
messages: [
101+
new UserMessage('', [
102+
Document::fromLocalPath('tests/Fixtures/long-document.pdf'),
103+
]),
104+
],
105+
systemPrompts: [
106+
new SystemMessage('You are a legal analyst.'),
107+
],
108+
ttl: 60
109+
);
110+
```
111+
112+
Then reference that object's name in your request using withProviderOptions:
113+
114+
```php
115+
$response = Prism::text()
116+
->using(Provider::Gemini, 'gemini-1.5-flash-002')
117+
->withProviderOptions(['cachedContentName' => $object->name])
118+
->withPrompt('In no more than 100 words, what is the document about?')
119+
->asText();
120+
```
121+
80122
## Embeddings
81123

82124
You can customize your Gemini embeddings request with additional parameters using `->withProviderOptions()`.

0 commit comments

Comments
 (0)