Merge openai platform package
This commit is contained in:
@@ -1,13 +1,500 @@
|
||||
#include "../AiDemos.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "../../fw/Router.hpp"
|
||||
#include "../../fw/Templates.hpp"
|
||||
#include "llm/config.h"
|
||||
#include "llm/error.h"
|
||||
#include "openai/platform/batches_api.h"
|
||||
#include "openai/platform/caps.h"
|
||||
#include "openai/platform/event_socket.h"
|
||||
#include "openai/platform/platform.h"
|
||||
#include "openai/platform/uploads_api.h"
|
||||
|
||||
namespace molo::example::app {
|
||||
|
||||
// OpenAI: realtime, files, uploads, batch, fine-tuning, vector stores, evals, conversations, containers, assistants. Stub: the module agent fills this in.
|
||||
// OpenAI platform: realtime, files, uploads, batch, fine-tuning, vector
|
||||
// stores, evals, conversations, containers, assistants, ChatKit, agents,
|
||||
// skills, vaults, live. One generic POST (/ai/openai/platform/call) runs any
|
||||
// call of the table (src/openai/platform/calls_table.inc); the other POSTs
|
||||
// are local helpers (event builder / parser, batch file, upload plan) or the
|
||||
// WebSocket session. Paid and destructive calls are dry runs unless the body
|
||||
// says "confirm":true. No credential: 503 not_configured on every route that
|
||||
// would call OpenAI; GET page, /status, /calls and /events answer 200.
|
||||
|
||||
namespace {
|
||||
|
||||
namespace oa = openai::platform;
|
||||
using Json = clients::body::Json;
|
||||
|
||||
constexpr std::size_t kMaxBody = 256 * 1024;
|
||||
constexpr int kMaxSocketSeconds = 30;
|
||||
constexpr std::size_t kMaxSocketEventsShown = 200;
|
||||
constexpr std::size_t kMaxClientEvents = 20;
|
||||
|
||||
const char kPage[] = R"OAIP({% extends "base.html" %}
|
||||
{% block content %}
|
||||
<h1>OpenAI platform</h1>
|
||||
<p class="hint">Files, uploads, batch, fine-tuning, vector stores, evals, conversations,
|
||||
containers, assistants, ChatKit, agents, skills, vaults, realtime and live. Paid and
|
||||
destructive calls are dry runs until you tick confirm.</p>
|
||||
<p id="oap-config" class="hint">Checking credentials...</p>
|
||||
<section class="oap">
|
||||
<h2>Call</h2>
|
||||
<label>Area <select id="oap-area"></select></label>
|
||||
<label>Operation <select id="oap-op"></select></label>
|
||||
<p id="oap-info" class="hint"></p>
|
||||
<label>Ids (JSON object) <textarea id="oap-ids" rows="2">{}</textarea></label>
|
||||
<label>Query (JSON object) <textarea id="oap-query" rows="2">{}</textarea></label>
|
||||
<label>Body (JSON object) <textarea id="oap-body" rows="5">{}</textarea></label>
|
||||
<label class="oap-check"><input id="oap-confirm" type="checkbox"> confirm (run paid or destructive calls)</label>
|
||||
<div class="oap-actions"><button id="oap-send" type="button" class="primary">Send</button></div>
|
||||
<h2>Realtime event</h2>
|
||||
<label>Profile <select id="oap-profile">
|
||||
<option value="ga">ga</option><option value="beta">beta</option><option value="translation">translation</option>
|
||||
<option value="live">live</option><option value="live_fork">live_fork</option><option value="live_sideband">live_sideband</option>
|
||||
</select></label>
|
||||
<label>Client event type <input id="oap-etype" type="text" maxlength="128" value="session.update"></label>
|
||||
<label>Fields (JSON object) <textarea id="oap-efields" rows="3">{"session":{"type":"realtime"}}</textarea></label>
|
||||
<div class="oap-actions">
|
||||
<button id="oap-ebuild" type="button">Build and validate</button>
|
||||
<button id="oap-ecatalog" type="button">Show catalog</button>
|
||||
</div>
|
||||
<label>Server event text <textarea id="oap-etext" rows="3">{"type":"session.created","event_id":"event_1"}</textarea></label>
|
||||
<div class="oap-actions"><button id="oap-eparse" type="button">Parse server event</button></div>
|
||||
<p id="oap-error" class="error" role="alert"></p>
|
||||
<pre id="oap-out" class="oap-out"></pre>
|
||||
</section>
|
||||
<style>
|
||||
.oap label{display:block;margin:.5rem 0}
|
||||
.oap input[type=text],.oap select,.oap textarea{width:100%;box-sizing:border-box}
|
||||
.oap-check{display:flex;gap:.5rem;align-items:center}
|
||||
.oap-actions{display:flex;flex-wrap:wrap;gap:.5rem;margin:.5rem 0}
|
||||
.oap-out{overflow:auto;max-height:24rem;white-space:pre-wrap;word-break:break-word}
|
||||
</style>
|
||||
<script src="/embedded/ai_openai_platform.js"></script>
|
||||
{% endblock %}
|
||||
)OAIP";
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
[[noreturn]] void bad(const std::string& why) { throw fw::HttpError(422, "validation_error", why); }
|
||||
|
||||
[[noreturn]] void rethrow(const llm::ApiError& e) {
|
||||
if (e.http_status() == 503 && e.type() == "NOT_CONFIGURED")
|
||||
throw fw::HttpError(503, "not_configured", e.message());
|
||||
if (e.http_status() == 400 && e.type() == "INVALID_ARGUMENT") bad(e.message());
|
||||
throw fw::HttpError(502, "openai_error", e.type() + ": " + e.message());
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
auto guarded(F&& f) -> decltype(f()) {
|
||||
try {
|
||||
return f();
|
||||
} catch (const llm::ApiError& e) {
|
||||
rethrow(e);
|
||||
}
|
||||
}
|
||||
|
||||
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& body, const char* key, bool required = false) {
|
||||
const Json* v = body.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");
|
||||
return v->as_string();
|
||||
}
|
||||
|
||||
Json object_field(const Json& body, const char* key) {
|
||||
const Json* v = body.find(key);
|
||||
if (!v || v->is_null()) return Json::object();
|
||||
if (!v->is_object()) bad(std::string(key) + " must be an object");
|
||||
return *v;
|
||||
}
|
||||
|
||||
bool flag(const Json& body, const char* key) {
|
||||
const Json* v = body.find(key);
|
||||
return v && v->is_bool() && v->as_bool();
|
||||
}
|
||||
|
||||
oa::Profile profile_of(const Json& body) {
|
||||
oa::Profile p = oa::Profile::kGa;
|
||||
std::string name = text(body, "profile");
|
||||
if (!name.empty() && !oa::Events::parse_profile(name, p))
|
||||
bad("profile must be ga, beta, translation, live, live_fork or live_sideband");
|
||||
return p;
|
||||
}
|
||||
|
||||
Json flags_json(unsigned flags) {
|
||||
Json arr = Json::array();
|
||||
static const std::pair<unsigned, const char*> kNames[] = {
|
||||
{oa::kDestructive, "destructive"}, {oa::kPaid, "paid"}, {oa::kBetaAssistants, "beta_assistants"},
|
||||
{oa::kBetaChatkit, "beta_chatkit"}, {oa::kIdempotency, "idempotency_key"}, {oa::kStreamable, "streamable"},
|
||||
{oa::kList, "list"}, {oa::kDeprecated, "deprecated"}, {oa::kBodyUnion, "body_union"}};
|
||||
for (const auto& [bit, name] : kNames)
|
||||
if (flags & bit) arr.push(Json(name));
|
||||
return arr;
|
||||
}
|
||||
|
||||
Json csv_json(const char* csv) {
|
||||
Json arr = Json::array();
|
||||
for (const std::string& s : oa::Calls::list(csv)) arr.push(Json(s));
|
||||
return arr;
|
||||
}
|
||||
|
||||
Json prepared_json(const oa::Prepared& p, const char* what = "request") {
|
||||
Json o = Json::object();
|
||||
o.set("dry_run", Json(true));
|
||||
o.set("operation", Json(p.operation_id));
|
||||
o.set(what, Json(p.method + " " + p.target));
|
||||
Json h = Json::object();
|
||||
for (const auto& [k, v] : p.headers) h.set(k, Json(v));
|
||||
o.set("headers", h);
|
||||
if (p.has_body) o.set("body", p.body);
|
||||
o.set("flags", flags_json(p.flags));
|
||||
o.set("hint", Json("send confirm:true to run it"));
|
||||
return o;
|
||||
}
|
||||
|
||||
class OpenaiPlatformRoutes {
|
||||
public:
|
||||
explicit OpenaiPlatformRoutes(std::shared_ptr<oa::Platform> platform, std::string config_error)
|
||||
: platform_(std::move(platform)), config_error_(std::move(config_error)) {}
|
||||
|
||||
fw::Response status(const fw::Request&) const {
|
||||
Json o = Json::object();
|
||||
o.set("configured", Json(platform_->configured()));
|
||||
o.set("org_set", Json(!llm::Config::get(oa::PlatformConfig::kOrgEnv).empty()));
|
||||
o.set("project_set", Json(!llm::Config::get(oa::PlatformConfig::kProjectEnv).empty()));
|
||||
o.set("base_url", Json(oa::PlatformConfig::kBaseUrl));
|
||||
o.set("credential_env", Json(oa::PlatformConfig::kKeyEnv));
|
||||
if (!config_error_.empty()) o.set("config_error", Json(config_error_));
|
||||
o.set("calls", Json(static_cast<int>(oa::Calls::count())));
|
||||
o.set("events", Json(static_cast<int>(oa::Events::count())));
|
||||
o.set("websocket_endpoints", Json(static_cast<int>(oa::EventSocket::endpoint_count())));
|
||||
o.set("max_socket_seconds", Json(kMaxSocketSeconds));
|
||||
return json_out(o);
|
||||
}
|
||||
|
||||
fw::Response calls(const fw::Request&) const {
|
||||
Json arr = Json::array();
|
||||
for (std::size_t i = 0; i < oa::Calls::count(); ++i) {
|
||||
const oa::CallSpec& s = oa::Calls::at(i);
|
||||
Json row = Json::object();
|
||||
row.set("operation", Json(s.operation_id));
|
||||
row.set("area", Json(s.area));
|
||||
row.set("http", Json(s.method));
|
||||
row.set("path", Json(s.path));
|
||||
Json ids = Json::array();
|
||||
for (const std::string& n : oa::Calls::placeholders(s)) ids.push(Json(n));
|
||||
row.set("ids", ids);
|
||||
row.set("body_fields", csv_json(s.body_fields));
|
||||
row.set("required", csv_json(s.required_fields));
|
||||
row.set("query", csv_json(s.query_params));
|
||||
row.set("json", Json((s.req & oa::kReqJson) != 0));
|
||||
row.set("flags", flags_json(s.flags));
|
||||
arr.push(row);
|
||||
}
|
||||
Json o = Json::object();
|
||||
o.set("calls", arr);
|
||||
return json_out(o);
|
||||
}
|
||||
|
||||
fw::Response events(const fw::Request&) const {
|
||||
Json arr = Json::array();
|
||||
for (std::size_t i = 0; i < oa::Events::count(); ++i) {
|
||||
const oa::EventSpec& e = oa::Events::at(i);
|
||||
Json row = Json::object();
|
||||
row.set("profile", Json(oa::Events::profile_name(e.profile)));
|
||||
row.set("direction", Json(e.direction == oa::Direction::kClientEvent ? "client" : "server"));
|
||||
row.set("type", Json(e.type));
|
||||
row.set("schema", Json(e.schema));
|
||||
row.set("required", csv_json(e.required));
|
||||
arr.push(row);
|
||||
}
|
||||
Json o = Json::object();
|
||||
o.set("events", arr);
|
||||
return json_out(o);
|
||||
}
|
||||
|
||||
// Generic table-driven call.
|
||||
fw::Response call(const fw::Request& req) const {
|
||||
Json b = parse_body(req);
|
||||
std::string op_name = text(b, "operation", true);
|
||||
const oa::CallSpec* spec = oa::Calls::find(op_name);
|
||||
if (!spec) bad("unknown operation '" + op_name + "'");
|
||||
if ((spec->req & oa::kReqJson) == 0 && (spec->req != oa::kReqNone))
|
||||
bad(op_name + " uploads a file or SDP; use its dedicated route or the library");
|
||||
if (spec->resp != oa::kRespJson && spec->resp != oa::kRespEmpty)
|
||||
bad(op_name + " answers non-JSON data; use the library for it");
|
||||
oa::PathArgs ids;
|
||||
for (const auto& [k, v] : object_field(b, "ids").as_object()) {
|
||||
if (!v.is_string()) bad("ids must be strings");
|
||||
ids.emplace_back(k, v.as_string());
|
||||
}
|
||||
Json query = object_field(b, "query");
|
||||
Json body = object_field(b, "body");
|
||||
const Json* body_ptr = (spec->req & oa::kReqJson) != 0 ? &body : nullptr;
|
||||
std::string idem = text(b, "idempotency_key");
|
||||
llm::Headers extra;
|
||||
if (!idem.empty()) extra["Idempotency-Key"] = idem;
|
||||
oa::Prepared p = guarded([&] {
|
||||
return oa::PlatformService::prepare(spec->op, ids, body_ptr, query, extra);
|
||||
});
|
||||
if ((spec->flags & (oa::kDestructive | oa::kPaid)) && !flag(b, "confirm")) return json_out(prepared_json(p));
|
||||
return guarded([&] {
|
||||
if (!platform_->configured()) throw llm::NotConfiguredError("openai", oa::PlatformConfig::kKeyEnv);
|
||||
Json out = platform_->generic.invoke(spec->op, ids, body_ptr, query, extra);
|
||||
return json_out(out);
|
||||
});
|
||||
}
|
||||
|
||||
fw::Response event_build(const fw::Request& req) const {
|
||||
Json b = parse_body(req);
|
||||
oa::Profile profile = profile_of(b);
|
||||
std::string type = text(b, "type", true);
|
||||
Json fields = object_field(b, "fields");
|
||||
return guarded([&] { return json_out(oa::Events::make_client(profile, type, fields, text(b, "event_id"))); });
|
||||
}
|
||||
|
||||
fw::Response event_parse(const fw::Request& req) const {
|
||||
Json b = parse_body(req);
|
||||
oa::Profile profile = profile_of(b);
|
||||
std::string raw = text(b, "text", true);
|
||||
return guarded([&] {
|
||||
oa::ParsedEvent ev;
|
||||
try {
|
||||
ev = oa::Events::parse_server(profile, raw);
|
||||
} catch (const llm::ApiError& e) {
|
||||
if (e.type() == "BAD_EVENT") bad(e.message()); // the caller's text, not a server reply
|
||||
throw;
|
||||
}
|
||||
Json o = Json::object();
|
||||
o.set("type", Json(ev.type));
|
||||
o.set("known", Json(ev.known));
|
||||
o.set("is_error", Json(ev.is_error));
|
||||
if (!ev.event_id.empty()) o.set("event_id", Json(ev.event_id));
|
||||
if (ev.spec) o.set("schema", Json(ev.spec->schema));
|
||||
if (ev.is_error) o.set("error_message", Json(ev.error_message));
|
||||
return json_out(o);
|
||||
});
|
||||
}
|
||||
|
||||
fw::Response event_catalog(const fw::Request& req) const {
|
||||
Json b = parse_body(req);
|
||||
oa::Profile profile = profile_of(b);
|
||||
std::string dir = text(b, "direction");
|
||||
if (!dir.empty() && dir != "client" && dir != "server") bad("direction must be client or server");
|
||||
Json arr = Json::array();
|
||||
for (oa::Direction d : {oa::Direction::kClientEvent, oa::Direction::kServerEvent}) {
|
||||
if (!dir.empty() && (dir == "client") != (d == oa::Direction::kClientEvent)) continue;
|
||||
for (const oa::EventSpec* e : oa::Events::catalog(profile, d)) {
|
||||
Json row = Json::object();
|
||||
row.set("direction", Json(d == oa::Direction::kClientEvent ? "client" : "server"));
|
||||
row.set("type", Json(e->type));
|
||||
row.set("fields", csv_json(e->fields));
|
||||
row.set("required", csv_json(e->required));
|
||||
arr.push(row);
|
||||
}
|
||||
}
|
||||
Json o = Json::object();
|
||||
o.set("profile", Json(oa::Events::profile_name(profile)));
|
||||
o.set("events", arr);
|
||||
return json_out(o);
|
||||
}
|
||||
|
||||
fw::Response batch_file(const fw::Request& req) const {
|
||||
Json b = parse_body(req);
|
||||
std::string endpoint = text(b, "endpoint", true);
|
||||
const Json* reqs = b.find("requests");
|
||||
if (!reqs || !reqs->is_array() || reqs->as_array().empty()) bad("requests must be a non-empty array");
|
||||
return guarded([&] {
|
||||
oa::BatchInput in(endpoint);
|
||||
for (const Json& r : reqs->as_array()) {
|
||||
if (!r.is_object()) bad("each request must be an object");
|
||||
in.add(text(r, "custom_id", true), object_field(r, "body"));
|
||||
}
|
||||
Json o = Json::object();
|
||||
o.set("requests", Json(static_cast<int>(in.size())));
|
||||
o.set("bytes", Json(static_cast<int>(in.jsonl().size())));
|
||||
o.set("jsonl", Json(in.jsonl()));
|
||||
return json_out(o);
|
||||
});
|
||||
}
|
||||
|
||||
fw::Response upload_plan(const fw::Request& req) const {
|
||||
Json b = parse_body(req);
|
||||
const Json* bytes = b.find("bytes");
|
||||
if (!bytes || !bytes->is_number() || bytes->as_number() < 1) bad("bytes must be a positive number");
|
||||
const Json* part = b.find("part_bytes");
|
||||
double part_bytes = part && part->is_number() ? part->as_number() : 8.0 * 1024 * 1024;
|
||||
return guarded([&] {
|
||||
oa::UploadPlan plan = oa::UploadPlan::make(static_cast<std::size_t>(bytes->as_number()),
|
||||
static_cast<std::size_t>(part_bytes));
|
||||
Json o = Json::object();
|
||||
o.set("parts", Json(static_cast<int>(plan.parts.size())));
|
||||
Json first = Json::array();
|
||||
for (std::size_t i = 0; i < plan.parts.size() && i < 5; ++i) {
|
||||
Json p = Json::object();
|
||||
p.set("offset", Json(static_cast<double>(plan.parts[i].offset)));
|
||||
p.set("size", Json(static_cast<double>(plan.parts[i].size)));
|
||||
first.push(p);
|
||||
}
|
||||
o.set("first_parts", first);
|
||||
return json_out(o);
|
||||
});
|
||||
}
|
||||
|
||||
// Runs a short WebSocket session: connect, send events, collect.
|
||||
fw::Response socket(const fw::Request& req) const {
|
||||
Json b = parse_body(req);
|
||||
static const std::pair<const char*, oa::WsKind> kKinds[] = {
|
||||
{"realtime", oa::WsKind::kRealtime}, {"realtime_beta", oa::WsKind::kRealtimeBeta},
|
||||
{"translation", oa::WsKind::kRealtimeTranslation}, {"live", oa::WsKind::kLiveSession},
|
||||
{"live_sideband", oa::WsKind::kLiveSideband}, {"live_fork", oa::WsKind::kLiveFork}};
|
||||
std::string kind_name = text(b, "kind", true);
|
||||
const oa::WsKind* kind = nullptr;
|
||||
for (const auto& [n, k] : kKinds)
|
||||
if (kind_name == n) kind = &k;
|
||||
if (!kind) bad("kind must be realtime, realtime_beta, translation, live, live_sideband or live_fork");
|
||||
oa::WsParams params;
|
||||
params.model = text(b, "model");
|
||||
params.call_id = text(b, "call_id");
|
||||
params.session_id = text(b, "session_id");
|
||||
params.graceful_close = flag(b, "graceful_close");
|
||||
int seconds = 5;
|
||||
if (const Json* s = b.find("seconds"); s && !s->is_null()) {
|
||||
if (!s->is_number() || s->as_int() < 1 || s->as_int() > kMaxSocketSeconds)
|
||||
bad("seconds must be 1 to " + std::to_string(kMaxSocketSeconds));
|
||||
seconds = static_cast<int>(s->as_int());
|
||||
}
|
||||
std::size_t max_events = 50;
|
||||
if (const Json* m = b.find("max_events"); m && !m->is_null()) {
|
||||
if (!m->is_number() || m->as_int() < 1 || m->as_int() > static_cast<long>(kMaxSocketEventsShown))
|
||||
bad("max_events must be 1 to " + std::to_string(kMaxSocketEventsShown));
|
||||
max_events = static_cast<std::size_t>(m->as_int());
|
||||
}
|
||||
oa::Profile profile = oa::EventSocket::profile_of(*kind);
|
||||
std::vector<Json> to_send;
|
||||
if (const Json* ev = b.find("events"); ev && !ev->is_null()) {
|
||||
if (!ev->is_array() || ev->as_array().size() > kMaxClientEvents) bad("events must be an array of up to 20");
|
||||
for (const Json& e : ev->as_array()) {
|
||||
if (!e.is_object()) bad("each event must be an object");
|
||||
Json fields = object_field(e, "fields");
|
||||
to_send.push_back(guarded([&] { return oa::Events::make_client(profile, text(e, "type", true), fields, text(e, "event_id")); }));
|
||||
}
|
||||
}
|
||||
std::string url = guarded([&] {
|
||||
return oa::EventSocket::url_for(*kind, "wss://api.openai.com/v1", params);
|
||||
});
|
||||
if (!flag(b, "confirm")) {
|
||||
Json o = Json::object();
|
||||
o.set("dry_run", Json(true));
|
||||
o.set("url", Json(url));
|
||||
Json arr = Json::array();
|
||||
for (const Json& e : to_send) arr.push(e);
|
||||
o.set("events", arr);
|
||||
o.set("hint", Json("send confirm:true to open the session (a session can cost money)"));
|
||||
return json_out(o);
|
||||
}
|
||||
return guarded([&] {
|
||||
if (!platform_->configured()) throw llm::NotConfiguredError("openai", oa::PlatformConfig::kKeyEnv);
|
||||
oa::EventSocketOptions opts;
|
||||
opts.organization = llm::Config::get(oa::PlatformConfig::kOrgEnv);
|
||||
opts.project = llm::Config::get(oa::PlatformConfig::kProjectEnv);
|
||||
oa::EventSocket sock(*kind, opts);
|
||||
sock.connect(params);
|
||||
for (const Json& e : to_send) sock.send(e);
|
||||
Json shown = Json::array();
|
||||
oa::RunStats st = sock.run(
|
||||
[&](const oa::ParsedEvent& ev) {
|
||||
Json row = Json::object();
|
||||
row.set("type", Json(ev.type));
|
||||
row.set("known", Json(ev.known));
|
||||
if (ev.is_error) row.set("error", Json(ev.error_message));
|
||||
shown.push(row);
|
||||
return shown.as_array().size() < max_events;
|
||||
},
|
||||
seconds);
|
||||
Json o = Json::object();
|
||||
o.set("events", shown);
|
||||
o.set("stop_reason", Json(st.stop_reason));
|
||||
o.set("received", Json(static_cast<int>(st.events)));
|
||||
o.set("unknown", Json(static_cast<int>(st.unknown)));
|
||||
return json_out(o);
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<oa::Platform> platform_;
|
||||
std::string config_error_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
void register_ai_openaiplatform_demo(fw::Router& router, Services& services) {
|
||||
(void)router;
|
||||
(void)services;
|
||||
std::shared_ptr<oa::Platform> platform;
|
||||
std::string config_error;
|
||||
try {
|
||||
platform = oa::Platform::from_env();
|
||||
} catch (const llm::ApiError& e) {
|
||||
// A bad OPENAI_ORG_ID / OPENAI_PROJECT_ID must not take the demo app down.
|
||||
config_error = e.message();
|
||||
llm::ApiClientConfig c;
|
||||
c.vendor = "openai";
|
||||
c.base_url = oa::PlatformConfig::kBaseUrl;
|
||||
c.key_env = oa::PlatformConfig::kKeyEnv;
|
||||
platform = std::make_shared<oa::Platform>(llm::ApiClient::from_env(c));
|
||||
}
|
||||
auto routes = std::make_shared<OpenaiPlatformRoutes>(platform, config_error);
|
||||
fw::Environment* env = services.templates.get();
|
||||
services.templates->add_template("ai_openai_platform.html", kPage);
|
||||
services.add_feature("openai-platform", "/ai/openai/platform");
|
||||
|
||||
router.get("/ai/openai/platform", "ai-openai-platform-page", [env](fw::Request req) {
|
||||
(void)req;
|
||||
fw::TemplateCtx ctx;
|
||||
ctx["title"] = std::string("OpenAI platform");
|
||||
return fw::Response(200, env->render("ai_openai_platform.html", ctx), "text/html; charset=utf-8");
|
||||
});
|
||||
router.get("/ai/openai/platform/status", "ai-openai-platform-status",
|
||||
[routes](fw::Request req) { return routes->status(req); });
|
||||
router.get("/ai/openai/platform/calls", "ai-openai-platform-calls",
|
||||
[routes](fw::Request req) { return routes->calls(req); });
|
||||
router.get("/ai/openai/platform/events", "ai-openai-platform-events",
|
||||
[routes](fw::Request req) { return routes->events(req); });
|
||||
router.post("/ai/openai/platform/call", "ai-openai-platform-call",
|
||||
[routes](fw::Request req) { return routes->call(req); });
|
||||
router.post("/ai/openai/platform/realtime/event", "ai-openai-platform-event-build",
|
||||
[routes](fw::Request req) { return routes->event_build(req); });
|
||||
router.post("/ai/openai/platform/realtime/parse", "ai-openai-platform-event-parse",
|
||||
[routes](fw::Request req) { return routes->event_parse(req); });
|
||||
router.post("/ai/openai/platform/realtime/catalog", "ai-openai-platform-event-catalog",
|
||||
[routes](fw::Request req) { return routes->event_catalog(req); });
|
||||
router.post("/ai/openai/platform/realtime/socket", "ai-openai-platform-socket",
|
||||
[routes](fw::Request req) { return routes->socket(req); });
|
||||
router.post("/ai/openai/platform/batch/file", "ai-openai-platform-batch-file",
|
||||
[routes](fw::Request req) { return routes->batch_file(req); });
|
||||
router.post("/ai/openai/platform/uploads/plan", "ai-openai-platform-upload-plan",
|
||||
[routes](fw::Request req) { return routes->upload_plan(req); });
|
||||
}
|
||||
|
||||
} // namespace molo::example::app
|
||||
|
||||
Reference in New Issue
Block a user