ember.cpp 0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
Loading...
Searching...
No Matches
ember::detail Namespace Reference

Functions

int thinking_field (const nlohmann::json &v, const char *name)
 
bool tool_turns (const chat::Template *t)
 
void refuse_tools_v1 (const nlohmann::json &r)
 
std::vector< chat::json > tool_definitions (const nlohmann::json &r, const chat::json &ordered)
 
std::string history_content (const nlohmann::json &h, size_t i, bool calls_may_omit=false)
 
void refuse_tool_members_v1 (const nlohmann::json &m, const std::string &at)
 
std::pair< std::string, std::string > history_pair (const nlohmann::json &h, size_t i)
 
chat::Message tool_turn_message (const std::string &role, const std::string &content, const nlohmann::json &m)
 
void push_system (std::vector< chat::Message > &m, const std::string &s)
 
void openai_thinking (const nlohmann::json &body, Conversation &c)
 

Function Documentation

◆ history_content()

std::string ember::detail::history_content ( const nlohmann::json &  h,
size_t  i,
bool  calls_may_omit = false 
)
inline

Definition at line 83 of file chat_request.h.

83 {
84 const std::string at = "history[" + std::to_string(i) + "]";
85 const bool calls = h.contains("tool_calls") && !h["tool_calls"].is_null();
86 if (!h.contains("content") || h["content"].is_null()) {
87 if (calls_may_omit && calls && h.value("role", "") == "assistant") return std::string();
88 throw RequestError(at + " needs a string content (an assistant entry with tool_calls may leave it null)");
89 }
90 if (!h["content"].is_string()) throw RequestError(at + ".content must be a string");
91 return h["content"].get<std::string>();
92}
size_t j

References j.

Referenced by ember::chat_conversation(), and ember::chat_conversation_tools().

◆ history_pair()

std::pair< std::string, std::string > ember::detail::history_pair ( const nlohmann::json &  h,
size_t  i 
)
inline

Definition at line 104 of file chat_request.h.

104 {
105 const std::string at = "history[" + std::to_string(i) + "]";
106 if (!h.contains("user") || !h["user"].is_string() || !h.contains("assistant") || !h["assistant"].is_string())
107 throw RequestError(at + " needs a string role and content, or string user and assistant");
108 return {h["user"].get<std::string>(), h["assistant"].get<std::string>()};
109}

References j.

Referenced by ember::chat_conversation(), and ember::chat_conversation_tools().

◆ openai_thinking()

void ember::detail::openai_thinking ( const nlohmann::json &  body,
Conversation &  c 
)
inline

Definition at line 291 of file chat_request.h.

291 {
292 if (body.contains("thinking")) c.thinking = thinking_field(body["thinking"], "thinking");
293 if (body.contains("chat_template_kwargs") && body["chat_template_kwargs"].is_object() &&
294 body["chat_template_kwargs"].contains("enable_thinking"))
295 c.thinking = thinking_field(body["chat_template_kwargs"]["enable_thinking"],
296 "chat_template_kwargs.enable_thinking");
297}
float c
int thinking_field(const nlohmann::json &v, const char *name)

References body, c, and thinking_field().

Referenced by ember::openai_conversation(), and ember::openai_conversation_tools().

◆ push_system()

void ember::detail::push_system ( std::vector< chat::Message > &  m,
const std::string &  s 
)
inline

Definition at line 128 of file chat_request.h.

128 {
129 if (!s.empty()) m.insert(m.begin(), chat::Message("system", s));
130}

References j.

Referenced by ember::chat_conversation_tools(), and ember::openai_conversation_tools().

◆ refuse_tool_members_v1()

void ember::detail::refuse_tool_members_v1 ( const nlohmann::json &  m,
const std::string &  at 
)
inline

Definition at line 96 of file chat_request.h.

96 {
97 if (m.contains("tool_calls"))
98 throw RequestError(at + ": tool_calls are not supported: this model's chat template has no tool turns");
99 if (m.contains("tool_call_id"))
100 throw RequestError(at + ": tool_call_id is not supported: this model's chat template has no tool messages");
101}

References j.

Referenced by ember::chat_conversation(), and ember::openai_conversation().

◆ refuse_tools_v1()

void ember::detail::refuse_tools_v1 ( const nlohmann::json &  r)
inline

Definition at line 58 of file chat_request.h.

58 {
59 if (!r.is_object() || !r.contains("tools") || r["tools"].is_null()) return;
60 if (r["tools"].is_array() && r["tools"].empty()) return;
61 throw RequestError("tools are not supported: this model's chat template has no tool turns");
62}

References j.

Referenced by ember::chat_conversation(), and ember::openai_conversation().

◆ thinking_field()

int ember::detail::thinking_field ( const nlohmann::json &  v,
const char *  name 
)
inline

Definition at line 49 of file chat_request.h.

49 {
50 if (!v.is_boolean()) throw RequestError(std::string(name) + " must be true or false");
51 return v.get<bool>() ? 1 : 0;
52}
std::vector< float > v
const char * name

References name, and v.

Referenced by ember::chat_conversation(), ember::chat_conversation_tools(), and openai_thinking().

◆ tool_definitions()

std::vector< chat::json > ember::detail::tool_definitions ( const nlohmann::json &  r,
const chat::json &  ordered 
)
inline

Definition at line 67 of file chat_request.h.

67 {
68 const chat::json src = !ordered.is_null() ? ordered
69 : r.is_object() && r.contains("tools") ? chat::json::parse(r["tools"].dump())
70 : chat::json();
71 std::vector<chat::json> out;
72 if (src.is_null()) return out;
73 if (!src.is_array()) throw RequestError("tools must be a list of tool definitions (JSON objects)");
74 for (const auto& d : src) {
75 if (!d.is_object()) throw RequestError("tools must be a list of tool definitions (JSON objects)");
76 out.push_back(d);
77 }
78 return out;
79}
node out
nlohmann::ordered_json json
nlohmann::json json
Definition server.cpp:68

References j, and out.

Referenced by ember::chat_conversation_tools(), and ember::openai_conversation_tools().

◆ tool_turn_message()

chat::Message ember::detail::tool_turn_message ( const std::string &  role,
const std::string &  content,
const nlohmann::json &  m 
)
inline

Definition at line 114 of file chat_request.h.

114 {
115 chat::json x = chat::json::object();
116 x["role"] = role;
117 x["content"] = content;
118 if (m.contains("tool_calls")) x["tool_calls"] = chat::json::parse(m["tool_calls"].dump());
119 if (m.contains("tool_call_id")) x["tool_call_id"] = chat::json::parse(m["tool_call_id"].dump());
120 try {
121 return chat::messages_from_json(chat::json::array({x}))[0];
122 } catch (const chat::Error& e) {
123 throw RequestError(e.what());
124 }
125}
std::vector< Message > messages_from_json(const J &a)

References j, and microtorch::chat::messages_from_json().

Referenced by ember::chat_conversation_tools(), and ember::openai_conversation_tools().

◆ tool_turns()

bool ember::detail::tool_turns ( const chat::Template *  t)
inline

Definition at line 54 of file chat_request.h.

54{ return t && t->has_tool; }

References j.

Referenced by ember::chat_conversation(), and ember::openai_conversation().