feat(clients): openai core: Responses, Chat, media, tools, ChatModel, spec checker, demo
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
#include "openai/core/decisions.h"
|
||||
|
||||
#include "openai/core/body.h"
|
||||
#include "openai/core/caps.h"
|
||||
#include "openai/core/check.h"
|
||||
#include "openai/core/form_body.h"
|
||||
|
||||
namespace molo::openai::core {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
DecisionQuestion DecisionQuestion::predicate(std::string name, std::string instructions) {
|
||||
DecisionQuestion q;
|
||||
q.name = std::move(name);
|
||||
q.instructions = std::move(instructions);
|
||||
return q;
|
||||
}
|
||||
|
||||
DecisionQuestion DecisionQuestion::choice(std::string name, std::string instructions,
|
||||
std::vector<std::string> choices) {
|
||||
DecisionQuestion q = predicate(std::move(name), std::move(instructions));
|
||||
q.kind = Kind::kChoice;
|
||||
q.choices = std::move(choices);
|
||||
return q;
|
||||
}
|
||||
|
||||
DecisionQuestion DecisionQuestion::score(std::string name, std::string instructions,
|
||||
std::vector<std::string> levels) {
|
||||
DecisionQuestion q = predicate(std::move(name), std::move(instructions));
|
||||
q.kind = Kind::kScore;
|
||||
q.levels = std::move(levels);
|
||||
return q;
|
||||
}
|
||||
|
||||
Json DecisionQuestion::to_json() const {
|
||||
Check::non_empty("question instructions", instructions);
|
||||
Check::max_chars("question instructions", instructions, CoreCaps::kMaxInstructionChars);
|
||||
Check::max_chars("question name", name, 1024);
|
||||
Json o = Json::object();
|
||||
o.set("instructions", Json(instructions));
|
||||
if (!name.empty()) o.set("name", Json(name));
|
||||
switch (kind) {
|
||||
case Kind::kPredicate: o.set("type", Json("predicate")); break;
|
||||
case Kind::kChoice: {
|
||||
Check::require(choices.size() >= 2, "a choice question needs at least two choices");
|
||||
o.set("type", Json("choice"));
|
||||
Json a = Json::array();
|
||||
for (const std::string& c : choices) {
|
||||
Check::non_empty("choice", c);
|
||||
Json item = Json::object();
|
||||
item.set("value", Json(c));
|
||||
a.push(item);
|
||||
}
|
||||
o.set("choices", a);
|
||||
break;
|
||||
}
|
||||
case Kind::kScore: {
|
||||
Check::require(levels.size() >= 2, "a score question needs at least two levels");
|
||||
o.set("type", Json("score"));
|
||||
Json a = Json::array();
|
||||
for (const std::string& l : levels) {
|
||||
Check::non_empty("level", l);
|
||||
Json item = Json::object();
|
||||
item.set("label", Json(l));
|
||||
a.push(item);
|
||||
}
|
||||
o.set("levels", a);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
void DecisionRequest::validate() const {
|
||||
Check::non_empty("model", model);
|
||||
Check::non_empty("input", input_text);
|
||||
Check::max_chars("input", input_text, CoreCaps::kMaxInputChars);
|
||||
Check::require(!questions.empty(), "at least one question is required");
|
||||
Check::max_count("questions", questions.size(), 64);
|
||||
Check::max_chars("safety_identifier", safety_identifier, CoreCaps::kMaxSafetyIdChars);
|
||||
Check::max_chars("prompt_cache_key", prompt_cache_key, CoreCaps::kMaxPromptCacheKeyChars);
|
||||
}
|
||||
|
||||
Json DecisionRequest::to_json() const {
|
||||
validate();
|
||||
Body b(Call::kDecisionCreate);
|
||||
b.put_text("model", model).put_text("input", input_text);
|
||||
Json qs = Json::array();
|
||||
for (const DecisionQuestion& q : questions) qs.push(q.to_json());
|
||||
b.put("questions", qs);
|
||||
b.put_text("safety_identifier", safety_identifier).put_text("prompt_cache_key", prompt_cache_key);
|
||||
b.merge_extra(extra);
|
||||
return b.build();
|
||||
}
|
||||
|
||||
void ProvenanceRequest::validate() const {
|
||||
Check::require(file.content_type.rfind("image/", 0) == 0 || file.content_type.rfind("audio/", 0) == 0,
|
||||
"provenance checks take an image or audio file");
|
||||
}
|
||||
|
||||
clients::body::MultipartBody ProvenanceRequest::to_multipart() const {
|
||||
validate();
|
||||
FormBody f(Call::kProvenanceCheck);
|
||||
f.file("file", file, "", CoreCaps::kMaxImageBytes);
|
||||
return f.form();
|
||||
}
|
||||
|
||||
Json DecisionsService::decide(const DecisionRequest& request) {
|
||||
Json body = request.to_json();
|
||||
return json_call(Call::kDecisionCreate, {}, &body);
|
||||
}
|
||||
|
||||
Json DecisionsService::check_provenance(const ProvenanceRequest& request) {
|
||||
return multipart_call(Call::kProvenanceCheck, {}, request.to_multipart());
|
||||
}
|
||||
|
||||
} // namespace molo::openai::core
|
||||
Reference in New Issue
Block a user