File tree Expand file tree Collapse file tree 5 files changed +59
-1
lines changed Expand file tree Collapse file tree 5 files changed +59
-1
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,11 @@ final class Factory
28
28
*/
29
29
private ?string $ organization = null ;
30
30
31
+ /**
32
+ * The project for the requests.
33
+ */
34
+ private ?string $ project = null ;
35
+
31
36
/**
32
37
* The HTTP client for the requests.
33
38
*/
@@ -74,6 +79,16 @@ public function withOrganization(?string $organization): self
74
79
return $ this ;
75
80
}
76
81
82
+ /**
83
+ * Sets the project for the requests.
84
+ */
85
+ public function withProject (?string $ project ): self
86
+ {
87
+ $ this ->project = $ project ;
88
+
89
+ return $ this ;
90
+ }
91
+
77
92
/**
78
93
* Sets the HTTP client for the requests.
79
94
* If no client is provided the factory will try to find one using PSR-18 HTTP Client Discovery.
@@ -141,6 +156,10 @@ public function make(): Client
141
156
$ headers = $ headers ->withOrganization ($ this ->organization );
142
157
}
143
158
159
+ if ($ this ->project !== null ) {
160
+ $ headers = $ headers ->withProject ($ this ->project );
161
+ }
162
+
144
163
foreach ($ this ->headers as $ name => $ value ) {
145
164
$ headers = $ headers ->withCustomHeader ($ name , $ value );
146
165
}
Original file line number Diff line number Diff line change @@ -10,11 +10,12 @@ final class OpenAI
10
10
/**
11
11
* Creates a new Open AI Client with the given API token.
12
12
*/
13
- public static function client (string $ apiKey , ?string $ organization = null ): Client
13
+ public static function client (string $ apiKey , ?string $ organization = null , ? string $ project = null ): Client
14
14
{
15
15
return self ::factory ()
16
16
->withApiKey ($ apiKey )
17
17
->withOrganization ($ organization )
18
+ ->withProject ($ project )
18
19
->withHttpHeader ('OpenAI-Beta ' , 'assistants=v2 ' )
19
20
->make ();
20
21
}
Original file line number Diff line number Diff line change @@ -62,6 +62,17 @@ public function withOrganization(string $organization): self
62
62
]);
63
63
}
64
64
65
+ /**
66
+ * Creates a new Headers value object, with the given project, and the existing headers.
67
+ */
68
+ public function withProject (string $ project ): self
69
+ {
70
+ return new self ([
71
+ ...$ this ->headers ,
72
+ 'OpenAI-Project ' => $ project ,
73
+ ]);
74
+ }
75
+
65
76
/**
66
77
* Creates a new Headers value object, with the newly added header, and the existing headers.
67
78
*/
Original file line number Diff line number Diff line change 17
17
expect ($ openAI )->toBeInstanceOf (Client::class);
18
18
});
19
19
20
+ it ('sets project when provided ' , function () {
21
+ $ openAI = OpenAI::client ('foo ' , 'nunomaduro ' , 'openai_proj ' );
22
+
23
+ expect ($ openAI )->toBeInstanceOf (Client::class);
24
+ });
25
+
20
26
it ('may create a client via factory ' , function () {
21
27
$ openAI = OpenAI::factory ()
22
28
->withApiKey ('foo ' )
33
39
expect ($ openAI )->toBeInstanceOf (Client::class);
34
40
});
35
41
42
+ it ('sets an project via factory ' , function () {
43
+ $ openAI = OpenAI::factory ()
44
+ ->withOrganization ('nunomaduro ' )
45
+ ->withProject ('openai_proj ' )
46
+ ->make ();
47
+
48
+ expect ($ openAI )->toBeInstanceOf (Client::class);
49
+ });
50
+
36
51
it ('sets a custom client via factory ' , function () {
37
52
$ openAI = OpenAI::factory ()
38
53
->withHttpClient (new GuzzleClient ())
Original file line number Diff line number Diff line change 44
44
]);
45
45
});
46
46
47
+ it ('can have project ' , function () {
48
+ $ headers = Headers::withAuthorization (ApiKey::from ('foo ' ))
49
+ ->withContentType (ContentType::JSON )
50
+ ->withProject ('openai_proj ' );
51
+
52
+ expect ($ headers ->toArray ())->toBe ([
53
+ 'Authorization ' => 'Bearer foo ' ,
54
+ 'Content-Type ' => 'application/json ' ,
55
+ 'OpenAI-Project ' => 'openai_proj ' ,
56
+ ]);
57
+ });
58
+
47
59
it ('can have custom header ' , function () {
48
60
$ headers = Headers::withAuthorization (ApiKey::from ('foo ' ))
49
61
->withContentType (ContentType::JSON )
You can’t perform that action at this time.
0 commit comments