mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
68 lines
2.1 KiB
PHP
68 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Anthropic\Services;
|
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use App\Classes\Exceptions\MalformedRequestException;
|
|
use App\Classes\Exceptions\ConnectionErrorException;
|
|
|
|
class CreatesClaudeResponseWithFiles
|
|
{
|
|
public function execute(string $userPrompt, array $fileIds)
|
|
{
|
|
try {
|
|
$content = [];
|
|
|
|
// User prompt
|
|
$content[] = [
|
|
'type' => 'text',
|
|
'text' => $userPrompt,
|
|
];
|
|
|
|
// Attach files
|
|
foreach ($fileIds as $fileId) {
|
|
$content[] = [
|
|
'type' => 'document',
|
|
'source' => [
|
|
'type' => 'file',
|
|
'file_id' => $fileId,
|
|
],
|
|
];
|
|
}
|
|
|
|
$response = Http::withHeaders([
|
|
'x-api-key' => config('anthropic.api_key'),
|
|
'anthropic-version' => config('anthropic.api_version', '2023-06-01'),
|
|
'anthropic-beta' => 'files-api-2025-04-14',
|
|
'Content-Type' => 'application/json',
|
|
])->post(config('anthropic.base_url') . '/v1/messages', [
|
|
'model' => config('anthropic.default_model', 'claude-sonnet-4-5-20250929'),
|
|
'max_tokens' => 1024,
|
|
'messages' => [
|
|
[
|
|
'role' => 'user',
|
|
'content' => $content,
|
|
],
|
|
],
|
|
]);
|
|
|
|
return $response->json();
|
|
}
|
|
catch (\Illuminate\Http\Client\ConnectionException $exception) {
|
|
$error = 'Failed to connect to Claude API';
|
|
throw new ConnectionErrorException(
|
|
$error,
|
|
$exception->getMessage(),
|
|
$userPrompt,
|
|
$exception->getTraceAsString()
|
|
);
|
|
}
|
|
catch (\Exception $exception) {
|
|
throw new MalformedRequestException(
|
|
'Unable to get correct response from Claude API: ' . $exception->getMessage()
|
|
);
|
|
}
|
|
}
|
|
}
|