359 lines
14 KiB
C++
359 lines
14 KiB
C++
#include "deepseek/responses.h"
|
|
|
|
#include <set>
|
|
|
|
#include "deepseek/calls.h"
|
|
#include "deepseek/caps.h"
|
|
#include "deepseek/rules.h"
|
|
|
|
namespace molo::deepseek {
|
|
|
|
// ---- builders --------------------------------------------------------------
|
|
|
|
Json ResponseItems::message(const std::string& role, const std::string& text) {
|
|
Json m = Json::object();
|
|
m.set("type", Json("message"));
|
|
m.set("role", Json(role));
|
|
m.set("content", Json(text));
|
|
return m;
|
|
}
|
|
Json ResponseItems::message_parts(const std::string& role, const std::vector<Json>& parts) {
|
|
Json arr = Json::array();
|
|
for (const Json& p : parts) arr.push(p);
|
|
Json m = Json::object();
|
|
m.set("type", Json("message"));
|
|
m.set("role", Json(role));
|
|
m.set("content", arr);
|
|
return m;
|
|
}
|
|
Json ResponseItems::input_text(const std::string& text) {
|
|
Json p = Json::object();
|
|
p.set("type", Json("input_text"));
|
|
p.set("text", Json(text));
|
|
return p;
|
|
}
|
|
Json ResponseItems::input_image_url(const std::string& url, const std::string& detail) {
|
|
Json p = Json::object();
|
|
p.set("type", Json("input_image"));
|
|
p.set("image_url", Json(url));
|
|
if (!detail.empty()) p.set("detail", Json(detail));
|
|
return p;
|
|
}
|
|
Json ResponseItems::input_image_file(const std::string& file_id) {
|
|
Json p = Json::object();
|
|
p.set("type", Json("input_image"));
|
|
p.set("file_id", Json(file_id));
|
|
return p;
|
|
}
|
|
Json ResponseItems::function_call(const std::string& call_id, const std::string& name,
|
|
const std::string& arguments) {
|
|
Json m = Json::object();
|
|
m.set("type", Json("function_call"));
|
|
m.set("call_id", Json(call_id));
|
|
m.set("name", Json(name));
|
|
m.set("arguments", Json(arguments));
|
|
return m;
|
|
}
|
|
Json ResponseItems::function_call_output(const std::string& call_id, const std::string& output) {
|
|
Json m = Json::object();
|
|
m.set("type", Json("function_call_output"));
|
|
m.set("call_id", Json(call_id));
|
|
m.set("output", Json(output));
|
|
return m;
|
|
}
|
|
Json ResponseItems::function_call_output_parts(const std::string& call_id,
|
|
const std::vector<Json>& parts) {
|
|
Json arr = Json::array();
|
|
for (const Json& p : parts) arr.push(p);
|
|
Json m = Json::object();
|
|
m.set("type", Json("function_call_output"));
|
|
m.set("call_id", Json(call_id));
|
|
m.set("output", arr);
|
|
return m;
|
|
}
|
|
Json ResponseItems::reasoning(const std::string& text) {
|
|
Json part = Json::object();
|
|
part.set("type", Json("reasoning_text"));
|
|
part.set("text", Json(text));
|
|
Json content = Json::array();
|
|
content.push(part);
|
|
Json m = Json::object();
|
|
m.set("type", Json("reasoning"));
|
|
m.set("content", content);
|
|
return m;
|
|
}
|
|
Json ResponseItems::function_tool(const std::string& name, const std::string& description,
|
|
const Json& parameters) {
|
|
Json t = Json::object();
|
|
t.set("type", Json("function"));
|
|
t.set("name", Json(name));
|
|
if (!description.empty()) t.set("description", Json(description));
|
|
t.set("parameters", parameters);
|
|
return t;
|
|
}
|
|
Json ResponseItems::web_search_tool() {
|
|
Json t = Json::object();
|
|
t.set("type", Json("web_search"));
|
|
return t;
|
|
}
|
|
Json ResponseItems::apply_patch_tool() {
|
|
Json t = Json::object();
|
|
t.set("type", Json("custom"));
|
|
t.set("name", Json("apply_patch"));
|
|
return t;
|
|
}
|
|
|
|
// ---- validation ------------------------------------------------------------
|
|
|
|
namespace {
|
|
|
|
void check_image(const Json& p) {
|
|
bool url = p.has("image_url"), file = p.has("file_id");
|
|
Rules::require(url || file, "input_image must have image_url or file_id");
|
|
Rules::require(!(url && file), "input_image cannot have both image_url and file_id");
|
|
if (url) Rules::image_url(J::str(p, "image_url"));
|
|
if (file) Rules::require(!J::str(p, "file_id").empty(), "file_id must not be empty");
|
|
Rules::detail(J::str(p, "detail"));
|
|
}
|
|
|
|
void check_content(const Json& content, bool images_ok, std::size_t& images) {
|
|
if (content.is_string()) return;
|
|
Rules::require(content.is_array(), "content must be a string or an array of parts");
|
|
for (const Json& p : content.as_array()) {
|
|
Rules::require(p.is_object(), "content part must be an object");
|
|
std::string type = J::str(p, "type");
|
|
if (type == "input_text" || type == "output_text") {
|
|
Rules::require(p.find("text") && p.find("text")->is_string(), "text part needs text");
|
|
} else if (type == "input_image") {
|
|
Rules::require(images_ok, "images are allowed only in user / developer messages and tool outputs");
|
|
check_image(p);
|
|
++images;
|
|
} else if (type == "reasoning_text") {
|
|
Rules::require(p.find("text") && p.find("text")->is_string(), "reasoning_text part needs text");
|
|
} else {
|
|
Rules::fail("unknown content part type '" + type + "' (file inputs are not supported)");
|
|
}
|
|
}
|
|
}
|
|
|
|
void check_items(const Json& items) {
|
|
Rules::require(items.as_array().size() <= DeepseekCaps::kMaxMessages, "too many input items");
|
|
std::set<std::string> calls, outputs, custom_calls, custom_outputs;
|
|
std::size_t images = 0;
|
|
for (const Json& it : items.as_array()) {
|
|
Rules::require(it.is_object(), "input item must be an object");
|
|
std::string type = J::str(it, "type");
|
|
if (type.empty() && it.has("role")) type = "message";
|
|
if (type == "message") {
|
|
std::string role = J::str(it, "role");
|
|
Rules::require(role == "user" || role == "assistant" || role == "system" || role == "developer",
|
|
"message role must be user, assistant, system or developer");
|
|
const Json* c = it.find("content");
|
|
Rules::require(c, "message needs content");
|
|
check_content(*c, role == "user" || role == "developer", images);
|
|
} else if (type == "function_call" || type == "custom_tool_call") {
|
|
std::string id = J::str(it, "call_id");
|
|
Rules::require(!id.empty(), type + " needs call_id");
|
|
Rules::tool_name(J::str(it, "name"));
|
|
auto& set = type == "function_call" ? calls : custom_calls;
|
|
Rules::require(set.insert(id).second, "call_id values must be unique");
|
|
} else if (type == "function_call_output" || type == "custom_tool_call_output") {
|
|
std::string id = J::str(it, "call_id");
|
|
Rules::require(!id.empty(), type + " needs call_id");
|
|
const Json* o = it.find("output");
|
|
Rules::require(o, type + " needs output");
|
|
check_content(*o, true, images);
|
|
(type == "function_call_output" ? outputs : custom_outputs).insert(id);
|
|
} else if (type == "reasoning") {
|
|
const Json* c = it.find("content");
|
|
Rules::require(c && c->is_array(), "reasoning item needs a content array");
|
|
check_content(*c, false, images);
|
|
} else if (type == "web_search_call") {
|
|
// restored search results of a previous response: opaque
|
|
} else {
|
|
Rules::fail("input item type '" + type + "' is not supported (the server would ignore it)");
|
|
}
|
|
}
|
|
for (const std::string& id : calls)
|
|
Rules::require(outputs.count(id) == 1, "every function_call needs a function_call_output");
|
|
for (const std::string& id : custom_calls)
|
|
Rules::require(custom_outputs.count(id) == 1, "every custom_tool_call needs a custom_tool_call_output");
|
|
Rules::require(images <= DeepseekCaps::kMaxImages, "at most 600 images per request");
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void ResponsesParams::validate() const {
|
|
Rules::model(model);
|
|
bool has_text = input_text.has_value();
|
|
bool has_items = input_items.is_array() && !input_items.as_array().empty();
|
|
Rules::require(!(has_text && has_items), "give input as text or as items, not both");
|
|
Rules::require(has_text || has_items || !instructions.empty(),
|
|
"at least one of input and instructions is required");
|
|
if (has_items) check_items(input_items);
|
|
if (!effort.empty()) Rules::effort(effort);
|
|
if (max_output_tokens)
|
|
Rules::require(*max_output_tokens >= 1 && *max_output_tokens <= DeepseekCaps::kMaxTokensCeiling,
|
|
"max_output_tokens must be 1..393216");
|
|
if (temperature) Rules::range(*temperature, 0, 2, "temperature");
|
|
if (top_p) Rules::top_p(*top_p);
|
|
if (format == TextFormat::kJsonSchema) {
|
|
Rules::require(!schema_name.empty(), "json_schema needs a name");
|
|
Rules::require(schema.is_object() && !schema.as_object().empty(), "json_schema needs a schema");
|
|
}
|
|
if (top_logprobs)
|
|
Rules::require(*top_logprobs >= 0 && *top_logprobs <= DeepseekCaps::kMaxTopLogprobs,
|
|
"top_logprobs must be 0..20");
|
|
if (!user.empty()) Rules::user_id(user);
|
|
|
|
Rules::require(tools.is_array() && tools.as_array().size() <= DeepseekCaps::kMaxTools, "too many tools");
|
|
std::set<std::string> names;
|
|
for (const Json& t : tools.as_array()) {
|
|
Rules::require(t.is_object(), "tool must be an object");
|
|
std::string type = J::str(t, "type");
|
|
if (type == "function") {
|
|
Rules::tool_name(J::str(t, "name"));
|
|
Rules::require(names.insert(J::str(t, "name")).second, "tool names must be unique");
|
|
} else if (type == "custom") {
|
|
Rules::require(J::str(t, "name") == "apply_patch", "the only custom tool is apply_patch");
|
|
} else if (type != "web_search") {
|
|
Rules::fail("tool type must be function, custom or web_search");
|
|
}
|
|
}
|
|
if (!tool_choice.empty()) {
|
|
bool mode = tool_choice == "none" || tool_choice == "auto" || tool_choice == "required";
|
|
if (!mode) {
|
|
Rules::tool_name(tool_choice);
|
|
Rules::require(names.count(tool_choice) == 1, "tool_choice names a tool that is not defined");
|
|
} else if (tool_choice == "required") {
|
|
Rules::require(!tools.as_array().empty(), "tool_choice required needs tools");
|
|
}
|
|
}
|
|
}
|
|
|
|
Json ResponsesParams::to_json(bool stream) const {
|
|
validate();
|
|
Json b = Json::object();
|
|
b.set("model", Json(model));
|
|
if (input_text) b.set("input", Json(*input_text));
|
|
else if (!input_items.as_array().empty()) b.set("input", input_items);
|
|
if (!instructions.empty()) b.set("instructions", Json(instructions));
|
|
if (!effort.empty()) {
|
|
Json r = Json::object();
|
|
r.set("effort", Json(effort));
|
|
b.set("reasoning", r);
|
|
}
|
|
if (max_output_tokens) b.set("max_output_tokens", Json(*max_output_tokens));
|
|
if (stream) b.set("stream", Json(true));
|
|
if (temperature) b.set("temperature", Json(*temperature));
|
|
if (top_p) b.set("top_p", Json(*top_p));
|
|
if (format != TextFormat::kText) {
|
|
Json f = Json::object();
|
|
if (format == TextFormat::kJsonObject) {
|
|
f.set("type", Json("json_object"));
|
|
} else {
|
|
f.set("type", Json("json_schema"));
|
|
f.set("name", Json(schema_name));
|
|
f.set("schema", schema);
|
|
}
|
|
Json t = Json::object();
|
|
t.set("format", f);
|
|
b.set("text", t);
|
|
}
|
|
if (!tools.as_array().empty()) b.set("tools", tools);
|
|
if (!tool_choice.empty()) {
|
|
if (tool_choice == "none" || tool_choice == "auto" || tool_choice == "required") {
|
|
b.set("tool_choice", Json(tool_choice));
|
|
} else {
|
|
Json tc = Json::object();
|
|
tc.set("type", Json("function"));
|
|
tc.set("name", Json(tool_choice));
|
|
b.set("tool_choice", tc);
|
|
}
|
|
}
|
|
if (top_logprobs) b.set("top_logprobs", Json(*top_logprobs));
|
|
if (!user.empty()) b.set("user", Json(user));
|
|
return b;
|
|
}
|
|
|
|
// ---- results ---------------------------------------------------------------
|
|
|
|
namespace {
|
|
std::string join_parts(const Json& output, const char* item_type, const char* part_type) {
|
|
std::string out;
|
|
for (const Json& item : output.as_array()) {
|
|
if (J::str(item, "type") != item_type) continue;
|
|
if (const Json* c = J::arr(item, "content"))
|
|
for (const Json& p : c->as_array())
|
|
if (J::str(p, "type") == part_type) out += J::str(p, "text");
|
|
}
|
|
return out;
|
|
}
|
|
} // namespace
|
|
|
|
std::string ResponsesResult::output_text() const { return join_parts(output, "message", "output_text"); }
|
|
std::string ResponsesResult::reasoning_text() const { return join_parts(output, "reasoning", "reasoning_text"); }
|
|
|
|
std::vector<FunctionCallItem> ResponsesResult::function_calls() const {
|
|
std::vector<FunctionCallItem> out;
|
|
for (const Json& item : output.as_array())
|
|
if (J::str(item, "type") == "function_call")
|
|
out.push_back({J::str(item, "call_id"), J::str(item, "name"), J::str(item, "arguments")});
|
|
return out;
|
|
}
|
|
|
|
ResponsesResult ResponsesResult::parse(const Json& doc) {
|
|
if (!doc.is_object()) throw llm::ApiError("deepseek", 502, "BAD_RESPONSE", "response is not an object");
|
|
ResponsesResult r;
|
|
r.raw = doc;
|
|
r.id = J::str(doc, "id");
|
|
r.model = J::str(doc, "model");
|
|
r.status = J::str(doc, "status");
|
|
r.created_at = J::num(doc, "created_at");
|
|
if (const Json* e = doc.find("error"); e && e->is_object()) r.error = *e;
|
|
if (const Json* d = J::obj(doc, "incomplete_details")) r.incomplete_reason = J::str(*d, "reason");
|
|
if (const Json* o = J::arr(doc, "output")) r.output = *o;
|
|
r.usage = TokenUsage::from_response(doc.find("usage"));
|
|
return r;
|
|
}
|
|
|
|
std::string ResponseEvent::text_delta() const {
|
|
if (type.size() > 6 && type.compare(type.size() - 6, 6, ".delta") == 0) return J::str(data, "delta");
|
|
return "";
|
|
}
|
|
|
|
ResponsesResult Responses::create(const ResponsesParams& params) {
|
|
Json body = params.to_json(false);
|
|
return ResponsesResult::parse(client_->post(Calls::render(Call::kResponses), body));
|
|
}
|
|
|
|
ResponsesResult Responses::stream(const ResponsesParams& params,
|
|
const std::function<bool(const ResponseEvent&)>& on_event) {
|
|
Json body = params.to_json(true);
|
|
ResponsesResult final_result;
|
|
bool terminal = false, stopped = false;
|
|
std::size_t events = 0;
|
|
client_->stream(Calls::render(Call::kResponses), body, [&](const llm::SseEvent& ev) {
|
|
if (++events > DeepseekCaps::kMaxStreamEvents)
|
|
throw llm::ApiError("deepseek", 502, "BAD_RESPONSE", "stream over the event cap");
|
|
ResponseEvent e;
|
|
e.type = J::str(ev.data, "type", ev.event);
|
|
e.sequence_number = J::num(ev.data, "sequence_number");
|
|
e.data = ev.data;
|
|
if (e.type == "response.completed" || e.type == "response.incomplete" || e.type == "response.failed") {
|
|
if (const Json* r = J::obj(ev.data, "response")) final_result = ResponsesResult::parse(*r);
|
|
terminal = true;
|
|
}
|
|
if (on_event && !on_event(e)) {
|
|
stopped = true;
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
if (!terminal && !stopped)
|
|
throw llm::ApiError("deepseek", 502, "STREAM_TRUNCATED", "stream ended without a terminal response event");
|
|
return final_result;
|
|
}
|
|
|
|
} // namespace molo::deepseek
|