<?php
// Fungsi untuk mengirim permintaan ke DeepSeek API
function chatAI($apiKey, $prompt, $model = "deepseek-chat") {
    $url = "https://api.deepseek.com/v1/chat/completions";
    
    // Data yang akan dikirim
    $data = [
        "model" => $model,
        "messages" => [
            ["role" => "user", "content" => $prompt]
        ],
        "temperature" => 0.0,
        "max_tokens" => 2000
    ];
    
    // Inisialisasi cURL
    $ch = curl_init($url);
    
    // Set header dan opsi cURL
    $headers = [
        "Content-Type: application/json",
        "Authorization: Bearer " . $apiKey
    ];
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    
    // Eksekusi permintaan
    $response = curl_exec($ch);
    
    // Cek error
    if (curl_errno($ch)) {
        return "Error: " . curl_error($ch);
    }
    
    curl_close($ch);
    
    // Decode response
    $result = json_decode($response, true);
    
    // Ambil konten jawaban
    if (isset($result['choices'][0]['message']['content'])) {
        return $result['choices'][0]['message']['content'];
    } else {
        return "Error: Tidak dapat mendapatkan respons dari API. " . print_r($result, true);
    }
}

// Contoh penggunaan
$apiKey = "sk-7b190cdf746e415cb146d37ab9fc737b"; // Ganti dengan API key DeepSeek Anda
$prompt = $_GET['prompt'];

$response = chatAI($apiKey, $prompt);

echo "<h2>Prompt:</h2>";
echo "<p>" . htmlspecialchars($prompt) . "</p>";
echo "<h2>Response:</h2>";
echo "<p>" . nl2br(htmlspecialchars($response)) . "</p>";
?>