Files
moloapp/tests/src/vendors/azure/test_chat_model.cpp
T

153 lines
6.5 KiB
C++

// AzureChatModel: wire bodies per dialect and parsing of the documented
// examples (chat completion, stream transcript, response object).
#include "rig.h"
#include "vendors/azure/chat_model.h"
using namespace molo::azure;
using namespace az_test;
namespace {
// Documented chat completion example (OpenAI / Azure OpenAI reference).
const char kChat[] =
R"({"id":"chatcmpl-123","object":"chat.completion","created":1677652288,"model":"gpt-4o-mini","choices":[{"index":0,"message":{"role":"assistant","content":"Hello there, how may I assist you today?"},"finish_reason":"stop"}],"usage":{"prompt_tokens":9,"completion_tokens":12,"total_tokens":21}})";
// Documented stream transcript (chat.completion.chunk).
const char kStream[] =
"data: {\"id\":\"chatcmpl-123\",\"object\":\"chat.completion.chunk\",\"created\":1694268190,\"model\":\"gpt-4o-mini\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\"},\"finish_reason\":null}]}\n\n"
"data: {\"id\":\"chatcmpl-123\",\"object\":\"chat.completion.chunk\",\"created\":1694268190,\"model\":\"gpt-4o-mini\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hello\"},\"finish_reason\":null}]}\n\n"
"data: {\"id\":\"chatcmpl-123\",\"object\":\"chat.completion.chunk\",\"created\":1694268190,\"model\":\"gpt-4o-mini\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n"
"data: [DONE]\n\n";
// Documented Responses example.
const char kResponse[] =
R"({"id":"resp_67ccd2bed1ec8190b14f964abc0542670bb6a6b452d3795b","object":"response","created_at":1741476542,"status":"completed","model":"gpt-4.1-2025-04-14","output":[{"type":"message","id":"msg_67ccd2bf17f0819081ff3bb2cf6508e60bb6a6b452d3795b","status":"completed","role":"assistant","content":[{"type":"output_text","text":"In a peaceful grove beneath a silver moon, a unicorn named Lumina discovered a hidden pool.","annotations":[]}]}],"usage":{"input_tokens":36,"output_tokens":87,"total_tokens":123}})";
molo::llm::ChatRequest req(const std::string& model = "gpt4o") {
molo::llm::ChatRequest r;
r.model = model;
r.system = "Be brief.";
r.messages.push_back(molo::llm::Message::user("Say hi"));
r.max_tokens = 32;
return r;
}
} // namespace
TEST(azure_chat_model, generate_over_chat_completions_v1) {
Rig rig(key_settings());
rig.openai().answer = kChat;
AzureChatModel m(rig.client);
ASSERT_EQ(m.vendor(), std::string("azure"));
auto out = m.generate(req());
ASSERT_EQ(out.text(), std::string("Hello there, how may I assist you today?"));
ASSERT_EQ(out.finish_reason, std::string("stop"));
ASSERT_EQ(out.usage.total_tokens, 21);
ASSERT_EQ(rig.last().target, std::string("/openai/v1/chat/completions"));
Json b = rig.last_body();
ASSERT_EQ(b.find("model")->as_string(), std::string("gpt4o"));
ASSERT_TRUE(b.has("messages"));
ASSERT_FALSE(b.has("stream"));
}
TEST(azure_chat_model, generate_over_the_dated_api_uses_the_deployment_url) {
Rig rig(key_settings("2024-10-21"));
rig.openai().answer = kChat;
AzureChatModel m(rig.client);
m.generate(req());
ASSERT_EQ(rig.last().target, std::string("/openai/deployments/gpt4o/chat/completions?api-version=2024-10-21"));
ASSERT_FALSE(rig.last_body().has("model"));
}
TEST(azure_chat_model, stream_decodes_the_documented_transcript) {
Rig rig(key_settings());
rig.openai().content_type = "text/event-stream";
rig.openai().answer = kStream;
AzureChatModel m(rig.client);
std::string text, finish;
m.stream(req(), [&](const molo::llm::ChatChunk& c) {
text += c.text_delta;
if (!c.finish_reason.empty()) finish = c.finish_reason;
return true;
});
ASSERT_EQ(text, std::string("Hello"));
ASSERT_EQ(finish, std::string("stop"));
Json b = rig.last_body();
ASSERT_TRUE(b.find("stream")->as_bool());
ASSERT_TRUE(b.find("stream_options")->find("include_usage")->as_bool());
}
TEST(azure_chat_model, stream_stops_when_the_consumer_says_so) {
Rig rig(key_settings());
rig.openai().content_type = "text/event-stream";
rig.openai().answer = kStream;
AzureChatModel m(rig.client);
int calls = 0;
m.stream(req(), [&](const molo::llm::ChatChunk&) { return ++calls < 2; });
ASSERT_EQ(calls, 2);
}
TEST(azure_chat_model, responses_dialect) {
Rig rig(key_settings());
rig.openai().answer = kResponse;
ChatModelConfig cfg;
cfg.dialect = Dialect::kResponses;
AzureChatModel m(rig.client, cfg);
auto out = m.generate(req("gpt41"));
ASSERT_TRUE(out.text().rfind("In a peaceful grove", 0) == 0);
ASSERT_EQ(out.usage.total_tokens, 123);
ASSERT_EQ(rig.last().target, std::string("/openai/v1/responses"));
ASSERT_EQ(rig.last_body().find("model")->as_string(), std::string("gpt41"));
}
TEST(azure_chat_model, foundry_dialect_goes_to_the_model_inference_host) {
Rig rig(key_settings());
rig.foundry().answer = kChat;
ChatModelConfig cfg;
cfg.dialect = Dialect::kFoundry;
cfg.extra_parameters = "drop";
AzureChatModel m(rig.client, cfg);
auto out = m.generate(req("Phi-4"));
ASSERT_TRUE(out.text().rfind("Hello there", 0) == 0);
const Seen& s = rig.foundry().seen.back();
ASSERT_EQ(s.target, std::string("/models/chat/completions?api-version=2024-05-01-preview"));
ASSERT_EQ(s.headers.get_or("extra-parameters", ""), std::string("drop"));
Json b = Json::parse(s.body);
ASSERT_TRUE(b.has("max_tokens"));
ASSERT_FALSE(b.has("max_completion_tokens"));
ASSERT_EQ(rig.openai().seen.size(), static_cast<std::size_t>(0));
}
TEST(azure_chat_model, validation_before_traffic) {
Rig rig(key_settings());
AzureChatModel m(rig.client);
molo::llm::ChatRequest r = req();
r.model = "";
ASSERT_TRUE(rejects([&] { m.generate(r); }, "deployment"));
r.model = "bad name";
ASSERT_TRUE(rejects([&] { m.generate(r); }, "deployment"));
r = req();
r.messages.clear();
ASSERT_TRUE(rejects([&] { m.generate(r); }, "messages"));
ASSERT_EQ(rig.openai().seen.size(), static_cast<std::size_t>(0));
}
TEST(azure_chat_model, not_configured_is_a_503_before_traffic) {
Settings none;
none.endpoint = "https://res.openai.azure.com";
Rig rig(none);
AzureChatModel m(rig.client);
ASSERT_FALSE(m.configured());
try {
m.generate(req());
FAIL("expected not configured");
} catch (const molo::llm::NotConfiguredError& e) {
ASSERT_EQ(e.http_status(), 503);
}
ASSERT_EQ(rig.openai().seen.size(), static_cast<std::size_t>(0));
}
TEST(azure_chat_model, wire_body_is_exposed_without_traffic) {
Rig rig(key_settings());
AzureChatModel m(rig.client);
Json b = m.wire_body(req(), false);
ASSERT_EQ(b.find("model")->as_string(), std::string("gpt4o"));
ASSERT_EQ(rig.openai().seen.size(), static_cast<std::size_t>(0));
}