Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
<?php
echo "Hello World!";
?>

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
Expand Down
43 changes: 43 additions & 0 deletions src/ChatSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace GenerativeAI;

use GenerativeAI\Enums\Role;
use GenerativeAI\Resources\Content;
use GenerativeAI\Resources\Parts\PartInterface;
use GenerativeAI\Responses\GenerateContentResponse;
use Psr\Http\Client\ClientExceptionInterface;

class ChatSession
{
/** @var Content[] */
private array $history;

public function __construct(
private readonly GenerativeModel $model,
) {
}

/**
* @throws ClientExceptionInterface
*/
public function sendMessage(PartInterface ...$parts): GenerateContentResponse
{
$this->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;
}
}
22 changes: 21 additions & 1 deletion src/GenerativeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -33,16 +36,33 @@ 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,
);

return $this->client->generateContent($request);
}

public function startChat(): ChatSession
{
return new ChatSession($this);
}

/**
* @throws ClientExceptionInterface
*/
Expand Down