Skip to content

Commit e3bc520

Browse files
Erdem Köseerdemkose
authored andcommitted
Add system instruction support
Closes #51
1 parent c4fecae commit e3bc520

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ _This library is not developed or endorsed by Google._
1919
- [Installation](#installation)
2020
- [How to use](#how-to-use)
2121
- [Basic text generation](#basic-text-generation)
22+
- [Text generation with system instruction](#text-generation-with-system-instruction)
2223
- [Multimodal input](#multimodal-input)
2324
- [Chat Session (Multi-Turn Conversations)](#chat-session-multi-turn-conversations)
2425
- [Chat Session with history](#chat-session-with-history)
@@ -66,6 +67,27 @@ print $response->text();
6667
// Easy to learn, widely used, and open-source.
6768
```
6869

70+
### Text generation with system instruction
71+
72+
> System instruction is currently supported only in beta version
73+
74+
```php
75+
use GeminiAPI\Client;
76+
use GeminiAPI\Resources\ModelName;
77+
use GeminiAPI\Resources\Parts\TextPart;
78+
79+
$client = new Client('GEMINI_API_KEY');
80+
$response = $client->withV1BetaVersion()
81+
->generativeModel(ModelName::GEMINI_1_5_FLASH)
82+
->withSystemInstruction('You are a cat. Your name is Neko.')
83+
->generateContent(
84+
new TextPart('PHP in less than 100 chars'),
85+
);
86+
87+
print $response->text();
88+
// Meow? <?php echo 'Hello, world!'; ?> Purrfectly concise, wouldn't you say? *Stretches luxuriously*
89+
```
90+
6991
### Multimodal input
7092

7193
> Image input modality is only enabled for Gemini Pro Vision model

src/GenerativeModel.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class GenerativeModel
2626

2727
private ?GenerationConfig $generationConfig = null;
2828

29+
private ?Content $systemInstruction = null;
30+
2931
public function __construct(
3032
private readonly Client $client,
3133
public readonly ModelName|string $modelName,
@@ -55,6 +57,7 @@ public function generateContentWithContents(array $contents): GenerateContentRes
5557
$contents,
5658
$this->safetySettings,
5759
$this->generationConfig,
60+
$this->systemInstruction,
5861
);
5962

6063
return $this->client->generateContent($request);
@@ -96,6 +99,7 @@ public function generateContentStreamWithContents(
9699
$contents,
97100
$this->safetySettings,
98101
$this->generationConfig,
102+
$this->systemInstruction,
99103
);
100104

101105
$this->client->generateContentStream($request, $callback, $ch);
@@ -135,4 +139,12 @@ public function withGenerationConfig(GenerationConfig $generationConfig): self
135139

136140
return $clone;
137141
}
142+
143+
public function withSystemInstruction(string $systemInstruction): self
144+
{
145+
$clone = clone $this;
146+
$clone->systemInstruction = Content::text($systemInstruction, Role::User);
147+
148+
return $clone;
149+
}
138150
}

src/Requests/GenerateContentRequest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ class GenerateContentRequest implements JsonSerializable, RequestInterface
2424
* @param Content[] $contents
2525
* @param SafetySetting[] $safetySettings
2626
* @param GenerationConfig|null $generationConfig
27+
* @param ?Content $systemInstruction
2728
*/
2829
public function __construct(
2930
public readonly ModelName|string $modelName,
3031
public readonly array $contents,
3132
public readonly array $safetySettings = [],
3233
public readonly ?GenerationConfig $generationConfig = null,
34+
public readonly ?Content $systemInstruction = null,
3335
) {
3436
$this->ensureArrayOfType($this->contents, Content::class);
3537
$this->ensureArrayOfType($this->safetySettings, SafetySetting::class);
@@ -56,6 +58,7 @@ public function getHttpPayload(): string
5658
* contents: Content[],
5759
* safetySettings?: SafetySetting[],
5860
* generationConfig?: GenerationConfig,
61+
* systemInstruction?: Content,
5962
* }
6063
*/
6164
public function jsonSerialize(): array
@@ -73,6 +76,10 @@ public function jsonSerialize(): array
7376
$arr['generationConfig'] = $this->generationConfig;
7477
}
7578

79+
if ($this->systemInstruction) {
80+
$arr['systemInstruction'] = $this->systemInstruction;
81+
}
82+
7683
return $arr;
7784
}
7885

src/Requests/GenerateContentStreamRequest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ class GenerateContentStreamRequest implements JsonSerializable, RequestInterface
2424
* @param Content[] $contents
2525
* @param SafetySetting[] $safetySettings
2626
* @param GenerationConfig|null $generationConfig
27+
* @param ?Content $systemInstruction
2728
*/
2829
public function __construct(
2930
public readonly ModelName|string $modelName,
3031
public readonly array $contents,
3132
public readonly array $safetySettings = [],
3233
public readonly ?GenerationConfig $generationConfig = null,
34+
public readonly ?Content $systemInstruction = null,
3335
) {
3436
$this->ensureArrayOfType($this->contents, Content::class);
3537
$this->ensureArrayOfType($this->safetySettings, SafetySetting::class);
@@ -56,6 +58,7 @@ public function getHttpPayload(): string
5658
* contents: Content[],
5759
* safetySettings?: SafetySetting[],
5860
* generationConfig?: GenerationConfig,
61+
* systemInstruction?: Content,
5962
* }
6063
*/
6164
public function jsonSerialize(): array
@@ -73,6 +76,10 @@ public function jsonSerialize(): array
7376
$arr['generationConfig'] = $this->generationConfig;
7477
}
7578

79+
if ($this->systemInstruction) {
80+
$arr['systemInstruction'] = $this->systemInstruction;
81+
}
82+
7683
return $arr;
7784
}
7885

0 commit comments

Comments
 (0)