-
-
Notifications
You must be signed in to change notification settings - Fork 516
/
Factory.php
204 lines (163 loc) · 5.04 KB
/
Factory.php
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
203
204
<?php
namespace OpenAI;
use Closure;
use Exception;
use GuzzleHttp\Client as GuzzleClient;
use Http\Discovery\Psr18ClientDiscovery;
use OpenAI\Transporters\HttpTransporter;
use OpenAI\ValueObjects\ApiKey;
use OpenAI\ValueObjects\Transporter\BaseUri;
use OpenAI\ValueObjects\Transporter\Headers;
use OpenAI\ValueObjects\Transporter\QueryParams;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpClient\Psr18Client;
final class Factory
{
/**
* The API key for the requests.
*/
private ?string $apiKey = null;
/**
* The organization for the requests.
*/
private ?string $organization = null;
/**
* The project for the requests.
*/
private ?string $project = null;
/**
* The HTTP client for the requests.
*/
private ?ClientInterface $httpClient = null;
/**
* The base URI for the requests.
*/
private ?string $baseUri = null;
/**
* The HTTP headers for the requests.
*
* @var array<string, string>
*/
private array $headers = [];
/**
* The query parameters for the requests.
*
* @var array<string, string|int>
*/
private array $queryParams = [];
private ?Closure $streamHandler = null;
/**
* Sets the API key for the requests.
*/
public function withApiKey(string $apiKey): self
{
$this->apiKey = trim($apiKey);
return $this;
}
/**
* Sets the organization for the requests.
*/
public function withOrganization(?string $organization): self
{
$this->organization = $organization;
return $this;
}
/**
* Sets the project for the requests.
*/
public function withProject(?string $project): self
{
$this->project = $project;
return $this;
}
/**
* Sets the HTTP client for the requests.
* If no client is provided the factory will try to find one using PSR-18 HTTP Client Discovery.
*/
public function withHttpClient(ClientInterface $client): self
{
$this->httpClient = $client;
return $this;
}
/**
* Sets the stream handler for the requests. Not required when using Guzzle.
*/
public function withStreamHandler(Closure $streamHandler): self
{
$this->streamHandler = $streamHandler;
return $this;
}
/**
* Sets the base URI for the requests.
* If no URI is provided the factory will use the default OpenAI API URI.
*/
public function withBaseUri(string $baseUri): self
{
$this->baseUri = $baseUri;
return $this;
}
/**
* Adds a custom HTTP header to the requests.
*/
public function withHttpHeader(string $name, string $value): self
{
$this->headers[$name] = $value;
return $this;
}
/**
* Adds a custom query parameter to the request url.
*/
public function withQueryParam(string $name, string $value): self
{
$this->queryParams[$name] = $value;
return $this;
}
/**
* Creates a new Open AI Client.
*/
public function make(): Client
{
$headers = Headers::create();
if ($this->apiKey !== null) {
$headers = Headers::withAuthorization(ApiKey::from($this->apiKey));
}
if ($this->organization !== null) {
$headers = $headers->withOrganization($this->organization);
}
if ($this->project !== null) {
$headers = $headers->withProject($this->project);
}
foreach ($this->headers as $name => $value) {
$headers = $headers->withCustomHeader($name, $value);
}
$baseUri = BaseUri::from($this->baseUri ?: 'api.openai.com/v1');
$queryParams = QueryParams::create();
foreach ($this->queryParams as $name => $value) {
$queryParams = $queryParams->withParam($name, $value);
}
$client = $this->httpClient ??= Psr18ClientDiscovery::find();
$sendAsync = $this->makeStreamHandler($client);
$transporter = new HttpTransporter($client, $baseUri, $headers, $queryParams, $sendAsync);
return new Client($transporter);
}
/**
* Creates a new stream handler for "stream" requests.
*/
private function makeStreamHandler(ClientInterface $client): Closure
{
if (! is_null($this->streamHandler)) {
return $this->streamHandler;
}
if ($client instanceof GuzzleClient) {
return fn (RequestInterface $request): ResponseInterface => $client->send($request, ['stream' => true]);
}
if ($client instanceof Psr18Client) { // @phpstan-ignore-line
return fn (RequestInterface $request): ResponseInterface => $client->sendRequest($request); // @phpstan-ignore-line
}
return function (RequestInterface $_): never {
throw new Exception('To use stream requests you must provide an stream handler closure via the OpenAI factory.');
};
}
}