feat(clients): deepseek: chat, beta, responses, anthropic, files, search, fetch, spec checker, demo
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
// Shared rig for the DeepSeek tests: a Platform over two MockTransports (one
|
||||
// per surface) with a fake key. The mock only records the requests the real
|
||||
// client builds; replies are documented examples taken verbatim from
|
||||
// api-docs.deepseek.com and labelled FIXTURE where they appear. Nothing here
|
||||
// verifies vendor behavior (see src/deepseek/SPEC.md ## Unverified).
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "clients/transports/mock_transport.h"
|
||||
#include "deepseek/platform.h"
|
||||
#include "framework.h"
|
||||
|
||||
namespace deepseek_test {
|
||||
|
||||
using molo::clients::ClientResponse;
|
||||
using molo::clients::HeaderMap;
|
||||
using molo::clients::Method;
|
||||
using molo::clients::transports::MockExpectation;
|
||||
using molo::clients::transports::MockTransport;
|
||||
using Json = molo::clients::body::Json;
|
||||
|
||||
inline ClientResponse reply(const std::string& body, int status = 200,
|
||||
const std::string& type = "application/json") {
|
||||
HeaderMap h;
|
||||
h.set("Content-Type", type);
|
||||
return ClientResponse(status, h, body);
|
||||
}
|
||||
|
||||
inline ClientResponse sse(const std::string& body) { return reply(body, 200, "text/event-stream"); }
|
||||
|
||||
inline const char* kKey = "sk-TESTKEY-DO-NOT-LEAK";
|
||||
|
||||
struct Rig {
|
||||
MockTransport* openai = nullptr;
|
||||
MockTransport* anthropic = nullptr;
|
||||
molo::deepseek::Platform platform;
|
||||
|
||||
explicit Rig(const std::string& key = kKey) {
|
||||
auto a = std::make_unique<MockTransport>();
|
||||
auto b = std::make_unique<MockTransport>();
|
||||
openai = a.get();
|
||||
anthropic = b.get();
|
||||
platform = molo::deepseek::Platform::with(std::move(a), std::move(b), key);
|
||||
}
|
||||
// Allows a path to answer more than once (retried statuses).
|
||||
void always(MockTransport* t, Method m, const std::string& path, ClientResponse r, int times = 5) {
|
||||
MockExpectation e;
|
||||
e.method = m;
|
||||
e.path = path;
|
||||
e.response = std::move(r);
|
||||
e.times = times;
|
||||
t->expect(e);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace deepseek_test
|
||||
@@ -0,0 +1,428 @@
|
||||
// DeepSeek chat over the real ApiClient with a recording transport. Replies
|
||||
// are documented examples (FIXTURE, verbatim from api-docs.deepseek.com);
|
||||
// the assertions are on what WE send and on how WE parse them.
|
||||
#include "deepseek/chat.h"
|
||||
#include "deepseek/chat_model.h"
|
||||
#include "deepseek/errors.h"
|
||||
#include "support.h"
|
||||
|
||||
using namespace molo::deepseek;
|
||||
using namespace deepseek_test;
|
||||
using molo::llm::ApiError;
|
||||
|
||||
namespace {
|
||||
bool has(const std::string& hay, const std::string& needle) { return hay.find(needle) != std::string::npos; }
|
||||
|
||||
// FIXTURE: api/create-chat-completion "Example" (200, no streaming)
|
||||
const char* kChatExample =
|
||||
"{\"id\":\"930c60df-bf64-41c9-a88e-3ec75f81e00e\",\"choices\":[{\"finish_reason\":\"stop\",\"index\":0,"
|
||||
"\"message\":{\"content\":\"Hello! How can I help you today?\",\"role\":\"assistant\"},\"logprobs\":null}],"
|
||||
"\"created\":1705651092,\"model\":\"deepseek-flash\",\"object\":\"chat.completion\",\"system_fingerprint\":\"fp_7a09fdf9c2\","
|
||||
"\"usage\":{\"completion_tokens\":10,\"prompt_tokens\":16,\"total_tokens\":26,\"prompt_tokens_details\":{\"cached_tokens\":0},"
|
||||
"\"prompt_cache_hit_tokens\":0,\"prompt_cache_miss_tokens\":16}}";
|
||||
|
||||
// FIXTURE: api/create-chat-completion "200 (Streaming)" example, with the
|
||||
// keep-alive comment the Rate Limit guide documents (": keep-alive") added
|
||||
// between events and one empty line before the first byte.
|
||||
const char* kStreamExample =
|
||||
": keep-alive\n\n"
|
||||
"data: {\"id\": \"1f633d8bfc032625086f14113c411638\", \"choices\": [{\"index\": 0, \"delta\": {\"content\": \"\", \"role\": \"assistant\"}, \"finish_reason\": null, \"logprobs\": null}], \"created\": 1718345013, \"model\": \"deepseek-flash\", \"system_fingerprint\": \"fp_a49d71b8a1\", \"object\": \"chat.completion.chunk\"}\n\n"
|
||||
"data: {\"choices\": [{\"delta\": {\"content\": \"Hello\", \"role\": \"assistant\"}, \"finish_reason\": null, \"index\": 0, \"logprobs\": null}], \"created\": 1718345013, \"id\": \"1f633d8bfc032625086f14113c411638\", \"model\": \"deepseek-flash\", \"object\": \"chat.completion.chunk\", \"system_fingerprint\": \"fp_a49d71b8a1\"}\n\n"
|
||||
": keep-alive\n\n"
|
||||
"data: {\"choices\": [{\"delta\": {\"content\": \"!\", \"role\": \"assistant\"}, \"finish_reason\": null, \"index\": 0, \"logprobs\": null}], \"created\": 1718345013, \"id\": \"1f633d8bfc032625086f14113c411638\", \"model\": \"deepseek-flash\", \"object\": \"chat.completion.chunk\", \"system_fingerprint\": \"fp_a49d71b8a1\"}\n\n"
|
||||
"data: {\"choices\": [{\"delta\": {\"content\": \"\", \"role\": null}, \"finish_reason\": \"stop\", \"index\": 0, \"logprobs\": null}], \"created\": 1718345013, \"id\": \"1f633d8bfc032625086f14113c411638\", \"model\": \"deepseek-flash\", \"object\": \"chat.completion.chunk\", \"system_fingerprint\": \"fp_a49d71b8a1\", \"usage\": {\"completion_tokens\": 9, \"prompt_tokens\": 17, \"total_tokens\": 26, \"prompt_tokens_details\": {\"cached_tokens\": 0}, \"prompt_cache_hit_tokens\": 0, \"prompt_cache_miss_tokens\": 17}}\n\n"
|
||||
"data: [DONE]\n\n";
|
||||
|
||||
// Thinking-mode transcript shaped like the documented sample output
|
||||
// (guides/thinking_mode, "Turn 1.1": reasoning_content, content and a get_date
|
||||
// tool call). Built from the guide's values; the framing is the documented
|
||||
// chunk shape. LABELLED: not a recorded vendor response.
|
||||
const char* kThinkingToolStream =
|
||||
"data: {\"id\":\"s1\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null,\"reasoning_content\":\"The user is asking about the weather\"},\"finish_reason\":null}],\"created\":1,\"model\":\"deepseek-flash\",\"object\":\"chat.completion.chunk\"}\n\n"
|
||||
"data: {\"id\":\"s1\",\"choices\":[{\"index\":0,\"delta\":{\"reasoning_content\":\" in Hangzhou tomorrow.\"},\"finish_reason\":null}],\"created\":1,\"model\":\"deepseek-flash\",\"object\":\"chat.completion.chunk\"}\n\n"
|
||||
"data: {\"id\":\"s1\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Let me check.\"},\"finish_reason\":null}],\"created\":1,\"model\":\"deepseek-flash\",\"object\":\"chat.completion.chunk\"}\n\n"
|
||||
"data: {\"id\":\"s1\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_00_kw66qNnNto11bSfJVIdlV5Oo\",\"type\":\"function\",\"function\":{\"name\":\"get_weather\",\"arguments\":\"\"}}]},\"finish_reason\":null}],\"created\":1,\"model\":\"deepseek-flash\",\"object\":\"chat.completion.chunk\"}\n\n"
|
||||
"data: {\"id\":\"s1\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"{\\\"location\\\": \"}}]},\"finish_reason\":null}],\"created\":1,\"model\":\"deepseek-flash\",\"object\":\"chat.completion.chunk\"}\n\n"
|
||||
"data: {\"id\":\"s1\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"Hangzhou\\\"}\"}}]},\"finish_reason\":null}],\"created\":1,\"model\":\"deepseek-flash\",\"object\":\"chat.completion.chunk\"}\n\n"
|
||||
"data: {\"id\":\"s1\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\"},\"finish_reason\":\"tool_calls\"}],\"created\":1,\"model\":\"deepseek-flash\",\"object\":\"chat.completion.chunk\",\"usage\":{\"completion_tokens\":30,\"prompt_tokens\":100,\"total_tokens\":130,\"prompt_cache_hit_tokens\":64,\"prompt_cache_miss_tokens\":36,\"completion_tokens_details\":{\"reasoning_tokens\":12}}}\n\n"
|
||||
"data: [DONE]\n\n";
|
||||
} // namespace
|
||||
|
||||
TEST(deepseek_flow, create_sends_bearer_json_and_parses_the_documented_example) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/chat/completions", reply(kChatExample));
|
||||
Chat chat(rig.platform.openai);
|
||||
ChatParams p = ChatParams::prompt("Hello!");
|
||||
p.add(Messages::system("You are a helpful assistant."));
|
||||
ChatResult r = chat.create(p);
|
||||
ASSERT_EQ(r.first().content, "Hello! How can I help you today?");
|
||||
ASSERT_EQ(r.first().finish_reason, "stop");
|
||||
ASSERT_EQ(r.model, "deepseek-flash");
|
||||
ASSERT_EQ(r.created, 1705651092L);
|
||||
ASSERT_EQ(r.usage.input_tokens, 16);
|
||||
ASSERT_EQ(r.usage.output_tokens, 10);
|
||||
ASSERT_EQ(r.usage.cache_miss_tokens, 16);
|
||||
ASSERT_EQ(r.usage.cache_hit_tokens, 0);
|
||||
ASSERT_EQ(r.usage.cache_hit_ratio(), 0.0);
|
||||
auto s = rig.openai->seen()[0];
|
||||
ASSERT_EQ(s.method, "POST");
|
||||
ASSERT_EQ(s.headers.get_or("Authorization", ""), std::string("Bearer ") + kKey);
|
||||
ASSERT_TRUE(has(s.headers.get_or("Content-Type", ""), "application/json"));
|
||||
ASSERT_TRUE(has(s.body, "\"model\":\"deepseek-flash\""));
|
||||
ASSERT_EQ(rig.anthropic->send_count(), 0u);
|
||||
ASSERT_EQ(s.target.find(kKey), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(deepseek_flow, keep_alive_blank_lines_before_the_json_body_are_harmless) {
|
||||
// Rate Limit guide: non-streaming requests may receive empty lines first.
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/chat/completions", reply(std::string("\n\n\n") + kChatExample));
|
||||
Chat chat(rig.platform.openai);
|
||||
ASSERT_EQ(chat.create(ChatParams::prompt("hi")).first().content, "Hello! How can I help you today?");
|
||||
}
|
||||
|
||||
TEST(deepseek_flow, not_configured_has_no_traffic) {
|
||||
Rig rig("");
|
||||
Chat chat(rig.platform.openai);
|
||||
try {
|
||||
chat.create(ChatParams::prompt("hi"));
|
||||
FAIL("expected");
|
||||
} catch (const molo::llm::NotConfiguredError& e) {
|
||||
ASSERT_EQ(e.http_status(), 503);
|
||||
ASSERT_TRUE(has(e.message(), "DEEPSEEK_API_KEY"));
|
||||
}
|
||||
ASSERT_EQ(rig.openai->send_count(), 0u);
|
||||
ASSERT_FALSE(rig.platform.configured());
|
||||
}
|
||||
|
||||
TEST(deepseek_flow, invalid_input_never_reaches_the_network) {
|
||||
Rig rig;
|
||||
Chat chat(rig.platform.openai);
|
||||
ChatParams p;
|
||||
ASSERT_THROW(chat.create(p), ApiError);
|
||||
ASSERT_THROW(chat.stream(p, nullptr), ApiError);
|
||||
ASSERT_EQ(rig.openai->send_count(), 0u);
|
||||
}
|
||||
|
||||
TEST(deepseek_flow, stream_documented_transcript_with_keep_alive_comments) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/chat/completions", sse(kStreamExample));
|
||||
Chat chat(rig.platform.openai);
|
||||
std::string text;
|
||||
int chunks = 0;
|
||||
ChatResult r = chat.stream(ChatParams::prompt("Hi"), [&](const StreamDelta& d) {
|
||||
text += d.content;
|
||||
++chunks;
|
||||
return true;
|
||||
});
|
||||
ASSERT_EQ(text, "Hello!");
|
||||
ASSERT_EQ(chunks, 4); // the ": keep-alive" comments produce no chunk
|
||||
ASSERT_EQ(r.first().content, "Hello!");
|
||||
ASSERT_EQ(r.first().finish_reason, "stop");
|
||||
ASSERT_EQ(r.id, "1f633d8bfc032625086f14113c411638");
|
||||
ASSERT_EQ(r.system_fingerprint, "fp_a49d71b8a1");
|
||||
// usage rides on the last content chunk, not a separate one
|
||||
ASSERT_EQ(r.usage.input_tokens, 17);
|
||||
ASSERT_EQ(r.usage.cache_miss_tokens, 17);
|
||||
ASSERT_EQ(r.usage.total_tokens, 26);
|
||||
auto s = rig.openai->seen()[0];
|
||||
ASSERT_TRUE(has(s.body, "\"stream\":true"));
|
||||
ASSERT_TRUE(has(s.body, "\"stream_options\":{\"include_usage\":true}"));
|
||||
ASSERT_EQ(s.headers.get_or("Accept", ""), "text/event-stream");
|
||||
}
|
||||
|
||||
TEST(deepseek_flow, stream_stop_early_and_truncation_is_loud) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/chat/completions", sse(kStreamExample));
|
||||
Chat chat(rig.platform.openai);
|
||||
ChatResult r = chat.stream(ChatParams::prompt("Hi"), [&](const StreamDelta& d) { return d.content != "Hello"; });
|
||||
ASSERT_EQ(r.first().content, "Hello"); // stopped by the sink: no truncation error
|
||||
// a transcript cut before any finish_reason (and not stopped by us) throws
|
||||
Rig cut;
|
||||
cut.openai->expect_response(Method::kPost, "/chat/completions",
|
||||
sse("data: {\"id\":\"x\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"par\"},\"finish_reason\":null}]}\n\n"));
|
||||
Chat chat2(cut.platform.openai);
|
||||
try {
|
||||
chat2.stream(ChatParams::prompt("Hi"), nullptr);
|
||||
FAIL("expected STREAM_TRUNCATED");
|
||||
} catch (const ApiError& e) {
|
||||
ASSERT_EQ(e.type(), "STREAM_TRUNCATED");
|
||||
}
|
||||
}
|
||||
|
||||
TEST(deepseek_flow, stream_error_body_is_an_api_error) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/chat/completions",
|
||||
sse("{\"error\":{\"message\":\"overloaded\",\"type\":\"server_error\"}}"));
|
||||
Chat chat(rig.platform.openai);
|
||||
try {
|
||||
chat.stream(ChatParams::prompt("Hi"), nullptr);
|
||||
FAIL("expected");
|
||||
} catch (const ApiError& e) {
|
||||
ASSERT_EQ(e.type(), "server_error");
|
||||
}
|
||||
}
|
||||
|
||||
TEST(deepseek_flow, thinking_tool_stream_folds_reasoning_and_fragments) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/chat/completions", sse(kThinkingToolStream));
|
||||
Chat chat(rig.platform.openai);
|
||||
ChatParams p = ChatParams::prompt("How's the weather in Hangzhou Tomorrow");
|
||||
ToolDef t;
|
||||
t.name = "get_weather";
|
||||
p.tools.push_back(t);
|
||||
std::string reasoning, content;
|
||||
std::vector<std::string> arg_deltas;
|
||||
ChatResult r = chat.stream(p, [&](const StreamDelta& d) {
|
||||
reasoning += d.reasoning;
|
||||
content += d.content;
|
||||
for (const auto& f : d.tools) arg_deltas.push_back(f.arguments_delta);
|
||||
return true;
|
||||
});
|
||||
const ChatChoice& c = r.first();
|
||||
ASSERT_EQ(reasoning, "The user is asking about the weather in Hangzhou tomorrow.");
|
||||
ASSERT_EQ(c.reasoning_content, reasoning);
|
||||
ASSERT_EQ(content, "Let me check.");
|
||||
ASSERT_EQ(c.finish_reason, "tool_calls");
|
||||
ASSERT_EQ(c.tool_calls.size(), 1u);
|
||||
ASSERT_EQ(c.tool_calls[0].id, "call_00_kw66qNnNto11bSfJVIdlV5Oo");
|
||||
ASSERT_EQ(c.tool_calls[0].name, "get_weather");
|
||||
ASSERT_EQ(c.tool_calls[0].arguments, "{\"location\": \"Hangzhou\"}");
|
||||
ASSERT_TRUE(c.tool_calls[0].arguments_valid());
|
||||
ASSERT_EQ(c.tool_calls[0].parsed().find("location")->as_string(), "Hangzhou");
|
||||
ASSERT_EQ(arg_deltas.size(), 3u);
|
||||
ASSERT_EQ(r.usage.reasoning_tokens, 12);
|
||||
ASSERT_EQ(r.usage.cache_hit_tokens, 64);
|
||||
ASSERT_EQ(r.usage.cache_hit_ratio(), 0.64);
|
||||
// the history message carries reasoning_content and tool_calls as returned
|
||||
Json h = c.history_message();
|
||||
ASSERT_EQ(h.find("reasoning_content")->as_string(), reasoning);
|
||||
ASSERT_EQ(h.find("tool_calls")->as_array()[0].find("function")->find("arguments")->as_string(), "{\"location\": \"Hangzhou\"}");
|
||||
}
|
||||
|
||||
TEST(deepseek_flow, conversation_tool_loop_passes_reasoning_back_every_round) {
|
||||
// Two turns scripted from the guide's sample output values (thinking_mode, Turn 1.1 and 1.2).
|
||||
const char* turn1 =
|
||||
"{\"id\":\"a\",\"object\":\"chat.completion\",\"created\":1,\"model\":\"deepseek-flash\",\"choices\":[{\"index\":0,\"finish_reason\":\"tool_calls\","
|
||||
"\"message\":{\"role\":\"assistant\",\"content\":\"Let me check tomorrow's weather in Hangzhou for you. First, let me get tomorrow's date.\","
|
||||
"\"reasoning_content\":\"The user is asking about the weather in Hangzhou tomorrow. I need to get tomorrow's date first.\","
|
||||
"\"tool_calls\":[{\"id\":\"call_00_kw66qNnNto11bSfJVIdlV5Oo\",\"type\":\"function\",\"function\":{\"name\":\"get_date\",\"arguments\":\"{}\"}}]}}]}";
|
||||
const char* turn2 =
|
||||
"{\"id\":\"b\",\"object\":\"chat.completion\",\"created\":2,\"model\":\"deepseek-flash\",\"choices\":[{\"index\":0,\"finish_reason\":\"stop\","
|
||||
"\"message\":{\"role\":\"assistant\",\"content\":\"Cloudy 7~13C tomorrow.\",\"reasoning_content\":\"The weather result is in.\"}}],"
|
||||
"\"usage\":{\"prompt_tokens\":10,\"completion_tokens\":5,\"total_tokens\":15}}";
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/chat/completions", reply(turn1));
|
||||
rig.openai->expect_response(Method::kPost, "/chat/completions", reply(turn2));
|
||||
Chat chat(rig.platform.openai);
|
||||
ChatParams base;
|
||||
ToolDef t;
|
||||
t.name = "get_date";
|
||||
t.description = "Get the current date";
|
||||
base.tools.push_back(t);
|
||||
Conversation conv(base);
|
||||
conv.user("How's the weather in Hangzhou Tomorrow");
|
||||
std::vector<std::string> called;
|
||||
ChatResult last = conv.run_tools(chat, [&](const std::string& name, const Json& args, const std::string& id) {
|
||||
called.push_back(name + ":" + id + ":" + args.dump());
|
||||
return std::string("2026-04-19");
|
||||
});
|
||||
ASSERT_EQ(last.first().content, "Cloudy 7~13C tomorrow.");
|
||||
ASSERT_EQ(called.size(), 1u);
|
||||
ASSERT_EQ(called[0], "get_date:call_00_kw66qNnNto11bSfJVIdlV5Oo:{}");
|
||||
auto seen = rig.openai->seen();
|
||||
ASSERT_EQ(seen.size(), 2u);
|
||||
// request 2 carries the round-1 reasoning_content, the tool_calls and the tool result
|
||||
ASSERT_TRUE(has(seen[1].body, "\"reasoning_content\":\"The user is asking about the weather in Hangzhou tomorrow. I need to get tomorrow's date first.\""));
|
||||
ASSERT_TRUE(has(seen[1].body, "\"tool_call_id\":\"call_00_kw66qNnNto11bSfJVIdlV5Oo\""));
|
||||
ASSERT_TRUE(has(seen[1].body, "\"content\":\"2026-04-19\""));
|
||||
ASSERT_FALSE(has(seen[0].body, "reasoning_content"));
|
||||
// history: user, assistant(tool), tool, assistant(final)
|
||||
ASSERT_EQ(conv.params().messages.as_array().size(), 4u);
|
||||
// the round cap is explicit and bounded
|
||||
ASSERT_THROW(conv.run_tools(chat, nullptr, 0), ApiError);
|
||||
ASSERT_THROW(conv.run_tools(chat, nullptr, 17), ApiError);
|
||||
}
|
||||
|
||||
TEST(deepseek_flow, tool_loop_stops_at_the_round_cap) {
|
||||
const char* looping =
|
||||
"{\"id\":\"a\",\"created\":1,\"model\":\"m\",\"choices\":[{\"index\":0,\"finish_reason\":\"tool_calls\",\"message\":{\"role\":\"assistant\",\"content\":\"\","
|
||||
"\"tool_calls\":[{\"id\":\"c1\",\"type\":\"function\",\"function\":{\"name\":\"get_date\",\"arguments\":\"{}\"}}]}}]}";
|
||||
Rig rig;
|
||||
rig.always(rig.openai, Method::kPost, "/chat/completions", reply(looping));
|
||||
Chat chat(rig.platform.openai);
|
||||
ChatParams base;
|
||||
ToolDef t;
|
||||
t.name = "get_date";
|
||||
base.tools.push_back(t);
|
||||
Conversation conv(base);
|
||||
conv.user("loop");
|
||||
ASSERT_THROW(conv.run_tools(chat, [](const std::string&, const Json&, const std::string&) { return std::string("x"); }, 3), ApiError);
|
||||
ASSERT_EQ(rig.openai->send_count(), 3u);
|
||||
}
|
||||
|
||||
TEST(deepseek_flow, prefix_completion_uses_the_beta_path) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/beta/chat/completions", reply(kChatExample));
|
||||
Chat chat(rig.platform.openai);
|
||||
ChatParams p = ChatParams::prompt("Please write quick sort code");
|
||||
p.stop = {"```"};
|
||||
chat.prefix(p, "```python\n");
|
||||
auto s = rig.openai->seen()[0];
|
||||
ASSERT_TRUE(has(s.body, "\"prefix\":true"));
|
||||
ASSERT_TRUE(has(s.body, "\"stop\":[\"```\"]"));
|
||||
ASSERT_THROW(chat.prefix(p, ""), ApiError);
|
||||
}
|
||||
|
||||
TEST(deepseek_flow, error_statuses_map_to_api_errors_with_the_documented_shape) {
|
||||
// FIXTURE shape: the body observed from the live host for an invalid key
|
||||
// (src/deepseek/SPEC.md ## Verified live): type authentication_error.
|
||||
const char* body = "{\"error\":{\"message\":\"Authentication Fails, Your api key: ****alid is invalid (request_id: 7a6eb948)\",\"type\":\"authentication_error\",\"param\":null,\"code\":\"invalid_request_error\"}}";
|
||||
for (int status : {401, 402, 422}) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/chat/completions", reply(body, status));
|
||||
Chat chat(rig.platform.openai);
|
||||
try {
|
||||
chat.create(ChatParams::prompt("hi"));
|
||||
FAIL("expected");
|
||||
} catch (const ApiError& e) {
|
||||
ASSERT_EQ(e.http_status(), status);
|
||||
ASSERT_EQ(e.type(), "authentication_error");
|
||||
ASSERT_TRUE(has(e.message(), "Authentication Fails"));
|
||||
ASSERT_EQ(std::string(e.what()).find(kKey), std::string::npos);
|
||||
ASSERT_TRUE(Errors::find(status) != nullptr);
|
||||
}
|
||||
}
|
||||
// server busy: 503 on a POST is not retried by the client (body was sent); the error surfaces
|
||||
Rig busy;
|
||||
busy.always(busy.openai, Method::kPost, "/chat/completions", reply("Server Overloaded", 503, "text/plain"), 5);
|
||||
Chat chat(busy.platform.openai);
|
||||
try {
|
||||
chat.create(ChatParams::prompt("hi"));
|
||||
FAIL("expected");
|
||||
} catch (const ApiError& e) {
|
||||
ASSERT_EQ(e.http_status(), 503);
|
||||
ASSERT_TRUE(Errors::retryable(e.http_status()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(deepseek_model, generate_maps_neutral_request_and_response) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/chat/completions", reply(kChatExample));
|
||||
auto model = std::make_shared<DeepseekChatModel>(std::make_shared<Chat>(rig.platform.openai));
|
||||
ASSERT_EQ(model->vendor(), "deepseek");
|
||||
molo::llm::ChatRequest req;
|
||||
req.system = "Be brief.";
|
||||
req.messages.push_back(molo::llm::Message::user("Hello!"));
|
||||
req.max_tokens = 64;
|
||||
req.reasoning_effort = "low";
|
||||
req.extra = Json::object();
|
||||
req.extra.set("thinking", Json::parse("{\"type\":\"enabled\"}"));
|
||||
molo::llm::ChatResponse r = model->generate(req);
|
||||
ASSERT_EQ(r.text(), "Hello! How can I help you today?");
|
||||
ASSERT_EQ(r.finish_reason, "stop");
|
||||
ASSERT_EQ(r.usage.input_tokens, 16);
|
||||
ASSERT_EQ(r.usage.output_tokens, 10);
|
||||
ASSERT_EQ(r.usage.total_tokens, 26);
|
||||
auto s = rig.openai->seen()[0];
|
||||
ASSERT_TRUE(has(s.body, "\"reasoning_effort\":\"low\""));
|
||||
ASSERT_TRUE(has(s.body, "\"thinking\":{\"type\":\"enabled\"}"));
|
||||
ASSERT_TRUE(has(s.body, "\"role\":\"system\""));
|
||||
ASSERT_TRUE(has(s.body, "\"max_tokens\":64"));
|
||||
// response_schema is rejected loudly
|
||||
molo::llm::ChatRequest bad = req;
|
||||
bad.response_schema = Json::parse("{\"type\":\"object\"}");
|
||||
ASSERT_THROW(model->generate(bad), ApiError);
|
||||
ASSERT_EQ(rig.openai->send_count(), 1u);
|
||||
}
|
||||
|
||||
TEST(deepseek_model, tools_and_reasoning_round_trip_through_the_neutral_types) {
|
||||
const char* resp =
|
||||
"{\"id\":\"a\",\"object\":\"chat.completion\",\"created\":1,\"model\":\"deepseek-flash\",\"choices\":[{\"index\":0,\"finish_reason\":\"tool_calls\","
|
||||
"\"message\":{\"role\":\"assistant\",\"content\":\"\",\"reasoning_content\":\"need the date\","
|
||||
"\"tool_calls\":[{\"id\":\"call_1\",\"type\":\"function\",\"function\":{\"name\":\"get_date\",\"arguments\":\"{\\\"tz\\\":\\\"UTC\\\"}\"}},"
|
||||
"{\"id\":\"call_2\",\"type\":\"function\",\"function\":{\"name\":\"get_date\",\"arguments\":\"{not json\"}}]}}],"
|
||||
"\"usage\":{\"prompt_tokens\":10,\"completion_tokens\":5,\"total_tokens\":15,\"prompt_cache_hit_tokens\":8,\"prompt_cache_miss_tokens\":2,\"completion_tokens_details\":{\"reasoning_tokens\":3}}}";
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/chat/completions", reply(resp));
|
||||
rig.openai->expect_response(Method::kPost, "/chat/completions", reply(kChatExample));
|
||||
DeepseekChatModel model(std::make_shared<Chat>(rig.platform.openai));
|
||||
molo::llm::ChatRequest req;
|
||||
req.messages.push_back(molo::llm::Message::user("date?"));
|
||||
molo::llm::ToolSpec spec;
|
||||
spec.name = "get_date";
|
||||
spec.description = "Get the current date";
|
||||
req.tools.push_back(spec);
|
||||
molo::llm::ChatResponse r = model.generate(req);
|
||||
ASSERT_EQ(r.finish_reason, "tool_calls");
|
||||
ASSERT_EQ(r.usage.cached_tokens, 8);
|
||||
ASSERT_EQ(r.usage.reasoning_tokens, 3);
|
||||
ASSERT_EQ(r.message.parts[0].reasoning, "need the date");
|
||||
auto calls = r.tool_calls();
|
||||
ASSERT_EQ(calls.size(), 2u);
|
||||
ASSERT_EQ(calls[0].arguments.find("tz")->as_string(), "UTC");
|
||||
ASSERT_EQ(calls[1].arguments.find("_invalid_json")->as_string(), "{not json");
|
||||
// feed the assistant message back plus tool results: reasoning must be sent (tools present)
|
||||
req.messages.push_back(r.message);
|
||||
molo::llm::Message results;
|
||||
results.role = molo::llm::Role::kTool;
|
||||
results.parts.push_back(molo::llm::ContentPart::of_result({"call_1", "2026-04-19", false}));
|
||||
results.parts.push_back(molo::llm::ContentPart::of_result({"call_2", "bad arguments", true}));
|
||||
req.messages.push_back(results);
|
||||
model.generate(req);
|
||||
std::string second = rig.openai->seen()[1].body;
|
||||
ASSERT_TRUE(has(second, "\"reasoning_content\":\"need the date\""));
|
||||
ASSERT_TRUE(has(second, "\"tool_call_id\":\"call_1\""));
|
||||
ASSERT_TRUE(has(second, "\"tool_call_id\":\"call_2\""));
|
||||
ASSERT_TRUE(has(second, "\"arguments\":\"{\\\"tz\\\":\\\"UTC\\\"}\""));
|
||||
// and without tools the same history drops it
|
||||
req.tools.clear();
|
||||
ChatParams p = model.to_params(req);
|
||||
ASSERT_FALSE(has(p.to_json().dump(), "reasoning_content"));
|
||||
}
|
||||
|
||||
TEST(deepseek_model, images_become_data_urls_and_stream_maps_chunks) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/chat/completions", sse(kThinkingToolStream));
|
||||
DeepseekChatModel model(std::make_shared<Chat>(rig.platform.openai));
|
||||
molo::llm::ChatRequest req;
|
||||
molo::llm::Message m = molo::llm::Message::user("What is this?");
|
||||
molo::llm::ImageData img;
|
||||
img.mime = "image/png";
|
||||
img.bytes = "PNGBYTES";
|
||||
m.parts.push_back(molo::llm::ContentPart::of_image(img));
|
||||
req.messages.push_back(m);
|
||||
molo::llm::ToolSpec spec;
|
||||
spec.name = "get_weather";
|
||||
req.tools.push_back(spec);
|
||||
std::string text, reasoning, args, finish;
|
||||
std::string call_id, call_name;
|
||||
int usage_seen = 0;
|
||||
model.stream(req, [&](const molo::llm::ChatChunk& c) {
|
||||
text += c.text_delta;
|
||||
reasoning += c.reasoning_delta;
|
||||
args += c.tool_args_delta;
|
||||
if (!c.tool_call_id.empty()) call_id = c.tool_call_id;
|
||||
if (!c.tool_call_name.empty()) call_name = c.tool_call_name;
|
||||
if (!c.finish_reason.empty()) finish = c.finish_reason;
|
||||
if (c.usage) ++usage_seen;
|
||||
return true;
|
||||
});
|
||||
ASSERT_EQ(text, "Let me check.");
|
||||
ASSERT_EQ(reasoning, "The user is asking about the weather in Hangzhou tomorrow.");
|
||||
ASSERT_EQ(args, "{\"location\": \"Hangzhou\"}");
|
||||
ASSERT_EQ(call_id, "call_00_kw66qNnNto11bSfJVIdlV5Oo");
|
||||
ASSERT_EQ(call_name, "get_weather");
|
||||
ASSERT_EQ(finish, "tool_calls");
|
||||
ASSERT_EQ(usage_seen, 1);
|
||||
ASSERT_TRUE(has(rig.openai->seen()[0].body, "data:image/png;base64,UE5HQllURVM="));
|
||||
// early stop from the consumer
|
||||
Rig rig2;
|
||||
rig2.openai->expect_response(Method::kPost, "/chat/completions", sse(kStreamExample));
|
||||
DeepseekChatModel model2(std::make_shared<Chat>(rig2.platform.openai));
|
||||
molo::llm::ChatRequest r2;
|
||||
r2.messages.push_back(molo::llm::Message::user("Hi"));
|
||||
int n = 0;
|
||||
model2.stream(r2, [&](const molo::llm::ChatChunk&) { return ++n < 2; });
|
||||
ASSERT_EQ(n, 2);
|
||||
}
|
||||
@@ -0,0 +1,659 @@
|
||||
// DeepSeek: models, balance, files, search, fetch, FIM, Responses and the
|
||||
// Anthropic-compatible surface. Replies are documented examples (FIXTURE,
|
||||
// verbatim from api-docs.deepseek.com); assertions are on our requests and
|
||||
// on our parsing.
|
||||
#include "deepseek/account.h"
|
||||
#include "deepseek/anthropic.h"
|
||||
#include "deepseek/files.h"
|
||||
#include "deepseek/fim.h"
|
||||
#include "deepseek/responses.h"
|
||||
#include "deepseek/web.h"
|
||||
#include "support.h"
|
||||
|
||||
using namespace molo::deepseek;
|
||||
using namespace deepseek_test;
|
||||
using molo::llm::ApiError;
|
||||
|
||||
namespace {
|
||||
bool has(const std::string& hay, const std::string& needle) { return hay.find(needle) != std::string::npos; }
|
||||
|
||||
// FIXTURE: api/list-models "Example"
|
||||
const char* kModels =
|
||||
"{\"object\":\"list\",\"data\":[{\"id\":\"deepseek-flash\",\"object\":\"model\",\"owned_by\":\"deepseek\",\"name\":\"DeepSeek-V4.1-Flash\","
|
||||
"\"context_window\":1048576,\"max_output_tokens\":393216,\"input_modalities\":[\"text\",\"image\"],\"output_modalities\":[\"text\"],"
|
||||
"\"effort\":{\"supported_levels\":[\"low\",\"high\",\"max\"],\"default_level\":\"high\"},"
|
||||
"\"api_capabilities\":{\"anthropic_messages\":{\"system_prompt_update\":\"in-history\"}}},"
|
||||
"{\"id\":\"deepseek-v4-pro\",\"object\":\"model\",\"owned_by\":\"deepseek\",\"name\":\"DeepSeek-V4-Pro\",\"context_window\":1048576,"
|
||||
"\"max_output_tokens\":393216,\"input_modalities\":[\"text\"],\"output_modalities\":[\"text\"],"
|
||||
"\"effort\":{\"supported_levels\":[\"low\",\"high\",\"max\"],\"default_level\":\"high\"},"
|
||||
"\"api_capabilities\":{\"anthropic_messages\":{\"system_prompt_update\":\"leading-only\"}}}]}";
|
||||
// FIXTURE: api/get-user-balance "Example"
|
||||
const char* kBalance =
|
||||
"{\"is_available\":true,\"balance_infos\":[{\"currency\":\"CNY\",\"total_balance\":\"110.00\",\"granted_balance\":\"10.00\",\"topped_up_balance\":\"100.00\"}]}";
|
||||
// FIXTURE: api/create-file "Example"
|
||||
const char* kFile =
|
||||
"{\"id\":\"file-api-0a1b2c3d4e5f60718293a4b5c6d7e8f9\",\"object\":\"file\",\"bytes\":102400,\"created_at\":1700000000,\"filename\":\"image.jpg\",\"purpose\":\"user_data\"}";
|
||||
// FIXTURE: api/list-files "Example"
|
||||
const char* kFileList =
|
||||
"{\"object\":\"list\",\"data\":[{\"id\":\"file-api-0a1b2c3d4e5f60718293a4b5c6d7e8f9\",\"object\":\"file\",\"bytes\":102400,\"created_at\":1700000000,\"filename\":\"image.jpg\",\"purpose\":\"user_data\"}],"
|
||||
"\"first_id\":\"file-api-0a1b2c3d4e5f60718293a4b5c6d7e8f9\",\"last_id\":\"file-api-0a1b2c3d4e5f60718293a4b5c6d7e8f9\",\"has_more\":false}";
|
||||
// FIXTURE: api/delete-file "Example"
|
||||
const char* kFileDeleted = "{\"id\":\"file-api-0a1b2c3d4e5f60718293a4b5c6d7e8f9\",\"object\":\"file\",\"deleted\":true}";
|
||||
// FIXTURE: guides/files_api (Anthropic-compatible file object and list)
|
||||
const char* kAnthropicFile =
|
||||
"{\"id\":\"file-api-xxxxxxxxxxxxxxxx\",\"type\":\"file\",\"size_bytes\":102400,\"created_at\":\"2026-01-01T00:00:00+00:00\",\"filename\":\"image.jpg\",\"mime_type\":\"image/jpeg\"}";
|
||||
// FIXTURE: api/search "Example"
|
||||
const char* kSearch =
|
||||
"{\"results\":[{\"rerank_score\":2.4,\"title\":\"DeepSeek V4 model released\",\"url\":\"https://example.com/deepseek-v4\","
|
||||
"\"snippet\":\"DeepSeek V4 is now available, with tool calling and long-context support.\","
|
||||
"\"summary\":\"DeepSeek has released the V4 model. The release adds improved tool calling and long-context support for agent workflows.\","
|
||||
"\"content\":\"DeepSeek V4 is now generally available...\",\"site_name\":\"Example News\",\"site_icon\":\"https://example.com/favicon.ico\","
|
||||
"\"date\":\"2026-06-01T00:00:00+00:00\",\"authority\":3}]}";
|
||||
// FIXTURE: api/fetch "Example"
|
||||
const char* kFetch = "{\"title\":\"Example article\",\"content\":\"# Example article\\n\\nArticle content.\"}";
|
||||
// FIXTURE: api/create-response "Example" (200 no streaming)
|
||||
const char* kResponse =
|
||||
"{\"id\":\"24778070-1c36-4ae0-a4bd-870afc7fc13e\",\"object\":\"response\",\"created_at\":1753000000,\"status\":\"completed\",\"model\":\"deepseek-flash\","
|
||||
"\"output\":[{\"type\":\"reasoning\",\"id\":\"rs_1\",\"status\":\"completed\",\"content\":[{\"type\":\"reasoning_text\",\"text\":\"The user greets me. I should reply politely.\"}],\"summary\":[]},"
|
||||
"{\"type\":\"message\",\"id\":\"msg_1\",\"status\":\"completed\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"Hello! How can I help you today?\",\"annotations\":[]}]}],"
|
||||
"\"usage\":{\"input_tokens\":22,\"input_tokens_details\":{\"cached_tokens\":0},\"output_tokens\":29,\"output_tokens_details\":{\"reasoning_tokens\":27},\"total_tokens\":51},"
|
||||
"\"store\":false,\"parallel_tool_calls\":true,\"previous_response_id\":null,\"error\":null,\"incomplete_details\":null}";
|
||||
// FIXTURE shape: api/create-response streaming example (event names and payload keys),
|
||||
// values completed to a full terminal event. LABELLED: abbreviated transcript.
|
||||
const char* kResponseStream =
|
||||
"event: response.created\ndata: {\"type\": \"response.created\", \"sequence_number\": 0, \"response\": {\"id\": \"r1\", \"object\": \"response\", \"status\": \"in_progress\"}}\n\n"
|
||||
": keep-alive\n\n"
|
||||
"event: response.reasoning_text.delta\ndata: {\"type\": \"response.reasoning_text.delta\", \"sequence_number\": 4, \"item_id\": \"rs_1\", \"output_index\": 0, \"content_index\": 0, \"delta\": \"The user\"}\n\n"
|
||||
"event: response.output_text.delta\ndata: {\"type\": \"response.output_text.delta\", \"sequence_number\": 11, \"item_id\": \"msg_1\", \"output_index\": 1, \"content_index\": 0, \"delta\": \"Hello\"}\n\n"
|
||||
"event: response.output_text.delta\ndata: {\"type\": \"response.output_text.delta\", \"sequence_number\": 12, \"item_id\": \"msg_1\", \"output_index\": 1, \"content_index\": 0, \"delta\": \"!\"}\n\n"
|
||||
"event: response.completed\ndata: {\"type\": \"response.completed\", \"sequence_number\": 20, \"response\": {\"id\": \"r1\", \"object\": \"response\", \"status\": \"completed\", \"model\": \"deepseek-flash\","
|
||||
" \"output\": [{\"type\":\"message\",\"id\":\"msg_1\",\"status\":\"completed\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"Hello!\"}]}],"
|
||||
" \"usage\": {\"input_tokens\": 5, \"input_tokens_details\": {\"cached_tokens\": 2}, \"output_tokens\": 2, \"output_tokens_details\": {\"reasoning_tokens\": 0}, \"total_tokens\": 7}}}\n\n";
|
||||
// FIXTURE: api/create-completion response shape (FIM), values filled in. LABELLED.
|
||||
const char* kFim =
|
||||
"{\"id\":\"f1\",\"choices\":[{\"finish_reason\":\"stop\",\"index\":0,\"logprobs\":null,\"text\":\" if a < 2:\\n return a\\n\"}],\"created\":1,\"model\":\"deepseek-flash\","
|
||||
"\"object\":\"text_completion\",\"usage\":{\"completion_tokens\":12,\"prompt_tokens\":20,\"total_tokens\":32,\"prompt_cache_hit_tokens\":4,\"prompt_cache_miss_tokens\":16}}";
|
||||
// Anthropic Messages shape (the compatible endpoint answers the Anthropic format).
|
||||
// LABELLED: shape from Anthropic's public format, values invented; DeepSeek's own reply never seen.
|
||||
const char* kMessage =
|
||||
"{\"id\":\"msg_1\",\"type\":\"message\",\"role\":\"assistant\",\"model\":\"deepseek-flash\",\"content\":[{\"type\":\"thinking\",\"thinking\":\"greet\"},{\"type\":\"text\",\"text\":\"Hi there\"},"
|
||||
"{\"type\":\"tool_use\",\"id\":\"tu_1\",\"name\":\"get_weather\",\"input\":{\"location\":\"Hangzhou\"}}],\"stop_reason\":\"tool_use\",\"usage\":{\"input_tokens\":10,\"output_tokens\":4}}";
|
||||
const char* kMessageStream =
|
||||
"event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_1\",\"type\":\"message\",\"role\":\"assistant\",\"model\":\"deepseek-flash\",\"content\":[],\"usage\":{\"input_tokens\":10,\"output_tokens\":1}}}\n\n"
|
||||
"event: ping\ndata: {\"type\":\"ping\"}\n\n"
|
||||
"event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n"
|
||||
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"Hi\"}}\n\n"
|
||||
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\" there\"}}\n\n"
|
||||
"event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"
|
||||
"event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"tool_use\",\"id\":\"tu_1\",\"name\":\"get_weather\",\"input\":{}}}\n\n"
|
||||
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"location\\\":\"}}\n\n"
|
||||
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"\\\"Hangzhou\\\"}\"}}\n\n"
|
||||
"event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":1}\n\n"
|
||||
"event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\"},\"usage\":{\"output_tokens\":15}}\n\n"
|
||||
"event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n";
|
||||
|
||||
const std::string kPng = std::string("\x89PNG\r\n\x1a\n", 8) + "rest-of-the-image";
|
||||
} // namespace
|
||||
|
||||
TEST(deepseek_account, models_and_balance_parse_the_documented_examples) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kGet, "/models", reply(kModels));
|
||||
rig.openai->expect_response(Method::kGet, "/user/balance", reply(kBalance));
|
||||
Account acct(rig.platform.openai);
|
||||
ModelList models = acct.models();
|
||||
ASSERT_EQ(models.models.size(), 2u);
|
||||
const ModelInfo* flash = models.find("deepseek-flash");
|
||||
ASSERT_TRUE(flash != nullptr);
|
||||
ASSERT_EQ(flash->name, "DeepSeek-V4.1-Flash");
|
||||
ASSERT_EQ(flash->context_window, 1048576L);
|
||||
ASSERT_EQ(flash->max_output_tokens, 393216L);
|
||||
ASSERT_TRUE(flash->accepts_images());
|
||||
ASSERT_EQ(flash->effort_levels.size(), 3u);
|
||||
ASSERT_EQ(flash->effort_default, "high");
|
||||
ASSERT_EQ(flash->system_prompt_update, "in-history");
|
||||
ASSERT_FALSE(models.find("deepseek-v4-pro")->accepts_images());
|
||||
ASSERT_EQ(models.find("deepseek-v4-pro")->system_prompt_update, "leading-only");
|
||||
ASSERT_TRUE(models.find("nope") == nullptr);
|
||||
Balance b = acct.balance();
|
||||
ASSERT_TRUE(b.available);
|
||||
ASSERT_EQ(b.entries.size(), 1u);
|
||||
ASSERT_EQ(b.entries[0].currency, "CNY");
|
||||
ASSERT_EQ(b.entries[0].total, "110.00");
|
||||
ASSERT_EQ(b.entries[0].topped_up, "100.00");
|
||||
ASSERT_EQ(rig.openai->seen()[0].method, "GET");
|
||||
ASSERT_EQ(rig.openai->seen()[0].headers.get_or("Authorization", ""), std::string("Bearer ") + kKey);
|
||||
}
|
||||
|
||||
TEST(deepseek_account, model_catalog_names_and_anthropic_mapping) {
|
||||
ASSERT_TRUE(ModelCatalog::status_of("deepseek-flash") == ModelStatus::kCurrent);
|
||||
ASSERT_TRUE(ModelCatalog::status_of("deepseek-v4-flash") == ModelStatus::kLegacyAlias);
|
||||
ASSERT_TRUE(ModelCatalog::status_of("deepseek-chat") == ModelStatus::kRetired);
|
||||
ASSERT_TRUE(ModelCatalog::status_of("whatever") == ModelStatus::kUnknown);
|
||||
ASSERT_EQ(ModelCatalog::served_as("deepseek-v4-flash"), "deepseek-flash");
|
||||
ASSERT_EQ(ModelCatalog::served_as("deepseek-v4-pro"), "deepseek-v4-pro");
|
||||
ASSERT_EQ(ModelCatalog::anthropic_mapped("claude-opus-4-8"), "deepseek-v4-pro");
|
||||
ASSERT_EQ(ModelCatalog::anthropic_mapped("claude-sonnet-5"), "deepseek-flash");
|
||||
ASSERT_EQ(ModelCatalog::anthropic_mapped("claude-haiku-4"), "deepseek-flash");
|
||||
ASSERT_EQ(ModelCatalog::anthropic_mapped("gpt-4"), "gpt-4");
|
||||
ASSERT_THROW(ModelCatalog::at(99), ApiError);
|
||||
}
|
||||
|
||||
TEST(deepseek_account, unconfigured_means_no_traffic) {
|
||||
Rig rig("");
|
||||
Account acct(rig.platform.openai);
|
||||
ASSERT_THROW(acct.models(), ApiError);
|
||||
ASSERT_THROW(acct.balance(), ApiError);
|
||||
ASSERT_EQ(rig.openai->send_count(), 0u);
|
||||
}
|
||||
|
||||
TEST(deepseek_files, upload_list_get_delete_requests_and_parsing) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/files", reply(kFile));
|
||||
rig.openai->expect_response(Method::kGet, "/files", reply(kFileList));
|
||||
rig.openai->expect_response(Method::kGet, "/files/file-api-0a1b2c3d4e5f60718293a4b5c6d7e8f9", reply(kFile));
|
||||
rig.openai->expect_response(Method::kDelete, "/files/file-api-0a1b2c3d4e5f60718293a4b5c6d7e8f9", reply(kFileDeleted));
|
||||
Files files(rig.platform.openai);
|
||||
UploadParams up;
|
||||
up.filename = "image.jpg";
|
||||
up.data = kPng;
|
||||
up.expires_after_seconds = 3600;
|
||||
FileInfo f = files.upload(up);
|
||||
ASSERT_EQ(f.id, "file-api-0a1b2c3d4e5f60718293a4b5c6d7e8f9");
|
||||
ASSERT_EQ(f.bytes, 102400L);
|
||||
ASSERT_EQ(f.purpose, "user_data");
|
||||
ASSERT_FALSE(f.expires_at.has_value());
|
||||
auto up_req = rig.openai->seen()[0];
|
||||
ASSERT_TRUE(has(up_req.headers.get_or("Content-Type", ""), "multipart/form-data"));
|
||||
ASSERT_TRUE(has(up_req.body, "name=\"purpose\"\r\nContent-Type: text/plain\r\n\r\nuser_data"));
|
||||
ASSERT_TRUE(has(up_req.body, "name=\"expires_after[anchor]\"\r\nContent-Type: text/plain\r\n\r\ncreated_at"));
|
||||
ASSERT_TRUE(has(up_req.body, "name=\"expires_after[seconds]\"\r\nContent-Type: text/plain\r\n\r\n3600"));
|
||||
ASSERT_TRUE(has(up_req.body, "filename=\"image.jpg\""));
|
||||
ASSERT_TRUE(has(up_req.body, "image/png"));
|
||||
FileListParams lp;
|
||||
lp.limit = 50;
|
||||
lp.order = "desc";
|
||||
lp.after = "file-api-prev";
|
||||
FileList l = files.list(lp);
|
||||
ASSERT_EQ(l.files.size(), 1u);
|
||||
ASSERT_FALSE(l.has_more);
|
||||
ASSERT_EQ(l.first_id, l.last_id);
|
||||
ASSERT_EQ(rig.openai->seen()[1].target, "/files?limit=50&purpose=user_data&order=desc&after=file-api-prev");
|
||||
ASSERT_EQ(files.get("file-api-0a1b2c3d4e5f60718293a4b5c6d7e8f9").filename, "image.jpg");
|
||||
FileDeleted d = files.remove("file-api-0a1b2c3d4e5f60718293a4b5c6d7e8f9");
|
||||
ASSERT_TRUE(d.deleted);
|
||||
ASSERT_EQ(rig.openai->seen()[3].method, "DELETE");
|
||||
}
|
||||
|
||||
TEST(deepseek_files, input_is_validated_before_the_network) {
|
||||
Rig rig;
|
||||
Files files(rig.platform.openai);
|
||||
UploadParams up;
|
||||
up.filename = "a.jpg";
|
||||
up.data = "not an image";
|
||||
ASSERT_THROW(files.upload(up), ApiError);
|
||||
up.data = kPng;
|
||||
up.filename = "";
|
||||
ASSERT_THROW(files.upload(up), ApiError);
|
||||
up.filename = "a/b.png";
|
||||
ASSERT_THROW(files.upload(up), ApiError);
|
||||
up.filename = std::string(513, 'a');
|
||||
ASSERT_THROW(files.upload(up), ApiError);
|
||||
up.filename = "a.png";
|
||||
up.expires_after_seconds = 3599;
|
||||
ASSERT_THROW(files.upload(up), ApiError);
|
||||
up.expires_after_seconds = 2592001;
|
||||
ASSERT_THROW(files.upload(up), ApiError);
|
||||
up.expires_after_seconds = std::nullopt;
|
||||
up.data.clear();
|
||||
ASSERT_THROW(files.upload(up), ApiError);
|
||||
FileListParams lp;
|
||||
lp.limit = 0;
|
||||
ASSERT_THROW(files.list(lp), ApiError);
|
||||
lp.limit = 1001;
|
||||
ASSERT_THROW(files.list(lp), ApiError);
|
||||
lp.limit = 10;
|
||||
lp.order = "sideways";
|
||||
ASSERT_THROW(files.list(lp), ApiError);
|
||||
lp.order = "";
|
||||
lp.after = "../x";
|
||||
ASSERT_THROW(files.list(lp), ApiError);
|
||||
ASSERT_THROW(files.get("a/b"), ApiError);
|
||||
ASSERT_THROW(files.remove(""), ApiError);
|
||||
ASSERT_EQ(rig.openai->send_count(), 0u);
|
||||
// format sniffing
|
||||
ASSERT_EQ(UploadParams::sniff(std::string("\xFF\xD8\xFF\xE0", 4)), "jpeg");
|
||||
ASSERT_EQ(UploadParams::sniff("GIF89a...."), "gif");
|
||||
ASSERT_EQ(UploadParams::sniff("RIFF\x01\x02\x03\x04WEBPVP8 "), "webp");
|
||||
ASSERT_EQ(UploadParams::sniff("%PDF-1.7"), "");
|
||||
}
|
||||
|
||||
TEST(deepseek_files, list_all_walks_pages_and_reports_truncation) {
|
||||
const char* page1 =
|
||||
"{\"object\":\"list\",\"data\":[{\"id\":\"file-api-1\",\"object\":\"file\",\"bytes\":1,\"created_at\":1,\"filename\":\"a\",\"purpose\":\"user_data\"}],\"first_id\":\"file-api-1\",\"last_id\":\"file-api-1\",\"has_more\":true}";
|
||||
const char* page2 =
|
||||
"{\"object\":\"list\",\"data\":[{\"id\":\"file-api-2\",\"object\":\"file\",\"bytes\":2,\"created_at\":2,\"filename\":\"b\",\"purpose\":\"user_data\"}],\"first_id\":\"file-api-2\",\"last_id\":\"file-api-2\",\"has_more\":false}";
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kGet, "/files", reply(page1));
|
||||
rig.openai->expect_response(Method::kGet, "/files", reply(page2));
|
||||
Files files(rig.platform.openai);
|
||||
FileList all = files.list_all(1);
|
||||
ASSERT_EQ(all.files.size(), 2u);
|
||||
ASSERT_FALSE(all.truncated);
|
||||
ASSERT_TRUE(has(rig.openai->seen()[1].target, "after=file-api-1"));
|
||||
// a server that always says has_more stops at the page cap and says so
|
||||
Rig endless;
|
||||
endless.always(endless.openai, Method::kGet, "/files", reply(page1), 100);
|
||||
Files files2(endless.platform.openai);
|
||||
FileList cut = files2.list_all(1);
|
||||
ASSERT_TRUE(cut.truncated);
|
||||
ASSERT_EQ(endless.openai->send_count(), 20u);
|
||||
}
|
||||
|
||||
TEST(deepseek_files, anthropic_surface_uses_header_key_and_beta_header) {
|
||||
Rig rig;
|
||||
rig.anthropic->expect_response(Method::kPost, "/anthropic/v1/files", reply(kAnthropicFile));
|
||||
rig.anthropic->expect_response(Method::kGet, "/anthropic/v1/files", reply("{\"data\":[" + std::string(kAnthropicFile) + "],\"first_id\":\"file-api-xxxxxxxxxxxxxxxx\",\"last_id\":\"file-api-xxxxxxxxxxxxxxxx\",\"has_more\":false}"));
|
||||
rig.anthropic->expect_response(Method::kGet, "/anthropic/v1/files/file-api-xxxxxxxxxxxxxxxx", reply(kAnthropicFile));
|
||||
rig.anthropic->expect_response(Method::kDelete, "/anthropic/v1/files/file-api-xxxxxxxxxxxxxxxx",
|
||||
reply("{\"id\":\"file-api-xxxxxxxxxxxxxxxx\",\"type\":\"file_deleted\"}"));
|
||||
AnthropicFiles files(rig.platform.anthropic);
|
||||
UploadParams up;
|
||||
up.filename = "image.jpg";
|
||||
up.data = kPng;
|
||||
FileInfo f = files.upload(up);
|
||||
ASSERT_EQ(f.bytes, 102400L);
|
||||
ASSERT_EQ(f.mime_type, "image/jpeg");
|
||||
ASSERT_EQ(f.created_text, "2026-01-01T00:00:00+00:00");
|
||||
auto s = rig.anthropic->seen()[0];
|
||||
ASSERT_EQ(s.headers.get_or("x-api-key", ""), kKey);
|
||||
ASSERT_EQ(s.headers.get_or("Authorization", ""), "");
|
||||
ASSERT_EQ(s.headers.get_or("anthropic-beta", ""), "files-api-2025-04-14");
|
||||
ASSERT_FALSE(has(s.body, "purpose")); // the Anthropic shape has no purpose field
|
||||
AnthropicFileListParams lp;
|
||||
lp.limit = 20;
|
||||
lp.after_id = "file-api-a";
|
||||
FileList l = files.list(lp);
|
||||
ASSERT_EQ(l.files.size(), 1u);
|
||||
ASSERT_EQ(rig.anthropic->seen()[1].target, "/anthropic/v1/files?limit=20&after_id=file-api-a");
|
||||
ASSERT_EQ(rig.anthropic->seen()[1].headers.get_or("anthropic-beta", ""), "files-api-2025-04-14");
|
||||
ASSERT_EQ(files.get("file-api-xxxxxxxxxxxxxxxx").filename, "image.jpg");
|
||||
ASSERT_TRUE(files.remove("file-api-xxxxxxxxxxxxxxxx").deleted);
|
||||
lp.before_id = "file-api-b";
|
||||
ASSERT_THROW(files.list(lp), ApiError); // after_id and before_id are mutually exclusive
|
||||
ASSERT_EQ(rig.openai->send_count(), 0u);
|
||||
}
|
||||
|
||||
TEST(deepseek_web, search_and_fetch) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/v1/tools/search", reply(kSearch));
|
||||
rig.openai->expect_response(Method::kPost, "/v1/tools/fetch", reply(kFetch));
|
||||
Web web(rig.platform.openai);
|
||||
SearchParams sp;
|
||||
sp.query = "DeepSeek V4 release";
|
||||
sp.count = 5;
|
||||
sp.need_content = true;
|
||||
SearchResults r = web.search(sp);
|
||||
ASSERT_EQ(r.hits.size(), 1u);
|
||||
ASSERT_EQ(r.hits[0].title, "DeepSeek V4 model released");
|
||||
ASSERT_EQ(r.hits[0].authority, 3);
|
||||
ASSERT_TRUE(r.hits[0].high_quality());
|
||||
ASSERT_EQ(r.hits[0].site_name, "Example News");
|
||||
ASSERT_EQ(rig.openai->seen()[0].body, "{\"count\":5,\"need_content\":true,\"query\":\"DeepSeek V4 release\"}");
|
||||
FetchResult f = web.fetch("https://example.com/article");
|
||||
ASSERT_EQ(f.title, "Example article");
|
||||
ASSERT_TRUE(has(f.content, "Article content."));
|
||||
ASSERT_EQ(rig.openai->seen()[1].body, "{\"url\":\"https://example.com/article\"}");
|
||||
}
|
||||
|
||||
TEST(deepseek_web, null_site_fields_and_empty_results_are_fine) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/v1/tools/search", reply("{\"results\":[{\"rerank_score\":0.2,\"title\":\"t\",\"url\":\"u\",\"snippet\":\"s\",\"summary\":\"m\",\"content\":\"\",\"site_name\":null,\"site_icon\":null,\"date\":null,\"authority\":-1}]}"));
|
||||
rig.openai->expect_response(Method::kPost, "/v1/tools/search", reply("{\"results\":[]}"));
|
||||
Web web(rig.platform.openai);
|
||||
SearchParams sp;
|
||||
sp.query = "x";
|
||||
SearchResults r = web.search(sp);
|
||||
ASSERT_EQ(r.hits[0].site_name, "");
|
||||
ASSERT_EQ(r.hits[0].authority, -1);
|
||||
ASSERT_FALSE(r.hits[0].high_quality());
|
||||
ASSERT_TRUE(web.search(sp).hits.empty());
|
||||
}
|
||||
|
||||
TEST(deepseek_web, validation_before_the_network) {
|
||||
Rig rig;
|
||||
Web web(rig.platform.openai);
|
||||
SearchParams sp;
|
||||
sp.query = " ";
|
||||
ASSERT_THROW(web.search(sp), ApiError);
|
||||
sp.query = std::string(129, 'a');
|
||||
ASSERT_THROW(web.search(sp), ApiError);
|
||||
sp.query = "ok";
|
||||
sp.count = 0;
|
||||
ASSERT_THROW(web.search(sp), ApiError);
|
||||
sp.count = 51;
|
||||
ASSERT_THROW(web.search(sp), ApiError);
|
||||
sp.count = 10;
|
||||
sp.ori_question = "";
|
||||
ASSERT_THROW(web.search(sp), ApiError);
|
||||
sp.query = std::string(128, 'a'); // exactly the cap passes
|
||||
sp.ori_question = std::nullopt;
|
||||
sp.validate();
|
||||
ASSERT_THROW(web.fetch(""), ApiError);
|
||||
ASSERT_THROW(web.fetch("ftp://x/y"), ApiError);
|
||||
ASSERT_THROW(web.fetch("https://user:pw@example.com/"), ApiError);
|
||||
ASSERT_THROW(web.fetch("https://example.com/" + std::string(8200, 'a')), ApiError);
|
||||
ASSERT_EQ(rig.openai->send_count(), 0u);
|
||||
}
|
||||
|
||||
TEST(deepseek_fim, request_and_parse) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/beta/completions", reply(kFim));
|
||||
Fim fim(rig.platform.openai);
|
||||
FimParams p;
|
||||
p.prompt = "def fib(a):";
|
||||
p.suffix = " return fib(a-1) + fib(a-2)";
|
||||
p.max_tokens = 128;
|
||||
FimResult r = fim.create(p);
|
||||
ASSERT_EQ(r.first().text, " if a < 2:\n return a\n");
|
||||
ASSERT_EQ(r.first().finish_reason, "stop");
|
||||
ASSERT_EQ(r.object, "text_completion");
|
||||
ASSERT_EQ(r.usage.cache_hit_tokens, 4);
|
||||
ASSERT_EQ(r.usage.cache_miss_tokens, 16);
|
||||
ASSERT_EQ(r.usage.cache_hit_ratio(), 0.2);
|
||||
auto s = rig.openai->seen()[0];
|
||||
ASSERT_TRUE(has(s.body, "\"suffix\":\" return fib(a-1) + fib(a-2)\""));
|
||||
ASSERT_TRUE(has(s.body, "\"prompt\":\"def fib(a):\""));
|
||||
}
|
||||
|
||||
TEST(deepseek_fim, stream_collects_text) {
|
||||
const char* stream =
|
||||
"data: {\"id\":\"f1\",\"choices\":[{\"index\":0,\"text\":\" if\",\"finish_reason\":null}],\"created\":1,\"model\":\"deepseek-flash\",\"object\":\"text_completion\"}\n\n"
|
||||
"data: {\"id\":\"f1\",\"choices\":[{\"index\":0,\"text\":\" a < 2:\",\"finish_reason\":null}],\"created\":1,\"model\":\"deepseek-flash\",\"object\":\"text_completion\"}\n\n"
|
||||
"data: {\"id\":\"f1\",\"choices\":[{\"index\":0,\"text\":\"\",\"finish_reason\":\"stop\"}],\"created\":1,\"model\":\"deepseek-flash\",\"object\":\"text_completion\",\"usage\":{\"completion_tokens\":5,\"prompt_tokens\":9,\"total_tokens\":14,\"prompt_cache_hit_tokens\":0,\"prompt_cache_miss_tokens\":9}}\n\n"
|
||||
"data: [DONE]\n\n";
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/beta/completions", sse(stream));
|
||||
Fim fim(rig.platform.openai);
|
||||
FimParams p;
|
||||
p.prompt = "def fib(a):";
|
||||
std::string text;
|
||||
FimResult r = fim.stream(p, [&](const std::string& d) {
|
||||
text += d;
|
||||
return true;
|
||||
});
|
||||
ASSERT_EQ(text, " if a < 2:");
|
||||
ASSERT_EQ(r.first().finish_reason, "stop");
|
||||
ASSERT_EQ(r.usage.total_tokens, 14);
|
||||
ASSERT_TRUE(has(rig.openai->seen()[0].body, "\"stream\":true"));
|
||||
}
|
||||
|
||||
TEST(deepseek_responses, create_parse_and_history) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/responses", reply(kResponse));
|
||||
Responses api(rig.platform.openai);
|
||||
ResponsesParams p;
|
||||
p.instructions = "You are a helpful assistant.";
|
||||
p.input_text = "Hi, how are you?";
|
||||
p.effort = "high";
|
||||
p.max_output_tokens = 256;
|
||||
ResponsesResult r = api.create(p);
|
||||
ASSERT_TRUE(r.completed());
|
||||
ASSERT_EQ(r.output_text(), "Hello! How can I help you today?");
|
||||
ASSERT_EQ(r.reasoning_text(), "The user greets me. I should reply politely.");
|
||||
ASSERT_EQ(r.usage.input_tokens, 22);
|
||||
ASSERT_EQ(r.usage.reasoning_tokens, 27);
|
||||
ASSERT_EQ(r.usage.total_tokens, 51);
|
||||
ASSERT_EQ(r.history_items().as_array().size(), 2u);
|
||||
ASSERT_TRUE(r.function_calls().empty());
|
||||
std::string body = rig.openai->seen()[0].body;
|
||||
ASSERT_TRUE(has(body, "\"input\":\"Hi, how are you?\""));
|
||||
ASSERT_TRUE(has(body, "\"instructions\":\"You are a helpful assistant.\""));
|
||||
ASSERT_TRUE(has(body, "\"reasoning\":{\"effort\":\"high\"}"));
|
||||
ASSERT_TRUE(has(body, "\"max_output_tokens\":256"));
|
||||
ASSERT_FALSE(has(body, "previous_response_id")); // unsupported, never sent
|
||||
ASSERT_FALSE(has(body, "\"store\""));
|
||||
}
|
||||
|
||||
TEST(deepseek_responses, validation_rules) {
|
||||
auto msg = [](ResponsesParams p) {
|
||||
try {
|
||||
p.validate();
|
||||
} catch (const ApiError& e) {
|
||||
return e.message();
|
||||
}
|
||||
return std::string("OK");
|
||||
};
|
||||
ResponsesParams empty;
|
||||
ASSERT_TRUE(has(msg(empty), "at least one of input and instructions"));
|
||||
ResponsesParams both;
|
||||
both.input_text = "x";
|
||||
both.input_items.push(ResponseItems::message("user", "y"));
|
||||
ASSERT_TRUE(has(msg(both), "not both"));
|
||||
ResponsesParams ins;
|
||||
ins.instructions = "only instructions is enough";
|
||||
ASSERT_EQ(msg(ins), "OK");
|
||||
ResponsesParams img;
|
||||
img.input_items.push(ResponseItems::message_parts("user", {ResponseItems::input_image_url("https://e.com/a.jpg", "low")}));
|
||||
ASSERT_EQ(msg(img), "OK");
|
||||
ResponsesParams sys_img;
|
||||
sys_img.input_items.push(ResponseItems::message_parts("system", {ResponseItems::input_image_url("https://e.com/a.jpg")}));
|
||||
ASSERT_TRUE(has(msg(sys_img), "only in user"));
|
||||
Json both_img = ResponseItems::input_image_url("https://e.com/a.jpg");
|
||||
both_img.set("file_id", Json("file-api-1"));
|
||||
ResponsesParams two;
|
||||
two.input_items.push(ResponseItems::message_parts("user", {both_img}));
|
||||
ASSERT_TRUE(has(msg(two), "cannot have both"));
|
||||
Json neither = Json::object();
|
||||
neither.set("type", Json("input_image"));
|
||||
ResponsesParams none;
|
||||
none.input_items.push(ResponseItems::message_parts("user", {neither}));
|
||||
ASSERT_TRUE(has(msg(none), "must have image_url or file_id"));
|
||||
ResponsesParams orphan;
|
||||
orphan.input_items.push(ResponseItems::message("user", "x"));
|
||||
orphan.input_items.push(ResponseItems::function_call("c1", "get_date", "{}"));
|
||||
ASSERT_TRUE(has(msg(orphan), "function_call_output"));
|
||||
orphan.input_items.push(ResponseItems::function_call_output("c1", "2026-04-19"));
|
||||
ASSERT_EQ(msg(orphan), "OK");
|
||||
orphan.input_items.push(ResponseItems::function_call("c1", "get_date", "{}"));
|
||||
ASSERT_TRUE(has(msg(orphan), "unique"));
|
||||
ResponsesParams unk;
|
||||
unk.input_items.push(ResponseItems::message("user", "x"));
|
||||
Json weird = Json::object();
|
||||
weird.set("type", Json("file_search_call"));
|
||||
unk.input_items.push(weird);
|
||||
ASSERT_TRUE(has(msg(unk), "not supported"));
|
||||
ResponsesParams tools;
|
||||
tools.input_text = "x";
|
||||
tools.tools.push(ResponseItems::function_tool("get_date", "d"));
|
||||
tools.tools.push(ResponseItems::function_tool("get_date", "d"));
|
||||
ASSERT_TRUE(has(msg(tools), "unique"));
|
||||
tools.tools = Json::array();
|
||||
tools.tools.push(ResponseItems::apply_patch_tool());
|
||||
tools.tools.push(ResponseItems::web_search_tool());
|
||||
ASSERT_EQ(msg(tools), "OK");
|
||||
Json custom = ResponseItems::apply_patch_tool();
|
||||
custom.set("name", Json("other"));
|
||||
tools.tools.push(custom);
|
||||
ASSERT_TRUE(has(msg(tools), "apply_patch"));
|
||||
ResponsesParams schema;
|
||||
schema.input_text = "x";
|
||||
schema.format = TextFormat::kJsonSchema;
|
||||
ASSERT_TRUE(has(msg(schema), "name"));
|
||||
schema.schema_name = "answer";
|
||||
ASSERT_TRUE(has(msg(schema), "schema"));
|
||||
schema.schema = Json::parse("{\"type\":\"object\"}");
|
||||
ASSERT_EQ(msg(schema), "OK");
|
||||
ASSERT_TRUE(has(schema.to_json().dump(), "\"text\":{\"format\":{\"name\":\"answer\",\"schema\":{\"type\":\"object\"},\"type\":\"json_schema\"}}"));
|
||||
ResponsesParams choice;
|
||||
choice.input_text = "x";
|
||||
choice.tool_choice = "required";
|
||||
ASSERT_TRUE(has(msg(choice), "needs tools"));
|
||||
choice.tools.push(ResponseItems::function_tool("f", ""));
|
||||
choice.tool_choice = "f";
|
||||
ASSERT_TRUE(has(choice.to_json().dump(), "\"tool_choice\":{\"name\":\"f\",\"type\":\"function\"}"));
|
||||
}
|
||||
|
||||
TEST(deepseek_responses, stream_events_end_with_a_terminal_event_no_done) {
|
||||
Rig rig;
|
||||
rig.openai->expect_response(Method::kPost, "/responses", sse(kResponseStream));
|
||||
Responses api(rig.platform.openai);
|
||||
ResponsesParams p;
|
||||
p.input_text = "Hi";
|
||||
std::string text, reasoning;
|
||||
std::vector<std::string> types;
|
||||
ResponsesResult r = api.stream(p, [&](const ResponseEvent& e) {
|
||||
types.push_back(e.type);
|
||||
if (e.type == "response.output_text.delta") text += e.text_delta();
|
||||
if (e.type == "response.reasoning_text.delta") reasoning += e.text_delta();
|
||||
return true;
|
||||
});
|
||||
ASSERT_EQ(text, "Hello!");
|
||||
ASSERT_EQ(reasoning, "The user");
|
||||
ASSERT_EQ(types.front(), "response.created");
|
||||
ASSERT_EQ(types.back(), "response.completed");
|
||||
ASSERT_EQ(r.status, "completed");
|
||||
ASSERT_EQ(r.output_text(), "Hello!");
|
||||
ASSERT_EQ(r.usage.cache_hit_tokens, 2);
|
||||
ASSERT_EQ(r.usage.cache_miss_tokens, 3);
|
||||
ASSERT_TRUE(has(rig.openai->seen()[0].body, "\"stream\":true"));
|
||||
// no terminal event: loud
|
||||
Rig cut;
|
||||
cut.openai->expect_response(Method::kPost, "/responses", sse("event: response.created\ndata: {\"type\":\"response.created\",\"sequence_number\":0,\"response\":{\"id\":\"r\"}}\n\n"));
|
||||
Responses api2(cut.platform.openai);
|
||||
try {
|
||||
api2.stream(p, nullptr);
|
||||
FAIL("expected");
|
||||
} catch (const ApiError& e) {
|
||||
ASSERT_EQ(e.type(), "STREAM_TRUNCATED");
|
||||
}
|
||||
}
|
||||
|
||||
TEST(deepseek_responses, failed_and_incomplete_terminals_are_results_not_throws) {
|
||||
const char* failed =
|
||||
"event: response.failed\ndata: {\"type\":\"response.failed\",\"sequence_number\":3,\"response\":{\"id\":\"r\",\"status\":\"failed\",\"error\":{\"code\":\"server_error\",\"message\":\"boom\"},\"output\":[]}}\n\n";
|
||||
const char* incomplete =
|
||||
"event: response.incomplete\ndata: {\"type\":\"response.incomplete\",\"sequence_number\":3,\"response\":{\"id\":\"r\",\"status\":\"incomplete\",\"incomplete_details\":{\"reason\":\"max_output_tokens\"},\"output\":[]}}\n\n";
|
||||
ResponsesParams p;
|
||||
p.input_text = "x";
|
||||
Rig a;
|
||||
a.openai->expect_response(Method::kPost, "/responses", sse(failed));
|
||||
ResponsesResult f = Responses(a.platform.openai).stream(p, nullptr);
|
||||
ASSERT_EQ(f.status, "failed");
|
||||
ASSERT_EQ(f.error.find("message")->as_string(), "boom");
|
||||
Rig b;
|
||||
b.openai->expect_response(Method::kPost, "/responses", sse(incomplete));
|
||||
ResponsesResult i = Responses(b.platform.openai).stream(p, nullptr);
|
||||
ASSERT_EQ(i.incomplete_reason, "max_output_tokens");
|
||||
}
|
||||
|
||||
TEST(deepseek_anthropic, request_shape_headers_and_parse) {
|
||||
Rig rig;
|
||||
rig.anthropic->expect_response(Method::kPost, "/anthropic/v1/messages", reply(kMessage));
|
||||
AnthropicMessages api(rig.platform.anthropic);
|
||||
MessagesParams p;
|
||||
p.max_tokens = 1000;
|
||||
p.system = "You are a helpful assistant.";
|
||||
p.add(AnthropicBlocks::message("user", {AnthropicBlocks::text("Hi, how are you?")}));
|
||||
p.thinking = true;
|
||||
p.effort = "high";
|
||||
p.user_id = "u-1";
|
||||
p.tools.push(AnthropicBlocks::tool("get_weather", "weather", Json::parse("{\"type\":\"object\"}")));
|
||||
p.tool_choice = "get_weather";
|
||||
MessagesResult r = api.create(p);
|
||||
ASSERT_EQ(r.text(), "Hi there");
|
||||
ASSERT_EQ(r.thinking_text(), "greet");
|
||||
ASSERT_EQ(r.stop_reason, "tool_use");
|
||||
ASSERT_EQ(r.tool_uses().size(), 1u);
|
||||
ASSERT_EQ(r.tool_uses()[0].input.find("location")->as_string(), "Hangzhou");
|
||||
ASSERT_EQ(r.usage.input_tokens, 10);
|
||||
auto s = rig.anthropic->seen()[0];
|
||||
ASSERT_EQ(s.headers.get_or("x-api-key", ""), kKey);
|
||||
ASSERT_EQ(s.headers.get_or("Authorization", ""), "");
|
||||
ASSERT_EQ(s.headers.get_or("anthropic-beta", ""), ""); // only file images need it
|
||||
ASSERT_TRUE(has(s.body, "\"max_tokens\":1000"));
|
||||
ASSERT_TRUE(has(s.body, "\"thinking\":{\"type\":\"enabled\"}"));
|
||||
ASSERT_FALSE(has(s.body, "budget_tokens"));
|
||||
ASSERT_TRUE(has(s.body, "\"output_config\":{\"effort\":\"high\"}"));
|
||||
ASSERT_TRUE(has(s.body, "\"metadata\":{\"user_id\":\"u-1\"}"));
|
||||
ASSERT_TRUE(has(s.body, "\"tool_choice\":{\"name\":\"get_weather\",\"type\":\"tool\"}"));
|
||||
ASSERT_FALSE(has(s.body, "top_k"));
|
||||
ASSERT_EQ(rig.openai->send_count(), 0u);
|
||||
}
|
||||
|
||||
TEST(deepseek_anthropic, file_image_adds_the_beta_header_and_unsupported_blocks_are_rejected) {
|
||||
Rig rig;
|
||||
rig.anthropic->expect_response(Method::kPost, "/anthropic/v1/messages", reply(kMessage));
|
||||
AnthropicMessages api(rig.platform.anthropic);
|
||||
MessagesParams p;
|
||||
p.add(AnthropicBlocks::message("user", {AnthropicBlocks::text("What is in this image?"), AnthropicBlocks::image_file("file-api-xxxxxxxxxxxxxxxx")}));
|
||||
ASSERT_TRUE(p.uses_file_image());
|
||||
api.create(p);
|
||||
ASSERT_EQ(rig.anthropic->seen()[0].headers.get_or("anthropic-beta", ""), "files-api-2025-04-14");
|
||||
for (const char* type : {"document", "search_result", "redacted_thinking", "code_execution_tool_result", "mcp_tool_use", "mcp_tool_result", "container_upload"}) {
|
||||
Json block = Json::object();
|
||||
block.set("type", Json(type));
|
||||
MessagesParams q;
|
||||
q.add(AnthropicBlocks::message("user", {block}));
|
||||
ASSERT_THROW(q.validate(), ApiError);
|
||||
}
|
||||
MessagesParams ok;
|
||||
ok.add(AnthropicBlocks::message("user", {AnthropicBlocks::image_base64("image/webp", "AAAA"), AnthropicBlocks::image_url("https://e.com/a.png")}));
|
||||
ok.validate();
|
||||
MessagesParams badmedia;
|
||||
badmedia.add(AnthropicBlocks::message("user", {AnthropicBlocks::image_base64("image/bmp", "AAAA")}));
|
||||
ASSERT_THROW(badmedia.validate(), ApiError);
|
||||
MessagesParams norole;
|
||||
norole.add(AnthropicBlocks::message_text("system", "x"));
|
||||
ASSERT_THROW(norole.validate(), ApiError);
|
||||
MessagesParams empty;
|
||||
ASSERT_THROW(empty.validate(), ApiError);
|
||||
MessagesParams tokens;
|
||||
tokens.add(AnthropicBlocks::message_text("user", "x"));
|
||||
tokens.max_tokens = 0;
|
||||
ASSERT_THROW(tokens.validate(), ApiError);
|
||||
tokens.max_tokens = 5;
|
||||
tokens.effort = "turbo";
|
||||
ASSERT_THROW(tokens.validate(), ApiError);
|
||||
}
|
||||
|
||||
TEST(deepseek_anthropic, stream_folds_text_and_tool_input) {
|
||||
Rig rig;
|
||||
rig.anthropic->expect_response(Method::kPost, "/anthropic/v1/messages", sse(kMessageStream));
|
||||
AnthropicMessages api(rig.platform.anthropic);
|
||||
MessagesParams p;
|
||||
p.add(AnthropicBlocks::message_text("user", "Hi"));
|
||||
std::string text;
|
||||
std::vector<std::string> events;
|
||||
MessagesResult r = api.stream(p, [&](const molo::llm::SseEvent& e, const std::string& delta) {
|
||||
events.push_back(e.event);
|
||||
text += delta;
|
||||
return true;
|
||||
});
|
||||
ASSERT_EQ(text, "Hi there");
|
||||
ASSERT_EQ(r.text(), "Hi there");
|
||||
ASSERT_EQ(r.stop_reason, "tool_use");
|
||||
ASSERT_EQ(r.tool_uses().size(), 1u);
|
||||
ASSERT_EQ(r.tool_uses()[0].input.find("location")->as_string(), "Hangzhou");
|
||||
ASSERT_EQ(r.usage.output_tokens, 15);
|
||||
ASSERT_EQ(r.usage.input_tokens, 10);
|
||||
ASSERT_EQ(events.front(), "message_start");
|
||||
ASSERT_EQ(events.back(), "message_stop");
|
||||
ASSERT_TRUE(has(rig.anthropic->seen()[0].body, "\"stream\":true"));
|
||||
// an error event ends the stream with an ApiError
|
||||
Rig bad;
|
||||
bad.anthropic->expect_response(Method::kPost, "/anthropic/v1/messages",
|
||||
sse("event: error\ndata: {\"type\":\"error\",\"error\":{\"type\":\"overloaded_error\",\"message\":\"busy\"}}\n\n"));
|
||||
AnthropicMessages api2(bad.platform.anthropic);
|
||||
try {
|
||||
api2.stream(p, nullptr);
|
||||
FAIL("expected");
|
||||
} catch (const ApiError& e) {
|
||||
ASSERT_EQ(e.type(), "overloaded_error");
|
||||
}
|
||||
// truncation without message_stop is loud
|
||||
Rig cut;
|
||||
cut.anthropic->expect_response(Method::kPost, "/anthropic/v1/messages",
|
||||
sse("event: ping\ndata: {\"type\":\"ping\"}\n\n"));
|
||||
AnthropicMessages api3(cut.platform.anthropic);
|
||||
ASSERT_THROW(api3.stream(p, nullptr), ApiError);
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// Opt-in live checks. MOLO_LIVE_NET=1 enables the network; DEEPSEEK_API_KEY
|
||||
// (via llm::Config) enables the read-only calls plus ONE tiny completion.
|
||||
// Without them each test prints a loud SKIP. Nothing is created or deleted.
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#include "clients/transports/http_transport.h"
|
||||
#include "deepseek/account.h"
|
||||
#include "deepseek/chat.h"
|
||||
#include "deepseek/platform.h"
|
||||
#include "support.h"
|
||||
|
||||
using namespace molo::deepseek;
|
||||
using molo::llm::ApiError;
|
||||
|
||||
namespace {
|
||||
bool net() { return std::getenv("MOLO_LIVE_NET") != nullptr; }
|
||||
} // namespace
|
||||
|
||||
// No key needed: a deliberately invalid key must answer 401 with the
|
||||
// OpenAI-style error envelope (type authentication_error).
|
||||
TEST(deepseek_live, invalid_key_answers_401_with_the_error_envelope) {
|
||||
if (!net()) {
|
||||
std::cout << "SKIP deepseek_live.invalid_key_answers_401_with_the_error_envelope (set MOLO_LIVE_NET=1)\n";
|
||||
return;
|
||||
}
|
||||
molo::llm::ApiClientConfig cfg = Platform::config(Surface::kOpenai);
|
||||
auto client = std::make_shared<molo::llm::ApiClient>(
|
||||
std::make_unique<molo::clients::transports::HttpTransport>(), cfg, "sk-invalid-molo-probe");
|
||||
Account acct(client);
|
||||
try {
|
||||
acct.models();
|
||||
FAIL("expected 401");
|
||||
} catch (const ApiError& e) {
|
||||
std::cout << " GET /models with an invalid key: " << e.http_status() << " " << e.type() << "\n";
|
||||
ASSERT_EQ(e.http_status(), 401);
|
||||
ASSERT_EQ(e.type(), "authentication_error");
|
||||
}
|
||||
}
|
||||
|
||||
TEST(deepseek_live, models_balance_and_one_tiny_completion) {
|
||||
Platform p = Platform::from_env();
|
||||
if (!net() || !p.configured()) {
|
||||
std::cout << "SKIP deepseek_live.models_balance_and_one_tiny_completion (set MOLO_LIVE_NET=1 and DEEPSEEK_API_KEY)\n";
|
||||
return;
|
||||
}
|
||||
Account acct(p.openai);
|
||||
ModelList models = acct.models();
|
||||
std::cout << " models: " << models.models.size() << "\n";
|
||||
ASSERT_TRUE(!models.models.empty());
|
||||
ASSERT_TRUE(models.find(ModelCatalog::default_model()) != nullptr);
|
||||
Balance b = acct.balance();
|
||||
std::cout << " balance entries: " << b.entries.size() << " available=" << b.available << "\n";
|
||||
ChatParams params = ChatParams::prompt("Reply with the single word: ok");
|
||||
params.max_tokens = 16;
|
||||
params.thinking = Thinking::kDisabled;
|
||||
ChatResult r = Chat(p.openai).create(params);
|
||||
std::cout << " completion finish=" << r.first().finish_reason << " tokens=" << r.usage.total_tokens << "\n";
|
||||
ASSERT_TRUE(r.usage.present);
|
||||
}
|
||||
@@ -0,0 +1,351 @@
|
||||
// DeepSeek: call table, error table, rules, chat request validation and body.
|
||||
// Local and deterministic: no reply is scripted here.
|
||||
#include <set>
|
||||
|
||||
#include "deepseek/calls.h"
|
||||
#include "deepseek/chat.h"
|
||||
#include "deepseek/chat_params.h"
|
||||
#include "deepseek/errors.h"
|
||||
#include "deepseek/fim.h"
|
||||
#include "deepseek/rules.h"
|
||||
#include "support.h"
|
||||
|
||||
using namespace molo::deepseek;
|
||||
using molo::llm::ApiError;
|
||||
|
||||
namespace {
|
||||
template <typename F>
|
||||
std::string invalid_message(F&& f) {
|
||||
try {
|
||||
f();
|
||||
} catch (const ApiError& e) {
|
||||
ASSERT_EQ(e.http_status(), 400);
|
||||
ASSERT_EQ(e.type(), "INVALID_ARGUMENT");
|
||||
ASSERT_EQ(e.vendor(), "deepseek");
|
||||
return e.message();
|
||||
}
|
||||
FAIL("expected ApiError");
|
||||
return "";
|
||||
}
|
||||
bool has(const std::string& hay, const std::string& needle) { return hay.find(needle) != std::string::npos; }
|
||||
} // namespace
|
||||
|
||||
TEST(deepseek_calls, table_shape_and_render) {
|
||||
ASSERT_EQ(Calls::count(), 17u);
|
||||
std::set<std::string> seen;
|
||||
for (std::size_t i = 0; i < Calls::count(); ++i) {
|
||||
const CallSpec& s = Calls::at(i);
|
||||
ASSERT_TRUE(seen.insert(std::string(s.http_method) + " " + s.path).second);
|
||||
ASSERT_TRUE(s.path[0] == '/');
|
||||
if (s.surface == Surface::kAnthropic) ASSERT_TRUE(std::string(s.path).rfind("/anthropic/", 0) == 0);
|
||||
}
|
||||
ASSERT_EQ(Calls::render(Call::kChat), "/chat/completions");
|
||||
ASSERT_EQ(Calls::render(Call::kChatBeta), "/beta/chat/completions");
|
||||
ASSERT_EQ(Calls::render(Call::kFim), "/beta/completions");
|
||||
ASSERT_EQ(Calls::render(Call::kFileGet, "file-api-abc"), "/files/file-api-abc");
|
||||
ASSERT_EQ(Calls::render(Call::kAnthropicFileDelete, "file-api-abc"), "/anthropic/v1/files/file-api-abc");
|
||||
ASSERT_THROW(Calls::render(Call::kFileGet, "../x"), ApiError);
|
||||
ASSERT_THROW(Calls::render(Call::kFileGet, ""), ApiError);
|
||||
ASSERT_THROW(Calls::at(99), ApiError);
|
||||
ASSERT_TRUE(Calls::method_of(Call::kFileDelete) == molo::clients::Method::kDelete);
|
||||
ASSERT_TRUE(Calls::method_of(Call::kModels) == molo::clients::Method::kGet);
|
||||
for (std::size_t i = 0; i < Calls::schema_count(); ++i) ASSERT_TRUE(std::string(Calls::schema_at(i).fields).size() > 0);
|
||||
}
|
||||
|
||||
TEST(deepseek_errors, documented_codes) {
|
||||
for (int s : {400, 401, 402, 422, 429, 500, 503}) ASSERT_TRUE(Errors::find(s) != nullptr);
|
||||
ASSERT_TRUE(Errors::find(418) == nullptr);
|
||||
ASSERT_TRUE(Errors::kind_of(402) == ErrorKind::kBalance);
|
||||
ASSERT_TRUE(Errors::kind_of(401) == ErrorKind::kAuth);
|
||||
ASSERT_TRUE(Errors::kind_of(502) == ErrorKind::kTransport);
|
||||
ASSERT_TRUE(Errors::kind_of(200) == ErrorKind::kNone);
|
||||
ASSERT_TRUE(Errors::kind_of(418) == ErrorKind::kOther);
|
||||
// wait and retry: 429, 500, 503 (+ search/fetch 408, 504); never the 4xx input errors
|
||||
for (int s : {408, 429, 500, 503, 504}) {
|
||||
ASSERT_TRUE(Errors::retryable(s));
|
||||
ASSERT_TRUE(Errors::wait_seconds(s) >= 1 && Errors::wait_seconds(s) <= Errors::kMaxWaitSeconds);
|
||||
}
|
||||
for (int s : {400, 401, 402, 403, 404, 413, 422}) {
|
||||
ASSERT_FALSE(Errors::retryable(s));
|
||||
ASSERT_EQ(Errors::wait_seconds(s), 0);
|
||||
}
|
||||
ApiError e("deepseek", 402, "invalid_request_error", "Insufficient Balance");
|
||||
ASSERT_TRUE(has(Errors::explain(e), "Insufficient Balance"));
|
||||
ASSERT_TRUE(has(Errors::explain(ApiError("deepseek", 418, "teapot", "short")), "teapot"));
|
||||
ASSERT_THROW(Errors::at(999), ApiError);
|
||||
}
|
||||
|
||||
TEST(deepseek_rules, field_rules) {
|
||||
ASSERT_THROW(Rules::model(""), ApiError);
|
||||
ASSERT_THROW(Rules::model("bad name"), ApiError);
|
||||
ASSERT_THROW(Rules::model(std::string(129, 'a')), ApiError);
|
||||
Rules::model("deepseek-v4-pro");
|
||||
ASSERT_THROW(Rules::user_id("a b"), ApiError);
|
||||
ASSERT_THROW(Rules::user_id(std::string(513, 'a')), ApiError);
|
||||
Rules::user_id("user_1-A");
|
||||
ASSERT_THROW(Rules::tool_name("get weather"), ApiError);
|
||||
ASSERT_THROW(Rules::tool_name(std::string(129, 'a')), ApiError);
|
||||
ASSERT_THROW(Rules::stop(std::vector<std::string>(17, "x")), ApiError);
|
||||
ASSERT_THROW(Rules::stop({""}), ApiError);
|
||||
Rules::stop(std::vector<std::string>(16, "x"));
|
||||
ASSERT_THROW(Rules::image_url("ftp://x/y.png"), ApiError);
|
||||
ASSERT_THROW(Rules::image_url("data:text/plain;base64,AAAA"), ApiError);
|
||||
ASSERT_THROW(Rules::image_url("https://example.com/" + std::string(8200, 'a')), ApiError);
|
||||
Rules::image_url("https://example.com/a.jpg");
|
||||
Rules::image_url("data:image/jpeg;base64,AAAA");
|
||||
ASSERT_THROW(Rules::detail("ultra"), ApiError);
|
||||
Rules::detail("original");
|
||||
for (const char* e : {"none", "minimal", "low", "medium", "high", "xhigh", "max", "ultra"}) Rules::effort(e);
|
||||
ASSERT_THROW(Rules::effort("extreme"), ApiError);
|
||||
ASSERT_THROW(Rules::top_p(0), ApiError);
|
||||
ASSERT_THROW(Rules::top_p(1.01), ApiError);
|
||||
Rules::top_p(1);
|
||||
ASSERT_TRUE(Rules::has_json_word("Output in JSON please"));
|
||||
ASSERT_FALSE(Rules::has_json_word("output as data"));
|
||||
ASSERT_TRUE(Rules::is_http_url("https://a.example/x?y=1"));
|
||||
ASSERT_FALSE(Rules::is_http_url("https://user:pw@a.example/"));
|
||||
ASSERT_FALSE(Rules::is_http_url("https:///nohost"));
|
||||
ASSERT_FALSE(Rules::is_http_url("https://a b.example/"));
|
||||
}
|
||||
|
||||
TEST(deepseek_chat, minimal_body_follows_the_documented_shape) {
|
||||
ChatParams p = ChatParams::prompt("Hello!");
|
||||
Json b = p.to_json();
|
||||
ASSERT_EQ(b.dump(), "{\"messages\":[{\"content\":\"Hello!\",\"role\":\"user\"}],\"model\":\"deepseek-flash\"}");
|
||||
p.add(Messages::system("You are a helpful assistant."));
|
||||
p.thinking = Thinking::kEnabled;
|
||||
p.reasoning_effort = "high";
|
||||
p.max_tokens = 128;
|
||||
p.stop = {"```"};
|
||||
p.user_id = "u-1";
|
||||
p.temperature = 0.5;
|
||||
p.top_p = 0.9;
|
||||
p.logprobs = true;
|
||||
p.top_logprobs = 3;
|
||||
Json full = p.to_json(true);
|
||||
ASSERT_EQ(full.find("thinking")->find("type")->as_string(), "enabled");
|
||||
ASSERT_EQ(full.find("reasoning_effort")->as_string(), "high");
|
||||
ASSERT_EQ(full.find("max_tokens")->as_int(), 128);
|
||||
ASSERT_TRUE(full.find("stream")->as_bool());
|
||||
ASSERT_TRUE(full.find("stream_options")->find("include_usage")->as_bool());
|
||||
ASSERT_EQ(full.find("user_id")->as_string(), "u-1");
|
||||
ASSERT_EQ(full.find("top_logprobs")->as_int(), 3);
|
||||
// frequency_penalty / presence_penalty are deprecated and never sent
|
||||
ASSERT_FALSE(full.has("frequency_penalty"));
|
||||
ASSERT_FALSE(full.has("presence_penalty"));
|
||||
ASSERT_FALSE(b.has("stream"));
|
||||
ASSERT_FALSE(b.has("stream_options"));
|
||||
}
|
||||
|
||||
TEST(deepseek_chat, validation_rules) {
|
||||
auto bad = [](const std::function<void(ChatParams&)>& mutate, const std::string& expect) {
|
||||
ChatParams p = ChatParams::prompt("hi");
|
||||
mutate(p);
|
||||
std::string msg = invalid_message([&] { p.to_json(); });
|
||||
ASSERT_TRUE(has(msg, expect));
|
||||
};
|
||||
bad([](ChatParams& p) { p.messages = Json::array(); }, "messages must not be empty");
|
||||
bad([](ChatParams& p) { p.max_tokens = 0; }, "max_tokens");
|
||||
bad([](ChatParams& p) { p.max_tokens = 393217; }, "max_tokens");
|
||||
bad([](ChatParams& p) { p.temperature = 2.5; }, "temperature");
|
||||
bad([](ChatParams& p) { p.top_p = 0.0; }, "top_p");
|
||||
bad([](ChatParams& p) { p.top_logprobs = 5; }, "needs logprobs");
|
||||
bad([](ChatParams& p) { p.logprobs = true; p.top_logprobs = 21; }, "top_logprobs");
|
||||
bad([](ChatParams& p) { p.reasoning_effort = "loud"; }, "reasoning effort");
|
||||
bad([](ChatParams& p) { p.thinking = Thinking::kEnabled; p.reasoning_effort = "none"; }, "contradicts");
|
||||
bad([](ChatParams& p) { p.json_mode = true; }, "'json'");
|
||||
bad([](ChatParams& p) { p.user_id = "no spaces"; }, "user_id");
|
||||
bad([](ChatParams& p) { p.stop = std::vector<std::string>(17, "x"); }, "16 stop");
|
||||
bad([](ChatParams& p) { p.messages.push(Json(5)); }, "message must be an object");
|
||||
bad([](ChatParams& p) { Json m = Json::object(); m.set("role", Json("robot")); m.set("content", Json("x")); p.add(m); }, "role");
|
||||
bad([](ChatParams& p) { p.add(Messages::tool("", "x")); }, "tool_call_id");
|
||||
bad([](ChatParams& p) { p.extra.set("model", Json("x")); }, "collides");
|
||||
bad([](ChatParams& p) { p.tool_choice = "auto"; }, "needs tools");
|
||||
// json mode passes with the word in a message
|
||||
ChatParams ok = ChatParams::prompt("Answer in JSON: {\"a\":1}");
|
||||
ok.json_mode = true;
|
||||
ASSERT_EQ(ok.to_json().find("response_format")->find("type")->as_string(), "json_object");
|
||||
}
|
||||
|
||||
TEST(deepseek_chat, tool_choice_rules_in_thinking_mode) {
|
||||
ChatParams p = ChatParams::prompt("weather?");
|
||||
ToolDef t;
|
||||
t.name = "get_weather";
|
||||
t.parameters = Json::parse("{\"type\":\"object\",\"properties\":{\"location\":{\"type\":\"string\"}},\"required\":[\"location\"]}");
|
||||
p.tools.push_back(t);
|
||||
p.tool_choice = "auto";
|
||||
p.validate();
|
||||
// thinking is on by default: required and named choices are a 400 upstream, rejected here
|
||||
p.tool_choice = "required";
|
||||
ASSERT_TRUE(has(invalid_message([&] { p.validate(); }), "thinking mode"));
|
||||
p.tool_choice = "get_weather";
|
||||
ASSERT_TRUE(has(invalid_message([&] { p.validate(); }), "thinking mode"));
|
||||
p.thinking = Thinking::kDisabled;
|
||||
Json b = p.to_json();
|
||||
ASSERT_EQ(b.find("tool_choice")->find("function")->find("name")->as_string(), "get_weather");
|
||||
p.tool_choice = "required";
|
||||
ASSERT_EQ(p.to_json().find("tool_choice")->as_string(), "required");
|
||||
// effort none also switches thinking off
|
||||
p.thinking = Thinking::kDefault;
|
||||
p.reasoning_effort = "none";
|
||||
ASSERT_FALSE(p.thinking_on());
|
||||
p.validate();
|
||||
p.tool_choice = "missing_tool";
|
||||
ASSERT_TRUE(has(invalid_message([&] { p.validate(); }), "not defined"));
|
||||
// names are unique, tools are capped
|
||||
p.tool_choice = "";
|
||||
p.tools.push_back(t);
|
||||
ASSERT_TRUE(has(invalid_message([&] { p.validate(); }), "unique"));
|
||||
}
|
||||
|
||||
TEST(deepseek_chat, reasoning_content_is_dropped_without_tools_and_kept_with_tools) {
|
||||
// Thinking Mode guide: with tools, every earlier reasoning_content is sent back
|
||||
// (400 otherwise); without tools the API ignores it, so it must not be sent.
|
||||
ChatParams p;
|
||||
p.add(Messages::user("9.11 and 9.8, which is greater?"));
|
||||
p.add(Messages::assistant("9.8 is greater", "compare 9.11 and 9.80 ..."));
|
||||
p.add(Messages::user("How many Rs are there in strawberry?"));
|
||||
Json no_tools = p.to_json();
|
||||
ASSERT_FALSE(no_tools.find("messages")->as_array()[1].has("reasoning_content"));
|
||||
ASSERT_EQ(p.messages.as_array()[1].find("reasoning_content")->as_string(), "compare 9.11 and 9.80 ..."); // input untouched
|
||||
|
||||
ToolDef t;
|
||||
t.name = "get_date";
|
||||
p.tools.push_back(t);
|
||||
Json with_tools = p.to_json();
|
||||
ASSERT_EQ(with_tools.find("messages")->as_array()[1].find("reasoning_content")->as_string(),
|
||||
"compare 9.11 and 9.80 ...");
|
||||
}
|
||||
|
||||
TEST(deepseek_chat, vision_and_file_parts) {
|
||||
ChatParams p;
|
||||
p.model = "deepseek-flash";
|
||||
p.add(Messages::user_parts({Messages::text_part("What is in this image?"),
|
||||
Messages::image_part("https://example.com/image.jpg", "low"),
|
||||
Messages::file_part("file-api-0a1b2c3d4e5f60718293a4b5c6d7e8f9")}));
|
||||
Json b = p.to_json();
|
||||
const Json& parts = *b.find("messages")->as_array()[0].find("content");
|
||||
ASSERT_EQ(parts.as_array()[1].find("image_url")->find("detail")->as_string(), "low");
|
||||
ASSERT_EQ(parts.as_array()[2].find("file_id")->as_string(), "file-api-0a1b2c3d4e5f60718293a4b5c6d7e8f9");
|
||||
// file_data inline needs a data URL and may carry a filename; file_id forbids filename
|
||||
p.add(Messages::user_parts({Messages::file_data_part("data:image/jpeg;base64,AAAA", "image.jpg")}));
|
||||
p.validate();
|
||||
Json both = Messages::file_part("file-api-x");
|
||||
both.set("filename", Json("a.jpg"));
|
||||
ChatParams q;
|
||||
q.add(Messages::user_parts({both}));
|
||||
ASSERT_TRUE(has(invalid_message([&] { q.validate(); }), "filename"));
|
||||
ChatParams r;
|
||||
Json neither = Json::object();
|
||||
neither.set("type", Json("file"));
|
||||
r.add(Messages::user_parts({neither}));
|
||||
ASSERT_TRUE(has(invalid_message([&] { r.validate(); }), "exactly one"));
|
||||
ChatParams s;
|
||||
s.add(Messages::user_parts({Messages::image_part("gopher://x")}));
|
||||
ASSERT_TRUE(has(invalid_message([&] { s.validate(); }), "image url"));
|
||||
ChatParams u;
|
||||
Json bad_part = Json::object();
|
||||
bad_part.set("type", Json("audio"));
|
||||
u.add(Messages::user_parts({bad_part}));
|
||||
ASSERT_TRUE(has(invalid_message([&] { u.validate(); }), "unknown content part"));
|
||||
// 600 images per request
|
||||
ChatParams many;
|
||||
std::vector<Json> parts600;
|
||||
for (int i = 0; i < 601; ++i) parts600.push_back(Messages::image_part("https://example.com/a.png"));
|
||||
many.add(Messages::user_parts(parts600));
|
||||
ASSERT_TRUE(has(invalid_message([&] { many.validate(); }), "600"));
|
||||
}
|
||||
|
||||
TEST(deepseek_chat, beta_prefix_and_strict_routing) {
|
||||
ChatParams p = ChatParams::prompt("Please write quick sort code");
|
||||
ASSERT_FALSE(p.needs_beta());
|
||||
ASSERT_TRUE(Chat::call_for(p) == Call::kChat);
|
||||
p.add(Messages::assistant_prefix("```python\n"));
|
||||
p.stop = {"```"};
|
||||
ASSERT_TRUE(p.has_prefix());
|
||||
ASSERT_TRUE(p.needs_beta());
|
||||
ASSERT_TRUE(Chat::call_for(p) == Call::kChatBeta);
|
||||
Json b = p.to_json();
|
||||
const Json& last = b.find("messages")->as_array().back();
|
||||
ASSERT_TRUE(last.find("prefix")->as_bool());
|
||||
ASSERT_EQ(last.find("role")->as_string(), "assistant");
|
||||
// the prefix must be last and an assistant message
|
||||
p.add(Messages::user("and then?"));
|
||||
ASSERT_TRUE(has(invalid_message([&] { p.validate(); }), "last message"));
|
||||
// the prefix message keeps its reasoning_content (CoT input) even without tools
|
||||
ChatParams q = ChatParams::prompt("hi");
|
||||
q.add(Messages::assistant_prefix("Answer:", "my chain of thought"));
|
||||
ASSERT_EQ(q.to_json().find("messages")->as_array().back().find("reasoning_content")->as_string(),
|
||||
"my chain of thought");
|
||||
}
|
||||
|
||||
TEST(deepseek_chat, strict_mode_schema_rules) {
|
||||
// FIXTURE (Tool Calls guide, strict Mode): the documented get_weather definition
|
||||
ToolDef t;
|
||||
t.name = "get_weather";
|
||||
t.description = "Get weather of a location, the user should supply a location first.";
|
||||
t.strict = true;
|
||||
t.parameters = Json::parse(
|
||||
"{\"type\":\"object\",\"properties\":{\"location\":{\"type\":\"string\",\"description\":\"The city and state, e.g. San Francisco, CA\"}},"
|
||||
"\"required\":[\"location\"],\"additionalProperties\":false}");
|
||||
ASSERT_EQ(StrictSchema::check(t.parameters), "");
|
||||
ChatParams p = ChatParams::prompt("weather in Hangzhou");
|
||||
p.tools.push_back(t);
|
||||
ASSERT_TRUE(p.needs_beta());
|
||||
ASSERT_TRUE(Chat::call_for(p) == Call::kChatBeta);
|
||||
Json body = p.to_json();
|
||||
ASSERT_TRUE(body.find("tools")->as_array()[0].find("function")->find("strict")->as_bool());
|
||||
|
||||
auto problem = [](const char* schema) { return StrictSchema::check(Json::parse(schema)); };
|
||||
ASSERT_TRUE(has(problem("{\"type\":\"object\",\"properties\":{\"a\":{\"type\":\"string\"}},\"additionalProperties\":false}"), "required"));
|
||||
ASSERT_TRUE(has(problem("{\"type\":\"object\",\"properties\":{\"a\":{\"type\":\"string\"}},\"required\":[\"a\"]}"), "additionalProperties"));
|
||||
ASSERT_TRUE(has(problem("{\"type\":\"object\",\"properties\":{\"a\":{\"type\":\"string\",\"minLength\":1}},\"required\":[\"a\"],\"additionalProperties\":false}"), "minLength"));
|
||||
ASSERT_TRUE(has(problem("{\"type\":\"object\",\"properties\":{\"a\":{\"type\":\"array\",\"items\":{\"type\":\"string\"},\"maxItems\":3}},\"required\":[\"a\"],\"additionalProperties\":false}"), "maxItems"));
|
||||
ASSERT_TRUE(has(problem("{\"type\":\"object\",\"properties\":{\"a\":{\"type\":\"null\"}},\"required\":[\"a\"],\"additionalProperties\":false}"), "null"));
|
||||
ASSERT_TRUE(has(problem("{\"type\":\"string\"}"), "root"));
|
||||
// anyOf, enum, array of strings, $ref pass
|
||||
ASSERT_EQ(problem("{\"type\":\"object\",\"properties\":{\"account\":{\"anyOf\":[{\"type\":\"string\",\"format\":\"email\"},{\"type\":\"string\",\"pattern\":\"^\\\\d{11}$\"}]},"
|
||||
"\"status\":{\"type\":\"string\",\"enum\":[\"a\",\"b\"]},\"k\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}}},"
|
||||
"\"required\":[\"account\",\"status\",\"k\"],\"additionalProperties\":false}"), "");
|
||||
// strict on one tool only is rejected: "all function need to set the strict property to true"
|
||||
ChatParams mixed = ChatParams::prompt("x");
|
||||
mixed.tools.push_back(t);
|
||||
ToolDef loose;
|
||||
loose.name = "other";
|
||||
mixed.tools.push_back(loose);
|
||||
ASSERT_TRUE(has(invalid_message([&] { mixed.validate(); }), "every function"));
|
||||
// an invalid strict schema is rejected before the network
|
||||
ToolDef broken = t;
|
||||
broken.parameters = Json::parse("{\"type\":\"object\",\"properties\":{\"a\":{\"type\":\"string\"}}}");
|
||||
ChatParams bad = ChatParams::prompt("x");
|
||||
bad.tools.push_back(broken);
|
||||
ASSERT_TRUE(has(invalid_message([&] { bad.validate(); }), "strict tool 'get_weather'"));
|
||||
}
|
||||
|
||||
TEST(deepseek_fim, validation_and_body) {
|
||||
FimParams f;
|
||||
f.prompt = "def fib(a):";
|
||||
f.suffix = " return fib(a-1) + fib(a-2)";
|
||||
f.max_tokens = 128;
|
||||
Json b = f.to_json();
|
||||
ASSERT_EQ(b.find("suffix")->as_string(), f.suffix);
|
||||
ASSERT_EQ(b.find("max_tokens")->as_int(), 128);
|
||||
ASSERT_FALSE(b.has("stream"));
|
||||
ASSERT_TRUE(f.to_json(true).find("stream")->as_bool());
|
||||
f.max_tokens = 4097;
|
||||
ASSERT_TRUE(has(invalid_message([&] { f.validate(); }), "4096"));
|
||||
f.max_tokens = 10;
|
||||
f.echo = true;
|
||||
ASSERT_TRUE(has(invalid_message([&] { f.validate(); }), "echo"));
|
||||
f.suffix.clear();
|
||||
f.logprobs = 3;
|
||||
ASSERT_TRUE(has(invalid_message([&] { f.validate(); }), "echo"));
|
||||
f.logprobs = std::nullopt;
|
||||
f.validate();
|
||||
FimParams empty;
|
||||
ASSERT_TRUE(has(invalid_message([&] { empty.validate(); }), "prompt"));
|
||||
FimParams lp;
|
||||
lp.prompt = "x";
|
||||
lp.logprobs = 21;
|
||||
ASSERT_TRUE(has(invalid_message([&] { lp.validate(); }), "logprobs"));
|
||||
}
|
||||
Reference in New Issue
Block a user