First Commit
This commit is contained in:
Executable
+73
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Ai;
|
||||
|
||||
use App\Models\AiProvider;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class LlamaCppService implements AiServiceInterface
|
||||
{
|
||||
/**
|
||||
* @param AiProvider $provider
|
||||
*/
|
||||
public function __construct(private AiProvider $provider)
|
||||
{
|
||||
$this->client = new Client([
|
||||
'base_uri' => rtrim((string) $provider->base_url, '/') . '/',
|
||||
'timeout' => 120,
|
||||
]);
|
||||
}
|
||||
|
||||
public function chat(array $messages, array $data = []): array
|
||||
{
|
||||
$baseUrl = rtrim((string) $this->provider->base_url, '/');
|
||||
|
||||
if (empty($baseUrl)) {
|
||||
throw new \InvalidArgumentException('Llama.cpp base URL is not configured.');
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->client->post('v1/chat/completions', [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
'json' => [
|
||||
'model' => (string) ($data['model'] ?? ''),
|
||||
'messages' => $messages,
|
||||
'temperature' => isset($data['temperature']) ? (float) $data['temperature'] : null,
|
||||
'max_tokens' => isset($data['max_tokens']) ? (int) $data['max_tokens'] : null,
|
||||
],
|
||||
]);
|
||||
|
||||
$body = json_decode($response->getBody()->getContents(), true);
|
||||
|
||||
if (!isset($body['choices'][0]['message']['content'])) {
|
||||
throw new \RuntimeException('Invalid response structure from Llama.cpp server.');
|
||||
}
|
||||
|
||||
return [
|
||||
'content' => $body['choices'][0]['message']['content'],
|
||||
'tokens_used' => $body['usage']['total_tokens'] ?? null,
|
||||
'model' => $body['model'] ?? (string) ($data['model'] ?? ''),
|
||||
];
|
||||
} catch (GuzzleException $e) {
|
||||
Log::error('Llama.cpp API error: ' . $e->getMessage());
|
||||
throw new \RuntimeException('Failed to generate prompt with Llama.cpp: ' . $e->getMessage(), (int) $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function generatePrompt(array $data): array
|
||||
{
|
||||
return $this->chat(
|
||||
[['role' => 'user', 'content' => (string) ($data['prompt'] ?? '')]],
|
||||
$data
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user