Merge openai core package
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
// Shared test rig: an OpenAI ApiClient over MockTransport. The mock only
|
||||
// records requests (and answers a documented fixture where a call parses its
|
||||
// answer); no vendor behavior is scripted beyond the documented examples.
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "clients/transports/mock_transport.h"
|
||||
#include "framework.h"
|
||||
#include "openai/core/client.h"
|
||||
|
||||
namespace oai_test {
|
||||
|
||||
using molo::clients::ClientResponse;
|
||||
using molo::clients::HeaderMap;
|
||||
using molo::clients::Method;
|
||||
using molo::clients::body::Json;
|
||||
using molo::clients::transports::MockTransport;
|
||||
|
||||
inline ClientResponse ok(const std::string& body, const std::string& type = "application/json") {
|
||||
HeaderMap h;
|
||||
h.set("Content-Type", type);
|
||||
return ClientResponse(200, h, body);
|
||||
}
|
||||
|
||||
struct Rig {
|
||||
MockTransport* mock;
|
||||
std::shared_ptr<molo::llm::ApiClient> client;
|
||||
explicit Rig(const std::string& key = "sk-test-KEY", const std::string& org = "",
|
||||
const std::string& project = "") {
|
||||
auto t = std::make_unique<MockTransport>();
|
||||
mock = t.get();
|
||||
client = molo::openai::core::OpenaiClient::with_transport(std::move(t), key, org, project);
|
||||
}
|
||||
// Answers the next call of method/path with `body`.
|
||||
void answer(Method m, const std::string& path, const std::string& body = "{}",
|
||||
const std::string& type = "application/json") {
|
||||
mock->expect_response(m, "/v1" + path, ok(body, type));
|
||||
}
|
||||
Json last_body() const { return Json::parse(mock->seen().back().body); }
|
||||
std::string last_target() const { return mock->seen().back().target; }
|
||||
};
|
||||
|
||||
// True when `fn` throws an ApiError whose message contains `needle`.
|
||||
template <typename F>
|
||||
bool rejects(F&& fn, const std::string& needle = "") {
|
||||
try {
|
||||
fn();
|
||||
} catch (const molo::llm::ApiError& e) {
|
||||
return needle.empty() || e.message().find(needle) != std::string::npos;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline Json parse(const char* text) { return Json::parse(text); }
|
||||
|
||||
} // namespace oai_test
|
||||
@@ -0,0 +1,66 @@
|
||||
// OpenAI core: opt-in live checks. MOLO_LIVE_NET=1 enables them; with
|
||||
// OPENAI_API_KEY they run read-only calls (models list) and at most one
|
||||
// smallest completion; paid generation additionally needs MOLO_LIVE_PAID=1.
|
||||
// Without a key the unauthenticated probe still runs (401 with the documented
|
||||
// error body). Everything else prints a loud SKIP. Nothing is created,
|
||||
// updated or deleted.
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#include "clients/transports/http_transport.h"
|
||||
#include "llm/config.h"
|
||||
#include "openai/core/chat_model.h"
|
||||
#include "openai/core/client.h"
|
||||
#include "openai/core/models.h"
|
||||
#include "openai/core/responses.h"
|
||||
#include "rig.h"
|
||||
|
||||
using namespace molo::openai::core;
|
||||
|
||||
namespace {
|
||||
// Read once at static init: other suites in this binary edit the environment.
|
||||
const bool kNet = std::getenv("MOLO_LIVE_NET") && std::string(std::getenv("MOLO_LIVE_NET")) == "1";
|
||||
const bool kPaid = std::getenv("MOLO_LIVE_PAID") && std::string(std::getenv("MOLO_LIVE_PAID")) == "1";
|
||||
const std::string kKey = molo::llm::Config::get("OPENAI_API_KEY");
|
||||
bool net() { return kNet; }
|
||||
bool paid() { return kPaid; }
|
||||
} // namespace
|
||||
|
||||
TEST(openai_live, unauthenticated_probe_is_401_with_error_body) {
|
||||
if (!net()) {
|
||||
std::cout << "SKIP openai_live probe: MOLO_LIVE_NET=1 not set\n";
|
||||
return;
|
||||
}
|
||||
auto client = OpenaiClient::with_transport(std::make_unique<molo::clients::transports::HttpTransport>(),
|
||||
"sk-invalid-probe-key");
|
||||
ModelsService models(client);
|
||||
try {
|
||||
models.list();
|
||||
FAIL("expected 401");
|
||||
} catch (const molo::llm::ApiError& e) {
|
||||
ASSERT_EQ(e.http_status(), 401);
|
||||
ASSERT_TRUE(!e.message().empty());
|
||||
std::cout << " probe: " << e.http_status() << " type=" << e.type() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
TEST(openai_live, models_list_and_one_tiny_response) {
|
||||
if (!net() || kKey.empty()) {
|
||||
std::cout << "SKIP openai_live models: needs MOLO_LIVE_NET=1 and OPENAI_API_KEY\n";
|
||||
return;
|
||||
}
|
||||
auto client = OpenaiClient::from_env();
|
||||
ModelsService models(client);
|
||||
ASSERT_TRUE(!models.ids().empty());
|
||||
if (!paid()) {
|
||||
std::cout << "SKIP openai_live completion: MOLO_LIVE_PAID=1 not set\n";
|
||||
return;
|
||||
}
|
||||
OpenaiChatModel model(client);
|
||||
molo::llm::ChatRequest in;
|
||||
in.model = "gpt-4.1-nano";
|
||||
in.messages.push_back(molo::llm::Message::user("Reply with the single word: ok"));
|
||||
in.max_tokens = 16;
|
||||
auto out = model.generate(in);
|
||||
ASSERT_TRUE(!out.text().empty());
|
||||
}
|
||||
@@ -0,0 +1,483 @@
|
||||
// OpenAI core: call table, request bodies against the documented curl bodies
|
||||
// (fixtures.h, verbatim from the OpenAPI document), validation, caps, ids,
|
||||
// headers, no-credential behavior. No scripted vendor behavior.
|
||||
#include "fixtures.h"
|
||||
#include "openai/core/calls.h"
|
||||
#include "openai/core/caps.h"
|
||||
#include "openai/core/chat_completions.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/voices.h"
|
||||
#include "openai/core/wire/tool_schemas.h"
|
||||
#include "rig.h"
|
||||
|
||||
using namespace molo::openai::core;
|
||||
using namespace oai_test;
|
||||
namespace fx = molo::openai::core::fixtures;
|
||||
|
||||
namespace {
|
||||
bool same(const Json& a, const char* doc) { return a.equals(Json::parse(doc)); }
|
||||
} // namespace
|
||||
|
||||
TEST(openai_core, call_table_shape) {
|
||||
ASSERT_EQ(call_count(), static_cast<std::size_t>(44));
|
||||
for (std::size_t i = 0; i < call_count(); ++i) {
|
||||
const CallSpec& c = spec_at(i);
|
||||
ASSERT_TRUE(std::string(c.path).rfind("/", 0) == 0);
|
||||
ASSERT_TRUE(std::string(c.operation_id).size() > 3);
|
||||
(void)method_of(c.call);
|
||||
}
|
||||
ASSERT_EQ(render(Call::kResponseGet, {"resp_1"}), std::string("/responses/resp_1"));
|
||||
ASSERT_EQ(render(Call::kModelGet, {"ft:gpt-4o:org::abc"}), std::string("/models/ft:gpt-4o:org::abc"));
|
||||
ASSERT_TRUE(rejects([] { render(Call::kResponseGet, {"../x"}); }, "bad response_id"));
|
||||
ASSERT_TRUE(rejects([] { render(Call::kResponseGet, {}); }));
|
||||
ASSERT_TRUE(rejects([] { render(Call::kResponseCreate, {"x"}); }));
|
||||
ASSERT_TRUE(accepts_field(Call::kResponseCreate, "previous_response_id"));
|
||||
ASSERT_FALSE(accepts_field(Call::kResponseCreate, "nonsense"));
|
||||
ASSERT_TRUE(accepts_query(Call::kChatList, "after"));
|
||||
ASSERT_TRUE(answers(Call::kChatCreate, "sse"));
|
||||
ASSERT_FALSE(answers(Call::kEmbeddingCreate, "sse"));
|
||||
}
|
||||
|
||||
TEST(openai_core, bodies_match_documented_curl) {
|
||||
{
|
||||
ResponseRequest r;
|
||||
r.model = "gpt-6-astra";
|
||||
r.input_text = "Tell me a three sentence bedtime story about a unicorn.";
|
||||
ASSERT_TRUE(r.to_json(false).equals(Json::parse(
|
||||
R"({"model":"gpt-6-astra","input":"Tell me a three sentence bedtime story about a unicorn."})")));
|
||||
ResponseRequest s;
|
||||
s.model = "gpt-6-astra";
|
||||
s.instructions = "You are a helpful assistant.";
|
||||
s.input_text = "Hello!";
|
||||
ASSERT_TRUE(same(s.to_json(true), fx::k_resp_stream_request));
|
||||
ResponseRequest re;
|
||||
re.model = "gpt-6-astra";
|
||||
re.input_text = "How much wood would a woodchuck chuck?";
|
||||
re.reasoning.effort = "high";
|
||||
ASSERT_TRUE(same(re.to_json(false), fx::k_resp_reason_request));
|
||||
ResponseRequest ws;
|
||||
ws.model = "gpt-6-astra";
|
||||
ws.input_text = "What was a positive news story from today?";
|
||||
WebSearchOptions o;
|
||||
o.type = "web_search_preview";
|
||||
ws.tools.push_back(ToolSchemas::web_search(o));
|
||||
ASSERT_TRUE(same(ws.to_json(false), fx::k_resp_websearch_request));
|
||||
ResponseRequest fs;
|
||||
fs.model = "gpt-6-astra";
|
||||
fs.input_text = "What are the attributes of an ancient brown dragon?";
|
||||
fs.tools.push_back(ToolSchemas::file_search({"vs_1234567890"}, 20));
|
||||
ASSERT_TRUE(same(fs.to_json(false), fx::k_resp_filesearch_request));
|
||||
}
|
||||
{
|
||||
ChatCompletionRequest c;
|
||||
c.model = "gpt-6-astra";
|
||||
c.messages = {ChatMessages::developer("You are a helpful assistant."), ChatMessages::user("Hello!")};
|
||||
ASSERT_TRUE(same(c.to_json(false), fx::k_chat_default_request));
|
||||
ASSERT_TRUE(same(c.to_json(true), fx::k_chat_stream_request));
|
||||
ChatCompletionRequest f;
|
||||
f.model = "gpt-6-astra";
|
||||
f.messages = {ChatMessages::user("What is the weather like in Boston today?")};
|
||||
f.tools = {ToolSchemas::chat_function(
|
||||
"get_current_weather", "Get the current weather in a given location",
|
||||
Json::parse(R"({"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]})"))};
|
||||
f.tool_choice = Json("auto");
|
||||
ASSERT_TRUE(same(f.to_json(false), fx::k_chat_func_request));
|
||||
}
|
||||
{
|
||||
CompletionRequest c;
|
||||
c.model = "gpt-3.5-turbo-instruct";
|
||||
c.prompt = "Say this is a test";
|
||||
c.max_tokens = 7;
|
||||
c.temperature = 0;
|
||||
ASSERT_TRUE(same(c.to_json(false), fx::k_comp_request));
|
||||
ASSERT_TRUE(same(c.to_json(true), fx::k_comp_stream_request));
|
||||
}
|
||||
{
|
||||
EmbeddingRequest e;
|
||||
e.model = "text-embedding-3-small";
|
||||
e.inputs = {"Hello world"};
|
||||
e.encoding_format = "float";
|
||||
e.dimensions = 3;
|
||||
ASSERT_TRUE(same(e.to_json(), fx::k_embed_request));
|
||||
}
|
||||
{
|
||||
TokenCountRequest t;
|
||||
t.model = "gpt-6-astra";
|
||||
t.input_text = "Tell me a joke.";
|
||||
ASSERT_TRUE(same(t.to_json(), fx::k_input_tokens_request));
|
||||
}
|
||||
{
|
||||
DecisionRequest d;
|
||||
d.model = "gpt-6-luna";
|
||||
d.input_text = "The package arrived with a broken screen.";
|
||||
d.questions = {DecisionQuestion::predicate("damaged", "Does the customer report a damaged item?")};
|
||||
ASSERT_TRUE(same(d.to_json(), fx::k_decision_request));
|
||||
}
|
||||
{
|
||||
ImageGenerateRequest g;
|
||||
g.model = "gpt-image-2.5-flare";
|
||||
g.prompt = "A cute baby sea otter";
|
||||
g.n = 1;
|
||||
g.size = "1024x1024";
|
||||
ASSERT_TRUE(same(g.to_json(false), fx::k_img_gen_request));
|
||||
}
|
||||
{
|
||||
ModerationRequest m;
|
||||
m.model = "omni-moderation-latest";
|
||||
m.parts = {ModerationRequest::text_part("...text to classify goes here..."),
|
||||
ModerationRequest::image_part("https://example.com/image.png")};
|
||||
ASSERT_TRUE(same(m.to_json(), fx::k_mod_image_request));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(openai_core, response_request_validation) {
|
||||
auto base = [] {
|
||||
ResponseRequest r;
|
||||
r.model = "m";
|
||||
r.input_text = "hi";
|
||||
return r;
|
||||
};
|
||||
ASSERT_TRUE(rejects([] { ResponseRequest r; r.input_text = "x"; r.to_json(false); }, "model is required"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.input_items = {Json::object()}; r.to_json(false); }, "not both"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.previous_response_id = "resp_1"; r.conversation_id = "conv_1"; r.to_json(false); }, "mutually exclusive"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.background = true; r.store = false; r.to_json(false); }, "store=true"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.max_output_tokens = 5; r.to_json(false); }, "at least 16"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.temperature = 3; r.to_json(false); }, "temperature"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.include = {"bogus"}; r.to_json(false); }, "include"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.reasoning.effort = "extreme"; r.to_json(false); }, "reasoning effort"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.include_obfuscation = true; r.to_json(false); }, "streaming only"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.previous_response_id = "../x"; r.to_json(false); }, "bad previous_response"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.input_text.assign(CoreCaps::kMaxInputChars + 1, 'x'); r.to_json(false); }, "characters"));
|
||||
// extra: unknown key and a key already set are loud.
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.extra = Json::parse(R"({"bogus":1})"); r.to_json(false); }, "not a request property"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.extra = Json::parse(R"({"model":"other"})"); r.to_json(false); }, "already set"));
|
||||
auto r = base();
|
||||
r.extra = Json::parse(R"({"safety_identifier":"u1","top_logprobs":2})");
|
||||
Json j = r.to_json(false);
|
||||
ASSERT_EQ(j.find("safety_identifier")->as_string(), std::string("u1"));
|
||||
// background + structured output + conversation render.
|
||||
auto b = base();
|
||||
b.background = true;
|
||||
b.conversation_id = "conv_9";
|
||||
b.text_format = TextFormat::json_schema("out", Json::parse(R"({"type":"object","properties":{"a":{"type":"string"}},"required":["a"],"additionalProperties":false})"));
|
||||
b.verbosity = "low";
|
||||
Json bj = b.to_json(false);
|
||||
ASSERT_EQ(bj.find("conversation")->as_string(), std::string("conv_9"));
|
||||
ASSERT_EQ(bj.find("text")->find("format")->find("type")->as_string(), std::string("json_schema"));
|
||||
ASSERT_EQ(bj.find("text")->find("verbosity")->as_string(), std::string("low"));
|
||||
ASSERT_TRUE(bj.find("background")->as_bool());
|
||||
}
|
||||
|
||||
TEST(openai_core, strict_schema_and_tools) {
|
||||
ASSERT_TRUE(rejects([] { TextFormat::json_schema("n", Json::parse(R"({"type":"object","properties":{"a":{"type":"string"}},"required":["a"]})")).validate(); }, "additionalProperties"));
|
||||
ASSERT_TRUE(rejects([] { TextFormat::json_schema("n", Json::parse(R"({"type":"object","properties":{"a":{"type":"string"}},"required":[],"additionalProperties":false})")).validate(); }, "required"));
|
||||
ASSERT_TRUE(rejects([] { TextFormat::json_schema("bad name", Json::object()).validate(); }, "schema name"));
|
||||
TextFormat::json_schema("n", Json::parse(R"({"type":"object","properties":{"a":{"type":"string"}},"required":["a"]})"), false).validate();
|
||||
ASSERT_TRUE(rejects([] { ToolSchemas::function("bad name!", "", Json::object()); }, "tool name"));
|
||||
ASSERT_TRUE(rejects([] { ToolSchemas::mcp(McpOptions{}); }, "server_label"));
|
||||
McpOptions m;
|
||||
m.server_label = "docs";
|
||||
ASSERT_TRUE(rejects([&] { ToolSchemas::mcp(m); }, "exactly one"));
|
||||
m.server_url = "https://example.com/mcp";
|
||||
m.require_approval = "never";
|
||||
m.allowed_tools = {"search"};
|
||||
Json t = ToolSchemas::mcp(m);
|
||||
ASSERT_EQ(t.find("type")->as_string(), std::string("mcp"));
|
||||
m.connector_id = "connector_gmail";
|
||||
ASSERT_TRUE(rejects([&] { ToolSchemas::mcp(m); }, "exactly one"));
|
||||
McpOptions c;
|
||||
c.server_label = "gm";
|
||||
c.connector_id = "connector_nope";
|
||||
ASSERT_TRUE(rejects([&] { ToolSchemas::mcp(c); }, "connector_id"));
|
||||
ASSERT_EQ(ToolSchemas::code_interpreter_auto({"file_1"}).find("container")->find("type")->as_string(), std::string("auto"));
|
||||
ASSERT_EQ(ToolSchemas::computer_use_preview("browser", 1024, 768).find("display_width")->as_int(), 1024L);
|
||||
ASSERT_TRUE(rejects([] { ToolSchemas::computer_use_preview("amiga", 1, 1); }, "environment"));
|
||||
ASSERT_EQ(ToolSchemas::image_generation(ImageGenOptions{}).find("type")->as_string(), std::string("image_generation"));
|
||||
ASSERT_TRUE(rejects([] { ImageGenOptions o; o.partial_images = 9; ToolSchemas::image_generation(o); }, "partial_images"));
|
||||
ASSERT_EQ(ToolSchemas::custom("patch", "apply", ToolSchemas::grammar_format("lark", "start: x")).find("format")->find("syntax")->as_string(), std::string("lark"));
|
||||
ASSERT_TRUE(rejects([] { ToolSchemas::validate_response_tools({Json::parse(R"({"type":"warp_drive"})")}); }, "unknown tool type"));
|
||||
ASSERT_TRUE(rejects([] { ToolSchemas::validate_chat_tools({Json::parse(R"({"type":"web_search"})")}); }, "function or custom"));
|
||||
ASSERT_TRUE(ToolSchemas::is_response_tool_type("web_search_preview_2025_03_11"));
|
||||
std::vector<Json> many(CoreCaps::kMaxTools + 1, ToolSchemas::local_shell());
|
||||
ASSERT_TRUE(rejects([&] { ToolSchemas::validate_response_tools(many); }, "tools"));
|
||||
ASSERT_EQ(ToolSchemas::choice_from_neutral("get_x", true).find("function")->find("name")->as_string(), std::string("get_x"));
|
||||
ASSERT_EQ(ToolSchemas::choice_from_neutral("get_x", false).find("name")->as_string(), std::string("get_x"));
|
||||
ASSERT_TRUE(ToolSchemas::choice_from_neutral("", false).is_null());
|
||||
ASSERT_EQ(ToolSchemas::choice_allowed("auto", {ToolSchemas::choice_function("a")}).find("type")->as_string(), std::string("allowed_tools"));
|
||||
}
|
||||
|
||||
TEST(openai_core, chat_and_other_validation) {
|
||||
auto base = [] {
|
||||
ChatCompletionRequest r;
|
||||
r.model = "m";
|
||||
r.messages = {ChatMessages::user("hi")};
|
||||
return r;
|
||||
};
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.messages.clear(); r.to_json(false); }, "messages"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.n = 500; r.to_json(false); }, "n must"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.stop = {"a", "b", "c", "d", "e"}; r.to_json(false); }, "stop"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.top_logprobs = 3; r.to_json(false); }, "logprobs"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.tool_choice = Json("auto"); r.to_json(false); }, "needs tools"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.include_usage = true; r.to_json(false); }, "streaming only"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.logit_bias["50256"] = 500; r.to_json(false); }, "logit_bias"));
|
||||
ASSERT_TRUE(rejects([&] { auto r = base(); r.modalities = {"video"}; r.to_json(false); }, "modality"));
|
||||
auto r = base();
|
||||
r.include_usage = true;
|
||||
r.stop = {"END"};
|
||||
r.store = true;
|
||||
r.metadata = {{"k", "v"}};
|
||||
Json j = r.to_json(true);
|
||||
ASSERT_EQ(j.find("stop")->as_string(), std::string("END"));
|
||||
ASSERT_TRUE(j.find("stream_options")->find("include_usage")->as_bool());
|
||||
ASSERT_TRUE(j.find("store")->as_bool());
|
||||
ASSERT_TRUE(rejects([] { CompletionRequest c; c.model = "m"; c.to_json(false); }, "prompt"));
|
||||
ASSERT_TRUE(rejects([] { CompletionRequest c; c.model = "m"; c.prompt = "x"; c.best_of = 2; c.to_json(true); }, "best_of"));
|
||||
ASSERT_TRUE(rejects([] { EmbeddingRequest e; e.model = "m"; e.to_json(); }, "exactly one"));
|
||||
ASSERT_TRUE(rejects([] { EmbeddingRequest e; e.model = "m"; e.inputs = {"a"}; e.dimensions = 0; e.to_json(); }, "dimensions"));
|
||||
EmbeddingRequest tok;
|
||||
tok.model = "m";
|
||||
tok.token_inputs = {{1, 2, 3}, {4}};
|
||||
ASSERT_EQ(tok.to_json().find("input")->as_array().size(), static_cast<std::size_t>(2));
|
||||
ASSERT_TRUE(rejects([] { ModerationRequest m; m.to_json(); }, "exactly one"));
|
||||
ASSERT_TRUE(rejects([] { TokenCountRequest t; t.input_text = "a"; t.input_items = {Json::object()}; t.to_json(); }, "not both"));
|
||||
ASSERT_TRUE(rejects([] { CompactRequest c; c.to_json(); }, "model"));
|
||||
ASSERT_TRUE(rejects([] { DecisionRequest d; d.model = "m"; d.input_text = "x"; d.to_json(); }, "question"));
|
||||
ASSERT_TRUE(rejects([] { DecisionQuestion::choice("c", "pick", {"a"}).to_json(); }, "two choices"));
|
||||
}
|
||||
|
||||
TEST(openai_core, media_validation) {
|
||||
ImageGenerateRequest g;
|
||||
g.model = "dall-e-3";
|
||||
g.prompt = "cat";
|
||||
g.n = 2;
|
||||
ASSERT_TRUE(rejects([&] { g.to_json(false); }, "one image"));
|
||||
g.n = 1;
|
||||
g.partial_images = 1;
|
||||
ASSERT_TRUE(rejects([&] { g.to_json(false); }, "streaming only"));
|
||||
g.partial_images.reset();
|
||||
g.size = "big";
|
||||
ASSERT_TRUE(rejects([&] { g.to_json(false); }, "WIDTHxHEIGHT"));
|
||||
g.size = "1024x1024";
|
||||
g.prompt.assign(4001, 'x');
|
||||
ASSERT_TRUE(rejects([&] { g.to_json(false); }, "characters"));
|
||||
g.prompt = "cat";
|
||||
g.background = "transparent";
|
||||
g.output_format = "jpeg";
|
||||
ASSERT_TRUE(rejects([&] { g.to_json(false); }, "transparent"));
|
||||
|
||||
ImageEditRequest e;
|
||||
e.model = "gpt-image-2";
|
||||
e.prompt = "edit";
|
||||
ASSERT_TRUE(rejects([&] { e.validate(false); }, "exactly one"));
|
||||
e.images.push_back({"a.png", "image/png", "PNGDATA"});
|
||||
e.images.push_back({"b.png", "image/png", "PNGDATA2"});
|
||||
e.mask = UploadFile{"m.png", "image/png", "MASK"};
|
||||
auto form = e.to_multipart();
|
||||
int image_parts = 0;
|
||||
bool mask = false;
|
||||
for (const auto& p : form.parts()) {
|
||||
if (p.name == "image[]") ++image_parts;
|
||||
if (p.name == "mask") mask = true;
|
||||
}
|
||||
ASSERT_EQ(image_parts, 2);
|
||||
ASSERT_TRUE(mask);
|
||||
ASSERT_TRUE(rejects([&] { e.validate(true); }, "JSON mode"));
|
||||
e.images.clear();
|
||||
e.mask.reset();
|
||||
e.image_refs = {Json::parse(R"({"image_url":"https://x/y.png"})")};
|
||||
e.partial_images = 2;
|
||||
Json ej = e.to_json(true);
|
||||
ASSERT_TRUE(ej.find("stream")->as_bool());
|
||||
ASSERT_EQ(ej.find("images")->as_array().size(), static_cast<std::size_t>(1));
|
||||
e.image_refs = {Json::parse(R"({"image_url":"u","file_id":"f"})")};
|
||||
ASSERT_TRUE(rejects([&] { e.to_json(true); }, "exactly one of"));
|
||||
|
||||
ImageVariationRequest v;
|
||||
v.image = {"a.jpg", "image/jpeg", "X"};
|
||||
ASSERT_TRUE(rejects([&] { v.validate(); }, "image/png"));
|
||||
v.image = UploadFile{"a.png", "image/png", std::string(CoreCaps::kMaxVariationBytes + 1, 'x')};
|
||||
ASSERT_TRUE(rejects([&] { v.to_multipart(); }, "file is over"));
|
||||
|
||||
SpeechRequest s;
|
||||
s.model = "gpt-4o-mini-tts";
|
||||
s.input = "hello";
|
||||
ASSERT_TRUE(rejects([&] { s.to_json(false); }, "exactly one"));
|
||||
s.voice = "alloy";
|
||||
s.instructions = "calm";
|
||||
s.speed = 1.5;
|
||||
Json sj = s.to_json(false);
|
||||
ASSERT_EQ(sj.find("voice")->as_string(), std::string("alloy"));
|
||||
s.voice.clear();
|
||||
s.custom_voice_id = "voice_123";
|
||||
ASSERT_EQ(s.to_json(true).find("voice")->find("id")->as_string(), std::string("voice_123"));
|
||||
ASSERT_EQ(s.to_json(true).find("stream_format")->as_string(), std::string("sse"));
|
||||
s.model = "tts-1";
|
||||
ASSERT_TRUE(rejects([&] { s.to_json(false); }, "not supported by tts-1"));
|
||||
s.instructions.clear();
|
||||
ASSERT_TRUE(rejects([&] { s.to_json(true); }, "SSE streaming"));
|
||||
s.model = "gpt-4o-mini-tts";
|
||||
s.input.assign(4097, 'x');
|
||||
ASSERT_TRUE(rejects([&] { s.to_json(false); }, "characters"));
|
||||
s.input = "x";
|
||||
s.speed = 9;
|
||||
ASSERT_TRUE(rejects([&] { s.to_json(false); }, "speed"));
|
||||
|
||||
TranscriptionRequest t;
|
||||
t.file = {"a.mp3", "audio/mpeg", "AUDIO"};
|
||||
t.model = "gpt-4o-transcribe";
|
||||
t.timestamp_granularities = {"word"};
|
||||
ASSERT_TRUE(rejects([&] { t.to_multipart(); }, "verbose_json"));
|
||||
t.response_format = "verbose_json";
|
||||
t.include = {"logprobs"};
|
||||
t.temperature = 0.2;
|
||||
auto tf = t.to_multipart();
|
||||
bool gran = false, temp = false;
|
||||
for (const auto& p : tf.parts()) {
|
||||
if (p.name == "timestamp_granularities[]") gran = true;
|
||||
if (p.name == "temperature") temp = p.data == "0.2";
|
||||
}
|
||||
ASSERT_TRUE(gran);
|
||||
ASSERT_TRUE(temp);
|
||||
t.file.bytes.assign(CoreCaps::kMaxAudioFileBytes + 1, 'x');
|
||||
ASSERT_TRUE(rejects([&] { t.to_multipart(); }, "25 MiB"));
|
||||
TranslationRequest tr;
|
||||
tr.file = {"a.mp3", "audio/mpeg", "A"};
|
||||
tr.model = "gpt-4o-transcribe";
|
||||
ASSERT_TRUE(rejects([&] { tr.to_multipart(); }, "whisper-1"));
|
||||
|
||||
VideoCreateRequest vc;
|
||||
vc.prompt = "a fox";
|
||||
vc.model = "sora-2";
|
||||
vc.seconds = "5";
|
||||
ASSERT_TRUE(rejects([&] { vc.to_json(); }, "seconds"));
|
||||
vc.seconds = "8";
|
||||
vc.size = "1280x720";
|
||||
vc.reference_file_id = "file_1";
|
||||
Json vj = vc.to_json();
|
||||
ASSERT_EQ(vj.find("input_reference")->find("file_id")->as_string(), std::string("file_1"));
|
||||
vc.reference_image_url = "https://x/y.png";
|
||||
ASSERT_TRUE(rejects([&] { vc.to_json(); }, "at most one"));
|
||||
VideoExtendRequest ve;
|
||||
ve.prompt = "more";
|
||||
ve.seconds = "4";
|
||||
ve.video_id = "video_1";
|
||||
ASSERT_EQ(ve.to_json().find("video")->find("id")->as_string(), std::string("video_1"));
|
||||
VideoCharacterRequest vch;
|
||||
vch.name = std::string(81, 'n');
|
||||
vch.video = {"v.mp4", "video/mp4", "V"};
|
||||
ASSERT_TRUE(rejects([&] { vch.to_multipart(); }, "characters"));
|
||||
VoiceFromPromptRequest vp;
|
||||
vp.name = "v";
|
||||
vp.prompt = std::string(2001, 'p');
|
||||
ASSERT_TRUE(rejects([&] { vp.to_json(); }, "characters"));
|
||||
VoiceFromSampleRequest vs;
|
||||
vs.name = "v";
|
||||
vs.consent_id = "../c";
|
||||
ASSERT_TRUE(rejects([&] { vs.to_multipart(); }, "bad consent"));
|
||||
ProvenanceRequest pr;
|
||||
pr.file = {"a.txt", "text/plain", "x"};
|
||||
ASSERT_TRUE(rejects([&] { pr.to_multipart(); }, "image or audio"));
|
||||
}
|
||||
|
||||
TEST(openai_core, services_validate_ids_before_network) {
|
||||
Rig rig;
|
||||
ResponsesService resp(rig.client);
|
||||
ChatCompletionsService chat(rig.client);
|
||||
ModelsService models(rig.client);
|
||||
VideosService videos(rig.client);
|
||||
VoicesService voices(rig.client);
|
||||
ASSERT_TRUE(rejects([&] { resp.retrieve("a/b"); }, "bad response"));
|
||||
ASSERT_TRUE(rejects([&] { resp.cancel(""); }));
|
||||
ASSERT_TRUE(rejects([&] { resp.remove(".."); }));
|
||||
ASSERT_TRUE(rejects([&] { chat.retrieve("x y"); }, "bad completion"));
|
||||
ASSERT_TRUE(rejects([&] { chat.remove("../1"); }));
|
||||
ASSERT_TRUE(rejects([&] { models.retrieve("a/b"); }));
|
||||
ASSERT_TRUE(rejects([&] { models.remove("gpt-4o"); }, "fine-tuned"));
|
||||
ASSERT_TRUE(rejects([&] { videos.retrieve("v?x"); }));
|
||||
ASSERT_TRUE(rejects([&] { videos.content("v1", "gif"); }, "variant"));
|
||||
ASSERT_TRUE(rejects([&] { videos.remix("v1", ""); }, "prompt"));
|
||||
ASSERT_TRUE(rejects([&] { voices.get_consent("a b"); }));
|
||||
ListParams bad;
|
||||
bad.limit = 500;
|
||||
ASSERT_TRUE(rejects([&] { resp.input_items("resp_1", bad); }, "limit"));
|
||||
bad.limit = 5;
|
||||
bad.order = "sideways";
|
||||
ASSERT_TRUE(rejects([&] { chat.messages("chatcmpl_1", bad); }, "order"));
|
||||
ASSERT_TRUE(rejects([&] { resp.input_items_all("resp_1", {}, {}, 99); }, "max_pages"));
|
||||
ASSERT_TRUE(rejects([&] { resp.retrieve("resp_1", {"nope"}); }, "include"));
|
||||
WaitPolicy w;
|
||||
w.interval_seconds = 0;
|
||||
ASSERT_TRUE(rejects([&] { resp.wait("resp_1", w); }, "interval_seconds"));
|
||||
VideoWaitPolicy vw;
|
||||
vw.max_wait_seconds = 9999;
|
||||
ASSERT_TRUE(rejects([&] { videos.wait("video_1", vw); }, "max_wait_seconds"));
|
||||
ASSERT_EQ(rig.mock->send_count(), static_cast<std::size_t>(0));
|
||||
}
|
||||
|
||||
TEST(openai_core, paths_queries_headers_and_auth) {
|
||||
Rig rig("sk-test-KEY", "org-1", "proj-1");
|
||||
ResponsesService resp(rig.client);
|
||||
ChatCompletionsService chat(rig.client);
|
||||
rig.answer(Method::kGet, "/responses/resp_1");
|
||||
resp.retrieve("resp_1", {"reasoning.encrypted_content", "web_search_call.results"});
|
||||
ASSERT_EQ(rig.last_target(), std::string("/v1/responses/resp_1?include%5B%5D=reasoning.encrypted_content&include%5B%5D=web_search_call.results"));
|
||||
auto seen = rig.mock->seen().back();
|
||||
ASSERT_EQ(seen.headers.get_or("Authorization", ""), std::string("Bearer sk-test-KEY"));
|
||||
ASSERT_EQ(seen.headers.get_or("OpenAI-Organization", ""), std::string("org-1"));
|
||||
ASSERT_EQ(seen.headers.get_or("OpenAI-Project", ""), std::string("proj-1"));
|
||||
ASSERT_EQ(seen.target.find("sk-test"), std::string::npos);
|
||||
|
||||
StoredListParams p;
|
||||
p.page.limit = 10;
|
||||
p.page.order = "desc";
|
||||
p.model = "gpt-4o";
|
||||
p.metadata = {{"team", "a"}};
|
||||
rig.answer(Method::kGet, "/chat/completions");
|
||||
chat.list(p);
|
||||
std::string t = rig.last_target();
|
||||
ASSERT_TRUE(t.find("limit=10") != std::string::npos);
|
||||
ASSERT_TRUE(t.find("model=gpt-4o") != std::string::npos);
|
||||
ASSERT_TRUE(t.find("metadata%5Bteam%5D=a") != std::string::npos);
|
||||
|
||||
rig.answer(Method::kPost, "/chat/completions/chatcmpl_1");
|
||||
chat.update("chatcmpl_1", {{"a", "b"}});
|
||||
ASSERT_EQ(rig.last_body().find("metadata")->find("a")->as_string(), std::string("b"));
|
||||
|
||||
rig.answer(Method::kPost, "/responses");
|
||||
ResponseRequest r;
|
||||
r.model = "m";
|
||||
r.input_text = "x";
|
||||
r.betas = {"responses_multi_agent=v1"};
|
||||
resp.create(r);
|
||||
ASSERT_EQ(rig.mock->seen().back().headers.get_or("OpenAI-Beta", ""), std::string("responses_multi_agent=v1"));
|
||||
rig.answer(Method::kDelete, "/responses/resp_1");
|
||||
resp.remove("resp_1");
|
||||
ASSERT_EQ(rig.mock->seen().back().method, std::string("DELETE"));
|
||||
}
|
||||
|
||||
TEST(openai_core, not_configured_before_network) {
|
||||
Rig rig("");
|
||||
ResponsesService resp(rig.client);
|
||||
ResponseRequest r;
|
||||
r.model = "m";
|
||||
r.input_text = "x";
|
||||
ASSERT_THROW(resp.create(r), molo::llm::NotConfiguredError);
|
||||
ModelsService models(rig.client);
|
||||
ASSERT_THROW(models.list(), molo::llm::NotConfiguredError);
|
||||
ASSERT_EQ(rig.mock->send_count(), static_cast<std::size_t>(0));
|
||||
try {
|
||||
models.list();
|
||||
} catch (const molo::llm::ApiError& e) {
|
||||
ASSERT_EQ(e.http_status(), 503);
|
||||
ASSERT_TRUE(e.message().find("OPENAI_API_KEY") != std::string::npos);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
// OpenAI core: parsing of the documented example responses, SSE decoding of
|
||||
// the documented streams, the neutral mapping, and OpenaiChatModel in both
|
||||
// dialects (the mock only records requests and replays the documented
|
||||
// example for the call under test).
|
||||
#include "fixtures.h"
|
||||
#include "llm/sse.h"
|
||||
#include "openai/core/chat_model.h"
|
||||
#include "openai/core/embeddings.h"
|
||||
#include "openai/core/images.h"
|
||||
#include "openai/core/models.h"
|
||||
#include "openai/core/moderations.h"
|
||||
#include "openai/core/wire/chat_wire.h"
|
||||
#include "openai/core/wire/response_events.h"
|
||||
#include "rig.h"
|
||||
|
||||
using namespace molo::openai::core;
|
||||
using namespace oai_test;
|
||||
namespace fx = molo::openai::core::fixtures;
|
||||
|
||||
namespace {
|
||||
std::vector<molo::llm::SseEvent> decode(const std::string& text) {
|
||||
std::vector<molo::llm::SseEvent> out;
|
||||
molo::llm::SseDecoder d("openai", [&](const molo::llm::SseEvent& e) {
|
||||
out.push_back(e);
|
||||
return true;
|
||||
});
|
||||
d.feed(text);
|
||||
d.finish();
|
||||
return out;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST(openai_wire, chat_completion_documents_parse) {
|
||||
auto r = ChatWire::response(parse(fx::k_chat_default_response));
|
||||
ASSERT_TRUE(!r.text().empty());
|
||||
ASSERT_EQ(r.finish_reason, std::string("stop"));
|
||||
ASSERT_TRUE(r.usage.total_tokens > 0);
|
||||
ASSERT_EQ(r.usage.total_tokens, r.usage.input_tokens + r.usage.output_tokens);
|
||||
auto f = ChatWire::response(parse(fx::k_chat_func_response));
|
||||
ASSERT_EQ(f.finish_reason, std::string("tool_calls"));
|
||||
auto calls = f.tool_calls();
|
||||
ASSERT_EQ(calls.size(), static_cast<std::size_t>(1));
|
||||
ASSERT_EQ(calls[0].name, std::string("get_current_weather"));
|
||||
ASSERT_TRUE(calls[0].arguments.is_object());
|
||||
ASSERT_TRUE(calls[0].arguments.has("location"));
|
||||
ASSERT_TRUE(rejects([] { ChatWire::response(Json::object()); }));
|
||||
}
|
||||
|
||||
TEST(openai_wire, responses_documents_parse) {
|
||||
auto t = ResponsesWire::response(parse(fx::k_resp_text_response));
|
||||
ASSERT_TRUE(!t.text().empty());
|
||||
ASSERT_EQ(t.finish_reason, std::string("stop"));
|
||||
ASSERT_TRUE(t.usage.input_tokens > 0);
|
||||
auto f = ResponsesWire::response(parse(fx::k_resp_func_response));
|
||||
ASSERT_EQ(f.finish_reason, std::string("tool_calls"));
|
||||
ASSERT_EQ(f.tool_calls().size(), static_cast<std::size_t>(1));
|
||||
ASSERT_EQ(f.tool_calls()[0].name, std::string("get_current_weather"));
|
||||
auto w = ResponsesWire::response(parse(fx::k_resp_websearch_response));
|
||||
ASSERT_TRUE(!w.text().empty());
|
||||
auto re = ResponsesWire::response(parse(fx::k_resp_reason_response));
|
||||
ASSERT_TRUE(re.usage.total_tokens > 0);
|
||||
Json failed = Json::parse(R"({"id":"r","status":"failed","error":{"code":"server_error","message":"boom"}})");
|
||||
ASSERT_TRUE(rejects([&] { ResponsesWire::response(failed); }, "boom"));
|
||||
Json incomplete = Json::parse(R"({"id":"r","status":"incomplete","incomplete_details":{"reason":"max_output_tokens"},"output":[]})");
|
||||
ASSERT_EQ(ResponsesWire::response(incomplete).finish_reason, std::string("length"));
|
||||
Json queued = Json::parse(R"({"id":"r","status":"queued","output":[]})");
|
||||
ASSERT_EQ(ResponsesWire::response(queued).finish_reason, std::string("queued"));
|
||||
}
|
||||
|
||||
TEST(openai_wire, other_documents_parse) {
|
||||
auto e = EmbeddingsService::parse(parse(fx::k_embed_response));
|
||||
ASSERT_EQ(e.vectors.size(), static_cast<std::size_t>(1));
|
||||
ASSERT_EQ(e.vectors[0].size(), static_cast<std::size_t>(3));
|
||||
// base64 form: three little-endian float32 values 1.0, 2.0, -1.5.
|
||||
Json b64 = Json::parse(R"({"data":[{"index":0,"embedding":"AACAPwAAAEAAAMC/"}],"model":"m","usage":{"prompt_tokens":1,"total_tokens":1}})");
|
||||
auto bv = EmbeddingsService::parse(b64);
|
||||
ASSERT_EQ(bv.vectors[0].size(), static_cast<std::size_t>(3));
|
||||
ASSERT_TRUE(bv.vectors[0][0] == 1.0f && bv.vectors[0][1] == 2.0f && bv.vectors[0][2] == -1.5f);
|
||||
ASSERT_TRUE(rejects([] { EmbeddingsService::parse(Json::parse(R"({"data":[{"index":5,"embedding":[1]}]})")); }, "range"));
|
||||
auto m = ModerationsService::parse(parse(fx::k_mod_image_response));
|
||||
ASSERT_EQ(m.results.size(), static_cast<std::size_t>(1));
|
||||
ASSERT_TRUE(!m.results[0].categories.empty());
|
||||
ASSERT_TRUE(!m.results[0].scores.empty());
|
||||
auto g = ImagesService::parse(parse(fx::k_img_gen_response));
|
||||
ASSERT_TRUE(!g.data.empty());
|
||||
ASSERT_TRUE(g.created > 0);
|
||||
Rig rig;
|
||||
ModelsService models(rig.client);
|
||||
rig.answer(Method::kGet, "/models", fx::k_models_response);
|
||||
ASSERT_TRUE(!models.ids().empty());
|
||||
Json v = parse(fx::k_video_create_response);
|
||||
ASSERT_EQ(v.find("object")->as_string(), std::string("video"));
|
||||
}
|
||||
|
||||
TEST(openai_wire, chat_stream_document_decodes) {
|
||||
auto events = decode(fx::k_chat_stream_response);
|
||||
ASSERT_TRUE(events.size() >= 3);
|
||||
ChatChunkDecoder dec;
|
||||
std::vector<molo::llm::ChatChunk> chunks;
|
||||
for (const auto& e : events) dec.decode(e.data, chunks);
|
||||
std::string text;
|
||||
std::string finish;
|
||||
for (const auto& c : chunks) {
|
||||
text += c.text_delta;
|
||||
if (!c.finish_reason.empty()) finish = c.finish_reason;
|
||||
}
|
||||
ASSERT_TRUE(!text.empty());
|
||||
ASSERT_EQ(finish, std::string("stop"));
|
||||
}
|
||||
|
||||
TEST(openai_wire, tool_call_fragments_carry_ids) {
|
||||
ChatChunkDecoder dec;
|
||||
std::vector<molo::llm::ChatChunk> out;
|
||||
dec.decode(Json::parse(R"({"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"f","arguments":""}}]}}]})"), out);
|
||||
dec.decode(Json::parse(R"({"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"a\":"}}]}}]})"), out);
|
||||
dec.decode(Json::parse(R"({"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}],"usage":null})"), out);
|
||||
dec.decode(Json::parse(R"({"choices":[],"usage":{"prompt_tokens":3,"completion_tokens":2,"total_tokens":5,"prompt_tokens_details":{"cached_tokens":1},"completion_tokens_details":{"reasoning_tokens":1}}})"), out);
|
||||
ASSERT_EQ(out[0].tool_call_id, std::string("call_1"));
|
||||
ASSERT_EQ(out[0].tool_call_name, std::string("f"));
|
||||
ASSERT_EQ(out[1].tool_call_id, std::string("call_1"));
|
||||
ASSERT_EQ(out[1].tool_args_delta, std::string("{\"a\":"));
|
||||
ASSERT_EQ(out[2].finish_reason, std::string("tool_calls"));
|
||||
ASSERT_TRUE(out[3].usage.has_value());
|
||||
ASSERT_EQ(out[3].usage->cached_tokens, 1);
|
||||
ASSERT_EQ(out[3].usage->reasoning_tokens, 1);
|
||||
std::vector<molo::llm::ChatChunk> none;
|
||||
ASSERT_TRUE(rejects([&] { dec.decode(Json::parse(R"({"error":{"type":"server_error","message":"x"}})"), none); }, "x"));
|
||||
}
|
||||
|
||||
TEST(openai_wire, responses_stream_document_decodes) {
|
||||
auto sse = decode(fx::k_resp_stream_response);
|
||||
ASSERT_TRUE(sse.size() >= 8);
|
||||
ResponseAccumulator acc;
|
||||
ResponsesChunkDecoder dec;
|
||||
std::string text;
|
||||
for (const auto& e : sse) {
|
||||
ResponseEvent ev = ResponseEvent::from_sse(e);
|
||||
ASSERT_TRUE(ev.known);
|
||||
acc.add(ev);
|
||||
molo::llm::ChatChunk c;
|
||||
if (dec.decode(ev, c)) text += c.text_delta;
|
||||
}
|
||||
ASSERT_TRUE(acc.done());
|
||||
ASSERT_EQ(acc.status(), std::string("completed"));
|
||||
ASSERT_TRUE(acc.text().rfind("Hi", 0) == 0);
|
||||
ASSERT_EQ(text, acc.text());
|
||||
ASSERT_EQ(acc.unknown_events(), static_cast<std::size_t>(0));
|
||||
ASSERT_TRUE(acc.final_response().is_object());
|
||||
}
|
||||
|
||||
TEST(openai_wire, event_table_and_unknown_events) {
|
||||
ASSERT_EQ(ResponseEvents::count(), static_cast<std::size_t>(59));
|
||||
ASSERT_TRUE(ResponseEvents::known("response.web_search_call.searching"));
|
||||
ASSERT_TRUE(ResponseEvents::known("response.image_generation_call.partial_image"));
|
||||
ASSERT_TRUE(ResponseEvents::known("response.mcp_call_arguments.delta"));
|
||||
ASSERT_TRUE(ResponseEvents::find("error")->terminal);
|
||||
ASSERT_FALSE(ResponseEvents::known("response.future.thing"));
|
||||
ResponseAccumulator acc;
|
||||
molo::llm::SseEvent e;
|
||||
e.data = Json::parse(R"({"type":"response.future.thing","sequence_number":1})");
|
||||
acc.add(ResponseEvent::from_sse(e));
|
||||
ASSERT_EQ(acc.unknown_events(), static_cast<std::size_t>(1));
|
||||
// function call arguments stream into one tool call.
|
||||
ResponsesChunkDecoder dec;
|
||||
molo::llm::ChatChunk c;
|
||||
molo::llm::SseEvent added, delta;
|
||||
added.data = Json::parse(R"({"type":"response.output_item.added","output_index":0,"item":{"id":"fc_1","type":"function_call","call_id":"call_9","name":"f","arguments":""},"sequence_number":1})");
|
||||
delta.data = Json::parse(R"({"type":"response.function_call_arguments.delta","item_id":"fc_1","output_index":0,"delta":"{}","sequence_number":2})");
|
||||
ASSERT_TRUE(dec.decode(ResponseEvent::from_sse(added), c));
|
||||
ASSERT_EQ(c.tool_call_id, std::string("call_9"));
|
||||
ASSERT_TRUE(dec.decode(ResponseEvent::from_sse(delta), c));
|
||||
ASSERT_EQ(c.tool_call_id, std::string("call_9"));
|
||||
ASSERT_EQ(c.tool_args_delta, std::string("{}"));
|
||||
molo::llm::SseEvent failed;
|
||||
failed.data = Json::parse(R"({"type":"response.failed","response":{"error":{"message":"nope"}},"sequence_number":3})");
|
||||
ASSERT_TRUE(rejects([&] { dec.decode(ResponseEvent::from_sse(failed), c); }, "nope"));
|
||||
}
|
||||
|
||||
TEST(openai_wire, neutral_request_mapping) {
|
||||
molo::llm::ChatRequest in;
|
||||
in.model = "gpt-6-astra";
|
||||
in.system = "Be brief.";
|
||||
in.messages.push_back(molo::llm::Message::user("What is 2+2?"));
|
||||
molo::llm::Message a;
|
||||
a.role = molo::llm::Role::kAssistant;
|
||||
molo::llm::ToolCall call;
|
||||
call.id = "call_1";
|
||||
call.name = "calc";
|
||||
call.arguments = Json::parse(R"({"x":2})");
|
||||
a.parts.push_back(molo::llm::ContentPart::of_call(call));
|
||||
in.messages.push_back(a);
|
||||
molo::llm::Message tool;
|
||||
tool.role = molo::llm::Role::kTool;
|
||||
tool.parts.push_back(molo::llm::ContentPart::of_result({"call_1", "4", false}));
|
||||
in.messages.push_back(tool);
|
||||
in.tools.push_back({"calc", "calculate", Json::parse(R"({"type":"object","properties":{"x":{"type":"integer"}},"required":["x"],"additionalProperties":false})")});
|
||||
in.tool_choice = "calc";
|
||||
in.max_tokens = 100;
|
||||
in.temperature = 0.5;
|
||||
in.reasoning_effort = "low";
|
||||
in.response_schema = Json::parse(R"({"type":"object","properties":{"a":{"type":"string"}},"required":["a"],"additionalProperties":false})");
|
||||
|
||||
Json cj = ChatWire::request(in).to_json(false);
|
||||
const auto& msgs = cj.find("messages")->as_array();
|
||||
ASSERT_EQ(msgs.size(), static_cast<std::size_t>(4));
|
||||
ASSERT_EQ(msgs[0].find("role")->as_string(), std::string("system"));
|
||||
ASSERT_EQ(msgs[2].find("tool_calls")->as_array()[0].find("function")->find("arguments")->as_string(), std::string("{\"x\":2}"));
|
||||
ASSERT_EQ(msgs[3].find("role")->as_string(), std::string("tool"));
|
||||
ASSERT_EQ(cj.find("max_completion_tokens")->as_int(), 100L);
|
||||
ASSERT_EQ(cj.find("response_format")->find("type")->as_string(), std::string("json_schema"));
|
||||
ASSERT_EQ(cj.find("tool_choice")->find("function")->find("name")->as_string(), std::string("calc"));
|
||||
WireOptions dev;
|
||||
dev.developer_role = true;
|
||||
ASSERT_EQ(ChatWire::request(in, dev).to_json(false).find("messages")->as_array()[0].find("role")->as_string(), std::string("developer"));
|
||||
|
||||
Json rj = ResponsesWire::request(in).to_json(false);
|
||||
ASSERT_EQ(rj.find("instructions")->as_string(), std::string("Be brief."));
|
||||
const auto& items = rj.find("input")->as_array();
|
||||
ASSERT_EQ(items[1].find("type")->as_string(), std::string("function_call"));
|
||||
ASSERT_EQ(items[2].find("type")->as_string(), std::string("function_call_output"));
|
||||
ASSERT_EQ(rj.find("max_output_tokens")->as_int(), 100L);
|
||||
ASSERT_EQ(rj.find("reasoning")->find("effort")->as_string(), std::string("low"));
|
||||
ASSERT_EQ(rj.find("text")->find("format")->find("name")->as_string(), std::string("response"));
|
||||
ASSERT_EQ(rj.find("tools")->as_array()[0].find("name")->as_string(), std::string("calc"));
|
||||
in.max_tokens = 5;
|
||||
ASSERT_TRUE(rejects([&] { ResponsesWire::request(in).to_json(false); }, "at least 16"));
|
||||
in.max_tokens = 50;
|
||||
in.stop = {"x"};
|
||||
ASSERT_TRUE(rejects([&] { ResponsesWire::request(in); }, "stop"));
|
||||
molo::llm::ChatRequest img;
|
||||
img.model = "m";
|
||||
molo::llm::Message um;
|
||||
um.parts.push_back(molo::llm::ContentPart::of_text("look"));
|
||||
molo::llm::ImageData id;
|
||||
id.mime = "image/png";
|
||||
id.bytes = "PNG";
|
||||
um.parts.push_back(molo::llm::ContentPart::of_image(id));
|
||||
img.messages.push_back(um);
|
||||
Json ij = ChatWire::request(img).to_json(false);
|
||||
ASSERT_EQ(ij.find("messages")->as_array()[0].find("content")->as_array()[1].find("image_url")->find("url")->as_string(), std::string("data:image/png;base64,UE5H"));
|
||||
}
|
||||
|
||||
TEST(openai_wire, chat_model_both_dialects) {
|
||||
molo::llm::ChatRequest in;
|
||||
in.model = "gpt-6-astra";
|
||||
in.messages.push_back(molo::llm::Message::user("Hello!"));
|
||||
{
|
||||
Rig rig;
|
||||
rig.answer(Method::kPost, "/responses", fx::k_resp_text_response);
|
||||
OpenaiChatModel m(rig.client);
|
||||
ASSERT_EQ(m.vendor(), std::string("openai"));
|
||||
auto r = m.generate(in);
|
||||
ASSERT_TRUE(!r.text().empty());
|
||||
ASSERT_EQ(rig.last_body().find("model")->as_string(), std::string("gpt-6-astra"));
|
||||
ASSERT_FALSE(rig.last_body().has("stream"));
|
||||
}
|
||||
{
|
||||
Rig rig;
|
||||
rig.answer(Method::kPost, "/chat/completions", fx::k_chat_default_response);
|
||||
ChatModelConfig cfg;
|
||||
cfg.dialect = Dialect::kChatCompletions;
|
||||
OpenaiChatModel m(rig.client, cfg);
|
||||
auto r = m.generate(in);
|
||||
ASSERT_TRUE(!r.text().empty());
|
||||
ASSERT_EQ(rig.last_target(), std::string("/v1/chat/completions"));
|
||||
}
|
||||
Rig none("");
|
||||
OpenaiChatModel off(none.client);
|
||||
ASSERT_THROW(off.generate(in), molo::llm::NotConfiguredError);
|
||||
ASSERT_THROW(off.stream(in, [](const molo::llm::ChatChunk&) { return true; }), molo::llm::NotConfiguredError);
|
||||
molo::llm::ChatRequest empty;
|
||||
ASSERT_TRUE(rejects([&] { off.generate(empty); }, "model"));
|
||||
}
|
||||
Reference in New Issue
Block a user