First Commit
This commit is contained in:
Executable
+69
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\AiModel;
|
||||
use App\Models\AiProvider;
|
||||
use App\Services\Ai\AiServiceFactory;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use RuntimeException;
|
||||
|
||||
class AiAgentService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AiServiceFactory $aiServiceFactory
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Execute a prompt using the specified provider and model.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function execute(
|
||||
string $prompt,
|
||||
AiProvider $provider,
|
||||
AiModel $model,
|
||||
float $temperature = 0.7,
|
||||
int $maxTokens = 8192
|
||||
): array {
|
||||
$startTime = microtime(true);
|
||||
|
||||
$aiData = [
|
||||
'prompt' => $prompt,
|
||||
'temperature' => $temperature,
|
||||
'max_tokens' => $maxTokens,
|
||||
'model' => $model->slug,
|
||||
'api_key' => $provider->api_key,
|
||||
'base_url' => $provider->base_url,
|
||||
];
|
||||
|
||||
try {
|
||||
$service = $this->aiServiceFactory->make($provider);
|
||||
$result = $service->generatePrompt($aiData);
|
||||
} catch (ConnectionException|RuntimeException $e) {
|
||||
Log::error('AI Agent execution failed: ' . $e->getMessage());
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $e->getMessage(),
|
||||
'content' => '',
|
||||
'tokens_used' => null,
|
||||
'generation_time' => round(microtime(true) - $startTime, 3),
|
||||
'model' => $model->slug,
|
||||
'provider' => $provider->name,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'content' => $result['content'] ?? '',
|
||||
'tokens_used' => $result['tokens_used'] ?? null,
|
||||
'generation_time' => round(microtime(true) - $startTime, 3),
|
||||
'model' => $model->slug,
|
||||
'provider' => $provider->name,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user