diff --git a/README.md b/README.md index 1ea69f2..7c26461 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,15 @@ _This library is not developed or endorsed by Google._ - Erdem Köse - **[github.com/erdemkose](https://github.com/erdemkose)** +## Table of Contents +- [Installation](#installation) +- [How to use](#how-to-use) + - [Basic text generation](#basic-text-generation) + - [Multimodal input](#multimodal-input) + - [Chat Session (Multi-Turn Conversations)](#chat-session-multi-turn-conversations) + - [Tokens counting](#tokens-counting) + - [Listing models](#listing-models) + ## Installation > You need an API key to gain access to Google Generative AI services. @@ -67,6 +76,40 @@ print $response->text(); // The image is set against a starry background. ``` +### Chat Session (Multi-Turn Conversations) + +```php +$client = new GenerativeAI\Client('YOUR_GEMINI_PRO_API_TOKEN'); + +$chat = $client->GeminiPro()->startChat(); + +$response = $chat->sendMessage(new TextPart('Hello World in PHP')); +print $response->text(); + +$response = $chat->sendMessage(new TextPart('in Go')); +print $response->text(); +``` + +```text + + +This code will print "Hello World!" to the standard output. +``` + +```text +package main + +import "fmt" + +func main() { + fmt.Println("Hello World!") +} + +This code will print "Hello World!" to the standard output. +``` + ### Tokens counting ```php diff --git a/src/ChatSession.php b/src/ChatSession.php new file mode 100644 index 0000000..ed14072 --- /dev/null +++ b/src/ChatSession.php @@ -0,0 +1,43 @@ +history[] = new Content($parts, Role::User); + + $config = (new GenerationConfig()) + ->withCandidateCount(1); + $response = $this->model + ->withGenerationConfig($config) + ->generateContentWithContents($this->history); + + if(!empty($response->candidates)) { + $parts = $response->candidates[0]->content->parts; + $this->history[] = new Content($parts, Role::Model); + } + + return $response; + } +} diff --git a/src/GenerativeModel.php b/src/GenerativeModel.php index 1d8a9dc..db93602 100644 --- a/src/GenerativeModel.php +++ b/src/GenerativeModel.php @@ -12,10 +12,13 @@ use GenerativeAI\Responses\GenerateContentResponse; use GenerativeAI\Resources\Content; use GenerativeAI\Resources\Parts\PartInterface; +use GenerativeAI\Traits\ArrayTypeValidator; use Psr\Http\Client\ClientExceptionInterface; class GenerativeModel { + use ArrayTypeValidator; + /** @var SafetySetting[] */ private array $safetySettings = []; @@ -33,9 +36,21 @@ public function __construct( public function generateContent(PartInterface ...$parts): GenerateContentResponse { $content = new Content($parts, Role::User); + + return $this->generateContentWithContents([$content]); + } + + /** + * @param Content[] $contents + * @throws ClientExceptionInterface + */ + public function generateContentWithContents(array $contents): GenerateContentResponse + { + $this->ensureArrayOfType($contents, Content::class); + $request = new GenerateContentRequest( $this->modelName, - [$content], + $contents, $this->safetySettings, $this->generationConfig, ); @@ -43,6 +58,11 @@ public function generateContent(PartInterface ...$parts): GenerateContentRespons return $this->client->generateContent($request); } + public function startChat(): ChatSession + { + return new ChatSession($this); + } + /** * @throws ClientExceptionInterface */