Files
moloapp/example/app/ai/OpenaiDemo.cpp
T

493 lines
21 KiB
C++

#include "../AiDemos.hpp"
#include <functional>
#include "../../fw/Router.hpp"
#include "../../fw/Templates.hpp"
#include "../../fw/Validate.hpp"
#include "llm/error.h"
#include "openai/core/caps.h"
#include "openai/core/chat_completions.h"
#include "openai/core/chat_model.h"
#include "openai/core/check.h"
#include "openai/core/client.h"
#include "openai/core/completions.h"
#include "openai/core/decisions.h"
#include "openai/core/embeddings.h"
#include "openai/core/images.h"
#include "openai/core/models.h"
#include "openai/core/moderations.h"
#include "openai/core/responses.h"
#include "openai/core/speech.h"
#include "openai/core/transcriptions.h"
#include "openai/core/videos.h"
#include "openai/core/wire/tool_schemas.h"
#include "security/crypto/Base64.h"
namespace molo::example::app {
// OpenAI core platform (src/openai/core): Responses, Chat Completions,
// Completions, Embeddings, Moderations, Models, Images, Audio, Videos.
// Routes live under /ai/openai. GET routes need no credential. Every route
// that calls OpenAI is a POST: bad input is 422 (validated before any
// network traffic), paid media and destructive routes are dry runs unless the
// body says "confirm":true, and a confirmed call without OPENAI_API_KEY is
// 503 not_configured.
namespace {
namespace oc = openai::core;
using Json_t = clients::body::Json;
const char kPage[] = R"OPENAI({% extends "base.html" %}
{% block content %}
<h1>OpenAI</h1>
<p class="hint">Responses, Chat Completions, embeddings, moderation, models,
images, speech, transcription and Sora videos through the OpenAI API. Paid
and destructive actions are dry runs until you tick <em>confirm</em>.</p>
<p id="o-config" class="hint">Checking OpenAI credentials...</p>
<section class="openai">
<label>Action
<select id="o-action">
<option value="responses">Responses: create</option>
<option value="chat">Chat Completions: create</option>
<option value="completions">Legacy completions</option>
<option value="embeddings">Embeddings</option>
<option value="moderations">Moderation</option>
<option value="models">Models: list</option>
<option value="tokens">Responses: count input tokens</option>
<option value="images">Image generation (paid)</option>
<option value="speech">Speech (paid)</option>
<option value="videos">Video, Sora (paid)</option>
</select></label>
<label>Model <input id="o-model" type="text" maxlength="128" placeholder="default for the action"></label>
<textarea id="o-prompt" rows="4" style="width:100%;box-sizing:border-box" maxlength="4000" placeholder="Prompt or input"></textarea>
<label><input id="o-confirm" type="checkbox"> confirm (spend money)</label>
<button id="o-run" type="button" class="primary">Run</button>
<p id="o-error" class="error" role="alert"></p>
<pre id="o-out" style="white-space:pre-wrap;word-break:break-word"></pre>
</section>
<script src="/embedded/ai_openai_core.js"></script>
{% endblock %}
)OPENAI";
constexpr std::size_t kMaxBody = 8u * 1024u * 1024u; // audio upload as base64
constexpr std::size_t kMaxShownString = 200000;
constexpr std::size_t kMaxInlineBytes = 1024u * 1024u;
[[noreturn]] void bad_input(const std::string& why) {
throw fw::HttpError(422, "validation_error", why);
}
Json_t 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_t::object();
try {
Json_t j = Json_t::parse(req.body);
if (!j.is_object()) bad_input("body must be a JSON object");
return j;
} catch (const clients::body::JsonError&) {
bad_input("body is not valid JSON");
}
}
std::string str_of(const Json_t& b, const char* key, bool required = false,
const std::string& fallback = "") {
const Json_t* v = b.find(key);
if (!v || v->is_null()) {
if (required) bad_input(std::string(key) + " is required");
return fallback;
}
if (!v->is_string()) bad_input(std::string(key) + " must be a string");
return v->as_string();
}
bool flag_of(const Json_t& b, const char* key) {
const Json_t* v = b.find(key);
if (!v || v->is_null()) return false;
if (!v->is_bool()) bad_input(std::string(key) + " must be true or false");
return v->as_bool();
}
std::optional<int> int_of(const Json_t& b, const char* key, int lo, int hi) {
const Json_t* v = b.find(key);
if (!v || v->is_null()) return std::nullopt;
if (!v->is_number()) bad_input(std::string(key) + " must be a number");
long n = v->as_int();
if (n < lo || n > hi) bad_input(std::string(key) + " must be " + std::to_string(lo) + ".." + std::to_string(hi));
return static_cast<int>(n);
}
fw::Response reply(const Json_t& j, int status = 200) {
fw::Response res = fw::JsonResponse(j.dump(), status);
res.headers.set("Cache-Control", "no-store");
return res;
}
// Long strings (images, audio) are summarised so a route never floods a page.
Json_t trimmed(const Json_t& j) {
if (j.is_string() && j.as_string().size() > kMaxShownString)
return Json_t("<" + std::to_string(j.as_string().size()) + " characters omitted>");
if (j.is_array()) {
Json_t a = Json_t::array();
for (const Json_t& v : j.as_array()) a.push(trimmed(v));
return a;
}
if (j.is_object()) {
Json_t o = Json_t::object();
for (const auto& [k, v] : j.as_object()) o.set(k, trimmed(v));
return o;
}
return j;
}
// A validated call: what would be sent, whether it costs money or destroys,
// and the call itself.
struct Plan {
Json_t preview;
bool gated = false;
std::function<Json_t()> run;
};
class OpenaiRoutes {
public:
explicit OpenaiRoutes(std::shared_ptr<llm::ApiClient> client)
: client_(std::move(client)), responses_(client_), chat_(client_), completions_(client_),
embeddings_(client_), moderations_(client_), models_(client_), images_(client_),
speech_(client_), transcriptions_(client_), videos_(client_) {}
bool configured() const { return client_->configured(); }
using Planner = std::function<Plan(const Json_t&)>;
fw::Response execute(const fw::Request& req, const Planner& planner) const {
Json_t body = parse_body(req);
Plan plan;
try {
plan = planner(body);
} catch (const llm::ApiError& e) {
throw map(e);
}
if (plan.gated && !flag_of(body, "confirm")) {
Json_t o = Json_t::object();
o.set("dry_run", Json_t(true));
o.set("request", trimmed(plan.preview));
o.set("hint", Json_t("send confirm:true to run it"));
return reply(o);
}
if (!configured())
throw fw::HttpError(503, "not_configured", "no OpenAI credential configured (set OPENAI_API_KEY)");
try {
return reply(trimmed(plan.run()));
} catch (const llm::ApiError& e) {
throw map(e);
}
}
static fw::HttpError map(const llm::ApiError& e) {
if (e.type() == "NOT_CONFIGURED") return fw::HttpError(503, "not_configured", e.message());
if (e.type() == "INVALID_ARGUMENT" && e.http_status() == 400)
return fw::HttpError(422, "validation_error", e.message());
return fw::HttpError(502, "openai_error",
std::to_string(e.http_status()) + " " + e.type() + ": " + e.message());
}
// --- planners ---------------------------------------------------------
Plan responses_create(const Json_t& b) const {
oc::ResponseRequest r;
r.model = str_of(b, "model", false, "gpt-4.1-mini");
r.input_text = str_of(b, "input", true);
r.instructions = str_of(b, "instructions");
r.reasoning.effort = str_of(b, "reasoning_effort");
r.max_output_tokens = int_of(b, "max_output_tokens", 16, 4096);
if (flag_of(b, "web_search")) r.tools.push_back(oc::ToolSchemas::web_search());
if (const Json_t* s = b.find("json_schema"); s && !s->is_null()) {
if (!s->is_object()) bad_input("json_schema must be a JSON Schema object");
r.text_format = oc::TextFormat::json_schema("result", *s);
}
Json_t preview = r.to_json(false);
return {preview, false, [this, r] { return responses_.create(r); }};
}
Plan responses_id(const Json_t& b, const char* verb) const {
std::string id = str_of(b, "id", true);
oc::Check::id("response", id);
Json_t preview = Json_t::object();
preview.set("id", Json_t(id));
std::string v = verb;
if (v == "get") return {preview, false, [this, id] { return responses_.retrieve(id); }};
if (v == "items") return {preview, false, [this, id] { return responses_.input_items(id); }};
if (v == "cancel") return {preview, true, [this, id] { return responses_.cancel(id); }};
return {preview, true, [this, id] { return responses_.remove(id); }};
}
Plan responses_tokens(const Json_t& b) const {
oc::TokenCountRequest r;
r.model = str_of(b, "model", false, "gpt-4.1-mini");
r.input_text = str_of(b, "input", true);
return {r.to_json(), false, [this, r] { return responses_.input_tokens(r); }};
}
Plan chat_create(const Json_t& b) const {
oc::ChatCompletionRequest r;
r.model = str_of(b, "model", false, "gpt-4.1-mini");
std::string system = str_of(b, "system");
if (!system.empty()) r.messages.push_back(oc::ChatMessages::system(system));
r.messages.push_back(oc::ChatMessages::user(str_of(b, "prompt", true)));
r.max_completion_tokens = int_of(b, "max_tokens", 1, 4096);
r.store = flag_of(b, "store") ? std::optional<bool>(true) : std::nullopt;
Json_t preview = r.to_json(false);
return {preview, false, [this, r] { return chat_.create(r); }};
}
Plan chat_stored(const Json_t& b) const {
std::string action = str_of(b, "action", true);
if (action != "get" && action != "list" && action != "messages") bad_input("action must be get, list or messages");
std::string id = action == "list" ? "" : str_of(b, "id", true);
if (!id.empty()) oc::Check::id("completion", id);
Json_t preview = Json_t::object();
preview.set("action", Json_t(action));
return {preview, false, [this, action, id] {
if (action == "get") return chat_.retrieve(id);
if (action == "messages") return chat_.messages(id);
oc::StoredListParams p;
p.page.limit = 20;
return chat_.list(p);
}};
}
Plan chat_delete(const Json_t& b) const {
std::string id = str_of(b, "id", true);
oc::Check::id("completion", id);
Json_t preview = Json_t::object();
preview.set("delete_completion", Json_t(id));
return {preview, true, [this, id] { return chat_.remove(id); }};
}
Plan completions_create(const Json_t& b) const {
oc::CompletionRequest r;
r.model = str_of(b, "model", false, "gpt-3.5-turbo-instruct");
r.prompt = str_of(b, "prompt", true);
r.max_tokens = int_of(b, "max_tokens", 1, 1024);
Json_t preview = r.to_json(false);
return {preview, false, [this, r] { return completions_.create(r); }};
}
Plan embeddings_create(const Json_t& b) const {
oc::EmbeddingRequest r;
r.model = str_of(b, "model", false, "text-embedding-3-small");
r.inputs = {str_of(b, "input", true)};
r.dimensions = int_of(b, "dimensions", 1, 3072);
Json_t preview = r.to_json();
return {preview, false, [this, r] {
oc::EmbeddingResult e = embeddings_.create(r);
Json_t o = Json_t::object();
o.set("model", Json_t(e.model));
o.set("prompt_tokens", Json_t(e.prompt_tokens));
Json_t dims = Json_t::array();
for (const auto& v : e.vectors) dims.push(Json_t(static_cast<int>(v.size())));
o.set("dimensions", dims);
Json_t first = Json_t::array();
if (!e.vectors.empty())
for (std::size_t i = 0; i < e.vectors[0].size() && i < 8; ++i)
first.push(Json_t(static_cast<double>(e.vectors[0][i])));
o.set("first_values", first);
return o;
}};
}
Plan moderations_create(const Json_t& b) const {
oc::ModerationRequest r;
r.model = str_of(b, "model");
r.texts = {str_of(b, "input", true)};
Json_t preview = r.to_json();
return {preview, false, [this, r] { return moderations_.create(r).raw; }};
}
Plan models_list(const Json_t& b) const {
std::string id = str_of(b, "id");
if (!id.empty()) oc::Check::id("model", id);
Json_t preview = Json_t::object();
preview.set("id", Json_t(id));
return {preview, false, [this, id] { return id.empty() ? models_.list() : models_.retrieve(id); }};
}
Plan models_delete(const Json_t& b) const {
std::string id = str_of(b, "id", true);
oc::Check::id("model", id);
if (id.rfind("ft:", 0) != 0) bad_input("only fine-tuned models (ft:...) can be deleted");
Json_t preview = Json_t::object();
preview.set("delete_model", Json_t(id));
return {preview, true, [this, id] { return models_.remove(id); }};
}
Plan images_generate(const Json_t& b) const {
oc::ImageGenerateRequest r;
r.model = str_of(b, "model", false, "gpt-image-1-mini");
r.prompt = str_of(b, "prompt", true);
r.size = str_of(b, "size");
r.quality = str_of(b, "quality", false, "low");
r.n = int_of(b, "n", 1, 4);
Json_t preview = r.to_json(false);
return {preview, true, [this, r] {
oc::ImagesResult out = images_.generate(r);
Json_t o = Json_t::object();
o.set("created", Json_t(static_cast<int>(out.created)));
Json_t items = Json_t::array();
for (const oc::ImageItem& i : out.data) {
Json_t it = Json_t::object();
it.set("url", Json_t(i.url));
std::string bytes = i.bytes();
it.set("bytes", Json_t(static_cast<int>(bytes.size())));
if (!bytes.empty() && bytes.size() <= kMaxInlineBytes) it.set("data_b64", Json_t(i.b64_json));
items.push(it);
}
o.set("images", items);
return o;
}};
}
Plan speech_create(const Json_t& b) const {
oc::SpeechRequest r;
r.model = str_of(b, "model", false, "gpt-4o-mini-tts");
r.input = str_of(b, "input", true);
r.voice = str_of(b, "voice", false, "alloy");
r.response_format = str_of(b, "format", false, "mp3");
Json_t preview = r.to_json(false);
return {preview, true, [this, r] {
oc::SpeechAudio a = speech_.speak(r);
Json_t o = Json_t::object();
o.set("content_type", Json_t(a.content_type));
o.set("bytes", Json_t(static_cast<int>(a.bytes.size())));
if (a.bytes.size() <= kMaxInlineBytes)
o.set("audio_b64", Json_t(security::crypto::Base64::encode(a.bytes)));
return o;
}};
}
Plan transcribe(const Json_t& b) const {
oc::TranscriptionRequest r;
r.model = str_of(b, "model", false, "gpt-4o-mini-transcribe");
std::string data;
if (!security::crypto::Base64::decode(str_of(b, "audio_b64", true), data)) bad_input("audio_b64 is not valid base64");
r.file = {str_of(b, "filename", false, "audio.mp3"), str_of(b, "content_type", false, "audio/mpeg"), data};
r.language = str_of(b, "language");
r.to_multipart();
Json_t preview = Json_t::object();
preview.set("model", Json_t(r.model));
preview.set("file_bytes", Json_t(static_cast<int>(data.size())));
return {preview, true, [this, r] {
oc::TextResult t = transcriptions_.transcribe(r);
Json_t o = Json_t::object();
o.set("text", Json_t(t.text));
return o;
}};
}
Plan videos_create(const Json_t& b) const {
oc::VideoCreateRequest r;
r.model = str_of(b, "model", false, "sora-2");
r.prompt = str_of(b, "prompt", true);
r.seconds = str_of(b, "seconds", false, "4");
r.size = str_of(b, "size", false, "720x1280");
Json_t preview = r.to_json();
return {preview, true, [this, r] { return videos_.create(r); }};
}
Plan videos_id(const Json_t& b, bool destructive) const {
std::string id = str_of(b, "id", true);
oc::Check::id("video", id);
Json_t preview = Json_t::object();
preview.set("id", Json_t(id));
if (destructive) return {preview, true, [this, id] { return videos_.remove(id); }};
return {preview, false, [this, id] { return videos_.retrieve(id); }};
}
Json_t status() const {
Json_t o = Json_t::object();
o.set("configured", Json_t(configured()));
o.set("base_url", Json_t(std::string(oc::OpenaiClient::kBaseUrl)));
o.set("calls", Json_t(static_cast<int>(oc::call_count())));
Json_t caps = Json_t::object();
caps.set("max_input_chars", Json_t(static_cast<int>(oc::CoreCaps::kMaxInputChars)));
caps.set("max_tools", Json_t(static_cast<int>(oc::CoreCaps::kMaxTools)));
caps.set("max_speech_chars", Json_t(static_cast<int>(oc::CoreCaps::kMaxSpeechChars)));
caps.set("max_audio_bytes", Json_t(static_cast<int>(oc::CoreCaps::kMaxAudioFileBytes)));
o.set("caps", caps);
return o;
}
private:
std::shared_ptr<llm::ApiClient> client_;
mutable oc::ResponsesService responses_;
mutable oc::ChatCompletionsService chat_;
mutable oc::CompletionsService completions_;
mutable oc::EmbeddingsService embeddings_;
mutable oc::ModerationsService moderations_;
mutable oc::ModelsService models_;
mutable oc::ImagesService images_;
mutable oc::SpeechService speech_;
mutable oc::TranscriptionsService transcriptions_;
mutable oc::VideosService videos_;
};
} // namespace
void register_ai_openai_demo(fw::Router& router, Services& services) {
auto routes = std::make_shared<OpenaiRoutes>(oc::OpenaiClient::from_env());
services.templates->add_template("ai_openai.html", kPage);
fw::Environment* env = services.templates.get();
services.add_feature("ai-openai", "/ai/openai");
router.get("/ai/openai", "ai-openai-page", [env](fw::Request) {
fw::TemplateCtx ctx;
ctx["title"] = std::string("OpenAI");
return fw::Response(200, env->render("ai_openai.html", ctx), "text/html; charset=utf-8");
});
router.get("/ai/openai/status", "ai-openai-status",
[routes](fw::Request) { return reply(routes->status()); });
router.get("/ai/openai/calls", "ai-openai-calls", [](fw::Request) {
Json_t a = Json_t::array();
for (std::size_t i = 0; i < oc::call_count(); ++i) {
const oc::CallSpec& c = oc::spec_at(i);
Json_t o = Json_t::object();
o.set("http", Json_t(std::string(c.http_method)));
o.set("path", Json_t(std::string(c.path)));
o.set("operation", Json_t(std::string(c.operation_id)));
a.push(o);
}
Json_t out = Json_t::object();
out.set("calls", a);
return reply(out);
});
using P = OpenaiRoutes;
auto add = [&](const char* path, const char* name, std::function<Plan(const P&, const Json_t&)> f) {
router.post(path, name, [routes, f](fw::Request req) {
return routes->execute(req, [&](const Json_t& b) { return f(*routes, b); });
});
};
add("/ai/openai/responses", "ai-openai-responses", [](const P& r, const Json_t& b) { return r.responses_create(b); });
add("/ai/openai/responses/get", "ai-openai-responses-get", [](const P& r, const Json_t& b) { return r.responses_id(b, "get"); });
add("/ai/openai/responses/input-items", "ai-openai-responses-items", [](const P& r, const Json_t& b) { return r.responses_id(b, "items"); });
add("/ai/openai/responses/tokens", "ai-openai-responses-tokens", [](const P& r, const Json_t& b) { return r.responses_tokens(b); });
add("/ai/openai/responses/cancel", "ai-openai-responses-cancel", [](const P& r, const Json_t& b) { return r.responses_id(b, "cancel"); });
add("/ai/openai/responses/delete", "ai-openai-responses-delete", [](const P& r, const Json_t& b) { return r.responses_id(b, "delete"); });
add("/ai/openai/chat", "ai-openai-chat", [](const P& r, const Json_t& b) { return r.chat_create(b); });
add("/ai/openai/chat/stored", "ai-openai-chat-stored", [](const P& r, const Json_t& b) { return r.chat_stored(b); });
add("/ai/openai/chat/delete", "ai-openai-chat-delete", [](const P& r, const Json_t& b) { return r.chat_delete(b); });
add("/ai/openai/completions", "ai-openai-completions", [](const P& r, const Json_t& b) { return r.completions_create(b); });
add("/ai/openai/embeddings", "ai-openai-embeddings", [](const P& r, const Json_t& b) { return r.embeddings_create(b); });
add("/ai/openai/moderations", "ai-openai-moderations", [](const P& r, const Json_t& b) { return r.moderations_create(b); });
add("/ai/openai/models", "ai-openai-models", [](const P& r, const Json_t& b) { return r.models_list(b); });
add("/ai/openai/models/delete", "ai-openai-models-delete", [](const P& r, const Json_t& b) { return r.models_delete(b); });
add("/ai/openai/images", "ai-openai-images", [](const P& r, const Json_t& b) { return r.images_generate(b); });
add("/ai/openai/speech", "ai-openai-speech", [](const P& r, const Json_t& b) { return r.speech_create(b); });
add("/ai/openai/transcribe", "ai-openai-transcribe", [](const P& r, const Json_t& b) { return r.transcribe(b); });
add("/ai/openai/videos", "ai-openai-videos", [](const P& r, const Json_t& b) { return r.videos_create(b); });
add("/ai/openai/videos/get", "ai-openai-videos-get", [](const P& r, const Json_t& b) { return r.videos_id(b, false); });
add("/ai/openai/videos/delete", "ai-openai-videos-delete", [](const P& r, const Json_t& b) { return r.videos_id(b, true); });
}
} // namespace molo::example::app