Files
moloapp/example/assets/ai_azure.js
T

73 lines
3.3 KiB
JavaScript

// Azure page: thin client over /ai/azure/* (all limits server side).
(function () {
"use strict";
var $ = function (id) { return document.getElementById(id); };
var out = $("az-out"), err = $("az-error");
var templates = {
"chat": { deployment: "gpt-4o", prompt: "Hello!", system: "Be brief.", max_tokens: 128, dialect: "chat" },
"embeddings": { deployment: "text-embedding-3-small", input: ["hello world"], dimensions: 256 },
"images": { deployment: "gpt-image-1", prompt: "A lighthouse at dawn", size: "1024x1024", n: 1 },
"speech": { deployment: "tts", input: "Hello from Azure.", voice: "alloy" },
"models": {},
"foundry": { action: "chat", model: "Phi-4", prompt: "Hello!", max_tokens: 64, extra_parameters: "drop" },
"safety": { action: "text", text: "I want to hurt myself.", categories: ["SelfHarm"] },
"entra": {},
"realtime": { action: "url", deployment: "gpt-realtime" },
"call": { operation: "ListVectorStores", deployment: "", ids: [], query: {}, body: null }
};
var sel = $("az-route"), opSel = $("az-op"), opLabel = $("az-op-label");
Object.keys(templates).forEach(function (k) {
var o = document.createElement("option");
o.value = k; o.textContent = k;
sel.appendChild(o);
});
function fill() {
var t = templates[sel.value];
if (sel.value === "call") t.operation = opSel.value || t.operation;
$("az-body").value = JSON.stringify(t, null, 1);
opLabel.style.display = sel.value === "call" ? "block" : "none";
}
sel.onchange = fill;
opSel.onchange = function () { templates.call.operation = opSel.value; fill(); };
fill();
fetch("/ai/azure/calls").then(function (r) { return r.json(); }).then(function (d) {
d.calls.filter(function (c) { return c.generic; }).forEach(function (c) {
var o = document.createElement("option");
o.value = c.name;
o.textContent = c.name + " (" + c.http + ")";
opSel.appendChild(o);
});
opSel.value = templates.call.operation;
});
function send(confirm) {
var body;
try { body = JSON.parse($("az-body").value || "{}"); }
catch (e) { err.textContent = "Body is not valid JSON"; return; }
if (confirm) body.confirm = true;
err.textContent = "";
out.textContent = "...";
fetch("/ai/azure/" + sel.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 = ""; });
}
$("az-send").onclick = function () { send(false); };
$("az-confirm").onclick = function () { send(true); };
fetch("/ai/azure/status").then(function (r) { return r.json(); }).then(function (s) {
$("az-config").textContent = s.configured
? "Credential found (" + s.auth + ", api version " + s.api_version + "). Calls are real; paid ones need confirm."
: "No endpoint or credential (AZURE_OPENAI_ENDPOINT plus AZURE_OPENAI_API_KEY or AZURE_TENANT_ID / AZURE_CLIENT_ID / AZURE_CLIENT_SECRET): calls answer 503 until configured; dry runs still work.";
});
})();