Skip to content

Commit 285f4a1

Browse files
Erdem Köseerdemkose
authored andcommitted
Allow to change API version
1 parent 3065e0f commit 285f4a1

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[]>
@@ -163,7 +164,7 @@ public function generateContentStream(
163164
}
164165
}
165166

166-
curl_setopt($ch, CURLOPT_URL, "{$this->baseUrl}/v1/{$request->getOperation()}");
167+
curl_setopt($ch, CURLOPT_URL, $this->getRequestUrl($request));
167168
curl_setopt($ch, CURLOPT_POST, true);
168169
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
169170
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerLines);
@@ -214,6 +215,19 @@ public function withBaseUrl(string $baseUrl): self
214215
return $clone;
215216
}
216217

218+
public function withV1BetaVersion(): self
219+
{
220+
return $this->withVersion(GeminiClientInterface::API_VERSION_V1_BETA);
221+
}
222+
223+
public function withVersion(string $version): self
224+
{
225+
$clone = clone $this;
226+
$clone->version = $version;
227+
228+
return $clone;
229+
}
230+
217231
/**
218232
* @param array<string, string|string[]> $headers
219233
* @return self
@@ -241,6 +255,16 @@ private function getRequestHeaders(): array
241255
];
242256
}
243257

258+
private function getRequestUrl(RequestInterface $request): string
259+
{
260+
return sprintf(
261+
'%s/%s/%s',
262+
$this->baseUrl,
263+
$this->version,
264+
$request->getOperation(),
265+
);
266+
}
267+
244268
/**
245269
* @throws ClientExceptionInterface
246270
*/
@@ -250,9 +274,11 @@ private function doRequest(RequestInterface $request): string
250274
throw new RuntimeException('Missing client or factory for Gemini API operation');
251275
}
252276

253-
$uri = "{$this->baseUrl}/v1/{$request->getOperation()}";
254277
$httpRequest = $this->requestFactory
255-
->createRequest($request->getHttpMethod(), $uri);
278+
->createRequest(
279+
$request->getHttpMethod(),
280+
$this->getRequestUrl($request),
281+
);
256282

257283
foreach ($this->getRequestHeaders() as $name => $value) {
258284
$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)