-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathGenerativeModel.php
More file actions
155 lines (126 loc) · 4.12 KB
/
GenerativeModel.php
File metadata and controls
155 lines (126 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace GeminiAPI;
use CurlHandle;
use GeminiAPI\Enums\ModelName;
use GeminiAPI\Enums\Role;
use GeminiAPI\Requests\CountTokensRequest;
use GeminiAPI\Requests\GenerateContentRequest;
use GeminiAPI\Requests\GenerateContentStreamRequest;
use GeminiAPI\Responses\CountTokensResponse;
use GeminiAPI\Responses\GenerateContentResponse;
use GeminiAPI\Resources\Content;
use GeminiAPI\Resources\Parts\PartInterface;
use GeminiAPI\Traits\ArrayTypeValidator;
use Psr\Http\Client\ClientExceptionInterface;
class GenerativeModel
{
use ArrayTypeValidator;
/** @var SafetySetting[] */
private array $safetySettings = [];
private ?GenerationConfig $generationConfig = null;
private ?Content $systemInstruction = null;
public function __construct(
private readonly Client $client,
public readonly ModelName|string $modelName,
) {
}
/**
* @throws ClientExceptionInterface
*/
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,
$contents,
$this->safetySettings,
$this->generationConfig,
$this->systemInstruction,
);
return $this->client->generateContent($request);
}
/**
* @param callable(GenerateContentResponse): void $callback
* @param PartInterface[] $parts
* @param CurlHandle|null $ch
* @return void
*/
public function generateContentStream(
callable $callback,
array $parts,
?CurlHandle $ch = null,
): void {
$this->ensureArrayOfType($parts, PartInterface::class);
$content = new Content($parts, Role::User);
$this->generateContentStreamWithContents($callback, [$content], $ch);
}
/**
* @param callable(GenerateContentResponse): void $callback
* @param Content[] $contents
* @param CurlHandle|null $ch
* @return void
*/
public function generateContentStreamWithContents(
callable $callback,
array $contents,
?CurlHandle $ch = null,
): void {
$this->ensureArrayOfType($contents, Content::class);
$request = new GenerateContentStreamRequest(
$this->modelName,
$contents,
$this->safetySettings,
$this->generationConfig,
$this->systemInstruction,
);
$this->client->generateContentStream($request, $callback, $ch);
}
public function startChat(): ChatSession
{
return new ChatSession($this);
}
/**
* @throws ClientExceptionInterface
*/
public function countTokens(PartInterface ...$parts): CountTokensResponse
{
$temporaryContent = new Content($parts, Role::User);
// Add system instruction if it exists
if ($this->systemInstruction !== null) {
$temporaryContent = $temporaryContent->addParts(...$this->systemInstruction->parts);
}
$request = new CountTokensRequest(
$this->modelName,
[$temporaryContent]
);
return $this->client->countTokens($request);
}
public function withAddedSafetySetting(SafetySetting $safetySetting): self
{
$clone = clone $this;
$clone->safetySettings[] = $safetySetting;
return $clone;
}
public function withGenerationConfig(GenerationConfig $generationConfig): self
{
$clone = clone $this;
$clone->generationConfig = $generationConfig;
return $clone;
}
public function withSystemInstruction(string $systemInstruction): self
{
$clone = clone $this;
$clone->systemInstruction = Content::text($systemInstruction, Role::User);
return $clone;
}
}