diff --git a/README.md b/README.md index 9448a80..22ff1a2 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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? Purrfectly concise, wouldn't you say? *Stretches luxuriously* +``` + ### Multimodal input > Image input modality is only enabled for Gemini Pro Vision model diff --git a/src/GenerativeModel.php b/src/GenerativeModel.php index 46f9fc1..5d13bb7 100644 --- a/src/GenerativeModel.php +++ b/src/GenerativeModel.php @@ -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, @@ -55,6 +57,7 @@ public function generateContentWithContents(array $contents): GenerateContentRes $contents, $this->safetySettings, $this->generationConfig, + $this->systemInstruction, ); return $this->client->generateContent($request); @@ -96,6 +99,7 @@ public function generateContentStreamWithContents( $contents, $this->safetySettings, $this->generationConfig, + $this->systemInstruction, ); $this->client->generateContentStream($request, $callback, $ch); @@ -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; + } } diff --git a/src/Requests/GenerateContentRequest.php b/src/Requests/GenerateContentRequest.php index d3027b9..5a4f24d 100644 --- a/src/Requests/GenerateContentRequest.php +++ b/src/Requests/GenerateContentRequest.php @@ -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); @@ -56,6 +58,7 @@ public function getHttpPayload(): string * contents: Content[], * safetySettings?: SafetySetting[], * generationConfig?: GenerationConfig, + * systemInstruction?: Content, * } */ public function jsonSerialize(): array @@ -73,6 +76,10 @@ public function jsonSerialize(): array $arr['generationConfig'] = $this->generationConfig; } + if ($this->systemInstruction) { + $arr['systemInstruction'] = $this->systemInstruction; + } + return $arr; } diff --git a/src/Requests/GenerateContentStreamRequest.php b/src/Requests/GenerateContentStreamRequest.php index cf19c3a..0355706 100644 --- a/src/Requests/GenerateContentStreamRequest.php +++ b/src/Requests/GenerateContentStreamRequest.php @@ -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); @@ -56,6 +58,7 @@ public function getHttpPayload(): string * contents: Content[], * safetySettings?: SafetySetting[], * generationConfig?: GenerationConfig, + * systemInstruction?: Content, * } */ public function jsonSerialize(): array @@ -73,6 +76,10 @@ public function jsonSerialize(): array $arr['generationConfig'] = $this->generationConfig; } + if ($this->systemInstruction) { + $arr['systemInstruction'] = $this->systemInstruction; + } + return $arr; }