105 lines
5.0 KiB
PHP
105 lines
5.0 KiB
PHP
<?php
|
|
header("Content-Type: application/json");
|
|
header("Access-Control-Allow-Origin: *");
|
|
|
|
$pdo = new PDO("pgsql:host=127.0.0.1;dbname=adx_system", "postgres", "");
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
$act = $_GET["action"] ?? $_POST["action"] ?? "";
|
|
$q = $_GET["q"] ?? $_POST["q"] ?? "";
|
|
|
|
// Search across ALL knowledge sources
|
|
if($act == "search") {
|
|
if(!$q) die(json_encode(["results" => []]));
|
|
$words = array_filter(explode(" ", preg_replace("/[^a-zA-ZÀ-ÿ0-9 ]/u","",strtolower($q))));
|
|
$keywords = array_filter($words, function($w){ return strlen($w) > 3; });
|
|
if(empty($keywords)) die(json_encode(["results" => []]));
|
|
|
|
$results = [];
|
|
|
|
// Source 1: knowledge_base (1529 entries)
|
|
$conds = []; $params = [];
|
|
foreach(array_slice(array_values($keywords),0,5) as $kw) {
|
|
$conds[] = "(LOWER(title) LIKE ? OR LOWER(content) LIKE ? OR LOWER(category) LIKE ?)";
|
|
$params[] = "%$kw%"; $params[] = "%$kw%"; $params[] = "%$kw%";
|
|
}
|
|
$sql = "SELECT title, substring(content,1,500) as content, category, 'knowledge_base' as source FROM admin.knowledge_base WHERE ".implode(" OR ",$conds)." ORDER BY views DESC LIMIT 5";
|
|
$stmt = $pdo->prepare($sql); $stmt->execute($params);
|
|
foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) $results[] = $r;
|
|
|
|
// Source 2: hamid_knowledge (32 entries - curated)
|
|
$conds2 = []; $params2 = [];
|
|
foreach(array_slice(array_values($keywords),0,5) as $kw) {
|
|
$conds2[] = "(LOWER(topic) LIKE ? OR LOWER(content) LIKE ?)";
|
|
$params2[] = "%$kw%"; $params2[] = "%$kw%";
|
|
}
|
|
$sql2 = "SELECT topic as title, substring(content,1,500) as content, category, 'hamid_knowledge' as source FROM admin.hamid_knowledge WHERE ".implode(" OR ",$conds2)." ORDER BY confidence DESC LIMIT 3";
|
|
$stmt2 = $pdo->prepare($sql2); $stmt2->execute($params2);
|
|
foreach($stmt2->fetchAll(PDO::FETCH_ASSOC) as $r) $results[] = $r;
|
|
|
|
// Source 3: weval_mind_knowledge (problem-solving)
|
|
$stmt3 = $pdo->query("SELECT problem_type as title, solution as content, 'mind_knowledge' as source FROM admin.weval_mind_knowledge WHERE success_rate > 0.5 ORDER BY success_rate DESC LIMIT 3");
|
|
foreach($stmt3->fetchAll(PDO::FETCH_ASSOC) as $r) $results[] = $r;
|
|
|
|
die(json_encode(["results" => $results, "total_sources" => count($results)]));
|
|
}
|
|
|
|
// Get user profile/memory across sessions
|
|
if($act == "user_memory") {
|
|
$session = $q;
|
|
// Get recent conversations
|
|
$stmt = $pdo->prepare("SELECT role, substring(message,1,200) as message FROM admin.chatbot_conversations WHERE session_id=? ORDER BY created_at DESC LIMIT 10");
|
|
$stmt->execute([$session]);
|
|
$convs = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Get conversation summaries
|
|
$stmt2 = $pdo->prepare("SELECT summary, category FROM admin.chatbot_conversations_history WHERE chat_id=? ORDER BY created_at DESC LIMIT 3");
|
|
$stmt2->execute([$session]);
|
|
$summaries = $stmt2->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Get learnings about this user
|
|
$stmt3 = $pdo->prepare("SELECT action, context::text FROM admin.weval_mind_learning WHERE context::text LIKE ? ORDER BY learned_at DESC LIMIT 5");
|
|
$stmt3->execute(["%$session%"]);
|
|
$learnings = $stmt3->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
die(json_encode(["conversations" => $convs, "summaries" => $summaries, "learnings" => $learnings]));
|
|
}
|
|
|
|
// Learn - store new knowledge from any interface
|
|
if($act == "learn") {
|
|
$data = json_decode(file_get_contents("php://input"), true) ?: $_POST;
|
|
$fact = $data["fact"] ?? "";
|
|
$category = $data["category"] ?? "learned";
|
|
$source = $data["source"] ?? "wevia";
|
|
$session = $data["session"] ?? "";
|
|
|
|
if($fact) {
|
|
$ctx = json_encode(["source" => $source, "session" => $session, "timestamp" => date("c")]);
|
|
$pdo->prepare("INSERT INTO admin.weval_mind_learning (action, success, context) VALUES (?,true,?::jsonb)")
|
|
->execute(["learn_$category", $ctx]);
|
|
die(json_encode(["status" => "learned", "fact" => $fact]));
|
|
}
|
|
die(json_encode(["error" => "empty fact"]));
|
|
}
|
|
|
|
// Stats
|
|
if($act == "stats") {
|
|
$stats = [];
|
|
$stats["knowledge_base"] = $pdo->query("SELECT count(*) FROM admin.knowledge_base")->fetchColumn();
|
|
$stats["hamid_knowledge"] = $pdo->query("SELECT count(*) FROM admin.hamid_knowledge")->fetchColumn();
|
|
$stats["mind_knowledge"] = $pdo->query("SELECT count(*) FROM admin.weval_mind_knowledge")->fetchColumn();
|
|
$stats["mind_learning"] = $pdo->query("SELECT count(*) FROM admin.weval_mind_learning")->fetchColumn();
|
|
$stats["conversations"] = $pdo->query("SELECT count(*) FROM admin.chatbot_conversations")->fetchColumn();
|
|
die(json_encode($stats));
|
|
}
|
|
|
|
// Web search proxy for servers that can't reach DDG directly
|
|
if($act == "websearch") {
|
|
if(!$q) die(json_encode(["results" => []]));
|
|
require_once __DIR__ . "/wevia-search.php";
|
|
$results = webSearch($q, 5);
|
|
die(json_encode(["results" => $results]));
|
|
}
|
|
|
|
echo json_encode(["status" => "ok", "actions" => ["search","user_memory","learn","stats","websearch"]]);
|