Skip to content

Commit

Permalink
Add system instruction support
Browse files Browse the repository at this point in the history
Closes #51
  • Loading branch information
Erdem Köse committed Dec 1, 2024
1 parent c4fecae commit e75094f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ _This library is not developed or endorsed by Google._
- [Installation](#installation)
- [How to use](#how-to-use)
- [Basic text generation](#basic-text-generation)
- [Text generation with system instruction](#text-generation-with-system-instruction)
- [Multimodal input](#multimodal-input)
- [Chat Session (Multi-Turn Conversations)](#chat-session-multi-turn-conversations)
- [Chat Session with history](#chat-session-with-history)
Expand Down Expand Up @@ -66,6 +67,27 @@ print $response->text();
// Easy to learn, widely used, and open-source.
```

### Text generation with system instruction

> System instruction is currently supported only in beta version
```php
use GeminiAPI\Client;
use GeminiAPI\Resources\ModelName;
use GeminiAPI\Resources\Parts\TextPart;

$client = new Client('GEMINI_API_KEY');
$response = $client->withV1BetaVersion()
->generativeModel(ModelName::GEMINI_1_5_FLASH)
->withSystemInstruction('You are a cat. Your name is Neko.')
->generateContent(
new TextPart('PHP in less than 100 chars'),
);

print $response->text();
// Meow? <?php echo 'Hello, world!'; ?> Purrfectly concise, wouldn't you say? *Stretches luxuriously*
```

### Multimodal input

> Image input modality is only enabled for Gemini Pro Vision model
Expand Down
12 changes: 12 additions & 0 deletions src/GenerativeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class GenerativeModel

private ?GenerationConfig $generationConfig = null;

private ?Content $systemInstruction = null;

public function __construct(
private readonly Client $client,
public readonly ModelName|string $modelName,
Expand Down Expand Up @@ -55,6 +57,7 @@ public function generateContentWithContents(array $contents): GenerateContentRes
$contents,
$this->safetySettings,
$this->generationConfig,
$this->systemInstruction,
);

return $this->client->generateContent($request);
Expand Down Expand Up @@ -96,6 +99,7 @@ public function generateContentStreamWithContents(
$contents,
$this->safetySettings,
$this->generationConfig,
$this->systemInstruction,
);

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

return $clone;
}

public function withSystemInstruction(string $systemInstruction): self
{
$clone = clone $this;
$clone->systemInstruction = Content::text($systemInstruction, Role::User);

return $clone;
}
}
7 changes: 7 additions & 0 deletions src/Requests/GenerateContentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ class GenerateContentRequest implements JsonSerializable, RequestInterface
* @param Content[] $contents
* @param SafetySetting[] $safetySettings
* @param GenerationConfig|null $generationConfig
* @param ?Content $systemInstruction
*/
public function __construct(
public readonly ModelName|string $modelName,
public readonly array $contents,
public readonly array $safetySettings = [],
public readonly ?GenerationConfig $generationConfig = null,
public readonly ?Content $systemInstruction = null,
) {
$this->ensureArrayOfType($this->contents, Content::class);
$this->ensureArrayOfType($this->safetySettings, SafetySetting::class);
Expand All @@ -56,6 +58,7 @@ public function getHttpPayload(): string
* contents: Content[],
* safetySettings?: SafetySetting[],
* generationConfig?: GenerationConfig,
* systemInstruction?: Content,
* }
*/
public function jsonSerialize(): array
Expand All @@ -73,6 +76,10 @@ public function jsonSerialize(): array
$arr['generationConfig'] = $this->generationConfig;
}

if ($this->systemInstruction) {
$arr['systemInstruction'] = $this->systemInstruction;
}

return $arr;
}

Expand Down
7 changes: 7 additions & 0 deletions src/Requests/GenerateContentStreamRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ class GenerateContentStreamRequest implements JsonSerializable, RequestInterface
* @param Content[] $contents
* @param SafetySetting[] $safetySettings
* @param GenerationConfig|null $generationConfig
* @param ?Content $systemInstruction
*/
public function __construct(
public readonly ModelName|string $modelName,
public readonly array $contents,
public readonly array $safetySettings = [],
public readonly ?GenerationConfig $generationConfig = null,
public readonly ?Content $systemInstruction = null,
) {
$this->ensureArrayOfType($this->contents, Content::class);
$this->ensureArrayOfType($this->safetySettings, SafetySetting::class);
Expand All @@ -56,6 +58,7 @@ public function getHttpPayload(): string
* contents: Content[],
* safetySettings?: SafetySetting[],
* generationConfig?: GenerationConfig,
* systemInstruction?: Content,
* }
*/
public function jsonSerialize(): array
Expand All @@ -73,6 +76,10 @@ public function jsonSerialize(): array
$arr['generationConfig'] = $this->generationConfig;
}

if ($this->systemInstruction) {
$arr['systemInstruction'] = $this->systemInstruction;
}

return $arr;
}

Expand Down

0 comments on commit e75094f

Please sign in to comment.