#include "deepseek/anthropic.h" #include #include "deepseek/calls.h" #include "deepseek/caps.h" #include "deepseek/rules.h" namespace molo::deepseek { // ---- builders -------------------------------------------------------------- Json AnthropicBlocks::text(const std::string& t) { Json b = Json::object(); b.set("type", Json("text")); b.set("text", Json(t)); return b; } static Json image_with(const Json& source) { Json b = Json::object(); b.set("type", Json("image")); b.set("source", source); return b; } Json AnthropicBlocks::image_base64(const std::string& media_type, const std::string& data) { Json s = Json::object(); s.set("type", Json("base64")); s.set("media_type", Json(media_type)); s.set("data", Json(data)); return image_with(s); } Json AnthropicBlocks::image_url(const std::string& url) { Json s = Json::object(); s.set("type", Json("url")); s.set("url", Json(url)); return image_with(s); } Json AnthropicBlocks::image_file(const std::string& file_id) { Json s = Json::object(); s.set("type", Json("file")); s.set("file_id", Json(file_id)); return image_with(s); } Json AnthropicBlocks::thinking(const std::string& t, const std::string& signature) { Json b = Json::object(); b.set("type", Json("thinking")); b.set("thinking", Json(t)); if (!signature.empty()) b.set("signature", Json(signature)); return b; } Json AnthropicBlocks::tool_use(const std::string& id, const std::string& name, const Json& input) { Json b = Json::object(); b.set("type", Json("tool_use")); b.set("id", Json(id)); b.set("name", Json(name)); b.set("input", input); return b; } Json AnthropicBlocks::tool_result(const std::string& tool_use_id, const std::string& content) { Json b = Json::object(); b.set("type", Json("tool_result")); b.set("tool_use_id", Json(tool_use_id)); b.set("content", Json(content)); return b; } Json AnthropicBlocks::message(const std::string& role, const std::vector& blocks) { Json arr = Json::array(); for (const Json& b : blocks) arr.push(b); Json m = Json::object(); m.set("role", Json(role)); m.set("content", arr); return m; } Json AnthropicBlocks::message_text(const std::string& role, const std::string& t) { Json m = Json::object(); m.set("role", Json(role)); m.set("content", Json(t)); return m; } Json AnthropicBlocks::tool(const std::string& name, const std::string& description, const Json& input_schema) { Json t = Json::object(); t.set("name", Json(name)); if (!description.empty()) t.set("description", Json(description)); t.set("input_schema", input_schema); return t; } // ---- params ---------------------------------------------------------------- namespace { const std::set& supported_blocks() { static const std::set s = {"text", "image", "thinking", "tool_use", "tool_result", "server_tool_use", "web_search_tool_result"}; return s; } const std::set& unsupported_blocks() { static const std::set s = {"document", "search_result", "redacted_thinking", "code_execution_tool_result", "mcp_tool_use", "mcp_tool_result", "container_upload"}; return s; } bool media_ok(const std::string& m) { return m == "image/jpeg" || m == "image/png" || m == "image/gif" || m == "image/webp"; } void check_block(const Json& b) { Rules::require(b.is_object(), "content block must be an object"); std::string type = J::str(b, "type"); Rules::require(unsupported_blocks().count(type) == 0, "content block '" + type + "' is not supported by the DeepSeek Anthropic endpoint"); Rules::require(supported_blocks().count(type) == 1, "unknown content block type '" + type + "'"); if (type == "image") { const Json* s = J::obj(b, "source"); Rules::require(s, "image block needs a source"); std::string st = J::str(*s, "type"); if (st == "base64") { Rules::require(media_ok(J::str(*s, "media_type")), "image media_type must be jpeg, png, gif or webp"); Rules::require(!J::str(*s, "data").empty(), "image data is empty"); } else if (st == "url") { Rules::image_url(J::str(*s, "url")); } else if (st == "file") { Rules::require(!J::str(*s, "file_id").empty(), "image file_id is empty"); } else { Rules::fail("image source type must be base64, url or file"); } } else if (type == "tool_use") { Rules::require(!J::str(b, "id").empty(), "tool_use needs id"); Rules::tool_name(J::str(b, "name")); } else if (type == "tool_result") { Rules::require(!J::str(b, "tool_use_id").empty(), "tool_result needs tool_use_id"); } } } // namespace bool MessagesParams::uses_file_image() const { for (const Json& m : messages.as_array()) { const Json* c = m.find("content"); if (!c || !c->is_array()) continue; for (const Json& b : c->as_array()) if (J::str(b, "type") == "image") if (const Json* s = J::obj(b, "source")) if (J::str(*s, "type") == "file") return true; } return false; } void MessagesParams::validate() const { Rules::model(model); Rules::require(max_tokens >= 1 && max_tokens <= DeepseekCaps::kMaxTokensCeiling, "max_tokens must be 1..393216"); Rules::require(messages.is_array() && !messages.as_array().empty(), "messages must not be empty"); Rules::require(messages.as_array().size() <= DeepseekCaps::kMaxMessages, "too many messages"); std::size_t images = 0; for (const Json& m : messages.as_array()) { Rules::require(m.is_object(), "message must be an object"); std::string role = J::str(m, "role"); Rules::require(role == "user" || role == "assistant", "message role must be user or assistant"); const Json* c = m.find("content"); Rules::require(c && (c->is_string() || c->is_array()), "message content must be a string or blocks"); if (c->is_array()) for (const Json& b : c->as_array()) { check_block(b); if (J::str(b, "type") == "image") ++images; } } Rules::require(images <= DeepseekCaps::kMaxImages, "at most 600 images per request"); if (!effort.empty()) Rules::require(effort == "low" || effort == "high" || effort == "max", "effort must be low, high or max"); if (temperature) Rules::range(*temperature, 0, 2, "temperature"); if (top_p) Rules::top_p(*top_p); Rules::stop(stop_sequences); if (!user_id.empty()) Rules::user_id(user_id); Rules::require(tools.is_array() && tools.as_array().size() <= DeepseekCaps::kMaxTools, "too many tools"); std::set names; for (const Json& t : tools.as_array()) { Rules::tool_name(J::str(t, "name")); Rules::require(names.insert(J::str(t, "name")).second, "tool names must be unique"); Rules::require(J::obj(t, "input_schema"), "tool needs input_schema"); } if (!tool_choice.empty()) { bool mode = tool_choice == "none" || tool_choice == "auto" || tool_choice == "any"; 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 == "any") { Rules::require(!names.empty(), "tool_choice any needs tools"); } } } Json MessagesParams::to_json(bool stream) const { validate(); Json b = Json::object(); b.set("model", Json(model)); b.set("max_tokens", Json(max_tokens)); if (!system.empty()) b.set("system", Json(system)); b.set("messages", messages); if (thinking) { Json t = Json::object(); t.set("type", Json(*thinking ? "enabled" : "disabled")); b.set("thinking", t); } if (!effort.empty()) { Json o = Json::object(); o.set("effort", Json(effort)); b.set("output_config", o); } if (!tools.as_array().empty()) b.set("tools", tools); if (!tool_choice.empty()) { Json tc = Json::object(); if (tool_choice == "none" || tool_choice == "auto" || tool_choice == "any") { tc.set("type", Json(tool_choice)); } else { tc.set("type", Json("tool")); tc.set("name", Json(tool_choice)); } b.set("tool_choice", tc); } if (!stop_sequences.empty()) { Json s = Json::array(); for (const std::string& v : stop_sequences) s.push(Json(v)); b.set("stop_sequences", s); } 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 (!user_id.empty()) { Json m = Json::object(); m.set("user_id", Json(user_id)); b.set("metadata", m); } return b; } // ---- results --------------------------------------------------------------- std::string MessagesResult::text() const { std::string out; for (const Json& b : content.as_array()) if (J::str(b, "type") == "text") out += J::str(b, "text"); return out; } std::string MessagesResult::thinking_text() const { std::string out; for (const Json& b : content.as_array()) if (J::str(b, "type") == "thinking") out += J::str(b, "thinking"); return out; } std::vector MessagesResult::tool_uses() const { std::vector out; for (const Json& b : content.as_array()) if (J::str(b, "type") == "tool_use") { ToolUse u; u.id = J::str(b, "id"); u.name = J::str(b, "name"); if (const Json* in = b.find("input")) u.input = *in; out.push_back(u); } return out; } MessagesResult MessagesResult::parse(const Json& doc) { if (!doc.is_object()) throw llm::ApiError("deepseek", 502, "BAD_RESPONSE", "response is not an object"); MessagesResult r; r.raw = doc; r.id = J::str(doc, "id"); r.type = J::str(doc, "type"); r.role = J::str(doc, "role"); r.model = J::str(doc, "model"); r.stop_reason = J::str(doc, "stop_reason"); r.stop_sequence = J::str(doc, "stop_sequence"); if (const Json* c = J::arr(doc, "content")) r.content = *c; r.usage = TokenUsage::from_anthropic(doc.find("usage")); return r; } std::string MessagesStreamAccumulator::feed(const llm::SseEvent& ev) { if (++events_ > DeepseekCaps::kMaxStreamEvents) throw llm::ApiError("deepseek", 502, "BAD_RESPONSE", "stream over the event cap"); const Json& d = ev.data; std::string type = J::str(d, "type", ev.event); std::string text; if (type == "message_start") { if (const Json* m = J::obj(d, "message")) { MessagesResult head = MessagesResult::parse(*m); head.content = Json::array(); result_ = head; } } else if (type == "content_block_start") { std::size_t idx = static_cast(J::num(d, "index")); if (idx >= DeepseekCaps::kMaxMessages) throw llm::ApiError("deepseek", 502, "BAD_RESPONSE", "block index out of range"); const Json* b = J::obj(d, "content_block"); if (b) { auto& arr = result_.content.as_array(); while (arr.size() <= idx) arr.push_back(Json::object()); arr[idx] = *b; if (partial_json_.size() <= idx) partial_json_.resize(idx + 1); } } else if (type == "content_block_delta") { std::size_t idx = static_cast(J::num(d, "index")); auto& arr = result_.content.as_array(); const Json* delta = J::obj(d, "delta"); if (delta && idx < arr.size() && arr[idx].is_object()) { Json& block = arr[idx]; std::string dt = J::str(*delta, "type"); if (dt == "text_delta") { text = J::str(*delta, "text"); block.set("text", Json(J::str(block, "text") + text)); } else if (dt == "thinking_delta") { block.set("thinking", Json(J::str(block, "thinking") + J::str(*delta, "thinking"))); } else if (dt == "signature_delta") { block.set("signature", Json(J::str(*delta, "signature"))); } else if (dt == "input_json_delta") { if (partial_json_.size() <= idx) partial_json_.resize(idx + 1); partial_json_[idx] += J::str(*delta, "partial_json"); } } } else if (type == "content_block_stop") { std::size_t idx = static_cast(J::num(d, "index")); auto& arr = result_.content.as_array(); if (idx < arr.size() && idx < partial_json_.size() && !partial_json_[idx].empty()) { try { arr[idx].set("input", Json::parse(partial_json_[idx])); } catch (const std::exception&) { arr[idx].set("input_invalid_json", Json(partial_json_[idx])); } } } else if (type == "message_delta") { if (const Json* delta = J::obj(d, "delta")) { std::string sr = J::str(*delta, "stop_reason"); if (!sr.empty()) result_.stop_reason = sr; std::string ss = J::str(*delta, "stop_sequence"); if (!ss.empty()) result_.stop_sequence = ss; } if (const Json* u = J::obj(d, "usage")) { TokenUsage add = TokenUsage::from_anthropic(u); if (add.output_tokens) result_.usage.output_tokens = add.output_tokens; if (add.input_tokens) result_.usage.input_tokens = add.input_tokens; result_.usage.total_tokens = result_.usage.input_tokens + result_.usage.output_tokens; result_.usage.present = true; } } else if (type == "message_stop") { done_ = true; } else if (type == "error") { const Json* e = J::obj(d, "error"); throw llm::ApiError("deepseek", 502, e ? J::str(*e, "type", "stream_error") : "stream_error", e ? J::str(*e, "message") : "stream error event"); } return text; } MessagesResult MessagesStreamAccumulator::result() const { return result_; } MessagesResult AnthropicMessages::create(const MessagesParams& params) { Json body = params.to_json(false); llm::Headers extra; if (params.uses_file_image()) extra["anthropic-beta"] = Calls::anthropic_files_beta(); return MessagesResult::parse(client_->post(Calls::render(Call::kMessages), body, extra)); } MessagesResult AnthropicMessages::stream( const MessagesParams& params, const std::function& on_event) { Json body = params.to_json(true); llm::Headers extra; if (params.uses_file_image()) extra["anthropic-beta"] = Calls::anthropic_files_beta(); MessagesStreamAccumulator acc; bool stopped = false; client_->stream(Calls::render(Call::kMessages), body, [&](const llm::SseEvent& ev) { std::string text = acc.feed(ev); if (on_event && !on_event(ev, text)) { stopped = true; return false; } return true; }, extra); if (!acc.done() && !stopped) throw llm::ApiError("deepseek", 502, "STREAM_TRUNCATED", "stream ended without message_stop"); return acc.result(); } } // namespace molo::deepseek