Files
wevads-platform/scripts/hamid-rag.php
2026-02-26 04:53:11 +01:00

28 lines
1.1 KiB
PHP
Executable File

<?php
header('Content-Type: application/json');
$query = $_POST['query'] ?? $_GET['query'] ?? '';
if (empty($query)) { echo json_encode(['error' => 'Query required']); exit; }
$pdo = new PDO('pgsql:host=localhost;dbname=adx_system', 'admin', 'admin123');
$words = array_filter(explode(' ', strtolower($query)), fn($w) => strlen($w) > 2);
$conds = array_map(fn($w) => "(LOWER(content) LIKE '%$w%')", $words);
$where = !empty($conds) ? implode(' OR ', $conds) : '1=1';
$mem = [];
foreach ($pdo->query("SELECT memory_type, content, importance FROM hamid_long_memory WHERE $where ORDER BY importance DESC LIMIT 5") as $r) {
$mem[] = $r;
}
$kb = [];
try {
$kbConds = array_map(fn($w) => "(LOWER(title) LIKE '%$w%' OR LOWER(content) LIKE '%$w%')", $words);
$kbWhere = !empty($kbConds) ? implode(' OR ', $kbConds) : '1=1';
foreach ($pdo->query("SELECT title, content FROM knowledge_base WHERE $kbWhere LIMIT 5") as $r) {
$kb[] = ['title' => $r['title'], 'content' => substr($r['content'], 0, 300)];
}
} catch (Exception $e) {}
echo json_encode(['success' => true, 'memory' => $mem, 'kb' => $kb, 'total' => count($mem) + count($kb)]);