|
7 | 7 | use Illuminate\Http\Client\Request;
|
8 | 8 | use Illuminate\Support\Facades\Http;
|
9 | 9 | use Prism\Prism\Enums\Provider;
|
| 10 | +use Prism\Prism\Facades\Tool; |
10 | 11 | use Prism\Prism\Prism;
|
11 | 12 | use Tests\Fixtures\FixtureResponse;
|
12 | 13 |
|
13 | 14 | beforeEach(function (): void {
|
14 | 15 | config()->set('prism.providers.gemini.api_key', env('GEMINI_API_KEY', 'sss-1234567890'));
|
15 | 16 | });
|
16 | 17 |
|
17 |
| -it('can generate text with a basic stream', function (): void { |
| 18 | +it('can generate text stream with a basic prompt', function (): void { |
18 | 19 | FixtureResponse::fakeResponseSequence('*', 'gemini/stream-basic-text');
|
19 | 20 |
|
20 | 21 | $response = Prism::text()
|
|
97 | 98 | ->and($text)
|
98 | 99 | ->toContain('The weather in San Francisco is currently 58°F (14°C) and partly cloudy. It feels like 55°F (13°C) with 79% humidity. There is a 0% chance of rain right now but showers are expected to develop.');
|
99 | 100 | });
|
| 101 | + |
| 102 | +it('can generate text stream using tools ', function (): void { |
| 103 | + FixtureResponse::fakeResponseSequence('*', 'gemini/stream-with-tools'); |
| 104 | + |
| 105 | + $tools = [ |
| 106 | + Tool::as('weather') |
| 107 | + ->for('useful when you need to search for current weather conditions') |
| 108 | + ->withStringParameter('city', 'The city that you want the weather for') |
| 109 | + ->using(fn (string $city): string => "The weather will be 75° and sunny in {$city}"), |
| 110 | + |
| 111 | + Tool::as('search') |
| 112 | + ->for('useful for searching current events or data') |
| 113 | + ->withStringParameter('query', 'The detailed search query') |
| 114 | + ->using(fn (string $query): string => "Search results for: {$query}"), |
| 115 | + ]; |
| 116 | + |
| 117 | + $response = Prism::text() |
| 118 | + ->using(Provider::Gemini, 'gemini-2.0-flash') |
| 119 | + ->withTools($tools) |
| 120 | + ->withPrompt('What\'s the current weather in San Francisco? And tell me if I need to wear a coat?') |
| 121 | + ->asStream(); |
| 122 | + |
| 123 | + $text = ''; |
| 124 | + $chunks = []; |
| 125 | + |
| 126 | + foreach ($response as $chunk) { |
| 127 | + $chunks[] = $chunk; |
| 128 | + $text .= $chunk->text; |
| 129 | + } |
| 130 | + |
| 131 | + expect($chunks) |
| 132 | + ->not->toBeEmpty() |
| 133 | + ->and($text)->not->toBeEmpty() |
| 134 | + ->and($text)->toContain('The weather in San Francisco is currently 58°F (14°C) and partly cloudy. It feels like 55°F (13°C) with 79% humidity. There is a 0% chance of rain right now but showers are expected to develop.') |
| 135 | + ->and($text)->toContain('a light jacket or coat would be advisable'); |
| 136 | + |
| 137 | + // Verify the HTTP request |
| 138 | + Http::assertSent(fn (Request $request): bool => str_contains($request->url(), 'streamGenerateContent?alt=sse') |
| 139 | + && isset($request->data()['contents'])); |
| 140 | +}); |
0 commit comments