forked from prism-php/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPendingRequest.php
More file actions
202 lines (164 loc) · 4.71 KB
/
Copy pathPendingRequest.php
File metadata and controls
202 lines (164 loc) · 4.71 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
declare(strict_types=1);
namespace Prism\Prism\Embeddings;
use Illuminate\Http\Client\RequestException;
use Prism\Prism\Concerns\ConfiguresClient;
use Prism\Prism\Concerns\ConfiguresProviders;
use Prism\Prism\Concerns\HasProviderOptions;
use Prism\Prism\Exceptions\PrismException;
use Prism\Prism\ValueObjects\Media\Audio;
use Prism\Prism\ValueObjects\Media\Document;
use Prism\Prism\ValueObjects\Media\Image;
use Prism\Prism\ValueObjects\Media\Media;
use Prism\Prism\ValueObjects\Media\Text;
use Prism\Prism\ValueObjects\Media\Video;
class PendingRequest
{
use ConfiguresClient;
use ConfiguresProviders;
use HasProviderOptions;
/** @var array<Content> */
protected array $contents = [];
public function fromInput(string $input): self
{
$this->contents[] = Content::make([$input]);
return $this;
}
/**
* @param array<string> $inputs
*/
public function fromArray(array $inputs): self
{
foreach ($inputs as $input) {
$this->fromInput($input);
}
return $this;
}
public function fromFile(string $path): self
{
if (! is_file($path)) {
throw new PrismException(sprintf('%s is not a valid file', $path));
}
$contents = file_get_contents($path);
if ($contents === false) {
throw new PrismException(sprintf('%s contents could not be read', $path));
}
return $this->fromInput($contents);
}
/**
* Add an image for embedding generation.
*
* Note: Not all providers support image embeddings. Check the provider's
* documentation to ensure the model you're using supports image input.
* Common providers that support image embeddings include CLIP-based models
* and multimodal embedding models like BGE-VL.
*/
public function fromImage(Image $image): self
{
$this->contents[] = Content::make([$image]);
return $this;
}
/**
* @param array<Image> $images
*/
public function fromImages(array $images): self
{
foreach ($images as $image) {
$this->fromImage($image);
}
return $this;
}
public function fromAudio(Audio $audio): self
{
$this->contents[] = Content::make([$audio]);
return $this;
}
/**
* @param array<Audio> $audios
*/
public function fromAudios(array $audios): self
{
foreach ($audios as $audio) {
$this->fromAudio($audio);
}
return $this;
}
public function fromVideo(Video $video): self
{
$this->contents[] = Content::make([$video]);
return $this;
}
/**
* @param array<Video> $videos
*/
public function fromVideos(array $videos): self
{
foreach ($videos as $video) {
$this->fromVideo($video);
}
return $this;
}
public function fromDocument(Document $document): self
{
$this->contents[] = Content::make([$document]);
return $this;
}
/**
* @param array<Document> $documents
*/
public function fromDocuments(array $documents): self
{
foreach ($documents as $document) {
$this->fromDocument($document);
}
return $this;
}
/**
* @param array<int, Media|Text|string> $parts
*/
public function fromContent(array $parts): self
{
$this->contents[] = Content::make($parts);
return $this;
}
/**
* @param array<int, array<int, Media|Text|string>> $contents
*/
public function fromContents(array $contents): self
{
foreach ($contents as $content) {
$this->fromContent($content);
}
return $this;
}
/**
* @deprecated Use `asEmbeddings` instead.
*/
public function generate(): Response
{
return $this->asEmbeddings();
}
public function asEmbeddings(): Response
{
if ($this->contents === []) {
throw new PrismException('Embeddings input is required (text, images, audio, video, documents, or content parts)');
}
$request = $this->toRequest();
try {
return $this->provider->embeddings($request);
} catch (RequestException $e) {
$this->provider->handleRequestException($request->model(), $e);
}
}
protected function toRequest(): Request
{
return new Request(
model: $this->model,
providerKey: $this->providerKey(),
clientOptions: $this->clientOptions,
clientRetry: $this->clientRetry,
providerOptions: $this->providerOptions,
contents: $this->contents,
);
}
}