<?php

$msg = $_POST['msg'] ?? '';

$api_key = "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";

$data = [
    "model" => "llama-3.1-8b-instant",
    "messages" => [
        [
            "role" => "user",
            "content" => $msg
        ]
    ]
];

$ch = curl_init("https://api.groq.com/openai/v1/chat/completions");

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . $api_key,
        "Content-Type: application/json"
    ],
    CURLOPT_POSTFIELDS => json_encode($data)
]);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo "Curl error: " . curl_error($ch);
    exit;
}

curl_close($ch);

$result = json_decode($response, true);

echo $result['choices'][0]['message']['content'] ?? "No response from AI";
