Your IP : 216.73.216.84


Current Path : /home/h/e/l/helpink/www/
Upload File :
Current File : /home/h/e/l/helpink/www/bob.php

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
header('Content-Type: application/json; charset=utf-8');

/* ========= CONFIG ========= */
$OPENAI_API_KEY = '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';
$MAX_RESULTS = 5;
$MAX_QUESTION_LENGTH = 300;

/* ========= DB ========= */
$pdo = new PDO(
  "mysql:host=helpinkhb.mysql.db:3306;dbname=helpinkhb;charset=utf8mb4",
  "helpinkhb",
  "Weg14vgkHib",
  [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

/* ========= INPUT ========= */
$data = json_decode(file_get_contents('php://input'), true);
$question = trim($data['question'] ?? '');

if ($question === '' || strlen($question) > $MAX_QUESTION_LENGTH) {
  echo json_encode(["reply" => "Peux-tu reformuler ta demande plus simplement ?"]);
  exit;
}

/* ========= OPENAI (1 appel ultra court) ========= */
$prompt = <<<PROMPT
Tu es une IA d'analyse STRICTE pour le secteur social et le sans-abrisme à Bruxelles.

Réponds UNIQUEMENT en JSON.
Si hors sujet, mets "offtopic": true.

Champs attendus :
- offtopic (bool)
- intent (repas, logement, aide_sociale, soins, accueil)
- lieu (ou null)
- mots_cles (max 3)

Question :
"$question"
PROMPT;

$payload = [
  "model" => "gpt-4.1-mini",
  "temperature" => 0,
  "messages" => [
    ["role" => "system", "content" => "Analyse stricte, pas de texte libre."],
    ["role" => "user", "content" => $prompt]
  ]
];

$ch = curl_init("https://api.openai.com/v1/chat/completions");
curl_setopt_array($ch, [
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_POST => true,
  CURLOPT_HTTPHEADER => [
    "Authorization: Bearer $OPENAI_API_KEY",
    "Content-Type: application/json"
  ],
  CURLOPT_POSTFIELDS => json_encode($payload)
]);

$response = curl_exec($ch);
curl_close($ch);

$ai = json_decode($response, true);
$content = $ai['choices'][0]['message']['content'] ?? '';

$analysis = json_decode($content, true);

/* ========= OFFTOPIC ========= */
if (!$analysis || !empty($analysis['offtopic'])) {
  echo json_encode([
    "reply" => "Je suis là pour aider autour du logement, des repas, de l’accueil et du soutien social à Bruxelles."
  ]);
  exit;
}

/* ========= SQL ========= */
$where = [];
$params = [];

foreach ($analysis['mots_cles'] as $i => $kw) {
  $where[] = "(name LIKE :k$i OR description LIKE :k$i)";
  $params["k$i"] = "%$kw%";
}

$sql = "
SELECT 
  name,
  address,
  city,
  phone
FROM cpj7c_jbusinessdirectory_companies
WHERE approved = 2
  AND state = 1
  AND (" . implode(" OR ", $where) . ")
LIMIT $MAX_RESULTS
";


$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

/* ========= OUTPUT ========= */
if (!$results) {
  echo json_encode([
    "reply" => "Je n’ai rien trouvé exactement pour cette demande. Tu veux que je cherche autrement ?"
  ]);
  exit;
}

$out = "";
foreach ($results as $r) {
  $out .= "<b>{$r['name']}</b><br>";
  $out .= "{$r['address']} – {$r['city']}<br>";
  if (!empty($r['phone'])) {
    $out .= "📞 {$r['phone']}<br>";
  }
  $out .= "<br>";
}

echo json_encode(["reply" => $out]);