Files
moloapp/example/assets/ai_retrieval.js
T

79 lines
3.4 KiB
JavaScript

// Retrieval page: thin client over /ai/retrieval/* (all limits server side).
(function () {
"use strict";
var $ = function (id) { return document.getElementById(id); };
var out = $("rt-out"), err = $("rt-error");
var templates = {
"embed": { provider: "voyage", texts: ["The quick brown fox", "A lazy dog"], input_type: "document" },
"rerank": { provider: "jina", query: "What is a vector database?", documents: ["Pinecone stores vectors.", "Bananas are yellow."], top_n: 1 },
"search": { provider: "tavily", query: "retrieval augmented generation", max_results: 3, recency: "month" },
"call": { provider: "qdrant", call: "get_collections", path: {}, query: {}, body: null }
};
var opSel = $("rt-op"), provSel = $("rt-provider"), callSel = $("rt-call");
Object.keys(templates).forEach(function (k) {
var o = document.createElement("option");
o.value = k; o.textContent = k;
opSel.appendChild(o);
});
function fill() {
var t = JSON.parse(JSON.stringify(templates[opSel.value]));
if (provSel.value) t.provider = provSel.value;
if (opSel.value === "call" && callSel.value) t.call = callSel.value;
$("rt-body").value = JSON.stringify(t, null, 1);
}
function loadCalls() {
callSel.innerHTML = "";
fetch("/ai/retrieval/calls?provider=" + encodeURIComponent(provSel.value)).then(function (r) { return r.json(); }).then(function (j) {
(j.calls || []).forEach(function (c) {
var o = document.createElement("option");
o.value = c.name;
o.textContent = c.name + " " + c.http + " " + c.path + (c.flags ? " [" + c.flags + "]" : "");
callSel.appendChild(o);
});
fill();
});
}
opSel.onchange = function () { fill(); };
provSel.onchange = loadCalls;
callSel.onchange = fill;
function send(confirm) {
var body;
try { body = JSON.parse($("rt-body").value || "{}"); }
catch (e) { err.textContent = "Body is not valid JSON"; return; }
if (confirm) body.confirm = true;
err.textContent = "";
out.textContent = "...";
fetch("/ai/retrieval/" + opSel.value, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
}).then(function (res) {
return res.text().then(function (t) {
var j;
try { j = JSON.parse(t); } catch (e) { j = { message: t }; }
if (!res.ok) { err.textContent = j.message || ("HTTP " + res.status); out.textContent = ""; }
else { out.textContent = JSON.stringify(j, null, 2); }
});
}).catch(function (e) { err.textContent = String(e); out.textContent = ""; });
}
$("rt-send").onclick = function () { send(false); };
$("rt-confirm").onclick = function () { send(true); };
fetch("/ai/retrieval/status").then(function (r) { return r.json(); }).then(function (s) {
var on = [], off = [];
s.providers.forEach(function (p) {
var o = document.createElement("option");
o.value = p.id; o.textContent = p.title;
provSel.appendChild(o);
(p.configured ? on : off).push(p.credential_env + (p.url_env ? "+" + p.url_env : ""));
});
$("rt-config").textContent = on.length
? "Configured: " + on.join(", ") + ". Calls are real; paid, writing and destructive ones need confirm."
: "No credential configured: calls answer 503 until one is set; dry runs still work (" + off.join(", ") + ").";
provSel.value = "voyage";
fill();
loadCalls();
});
})();