prediction with context
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.3) (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled

This commit is contained in:
Ovidiu U
2026-04-05 17:08:16 +01:00
parent 0bea50b843
commit 3ccdc28763
9 changed files with 239 additions and 142 deletions

View File

@@ -12,6 +12,7 @@ use Illuminate\Support\Facades\Http;
uses(RefreshDatabase::class);
beforeEach(function (): void {
Http::preventStrayRequests();
$this->service = new OilPriceService(new ApiLogger);
});
@@ -39,7 +40,7 @@ it('filters out FRED missing value markers', function (): void {
'*/fred/series/observations*' => Http::response([
'observations' => [
['date' => '2026-04-01', 'value' => '75.10'],
['date' => '2026-04-02', 'value' => '.'], // weekend/holiday
['date' => '2026-04-02', 'value' => '.'],
['date' => '2026-04-03', 'value' => '74.20'],
],
]),
@@ -105,7 +106,7 @@ it('returns flat when price movement is within threshold', function (): void {
->and($prediction->confidence)->toBe(50);
});
it('returns null when fewer than 14 prices are available', function (): void {
it('returns null when fewer than 14 prices are available for EWMA', function (): void {
$prices = collect(range(1, 10))->map(fn (int $i) => new BrentPrice([
'date' => now()->subDays(10 - $i)->toDateString(),
'price_usd' => 75.0,
@@ -172,9 +173,113 @@ it('returns null when LLM returns malformed JSON', function (): void {
expect($this->service->generateLlmPrediction($prices))->toBeNull();
});
// --- generateLlmPredictionWithContext ---
it('generates LLM prediction with context and returns LlmWithContext source', function (): void {
config(['services.anthropic.api_key' => 'test-key']);
$prices = collect(range(1, 20))->map(fn (int $i) => new BrentPrice([
'date' => now()->subDays(20 - $i)->toDateString(),
'price_usd' => 80.0 + $i * 0.5,
]));
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',
]),
]);
$prediction = $this->service->generateLlmPredictionWithContext($prices);
expect($prediction)->not->toBeNull()
->and($prediction->direction)->toBe(TrendDirection::Rising)
->and($prediction->confidence)->toBe(72)
->and($prediction->source)->toBe(PredictionSource::LlmWithContext)
->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 (): void {
config(['services.anthropic.api_key' => 'test-key']);
$prices = collect(range(1, 20))->map(fn (int $i) => new BrentPrice([
'date' => now()->subDays(20 - $i)->toDateString(),
'price_usd' => 80.0,
]));
Http::fake([
'https://api.anthropic.com/*' => Http::response([
'content' => [['type' => 'text', 'text' => '{"direction":"flat","confidence":50,"reasoning":"No clear trend."}']],
'stop_reason' => 'end_turn',
]),
]);
$this->service->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 prompt', function (): void {
config(['services.anthropic.api_key' => 'test-key']);
$prices = collect(range(1, 20))->map(fn (int $i) => new BrentPrice([
'date' => now()->subDays(20 - $i)->toDateString(),
'price_usd' => 80.0,
]));
Http::fake([
'https://api.anthropic.com/*' => Http::response([
'content' => [['type' => 'text', 'text' => '{"direction":"flat","confidence":50,"reasoning":"No clear trend."}']],
'stop_reason' => 'end_turn',
]),
]);
$this->service->generateLlmPredictionWithContext($prices);
Http::assertSent(function ($request) {
$content = $request->data()['messages'][0]['content'] ?? '';
return ! str_contains($content, 'EWMA') && ! str_contains($content, 'Pre-computed');
});
});
it('continues on pause_turn and returns final answer', function (): void {
config(['services.anthropic.api_key' => 'test-key']);
$prices = collect(range(1, 20))->map(fn (int $i) => new BrentPrice([
'date' => now()->subDays(20 - $i)->toDateString(),
'price_usd' => 80.0,
]));
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',
])
->push([
'content' => [['type' => 'text', 'text' => '{"direction":"falling","confidence":60,"reasoning":"Demand fears weigh on prices."}']],
'stop_reason' => 'end_turn',
]),
]);
$prediction = $this->service->generateLlmPredictionWithContext($prices);
expect($prediction)->not->toBeNull()
->and($prediction->direction)->toBe(TrendDirection::Falling);
Http::assertSentCount(2);
});
// --- generatePrediction (orchestrator) ---
it('uses LLM when API key is configured', function (): void {
it('uses LLM with context when API key is configured', function (): void {
config(['services.anthropic.api_key' => 'test-key']);
BrentPrice::insert(
@@ -186,19 +291,42 @@ it('uses LLM when API key is configured', function (): void {
Http::fake([
'https://api.anthropic.com/*' => Http::response([
'content' => [
['text' => '{"direction":"rising","confidence":70,"reasoning":"Trend is up."}'],
],
'content' => [['type' => 'text', 'text' => '{"direction":"rising","confidence":70,"reasoning":"Trend is up."}']],
'stop_reason' => 'end_turn',
]),
]);
$prediction = $this->service->generatePrediction();
expect($prediction->source)->toBe(PredictionSource::LlmWithContext)
->and(PricePrediction::count())->toBe(1);
});
it('falls back to plain LLM when context method fails', function (): void {
config(['services.anthropic.api_key' => 'test-key']);
BrentPrice::insert(
collect(range(1, 20))->map(fn (int $i) => [
'date' => now()->subDays(20 - $i)->toDateString(),
'price_usd' => 75.0 + ($i * 0.8),
])->all()
);
Http::fake([
'https://api.anthropic.com/*' => Http::sequence()
->push([], 500)
->push([
'content' => [['text' => '{"direction":"rising","confidence":70,"reasoning":"Trend up."}']],
]),
]);
$prediction = $this->service->generatePrediction();
expect($prediction->source)->toBe(PredictionSource::Llm)
->and(PricePrediction::count())->toBe(1);
});
it('falls back to EWMA when LLM fails', function (): void {
it('falls back to EWMA when both LLM methods fail', function (): void {
config(['services.anthropic.api_key' => 'test-key']);
BrentPrice::insert(
@@ -227,131 +355,3 @@ 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);
});