76 lines
3.1 KiB
PHP
76 lines
3.1 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
// 1. Fetch plans from the database to create a knowledge base.
|
|
$knowledge_base = "";
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->query("SELECT name, speed_mbps, price_monthly, contract_length_months FROM plans ORDER BY price_monthly ASC");
|
|
$plans = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if ($plans) {
|
|
$knowledge_base .= "Here is a list of our current internet plans:\n\n";
|
|
foreach ($plans as $plan) {
|
|
$knowledge_base .= "- Plan Name: " . $plan['name'] . "\n";
|
|
$knowledge_base .= " - Speed: " . $plan['speed_mbps'] . " Mbps\n";
|
|
$knowledge_base .= " - Price: $" . $plan['price_monthly'] . " per month\n";
|
|
$knowledge_base .= " - Contract: " . $plan['contract_length_months'] . " months\n\n";
|
|
}
|
|
$knowledge_base .= "You are a helpful and friendly customer support assistant for a telecommunications company. Your goal is to answer customer questions based ONLY on the information provided in this knowledge base. Do not invent or assume any details. If the answer is not in the knowledge base, say 'I'm sorry, I don't have that information, but I can connect you with a human agent.' Keep your answers concise and to the point.";
|
|
}
|
|
} catch (PDOException $e) {
|
|
// In a real app, you'd log this error.
|
|
// For now, we'll proceed with an empty knowledge base on failure.
|
|
}
|
|
|
|
// 2. Get the user's question
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$user_question = $data['question'] ?? '';
|
|
|
|
if (empty($user_question)) {
|
|
echo json_encode(['error' => 'No question provided.']);
|
|
exit;
|
|
}
|
|
|
|
// 3. Prepare the data for the Gemini API
|
|
// IMPORTANT: You must replace getenv('GEMINI_API_KEY') with your actual API key.
|
|
$api_key = getenv('GEMINI_API_KEY');
|
|
$api_url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' . $api_key;
|
|
|
|
$payload = [
|
|
'contents' => [
|
|
[
|
|
'parts' => [
|
|
['text' => $knowledge_base . "\n\nCustomer Question: " . $user_question]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
|
|
// 4. Use cURL to make the API call
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $api_url);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // For local development only
|
|
|
|
$response = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
// 5. Process and return the response
|
|
if ($http_code === 200) {
|
|
$result = json_decode($response, true);
|
|
$ai_response = $result['candidates'][0]['content']['parts'][0]['text'] ?? 'I am unable to answer at this time.';
|
|
echo json_encode(['reply' => $ai_response]);
|
|
} else {
|
|
// Log the error response for debugging
|
|
error_log("Gemini API Error: HTTP " . $http_code . " - " . $response);
|
|
echo json_encode(['error' => 'Failed to get a response from the AI assistant. Please try again later.']);
|
|
}
|
|
|