File tree Expand file tree Collapse file tree 17 files changed +35
-89
lines changed Expand file tree Collapse file tree 17 files changed +35
-89
lines changed Original file line number Diff line number Diff line change 13
13
14
14
trait HandlesRequestExceptions
15
15
{
16
- public function handleRequestExceptions (string $ model , Throwable $ e ): never
16
+ public function handleRequestException (string $ model , Throwable $ e ): never
17
17
{
18
18
// Keep already raised PrismException
19
19
if ($ e instanceof PrismException) {
Original file line number Diff line number Diff line change 4
4
5
5
namespace Prism \Prism \Embeddings ;
6
6
7
+ use Illuminate \Http \Client \RequestException ;
7
8
use Prism \Prism \Concerns \ConfiguresClient ;
8
9
use Prism \Prism \Concerns \ConfiguresProviders ;
9
10
use Prism \Prism \Concerns \HasProviderOptions ;
10
11
use Prism \Prism \Exceptions \PrismException ;
11
- use Throwable ;
12
12
13
13
class PendingRequest
14
14
{
@@ -71,8 +71,8 @@ public function asEmbeddings(): Response
71
71
72
72
try {
73
73
return $ this ->provider ->embeddings ($ request );
74
- } catch (Throwable $ e ) {
75
- $ this ->provider ->handleRequestExceptions ($ request ->model (), $ e );
74
+ } catch (RequestException $ e ) {
75
+ $ this ->provider ->handleRequestException ($ request ->model (), $ e );
76
76
}
77
77
}
78
78
Original file line number Diff line number Diff line change 5
5
namespace Prism \Prism \Images ;
6
6
7
7
use Illuminate \Contracts \View \View ;
8
+ use Illuminate \Http \Client \RequestException ;
8
9
use Prism \Prism \Concerns \ConfiguresClient ;
9
10
use Prism \Prism \Concerns \ConfiguresModels ;
10
11
use Prism \Prism \Concerns \ConfiguresProviders ;
@@ -28,7 +29,13 @@ public function withPrompt(string|View $prompt): self
28
29
29
30
public function generate (): Response
30
31
{
31
- return $ this ->provider ->images ($ this ->toRequest ());
32
+ $ request = $ this ->toRequest ();
33
+
34
+ try {
35
+ return $ this ->provider ->images ($ this ->toRequest ());
36
+ } catch (RequestException $ e ) {
37
+ $ this ->provider ->handleRequestException ($ request ->model (), $ e );
38
+ }
32
39
}
33
40
34
41
public function toRequest (): Request
Original file line number Diff line number Diff line change 22
22
use Prism \Prism \Structured \Response as StructuredResponse ;
23
23
use Prism \Prism \Text \Request as TextRequest ;
24
24
use Prism \Prism \Text \Response as TextResponse ;
25
- use Throwable ;
26
25
27
26
class Anthropic extends Provider
28
27
{
@@ -73,16 +72,8 @@ public function stream(TextRequest $request): Generator
73
72
return $ handler ->handle ($ request );
74
73
}
75
74
76
- public function handleRequestExceptions (string $ model , Throwable $ e ): never
75
+ public function handleRequestException (string $ model , RequestException $ e ): never
77
76
{
78
- if ($ e instanceof PrismException) {
79
- throw $ e ;
80
- }
81
-
82
- if (! $ e instanceof RequestException) {
83
- throw PrismException::providerRequestError ($ model , $ e );
84
- }
85
-
86
77
match ($ e ->response ->getStatusCode ()) {
87
78
429 => throw PrismRateLimitedException::make (
88
79
rateLimits: $ this ->processRateLimits ($ e ->response ),
Original file line number Diff line number Diff line change @@ -49,7 +49,7 @@ protected function sendRequest(): array
49
49
try {
50
50
$ response = $ this ->client ->post ('/cachedContents ' , $ request );
51
51
} catch (Throwable $ e ) {
52
- $ this ->handleRequestExceptions ($ this ->model , $ e );
52
+ $ this ->handleRequestException ($ this ->model , $ e );
53
53
}
54
54
55
55
return $ response ->json ();
Original file line number Diff line number Diff line change 4
4
5
5
namespace Prism \Prism \Providers \Gemini \Handlers ;
6
6
7
- use Exception ;
8
7
use Illuminate \Http \Client \PendingRequest ;
9
8
use Illuminate \Http \Client \Response as ClientResponse ;
10
9
use Illuminate \Support \Arr ;
@@ -89,7 +88,7 @@ protected function sendRequest(Request $request): ClientResponse
89
88
]);
90
89
91
90
if ($ request ->tools () !== [] && $ request ->providerTools () != []) {
92
- throw new Exception ('Use of provider tools with custom tools is not currently supported by Gemini. ' );
91
+ throw new PrismException ('Use of provider tools with custom tools is not currently supported by Gemini. ' );
93
92
}
94
93
95
94
$ tools = [];
Original file line number Diff line number Diff line change 20
20
use Prism \Prism \Structured \Response as StructuredResponse ;
21
21
use Prism \Prism \Text \Request as TextRequest ;
22
22
use Prism \Prism \Text \Response as TextResponse ;
23
- use Throwable ;
24
23
25
24
class Groq extends Provider
26
25
{
@@ -47,16 +46,8 @@ public function structured(StructuredRequest $request): StructuredResponse
47
46
return $ handler ->handle ($ request );
48
47
}
49
48
50
- public function handleRequestExceptions (string $ model , Throwable $ e ): never
49
+ public function handleRequestException (string $ model , RequestException $ e ): never
51
50
{
52
- if ($ e instanceof PrismException) {
53
- throw $ e ;
54
- }
55
-
56
- if (! $ e instanceof RequestException) {
57
- throw PrismException::providerRequestError ($ model , $ e );
58
- }
59
-
60
51
match ($ e ->response ->getStatusCode ()) {
61
52
429 => throw PrismRateLimitedException::make (
62
53
rateLimits: $ this ->processRateLimits ($ e ->response ),
Original file line number Diff line number Diff line change 4
4
5
5
namespace Prism \Prism \Providers \Groq \Maps ;
6
6
7
- use InvalidArgumentException ;
8
7
use Prism \Prism \Enums \ToolChoice ;
8
+ use Prism \Prism \Exceptions \PrismException ;
9
9
10
10
class ToolChoiceMap
11
11
{
@@ -26,7 +26,7 @@ public static function map(string|ToolChoice|null $toolChoice): string|array|nul
26
26
return match ($ toolChoice ) {
27
27
ToolChoice::Auto => 'auto ' ,
28
28
null => $ toolChoice ,
29
- default => throw new InvalidArgumentException ('Invalid tool choice ' )
29
+ default => throw new PrismException ('Invalid tool choice ' )
30
30
};
31
31
}
32
32
}
Original file line number Diff line number Diff line change 28
28
use Prism \Prism \Text \Request as TextRequest ;
29
29
use Prism \Prism \Text \Response as TextResponse ;
30
30
use Prism \Prism \ValueObjects \Messages \Support \Document ;
31
- use Throwable ;
32
31
33
32
class Mistral extends Provider
34
33
{
@@ -107,16 +106,8 @@ public function stream(TextRequest $request): Generator
107
106
return $ handler ->handle ($ request );
108
107
}
109
108
110
- public function handleRequestExceptions (string $ model , Throwable $ e ): never
109
+ public function handleRequestException (string $ model , RequestException $ e ): never
111
110
{
112
- if ($ e instanceof PrismException) {
113
- throw $ e ;
114
- }
115
-
116
- if (! $ e instanceof RequestException) {
117
- throw PrismException::providerRequestError ($ model , $ e );
118
- }
119
-
120
111
match ($ e ->response ->getStatusCode ()) {
121
112
429 => throw PrismRateLimitedException::make (
122
113
rateLimits: $ this ->processRateLimits ($ e ->response ),
Original file line number Diff line number Diff line change 6
6
7
7
use Illuminate \Http \Client \PendingRequest ;
8
8
use Illuminate \Http \Client \Response as ClientResponse ;
9
- use Prism \Prism \Exceptions \PrismException ;
10
9
use Prism \Prism \Images \Request ;
11
10
use Prism \Prism \Images \Response ;
12
11
use Prism \Prism \Images \ResponseBuilder ;
16
15
use Prism \Prism \ValueObjects \GeneratedImage ;
17
16
use Prism \Prism \ValueObjects \Meta ;
18
17
use Prism \Prism \ValueObjects \Usage ;
19
- use Throwable ;
20
18
21
19
class Images
22
20
{
@@ -53,11 +51,7 @@ public function handle(Request $request): Response
53
51
54
52
protected function sendRequest (Request $ request ): ClientResponse
55
53
{
56
- try {
57
- return $ this ->client ->post ('images/generations ' , ImageRequestMap::map ($ request ));
58
- } catch (Throwable $ e ) {
59
- throw PrismException::providerRequestError ($ request ->model (), $ e );
60
- }
54
+ return $ this ->client ->post ('images/generations ' , ImageRequestMap::map ($ request ));
61
55
}
62
56
63
57
/**
Original file line number Diff line number Diff line change 28
28
use Prism \Prism \Structured \Response as StructuredResponse ;
29
29
use Prism \Prism \Text \Request as TextRequest ;
30
30
use Prism \Prism \Text \Response as TextResponse ;
31
- use Throwable ;
32
31
33
32
class OpenAI extends Provider
34
33
{
@@ -96,16 +95,8 @@ public function stream(TextRequest $request): Generator
96
95
return $ handler ->handle ($ request );
97
96
}
98
97
99
- public function handleRequestExceptions (string $ model , Throwable $ e ): never
98
+ public function handleRequestException (string $ model , RequestException $ e ): never
100
99
{
101
- if ($ e instanceof PrismException) {
102
- throw $ e ;
103
- }
104
-
105
- if (! $ e instanceof RequestException) {
106
- throw PrismException::providerRequestError ($ model , $ e );
107
- }
108
-
109
100
match ($ e ->response ->getStatusCode ()) {
110
101
429 => throw PrismRateLimitedException::make (
111
102
rateLimits: $ this ->processRateLimits ($ e ->response ),
Original file line number Diff line number Diff line change 16
16
use Prism \Prism \Text \Chunk ;
17
17
use Prism \Prism \Text \Request as TextRequest ;
18
18
use Prism \Prism \Text \Response as TextResponse ;
19
- use Throwable ;
20
19
21
20
abstract class Provider
22
21
{
@@ -48,16 +47,8 @@ public function stream(TextRequest $request): Generator
48
47
throw PrismException::unsupportedProviderAction (__METHOD__ , class_basename ($ this ));
49
48
}
50
49
51
- public function handleRequestExceptions (string $ model , Throwable $ e ): never
50
+ public function handleRequestException (string $ model , RequestException $ e ): never
52
51
{
53
- if ($ e instanceof PrismException) {
54
- throw $ e ;
55
- }
56
-
57
- if (! $ e instanceof RequestException) {
58
- throw PrismException::providerRequestError ($ model , $ e );
59
- }
60
-
61
52
throw PrismException::providerRequestError ($ model , $ e );
62
53
}
63
54
}
Original file line number Diff line number Diff line change 4
4
5
5
namespace Prism \Prism \Structured ;
6
6
7
+ use Illuminate \Http \Client \RequestException ;
7
8
use Prism \Prism \Concerns \ConfiguresClient ;
8
9
use Prism \Prism \Concerns \ConfiguresModels ;
9
10
use Prism \Prism \Concerns \ConfiguresProviders ;
14
15
use Prism \Prism \Concerns \HasSchema ;
15
16
use Prism \Prism \Exceptions \PrismException ;
16
17
use Prism \Prism \ValueObjects \Messages \UserMessage ;
17
- use Throwable ;
18
18
19
19
class PendingRequest
20
20
{
@@ -41,8 +41,8 @@ public function asStructured(): Response
41
41
42
42
try {
43
43
return $ this ->provider ->structured ($ request );
44
- } catch (Throwable $ e ) {
45
- $ this ->provider ->handleRequestExceptions ($ request ->model (), $ e );
44
+ } catch (RequestException $ e ) {
45
+ $ this ->provider ->handleRequestException ($ request ->model (), $ e );
46
46
}
47
47
}
48
48
Original file line number Diff line number Diff line change 11
11
use Prism \Prism \Embeddings \Request as EmbeddingRequest ;
12
12
use Prism \Prism \Embeddings \Response as EmbeddingResponse ;
13
13
use Prism \Prism \Enums \FinishReason ;
14
- use Prism \Prism \Exceptions \PrismException ;
15
14
use Prism \Prism \Images \Request as ImageRequest ;
16
15
use Prism \Prism \Images \Response as ImageResponse ;
17
16
use Prism \Prism \Providers \Provider ;
25
24
use Prism \Prism \ValueObjects \GeneratedImage ;
26
25
use Prism \Prism \ValueObjects \Meta ;
27
26
use Prism \Prism \ValueObjects \Usage ;
28
- use Throwable ;
29
27
30
28
class PrismFake extends Provider
31
29
{
@@ -146,11 +144,6 @@ public function stream(TextRequest $request): Generator
146
144
yield from $ this ->chunksFromTextResponse ($ fixture );
147
145
}
148
146
149
- public function handleRequestExceptions (string $ model , Throwable $ e ): never
150
- {
151
- throw PrismException::providerRequestError ($ model , $ e );
152
- }
153
-
154
147
/**
155
148
* @param array<string, mixed> $config
156
149
*/
Original file line number Diff line number Diff line change 5
5
namespace Prism \Prism \Text ;
6
6
7
7
use Generator ;
8
+ use Illuminate \Http \Client \RequestException ;
8
9
use Prism \Prism \Concerns \ConfiguresClient ;
9
10
use Prism \Prism \Concerns \ConfiguresGeneration ;
10
11
use Prism \Prism \Concerns \ConfiguresModels ;
17
18
use Prism \Prism \Concerns \HasTools ;
18
19
use Prism \Prism \Exceptions \PrismException ;
19
20
use Prism \Prism \ValueObjects \Messages \UserMessage ;
20
- use Throwable ;
21
21
22
22
class PendingRequest
23
23
{
@@ -46,8 +46,8 @@ public function asText(): Response
46
46
47
47
try {
48
48
return $ this ->provider ->text ($ request );
49
- } catch (Throwable $ e ) {
50
- $ this ->provider ->handleRequestExceptions ($ request ->model (), $ e );
49
+ } catch (RequestException $ e ) {
50
+ $ this ->provider ->handleRequestException ($ request ->model (), $ e );
51
51
}
52
52
}
53
53
@@ -64,8 +64,8 @@ public function asStream(): Generator
64
64
foreach ($ chunks as $ chunk ) {
65
65
yield $ chunk ;
66
66
}
67
- } catch (Throwable $ e ) {
68
- $ this ->provider ->handleRequestExceptions ($ request ->model (), $ e );
67
+ } catch (RequestException $ e ) {
68
+ $ this ->provider ->handleRequestException ($ request ->model (), $ e );
69
69
}
70
70
}
71
71
Original file line number Diff line number Diff line change 142
142
->using ('groq ' , 'gpt-4 ' )
143
143
->withPrompt ('Who are you? ' )
144
144
->withToolChoice (ToolChoice::Any)
145
- ->generate ();
145
+ ->asText ();
146
146
});
147
147
});
148
148
Original file line number Diff line number Diff line change 21
21
use Prism \Prism \ValueObjects \Meta ;
22
22
use Prism \Prism \ValueObjects \ProviderResponse ;
23
23
use Prism \Prism \ValueObjects \Usage ;
24
- use Throwable ;
25
24
26
25
class TestProvider extends Provider
27
26
{
@@ -86,6 +85,10 @@ public function embeddings(EmbeddingRequest $request): EmbeddingResponse
86
85
return $ this ->responses [$ this ->callCount - 1 ] ?? new EmbeddingResponse (
87
86
embeddings: [],
88
87
usage: new EmbeddingsUsage (10 ),
88
+ meta: new Meta (
89
+ id: '123 ' ,
90
+ model: 'your-model ' ,
91
+ )
89
92
);
90
93
}
91
94
@@ -114,11 +117,6 @@ public function stream(TextRequest $request): Generator
114
117
throw PrismException::unsupportedProviderAction (__METHOD__ , class_basename ($ this ));
115
118
}
116
119
117
- public function handleRequestExceptions (string $ model , Throwable $ e ): never
118
- {
119
- throw PrismException::providerRequestError ($ model , $ e );
120
- }
121
-
122
120
public function withResponse (StructuredResponse |TextResponse $ response ): Provider
123
121
{
124
122
$ this ->responses [] = $ response ;
You can’t perform that action at this time.
0 commit comments