2026-10-25 09:14:26 +01:00
|
|
|
#include "../AiDemos.hpp"
|
|
|
|
|
|
2026-10-25 15:04:27 +01:00
|
|
|
#include <chrono>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <optional>
|
|
|
|
|
|
2026-10-25 09:14:26 +01:00
|
|
|
#include "../../fw/Router.hpp"
|
2026-10-25 15:04:27 +01:00
|
|
|
#include "clients/transports/http_transport.h"
|
|
|
|
|
#include "../../fw/Templates.hpp"
|
|
|
|
|
#include "llm/error.h"
|
|
|
|
|
#include "security/crypto/Base64.h"
|
|
|
|
|
#include "vendors/azure/audio.h"
|
|
|
|
|
#include "vendors/azure/chat_model.h"
|
|
|
|
|
#include "vendors/azure/check.h"
|
|
|
|
|
#include "vendors/azure/client.h"
|
|
|
|
|
#include "vendors/azure/embeddings.h"
|
|
|
|
|
#include "vendors/azure/foundry.h"
|
|
|
|
|
#include "vendors/azure/images.h"
|
|
|
|
|
#include "vendors/azure/models.h"
|
|
|
|
|
#include "vendors/azure/operations.h"
|
|
|
|
|
#include "vendors/azure/realtime.h"
|
|
|
|
|
#include "vendors/azure/safety.h"
|
2026-10-25 09:14:26 +01:00
|
|
|
|
|
|
|
|
namespace molo::example::app {
|
|
|
|
|
|
2026-10-25 15:04:27 +01:00
|
|
|
// Azure OpenAI / AI Foundry over the real src/vendors/azure part. GET routes
|
|
|
|
|
// need no credential. Every route that calls Azure is a POST: 422 on bad
|
|
|
|
|
// input, 503 not_configured without AZURE_OPENAI_ENDPOINT plus a credential
|
|
|
|
|
// (AZURE_OPENAI_API_KEY, or AZURE_TENANT_ID / AZURE_CLIENT_ID /
|
|
|
|
|
// AZURE_CLIENT_SECRET). Routes that cost money or change resources are dry
|
|
|
|
|
// runs that show the exact request (path, api-version, body) unless the body
|
|
|
|
|
// has "confirm":true.
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
namespace az = azure;
|
|
|
|
|
using Json = clients::body::Json;
|
|
|
|
|
|
|
|
|
|
constexpr std::size_t kMaxBody = 8 * 1024 * 1024;
|
|
|
|
|
constexpr std::size_t kMaxAudioInline = 256 * 1024; // bytes returned inline as base64
|
|
|
|
|
|
|
|
|
|
const char kPage[] = R"AZURE({% extends "base.html" %}
|
|
|
|
|
{% block content %}
|
|
|
|
|
<h1>Azure OpenAI and AI Foundry</h1>
|
|
|
|
|
<p class="hint">Deployments, v1 and dated API versions, key or Microsoft Entra auth, chat, responses,
|
|
|
|
|
embeddings, images, audio, realtime, assistants, batch, fine-tuning, files, vector stores, Foundry
|
|
|
|
|
model inference, content safety. Paid and changing calls are dry runs until the body says
|
|
|
|
|
<code>"confirm":true</code>.</p>
|
|
|
|
|
<p id="az-config" class="hint">Checking credential...</p>
|
|
|
|
|
<section class="az">
|
|
|
|
|
<label>Route
|
|
|
|
|
<select id="az-route"></select></label>
|
|
|
|
|
<label id="az-op-label">Operation (route call)
|
|
|
|
|
<select id="az-op"></select></label>
|
|
|
|
|
<label>Body (JSON object) <textarea id="az-body" rows="8"></textarea></label>
|
|
|
|
|
<div class="az-actions">
|
|
|
|
|
<button id="az-send" type="button" class="primary">Send</button>
|
|
|
|
|
<button id="az-confirm" type="button">Send with confirm</button>
|
|
|
|
|
</div>
|
|
|
|
|
<p id="az-error" class="error" role="alert"></p>
|
|
|
|
|
<pre id="az-out" class="az-out"></pre>
|
|
|
|
|
</section>
|
|
|
|
|
<style>
|
|
|
|
|
.az label{display:block;margin:.5rem 0}
|
|
|
|
|
.az select,.az textarea{width:100%;box-sizing:border-box}
|
|
|
|
|
.az-actions{display:flex;flex-wrap:wrap;gap:.5rem;margin:.5rem 0}
|
|
|
|
|
.az-out{overflow:auto;max-height:24rem;white-space:pre-wrap;word-break:break-word}
|
|
|
|
|
</style>
|
|
|
|
|
<script src="/embedded/ai_azure.js"></script>
|
|
|
|
|
{% endblock %}
|
|
|
|
|
)AZURE";
|
|
|
|
|
|
|
|
|
|
[[noreturn]] void bad(const std::string& why) { throw fw::HttpError(422, "validation_error", why); }
|
|
|
|
|
|
|
|
|
|
fw::Response json_out(const Json& j, int status = 200) {
|
|
|
|
|
fw::Response res = fw::JsonResponse(j.dump(), status);
|
|
|
|
|
res.headers.set("Cache-Control", "no-store");
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Json parse_body(const fw::Request& req) {
|
|
|
|
|
if (req.body.size() > kMaxBody) throw fw::HttpError(413, "payload_too_large", "body too large");
|
|
|
|
|
if (req.body.empty()) return Json::object();
|
|
|
|
|
try {
|
|
|
|
|
Json j = Json::parse(req.body);
|
|
|
|
|
if (!j.is_object()) bad("body must be a JSON object");
|
|
|
|
|
return j;
|
|
|
|
|
} catch (const clients::body::JsonError&) {
|
|
|
|
|
bad("body is not valid JSON");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string text(const Json& b, const char* key, bool required = false) {
|
|
|
|
|
const Json* v = b.find(key);
|
|
|
|
|
if (!v || v->is_null()) {
|
|
|
|
|
if (required) bad(std::string(key) + " is required");
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
if (!v->is_string()) bad(std::string(key) + " must be a string");
|
|
|
|
|
if (required && v->as_string().empty()) bad(std::string(key) + " is required");
|
|
|
|
|
return v->as_string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<int> number(const Json& b, const char* key) {
|
|
|
|
|
const Json* v = b.find(key);
|
|
|
|
|
if (!v || v->is_null()) return std::nullopt;
|
|
|
|
|
if (!v->is_number()) bad(std::string(key) + " must be a number");
|
|
|
|
|
return static_cast<int>(v->as_int());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool flag(const Json& b, const char* key) {
|
|
|
|
|
const Json* v = b.find(key);
|
|
|
|
|
if (!v || v->is_null()) return false;
|
|
|
|
|
if (!v->is_bool()) bad(std::string(key) + " must be true or false");
|
|
|
|
|
return v->as_bool();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> strings(const Json& b, const char* key) {
|
|
|
|
|
std::vector<std::string> out;
|
|
|
|
|
const Json* v = b.find(key);
|
|
|
|
|
if (!v || v->is_null()) return out;
|
|
|
|
|
if (v->is_string()) {
|
|
|
|
|
out.push_back(v->as_string());
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
if (!v->is_array()) bad(std::string(key) + " must be a string or an array of strings");
|
|
|
|
|
for (const Json& s : v->as_array()) {
|
|
|
|
|
if (!s.is_string()) bad(std::string(key) + " must be an array of strings");
|
|
|
|
|
out.push_back(s.as_string());
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Json usage_json(const llm::Usage& u) {
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("input_tokens", Json(u.input_tokens));
|
|
|
|
|
o.set("output_tokens", Json(u.output_tokens));
|
|
|
|
|
o.set("total_tokens", Json(u.total_tokens));
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Json dry_run(const Json& request, const std::string& path, const std::string& method) {
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("dry_run", Json(true));
|
|
|
|
|
o.set("would_send_to", Json(method + " " + path));
|
|
|
|
|
o.set("request", request);
|
|
|
|
|
o.set("hint", Json("send confirm:true to run it"));
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string auth_name(az::AuthKind k) {
|
|
|
|
|
switch (k) {
|
|
|
|
|
case az::AuthKind::kApiKey: return "api_key";
|
|
|
|
|
case az::AuthKind::kEntra: return "entra";
|
|
|
|
|
case az::AuthKind::kNone: break;
|
|
|
|
|
}
|
|
|
|
|
return "none";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// One route = validate into a Plan, then the generic runner decides dry run,
|
|
|
|
|
// credential, call and error mapping.
|
|
|
|
|
struct Plan {
|
|
|
|
|
Json request; // what would be sent (shown in a dry run)
|
|
|
|
|
std::string path; // path with api-version, for the dry-run message
|
|
|
|
|
std::string method = "POST";
|
|
|
|
|
bool needs_confirm = false; // paid or changes a resource
|
|
|
|
|
std::function<Json()> call;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AzureRoutes {
|
|
|
|
|
public:
|
|
|
|
|
AzureRoutes(std::shared_ptr<az::AzureClient> client, std::string config_error)
|
|
|
|
|
: c_(std::move(client)), config_error_(std::move(config_error)) {}
|
|
|
|
|
|
|
|
|
|
const az::AzureClient& client() const { return *c_; }
|
|
|
|
|
const std::string& config_error() const { return config_error_; }
|
|
|
|
|
|
|
|
|
|
using Planner = std::function<Plan(const Json&)>;
|
|
|
|
|
|
|
|
|
|
fw::Response run(const fw::Request& req, const Planner& planner) const {
|
|
|
|
|
Json body = parse_body(req);
|
|
|
|
|
Plan plan;
|
|
|
|
|
try {
|
|
|
|
|
plan = planner(body);
|
|
|
|
|
} catch (const llm::ApiError& e) {
|
|
|
|
|
map(e);
|
|
|
|
|
}
|
|
|
|
|
if (plan.needs_confirm && !flag(body, "confirm")) return json_out(dry_run(plan.request, plan.path, plan.method));
|
|
|
|
|
try {
|
|
|
|
|
return json_out(plan.call());
|
|
|
|
|
} catch (const llm::ApiError& e) {
|
|
|
|
|
map(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- planners -----------------------------------------------------------
|
|
|
|
|
Plan chat(const Json& b) const {
|
|
|
|
|
std::string dep = text(b, "deployment", true);
|
|
|
|
|
az::ChatModelConfig cfg;
|
|
|
|
|
std::string dialect = text(b, "dialect");
|
|
|
|
|
if (dialect == "responses") cfg.dialect = az::Dialect::kResponses;
|
|
|
|
|
else if (dialect == "foundry") cfg.dialect = az::Dialect::kFoundry;
|
|
|
|
|
else if (!dialect.empty() && dialect != "chat") bad("dialect must be chat, responses or foundry");
|
|
|
|
|
cfg.extra_parameters = text(b, "extra_parameters");
|
|
|
|
|
llm::ChatRequest r;
|
|
|
|
|
r.model = dep;
|
|
|
|
|
r.system = text(b, "system");
|
|
|
|
|
r.messages.push_back(llm::Message::user(text(b, "prompt", true)));
|
|
|
|
|
r.max_tokens = number(b, "max_tokens");
|
|
|
|
|
if (const Json* t = b.find("temperature"); t && t->is_number()) r.temperature = t->as_number();
|
|
|
|
|
auto model = std::make_shared<az::AzureChatModel>(c_, cfg);
|
|
|
|
|
Json body = model->wire_body(r, false);
|
|
|
|
|
Plan pl;
|
|
|
|
|
pl.request = body;
|
|
|
|
|
if (cfg.dialect == az::Dialect::kResponses) pl.path = path_of(az::Surface::kOpenAi, az::Op::kCreateResponse, {});
|
|
|
|
|
else if (cfg.dialect == az::Dialect::kFoundry)
|
|
|
|
|
pl.path = path_of(az::Surface::kFoundry, az::Op::kFoundryGetChatCompletions, {});
|
|
|
|
|
else pl.path = path_of(az::Surface::kOpenAi, az::Op::kCreateChatCompletion, {dep, {}});
|
|
|
|
|
pl.needs_confirm = true;
|
|
|
|
|
pl.call = [model, r] {
|
|
|
|
|
llm::ChatResponse out = model->generate(r);
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("id", Json(out.id));
|
|
|
|
|
o.set("model", Json(out.model));
|
|
|
|
|
o.set("finish_reason", Json(out.finish_reason));
|
|
|
|
|
o.set("text", Json(out.text()));
|
|
|
|
|
o.set("usage", usage_json(out.usage));
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Plan embeddings(const Json& b) const {
|
|
|
|
|
std::string dep = text(b, "deployment", true);
|
|
|
|
|
std::vector<std::string> input = strings(b, "input");
|
|
|
|
|
if (input.empty()) bad("input is required");
|
|
|
|
|
int dims = number(b, "dimensions").value_or(0);
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const std::string& s : input) arr.push(Json(s));
|
|
|
|
|
req.set("input", arr);
|
|
|
|
|
if (dims > 0) req.set("dimensions", Json(dims));
|
|
|
|
|
az::EmbeddingsService svc(c_);
|
|
|
|
|
Plan pl;
|
|
|
|
|
pl.request = svc.prepare_body(az::Op::kCreateEmbedding, {dep, {}}, req);
|
|
|
|
|
pl.path = path_of(az::Surface::kOpenAi, az::Op::kCreateEmbedding, {dep, {}});
|
|
|
|
|
pl.needs_confirm = true;
|
|
|
|
|
pl.call = [this, dep, input, dims] {
|
|
|
|
|
az::EmbeddingsService s(c_);
|
|
|
|
|
auto vectors = s.embed(dep, input, dims);
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("count", Json(static_cast<int>(vectors.size())));
|
|
|
|
|
o.set("dimensions", Json(static_cast<int>(vectors.empty() ? 0 : vectors[0].size())));
|
|
|
|
|
Json head = Json::array();
|
|
|
|
|
if (!vectors.empty())
|
|
|
|
|
for (std::size_t i = 0; i < vectors[0].size() && i < 8; ++i) head.push(Json(vectors[0][i]));
|
|
|
|
|
o.set("first_values", head);
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Plan images(const Json& b) const {
|
|
|
|
|
std::string dep = text(b, "deployment", true);
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("prompt", Json(text(b, "prompt", true)));
|
|
|
|
|
if (std::string size = text(b, "size"); !size.empty()) req.set("size", Json(size));
|
|
|
|
|
if (auto n = number(b, "n")) req.set("n", Json(*n));
|
|
|
|
|
if (std::string q = text(b, "quality"); !q.empty()) req.set("quality", Json(q));
|
|
|
|
|
az::ImagesService svc(c_);
|
|
|
|
|
Plan pl;
|
|
|
|
|
pl.request = svc.prepare_body(az::Op::kCreateImage, {dep, {}}, req);
|
|
|
|
|
pl.path = path_of(az::Surface::kOpenAi, az::Op::kCreateImage, {dep, {}});
|
|
|
|
|
pl.needs_confirm = true;
|
|
|
|
|
pl.call = [this, dep, req] {
|
|
|
|
|
az::ImagesService s(c_);
|
|
|
|
|
Json doc = s.generate(dep, req);
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
const Json* data = doc.find("data");
|
|
|
|
|
o.set("count", Json(data && data->is_array() ? static_cast<int>(data->as_array().size()) : 0));
|
|
|
|
|
Json items = Json::array();
|
|
|
|
|
if (data && data->is_array())
|
|
|
|
|
for (const Json& d : data->as_array()) {
|
|
|
|
|
Json x = Json::object();
|
|
|
|
|
if (const Json* u = d.find("url"); u && u->is_string()) x.set("url", *u);
|
|
|
|
|
if (const Json* p = d.find("revised_prompt"); p && p->is_string()) x.set("revised_prompt", *p);
|
|
|
|
|
if (const Json* j = d.find("b64_json"); j && j->is_string())
|
|
|
|
|
x.set("b64_json_chars", Json(static_cast<int>(j->as_string().size())));
|
|
|
|
|
items.push(x);
|
|
|
|
|
}
|
|
|
|
|
o.set("images", items);
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Plan speech(const Json& b) const {
|
|
|
|
|
std::string dep = text(b, "deployment", true);
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
req.set("input", Json(text(b, "input", true)));
|
|
|
|
|
req.set("voice", Json(text(b, "voice", true)));
|
|
|
|
|
if (std::string f = text(b, "response_format"); !f.empty()) req.set("response_format", Json(f));
|
|
|
|
|
az::AudioService svc(c_);
|
|
|
|
|
Plan pl;
|
|
|
|
|
pl.request = svc.prepare_body(az::Op::kCreateSpeech, {dep, {}}, req);
|
|
|
|
|
pl.path = path_of(az::Surface::kOpenAi, az::Op::kCreateSpeech, {dep, {}});
|
|
|
|
|
pl.needs_confirm = true;
|
|
|
|
|
pl.call = [this, dep, req] {
|
|
|
|
|
az::AudioService s(c_);
|
|
|
|
|
llm::RawResponse r = s.speech(dep, req);
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("content_type", Json(r.content_type));
|
|
|
|
|
o.set("bytes", Json(static_cast<int>(r.body.size())));
|
|
|
|
|
if (r.body.size() <= kMaxAudioInline)
|
|
|
|
|
o.set("audio_base64", Json(security::crypto::Base64::encode(r.body)));
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Plan models(const Json&) const {
|
|
|
|
|
return Plan{Json::object(), path_of(az::Surface::kOpenAi, az::Op::kListModels, {}), "GET", false, [this] {
|
|
|
|
|
az::ModelsService s(c_);
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const std::string& id : s.ids()) arr.push(Json(id));
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("models", arr);
|
|
|
|
|
return o;
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Plan foundry(const Json& b) const {
|
|
|
|
|
std::string action = text(b, "action", true);
|
|
|
|
|
if (action == "info") {
|
|
|
|
|
return Plan{Json::object(), path_of(az::Surface::kFoundry, az::Op::kFoundryGetModelInfo, {}), "GET", false, [this] {
|
|
|
|
|
az::FoundryService s(c_);
|
|
|
|
|
return s.info();
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
std::string extra = text(b, "extra_parameters");
|
|
|
|
|
if (action == "chat") {
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
Json msgs = Json::array();
|
|
|
|
|
if (std::string sys = text(b, "system"); !sys.empty()) {
|
|
|
|
|
Json m = Json::object();
|
|
|
|
|
m.set("role", Json("system"));
|
|
|
|
|
m.set("content", Json(sys));
|
|
|
|
|
msgs.push(m);
|
|
|
|
|
}
|
|
|
|
|
Json m = Json::object();
|
|
|
|
|
m.set("role", Json("user"));
|
|
|
|
|
m.set("content", Json(text(b, "prompt", true)));
|
|
|
|
|
msgs.push(m);
|
|
|
|
|
req.set("messages", msgs);
|
|
|
|
|
if (std::string model = text(b, "model"); !model.empty()) req.set("model", Json(model));
|
|
|
|
|
if (auto n = number(b, "max_tokens")) req.set("max_tokens", Json(*n));
|
|
|
|
|
az::FoundryService svc(c_);
|
|
|
|
|
Plan pl;
|
|
|
|
|
pl.request = svc.prepare_body(az::Op::kFoundryGetChatCompletions, {}, req);
|
|
|
|
|
pl.path = path_of(az::Surface::kFoundry, az::Op::kFoundryGetChatCompletions, {});
|
|
|
|
|
pl.needs_confirm = true;
|
|
|
|
|
pl.call = [this, req, extra] {
|
|
|
|
|
az::FoundryService s(c_);
|
|
|
|
|
Json doc = s.chat(req, extra);
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("model", doc.find("model") ? *doc.find("model") : Json(""));
|
|
|
|
|
std::string t;
|
|
|
|
|
if (const Json* ch = doc.find("choices"); ch && ch->is_array() && !ch->as_array().empty())
|
|
|
|
|
if (const Json* msg = ch->as_array()[0].find("message"))
|
|
|
|
|
if (const Json* c = msg->find("content"); c && c->is_string()) t = c->as_string();
|
|
|
|
|
o.set("text", Json(t));
|
|
|
|
|
if (const Json* u = doc.find("usage")) o.set("usage", *u);
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
if (action == "embeddings") {
|
|
|
|
|
std::vector<std::string> input = strings(b, "input");
|
|
|
|
|
if (input.empty()) bad("input is required");
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const std::string& s : input) arr.push(Json(s));
|
|
|
|
|
req.set("input", arr);
|
|
|
|
|
if (std::string model = text(b, "model"); !model.empty()) req.set("model", Json(model));
|
|
|
|
|
az::FoundryService svc(c_);
|
|
|
|
|
Plan pl;
|
|
|
|
|
pl.request = svc.prepare_body(az::Op::kFoundryGetEmbeddings, {}, req);
|
|
|
|
|
pl.path = path_of(az::Surface::kFoundry, az::Op::kFoundryGetEmbeddings, {});
|
|
|
|
|
pl.needs_confirm = true;
|
|
|
|
|
pl.call = [this, req, extra] {
|
|
|
|
|
az::FoundryService s(c_);
|
|
|
|
|
Json doc = s.embeddings(req, extra);
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
const Json* data = doc.find("data");
|
|
|
|
|
o.set("count", Json(data && data->is_array() ? static_cast<int>(data->as_array().size()) : 0));
|
|
|
|
|
if (const Json* u = doc.find("usage")) o.set("usage", *u);
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
bad("action must be info, chat or embeddings");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Plan safety(const Json& b) const {
|
|
|
|
|
std::string action = text(b, "action", true);
|
|
|
|
|
az::SafetyService svc(c_);
|
|
|
|
|
if (action == "blocklists") {
|
|
|
|
|
return Plan{Json::object(), path_of(az::Surface::kSafety, az::Op::kSafetyTextBlocklistsListTextBlocklists, {}),
|
|
|
|
|
"GET", false, [this] {
|
|
|
|
|
az::SafetyService s(c_);
|
|
|
|
|
return s.list_blocklists();
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
az::Op op;
|
|
|
|
|
if (action == "text") {
|
|
|
|
|
req.set("text", Json(text(b, "text", true)));
|
|
|
|
|
std::vector<std::string> cats = strings(b, "categories");
|
|
|
|
|
if (!cats.empty()) {
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const std::string& c : cats) arr.push(Json(c));
|
|
|
|
|
req.set("categories", arr);
|
|
|
|
|
}
|
|
|
|
|
op = az::Op::kSafetyTextOperationsAnalyzeText;
|
|
|
|
|
} else if (action == "image") {
|
|
|
|
|
Json img = Json::object();
|
|
|
|
|
img.set("content", Json(text(b, "content_base64", true)));
|
|
|
|
|
req.set("image", img);
|
|
|
|
|
op = az::Op::kSafetyImageOperationsAnalyzeImage;
|
|
|
|
|
} else if (action == "shield") {
|
|
|
|
|
if (std::string p = text(b, "user_prompt"); !p.empty()) req.set("userPrompt", Json(p));
|
|
|
|
|
std::vector<std::string> docs = strings(b, "documents");
|
|
|
|
|
if (!docs.empty()) {
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const std::string& d : docs) arr.push(Json(d));
|
|
|
|
|
req.set("documents", arr);
|
|
|
|
|
}
|
|
|
|
|
if (!req.has("userPrompt") && !req.has("documents")) bad("user_prompt or documents is required");
|
|
|
|
|
op = az::Op::kSafetyTextOperationsShieldPrompt;
|
|
|
|
|
} else if (action == "protected") {
|
|
|
|
|
req.set("text", Json(text(b, "text", true)));
|
|
|
|
|
op = az::Op::kSafetyTextOperationsDetectTextProtectedMaterial;
|
|
|
|
|
} else {
|
|
|
|
|
bad("action must be text, image, shield, protected or blocklists");
|
|
|
|
|
}
|
|
|
|
|
Plan pl;
|
|
|
|
|
pl.request = svc.prepare_body(op, {}, req);
|
|
|
|
|
pl.path = path_of(az::Surface::kSafety, op, {});
|
|
|
|
|
pl.needs_confirm = true;
|
|
|
|
|
pl.call = [this, action, req] {
|
|
|
|
|
az::SafetyService s(c_);
|
|
|
|
|
if (action == "text") return s.analyze_text(req);
|
|
|
|
|
if (action == "image") return s.analyze_image(req);
|
|
|
|
|
if (action == "shield") return s.shield_prompt(req);
|
|
|
|
|
return s.detect_protected_material(req.find("text")->as_string());
|
|
|
|
|
};
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Plan entra(const Json&) const {
|
|
|
|
|
return Plan{Json::object(), c_->settings().entra.complete() ? c_->settings().entra.token_url() : "", "POST", false,
|
|
|
|
|
[this] {
|
|
|
|
|
if (!c_->tokens())
|
|
|
|
|
throw llm::NotConfiguredError("azure", "AZURE_TENANT_ID / AZURE_CLIENT_ID / AZURE_CLIENT_SECRET");
|
|
|
|
|
c_->tokens()->token(); // the token itself is never returned
|
|
|
|
|
std::int64_t now = std::chrono::duration_cast<std::chrono::seconds>(
|
|
|
|
|
std::chrono::system_clock::now().time_since_epoch())
|
|
|
|
|
.count();
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("ok", Json(true));
|
|
|
|
|
o.set("expires_in_seconds", Json(static_cast<int>(c_->tokens()->expires_at() - now)));
|
|
|
|
|
o.set("exchanges", Json(static_cast<int>(c_->tokens()->exchanges())));
|
|
|
|
|
o.set("scope", Json(c_->settings().entra.scope));
|
|
|
|
|
return o;
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Plan realtime(const Json& b) const {
|
|
|
|
|
std::string action = text(b, "action", true);
|
|
|
|
|
std::string dep = text(b, "deployment");
|
|
|
|
|
if (action == "url") {
|
|
|
|
|
if (dep.empty()) bad("deployment is required");
|
|
|
|
|
az::Check::deployment(dep);
|
|
|
|
|
return Plan{Json::object(), "", "GET", false, [this, dep] {
|
|
|
|
|
if (c_->settings().endpoint.empty())
|
|
|
|
|
throw llm::NotConfiguredError("azure", az::Settings::kEndpointEnv);
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("url", Json(az::RealtimeUrl::build(c_->settings(), dep)));
|
|
|
|
|
o.set("auth_header", Json(c_->auth() == az::AuthKind::kEntra ? "Authorization: Bearer <entra token>"
|
|
|
|
|
: "api-key: <resource key>"));
|
|
|
|
|
o.set("note", Json("the credential travels in a header, never in the URL"));
|
|
|
|
|
return o;
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
if (action == "secret") {
|
|
|
|
|
Json req = Json::object();
|
|
|
|
|
Json session = Json::object();
|
|
|
|
|
session.set("type", Json("realtime"));
|
|
|
|
|
if (!dep.empty()) session.set("model", Json(dep));
|
|
|
|
|
req.set("session", session);
|
|
|
|
|
if (auto s = number(b, "seconds")) {
|
|
|
|
|
Json ea = Json::object();
|
|
|
|
|
ea.set("anchor", Json("created_at"));
|
|
|
|
|
ea.set("seconds", Json(*s));
|
|
|
|
|
req.set("expires_after", ea);
|
|
|
|
|
}
|
|
|
|
|
az::RealtimeService svc(c_);
|
|
|
|
|
Plan pl;
|
|
|
|
|
pl.request = svc.prepare_body(az::Op::kCreateRealtimeClientSecret, {}, req);
|
|
|
|
|
pl.path = path_of(az::Surface::kOpenAi, az::Op::kCreateRealtimeClientSecret, {});
|
|
|
|
|
pl.needs_confirm = true;
|
|
|
|
|
pl.call = [this, req] {
|
|
|
|
|
az::RealtimeService s(c_);
|
|
|
|
|
Json doc = s.create_client_secret(req);
|
|
|
|
|
// The secret is handed to the caller exactly once; report that it exists, not its value.
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("created", Json(true));
|
|
|
|
|
o.set("expires_at", doc.find("expires_at") ? *doc.find("expires_at") : Json(0));
|
|
|
|
|
o.set("secret_chars", Json(doc.find("value") && doc.find("value")->is_string()
|
|
|
|
|
? static_cast<int>(doc.find("value")->as_string().size())
|
|
|
|
|
: 0));
|
|
|
|
|
return o;
|
|
|
|
|
};
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
bad("action must be url or secret");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Any table operation: assistants, threads, batch, fine-tuning, files (JSON),
|
|
|
|
|
// vector stores, containers, conversations, evals, models, ...
|
|
|
|
|
Plan call(const Json& b) const {
|
|
|
|
|
std::string name = text(b, "operation", true);
|
|
|
|
|
az::Target t;
|
|
|
|
|
t.deployment = text(b, "deployment");
|
|
|
|
|
t.ids = strings(b, "ids");
|
|
|
|
|
std::map<std::string, std::string> query;
|
|
|
|
|
if (const Json* q = b.find("query"); q && q->is_object())
|
|
|
|
|
for (const auto& [k, v] : q->as_object()) {
|
|
|
|
|
if (!v.is_string()) bad("query values must be strings");
|
|
|
|
|
query[k] = v.as_string();
|
|
|
|
|
}
|
|
|
|
|
Json body;
|
|
|
|
|
const Json* bp = nullptr;
|
|
|
|
|
if (const Json* x = b.find("body"); x && !x->is_null()) {
|
|
|
|
|
if (!x->is_object()) bad("body must be a JSON object");
|
|
|
|
|
body = *x;
|
|
|
|
|
bp = &body;
|
|
|
|
|
}
|
|
|
|
|
auto ops = std::make_shared<az::OperationsService>(c_);
|
|
|
|
|
az::OperationsService::Plan p = ops->plan(name, t, bp, query);
|
|
|
|
|
Plan pl;
|
|
|
|
|
pl.request = p.has_body ? p.body : Json::object();
|
|
|
|
|
pl.path = p.path;
|
|
|
|
|
pl.method = p.method;
|
|
|
|
|
pl.needs_confirm = p.method != "GET";
|
|
|
|
|
pl.call = [ops, name, t, body, bp, query] { return ops->invoke(name, t, bp ? &body : nullptr, query); };
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::string path_of(az::Surface s, az::Op op, const az::Target& t) const {
|
|
|
|
|
az::AzureService g(c_, s);
|
|
|
|
|
return g.request_path(op, t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[noreturn]] static void map(const llm::ApiError& e) {
|
|
|
|
|
if (e.http_status() == 503 && e.type() == "NOT_CONFIGURED")
|
|
|
|
|
throw fw::HttpError(503, "not_configured", e.message());
|
|
|
|
|
if (e.type() == "INVALID_ARGUMENT" && e.http_status() == 400 && e.request_id().empty())
|
|
|
|
|
throw fw::HttpError(422, "validation_error", e.message());
|
|
|
|
|
throw fw::HttpError(502, "azure_error", e.type() + ": " + e.message());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<az::AzureClient> c_;
|
|
|
|
|
std::string config_error_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2026-10-25 09:14:26 +01:00
|
|
|
void register_ai_azure_demo(fw::Router& router, Services& services) {
|
2026-10-25 15:04:27 +01:00
|
|
|
std::shared_ptr<az::AzureClient> client;
|
|
|
|
|
std::string config_error;
|
|
|
|
|
try {
|
|
|
|
|
client = az::AzureClient::from_env();
|
|
|
|
|
} catch (const llm::ApiError& e) {
|
|
|
|
|
// A malformed AZURE_* value must not stop the app; the status route says so.
|
|
|
|
|
config_error = e.message();
|
|
|
|
|
client = az::AzureClient::create(az::Settings{}, [](az::Channel) -> std::unique_ptr<clients::Transport> {
|
|
|
|
|
return std::make_unique<clients::transports::HttpTransport>();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
auto routes = std::make_shared<AzureRoutes>(client, config_error);
|
|
|
|
|
fw::Environment* env = services.templates.get();
|
|
|
|
|
services.templates->add_template("ai_azure.html", kPage);
|
|
|
|
|
services.add_feature("ai-azure", "/ai/azure");
|
|
|
|
|
|
|
|
|
|
router.get("/ai/azure", "ai-azure-page", [env](fw::Request req) {
|
|
|
|
|
(void)req;
|
|
|
|
|
fw::TemplateCtx ctx;
|
|
|
|
|
ctx["title"] = std::string("Azure OpenAI and AI Foundry");
|
|
|
|
|
return fw::Response(200, env->render("ai_azure.html", ctx), "text/html; charset=utf-8");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.get("/ai/azure/status", "ai-azure-status", [routes](fw::Request req) {
|
|
|
|
|
(void)req;
|
|
|
|
|
const az::Settings& s = routes->client().settings();
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("configured", Json(routes->client().configured()));
|
|
|
|
|
o.set("auth", Json(auth_name(routes->client().auth())));
|
|
|
|
|
o.set("api_version", Json(s.api_version.value()));
|
|
|
|
|
o.set("endpoint", Json(s.endpoint));
|
|
|
|
|
o.set("config_error", Json(routes->config_error()));
|
|
|
|
|
Json env_names = Json::array();
|
|
|
|
|
for (const char* n : {az::Settings::kEndpointEnv, az::Settings::kKeyEnv, az::Settings::kVersionEnv,
|
|
|
|
|
az::Settings::kTenantEnv, az::Settings::kClientIdEnv, az::Settings::kClientSecretEnv})
|
|
|
|
|
env_names.push(Json(n));
|
|
|
|
|
o.set("credential_env", env_names);
|
|
|
|
|
Json surfaces = Json::object();
|
|
|
|
|
surfaces.set("openai", Json(routes->client().configured(az::Surface::kOpenAi)));
|
|
|
|
|
surfaces.set("foundry", Json(routes->client().configured(az::Surface::kFoundry)));
|
|
|
|
|
surfaces.set("safety", Json(routes->client().configured(az::Surface::kSafety)));
|
|
|
|
|
o.set("surfaces", surfaces);
|
|
|
|
|
o.set("calls", Json(static_cast<int>(az::Calls::count())));
|
|
|
|
|
o.set("dated_fallback_version", Json(az::ApiVersion::kDatedFallback));
|
|
|
|
|
o.set("foundry_version", Json(s.foundry_version));
|
|
|
|
|
o.set("safety_version", Json(s.safety_version));
|
|
|
|
|
return json_out(o);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.get("/ai/azure/calls", "ai-azure-calls", [](fw::Request req) {
|
|
|
|
|
(void)req;
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (std::size_t i = 0; i < az::Calls::count(); ++i) {
|
|
|
|
|
const az::CallSpec& c = az::Calls::at(i);
|
|
|
|
|
Json row = Json::object();
|
|
|
|
|
row.set("name", Json(c.name));
|
|
|
|
|
row.set("surface", Json(c.surface == az::Surface::kOpenAi ? "openai"
|
|
|
|
|
: c.surface == az::Surface::kFoundry ? "foundry" : "safety"));
|
|
|
|
|
row.set("http", Json(c.method));
|
|
|
|
|
row.set("v1_path", Json(c.v1.path));
|
|
|
|
|
row.set("dated_path", Json(c.dated.path));
|
|
|
|
|
row.set("deployment", Json(c.deployment));
|
|
|
|
|
row.set("destructive", Json(az::OperationsService::destructive(c)));
|
|
|
|
|
row.set("generic", Json(az::OperationsService::generic(c)));
|
|
|
|
|
arr.push(row);
|
|
|
|
|
}
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("calls", arr);
|
|
|
|
|
return json_out(o);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
using Member = AzureRoutes::Planner;
|
|
|
|
|
auto add = [&](const char* path, const char* name, Member planner) {
|
|
|
|
|
router.post(path, name, [routes, planner](fw::Request req) { return routes->run(req, planner); });
|
|
|
|
|
};
|
|
|
|
|
AzureRoutes* r = routes.get();
|
|
|
|
|
auto bind = [r](Plan (AzureRoutes::*m)(const Json&) const) {
|
|
|
|
|
return Member([r, m](const Json& b) { return (r->*m)(b); });
|
|
|
|
|
};
|
|
|
|
|
add("/ai/azure/chat", "ai-azure-chat", bind(&AzureRoutes::chat));
|
|
|
|
|
add("/ai/azure/embeddings", "ai-azure-embeddings", bind(&AzureRoutes::embeddings));
|
|
|
|
|
add("/ai/azure/images", "ai-azure-images", bind(&AzureRoutes::images));
|
|
|
|
|
add("/ai/azure/speech", "ai-azure-speech", bind(&AzureRoutes::speech));
|
|
|
|
|
add("/ai/azure/models", "ai-azure-models", bind(&AzureRoutes::models));
|
|
|
|
|
add("/ai/azure/foundry", "ai-azure-foundry", bind(&AzureRoutes::foundry));
|
|
|
|
|
add("/ai/azure/safety", "ai-azure-safety", bind(&AzureRoutes::safety));
|
|
|
|
|
add("/ai/azure/entra", "ai-azure-entra", bind(&AzureRoutes::entra));
|
|
|
|
|
add("/ai/azure/realtime", "ai-azure-realtime", bind(&AzureRoutes::realtime));
|
|
|
|
|
add("/ai/azure/call", "ai-azure-call", bind(&AzureRoutes::call));
|
2026-10-25 09:14:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace molo::example::app
|