Skip to content

Commit ae80991

Browse files
author
Erdem Köse
committed
Allow to change API version
1 parent 295c07e commit ae80991

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ _This library is not developed or endorsed by Google._
2727
- [Tokens counting](#tokens-counting)
2828
- [Listing models](#listing-models)
2929
- [Advanced Usages](#advanced-usages)
30+
- [Using Beta version](#using-beta-version)
3031
- [Safety Settings and Generation Configuration](#safety-settings-and-generation-configuration)
3132
- [Using your own HTTP client](#using-your-own-http-client)
3233
- [Using your own HTTP client for streaming responses](#using-your-own-http-client-for-streaming-responses)
@@ -329,6 +330,23 @@ print_r($response->models);
329330

330331
### Advanced Usages
331332

333+
#### Using Beta version
334+
335+
```php
336+
use GeminiAPI\Client;
337+
use GeminiAPI\Resources\ModelName;
338+
use GeminiAPI\Resources\Parts\TextPart;
339+
340+
$client = (new Client('GEMINI_API_KEY'))
341+
->withV1BetaVersion();
342+
$response = $client->generativeModel(ModelName::GEMINI_PRO)->countTokens(
343+
new TextPart('PHP in less than 100 chars'),
344+
);
345+
346+
print $response->totalTokens;
347+
// 10
348+
```
349+
332350
#### Safety Settings and Generation Configuration
333351

334352
```php

src/Client.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
class Client implements GeminiClientInterface
4141
{
4242
private string $baseUrl = 'https://generativelanguage.googleapis.com';
43+
private string $version = GeminiClientInterface::API_VERSION_V1;
4344

4445
/**
4546
* @var array<string, string|string[]>
@@ -133,7 +134,7 @@ public function generateContentStream(
133134
}
134135
}
135136

136-
curl_setopt($ch, CURLOPT_URL, "{$this->baseUrl}/v1/{$request->getOperation()}");
137+
curl_setopt($ch, CURLOPT_URL, $this->getRequestUrl($request));
137138
curl_setopt($ch, CURLOPT_POST, true);
138139
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
139140
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerLines);
@@ -184,6 +185,19 @@ public function withBaseUrl(string $baseUrl): self
184185
return $clone;
185186
}
186187

188+
public function withV1BetaVersion(): self
189+
{
190+
return $this->withVersion(GeminiClientInterface::API_VERSION_V1_BETA);
191+
}
192+
193+
public function withVersion(string $version): self
194+
{
195+
$clone = clone $this;
196+
$clone->version = $version;
197+
198+
return $clone;
199+
}
200+
187201
/**
188202
* @param array<string, string|string[]> $headers
189203
* @return self
@@ -211,6 +225,16 @@ private function getRequestHeaders(): array
211225
];
212226
}
213227

228+
private function getRequestUrl(RequestInterface $request): string
229+
{
230+
return sprintf(
231+
'%s/%s/%s',
232+
$this->baseUrl,
233+
$this->version,
234+
$request->getOperation(),
235+
);
236+
}
237+
214238
/**
215239
* @throws ClientExceptionInterface
216240
*/
@@ -220,9 +244,11 @@ private function doRequest(RequestInterface $request): string
220244
throw new RuntimeException('Missing client or factory for Gemini API operation');
221245
}
222246

223-
$uri = "{$this->baseUrl}/v1/{$request->getOperation()}";
224247
$httpRequest = $this->requestFactory
225-
->createRequest($request->getHttpMethod(), $uri);
248+
->createRequest(
249+
$request->getHttpMethod(),
250+
$this->getRequestUrl($request),
251+
);
226252

227253
foreach ($this->getRequestHeaders() as $name => $value) {
228254
$httpRequest = $httpRequest->withAddedHeader($name, $value);

src/ClientInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
interface ClientInterface
2222
{
2323
public const API_KEY_HEADER_NAME = 'x-goog-api-key';
24+
public const API_VERSION_V1 = 'v1';
25+
public const API_VERSION_V1_BETA = 'v1beta';
2426

2527
public function countTokens(CountTokensRequest $request): CountTokensResponse;
2628
public function generateContent(GenerateContentRequest $request): GenerateContentResponse;

0 commit comments

Comments
 (0)