feat: add generateLlmPredictionWithContext with web search geopolitical context

- New method uses web_search_20260209 server-side tool so Claude fetches
  48h of oil/geopolitical news autonomously before predicting direction
- Prompt uses raw prices only — no pre-computed EWMA indicators
- pause_turn loop handles server-side search continuation (up to 5 iters)
- generatePrediction() now tries context method first, falls back to
  generateLlmPrediction(), then EWMA
- Default model updated to claude-sonnet-4-6

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Ovidiu U
2026-04-04 15:32:53 +01:00
parent 1d2eb12e83
commit fb4c413926
3 changed files with 229 additions and 2 deletions

View File

@@ -227,3 +227,131 @@ it('returns null when there is insufficient price data', function (): void {
expect($this->service->generatePrediction())->toBeNull()
->and(PricePrediction::count())->toBe(0);
});
// --- generateLlmPredictionWithContext ---
it('generates llm prediction with context using web search and raw prices', function () {
config(['services.anthropic.api_key' => 'test-key']);
BrentPrice::factory()->count(20)->sequence(fn ($s) => [
'date' => now()->subDays(20 - $s->index)->toDateString(),
'price_usd' => 80.0 + $s->index * 0.5,
])->create();
Http::fake([
'https://api.anthropic.com/*' => Http::response([
'content' => [
['type' => 'text', 'text' => '{"direction":"rising","confidence":72,"reasoning":"OPEC+ extended cuts while prices trend upward."}'],
],
'stop_reason' => 'end_turn',
], 200),
]);
$prices = BrentPrice::orderBy('date', 'desc')->limit(30)->get();
$prediction = app(OilPriceService::class)->generateLlmPredictionWithContext($prices);
expect($prediction)->not->toBeNull()
->and($prediction->direction)->toBe(TrendDirection::Rising)
->and($prediction->confidence)->toBe(72)
->and($prediction->source)->toBe(PredictionSource::Llm)
->and($prediction->reasoning)->toBe('OPEC+ extended cuts while prices trend upward.');
Http::assertSentCount(1);
});
it('sends web_search tool in the context prediction request', function () {
config(['services.anthropic.api_key' => 'test-key']);
BrentPrice::factory()->count(20)->sequence(fn ($s) => [
'date' => now()->subDays(20 - $s->index)->toDateString(),
'price_usd' => 80.0,
])->create();
Http::fake([
'https://api.anthropic.com/*' => Http::response([
'content' => [['type' => 'text', 'text' => '{"direction":"flat","confidence":50,"reasoning":"No clear trend."}']],
'stop_reason' => 'end_turn',
], 200),
]);
$prices = BrentPrice::orderBy('date', 'desc')->limit(30)->get();
app(OilPriceService::class)->generateLlmPredictionWithContext($prices);
Http::assertSent(function ($request) {
$tools = $request->data()['tools'] ?? [];
return collect($tools)->contains(fn ($t) => $t['type'] === 'web_search_20260209');
});
});
it('does not include ewma indicators in the context prediction request', function () {
config(['services.anthropic.api_key' => 'test-key']);
BrentPrice::factory()->count(20)->sequence(fn ($s) => [
'date' => now()->subDays(20 - $s->index)->toDateString(),
'price_usd' => 80.0,
])->create();
Http::fake([
'https://api.anthropic.com/*' => Http::response([
'content' => [['type' => 'text', 'text' => '{"direction":"flat","confidence":50,"reasoning":"No clear trend."}']],
'stop_reason' => 'end_turn',
], 200),
]);
$prices = BrentPrice::orderBy('date', 'desc')->limit(30)->get();
app(OilPriceService::class)->generateLlmPredictionWithContext($prices);
Http::assertSent(function ($request) {
$content = $request->data()['messages'][0]['content'] ?? '';
return ! str_contains($content, 'EWMA') && ! str_contains($content, 'Pre-computed');
});
});
it('context prediction continues on pause_turn and returns final answer', function () {
config(['services.anthropic.api_key' => 'test-key']);
BrentPrice::factory()->count(20)->sequence(fn ($s) => [
'date' => now()->subDays(20 - $s->index)->toDateString(),
'price_usd' => 80.0,
])->create();
Http::fake([
'https://api.anthropic.com/*' => Http::sequence()
->push([
'content' => [['type' => 'server_tool_use', 'id' => 'sttool_1', 'name' => 'web_search', 'input' => ['query' => 'Brent crude news']]],
'stop_reason' => 'pause_turn',
], 200)
->push([
'content' => [['type' => 'text', 'text' => '{"direction":"falling","confidence":60,"reasoning":"Demand fears weigh on prices."}']],
'stop_reason' => 'end_turn',
], 200),
]);
$prices = BrentPrice::orderBy('date', 'desc')->limit(30)->get();
$prediction = app(OilPriceService::class)->generateLlmPredictionWithContext($prices);
expect($prediction)->not->toBeNull()
->and($prediction->direction)->toBe(TrendDirection::Falling);
Http::assertSentCount(2);
});
it('generatePrediction falls through to ewma when both llm methods fail', function () {
config(['services.anthropic.api_key' => 'test-key']);
BrentPrice::factory()->count(20)->sequence(fn ($s) => [
'date' => now()->subDays(20 - $s->index)->toDateString(),
'price_usd' => 80.0,
])->create();
Http::fake([
'https://api.anthropic.com/*' => Http::response([], 500),
]);
$prediction = app(OilPriceService::class)->generatePrediction();
expect($prediction)->not->toBeNull()
->and($prediction->source)->toBe(PredictionSource::Ewma);
});