Feature: Add configurable LLM endpoint for Claude-compatible alternatives
This commit is contained in:
@@ -17,8 +17,8 @@ class LLMService {
|
||||
private IConfig $config;
|
||||
private ?ILogger $logger;
|
||||
|
||||
// Anthropic API endpoint
|
||||
private const ANTHROPIC_API = 'https://api.anthropic.com/v1/messages';
|
||||
// Anthropic API endpoint (default)
|
||||
private const DEFAULT_API_ENDPOINT = 'https://api.anthropic.com/v1/messages';
|
||||
private const MAX_RETRIES = 3;
|
||||
private const RATE_LIMIT_DELAY = 12; // Seconds to wait between retries
|
||||
private const TIMEOUT_SECONDS = 30;
|
||||
@@ -44,17 +44,21 @@ class LLMService {
|
||||
|
||||
try {
|
||||
$apiKey = $this->config->getAppValue('anthropic_api_key', 'analyticshub');
|
||||
$apiEndpoint = $this->config->getAppValue('llm_api_endpoint', 'analyticshub', '');
|
||||
|
||||
if (empty($apiKey)) {
|
||||
throw new \Exception('Anthropic API key not configured');
|
||||
throw new \Exception('API key not configured');
|
||||
}
|
||||
|
||||
// Use configured endpoint or default to Anthropic
|
||||
$endpoint = !empty($apiEndpoint) ? $apiEndpoint : self::DEFAULT_API_ENDPOINT;
|
||||
|
||||
// Build prompt
|
||||
$systemPrompt = $this->buildSystemPrompt($client);
|
||||
$userPrompt = $this->buildUserPrompt($processedData);
|
||||
|
||||
// Call with retry
|
||||
$response = $this->callWithRetry($systemPrompt, $userPrompt, $apiKey);
|
||||
$response = $this->callWithRetry($systemPrompt, $userPrompt, $apiKey, $endpoint);
|
||||
|
||||
return $response;
|
||||
|
||||
@@ -120,12 +124,12 @@ PROMPT;
|
||||
/**
|
||||
* Call Claude API with retry logic
|
||||
*/
|
||||
private function callWithRetry(string $systemPrompt, string $userPrompt, string $apiKey): string {
|
||||
private function callWithRetry(string $systemPrompt, string $userPrompt, string $apiKey, string $endpoint): string {
|
||||
for ($attempt = 0; $attempt < self::MAX_RETRIES; $attempt++) {
|
||||
$this->logger->info("LLM API call attempt {$attempt}/" . self::MAX_RETRIES);
|
||||
|
||||
try {
|
||||
$response = $this->makeLLMRequest($systemPrompt, $userPrompt, $apiKey);
|
||||
$response = $this->makeLLMRequest($systemPrompt, $userPrompt, $apiKey, $endpoint);
|
||||
|
||||
// Validate response
|
||||
$this->validateResponse($response);
|
||||
@@ -180,9 +184,9 @@ PROMPT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make HTTP request to Anthropic API
|
||||
* Make HTTP request to LLM API
|
||||
*/
|
||||
private function makeLLMRequest(string $systemPrompt, string $userPrompt, string $apiKey): string {
|
||||
private function makeLLMRequest(string $systemPrompt, string $userPrompt, string $apiKey, string $endpoint): string {
|
||||
$ch = curl_init();
|
||||
|
||||
$payload = [
|
||||
@@ -197,7 +201,7 @@ PROMPT;
|
||||
]
|
||||
];
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, self::ANTHROPIC_API);
|
||||
curl_setopt($ch, CURLOPT_URL, $endpoint);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
|
||||
|
||||
Reference in New Issue
Block a user