/home/techb158/workloadmatch.com/Manager/Inc/LLMProviders
Edit: /home/techb158/workloadmatch.com/Manager/Inc/LLMProviders/OpenAIProvider.php (2166B)
apiKey = $apiKey;
$this->model = $model;
}
public function getName(): string {
return 'openai';
}
public function sendPrompt(string $systemPrompt, string $userPrompt, array $options = []): array {
$temperature = $options['temperature'] ?? 0.3;
$payload = [
'model' => $this->model,
'messages' => [
['role' => 'system', 'content' => $systemPrompt],
['role' => 'user', 'content' => $userPrompt],
],
'temperature' => $temperature,
'response_format' => ['type' => 'json_object'],
];
$ch = curl_init('https://api.openai.com/v1/chat/completions');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Bearer ' . $this->apiKey,
],
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
throw new RuntimeException("OpenAI API error: $error");
}
$data = json_decode($response, true);
if ($httpCode !== 200) {
$msg = $data['error']['message'] ?? 'Unknown error';
throw new RuntimeException("OpenAI API error ($httpCode): $msg");
}
$content = $data['choices'][0]['message']['content'] ?? '';
$parsed = json_decode($content, true);
if (!is_array($parsed)) {
throw new RuntimeException("OpenAI: Failed to parse JSON response: " . substr($content, 0, 200));
}
return $parsed;
}
}