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

#include <chat_tokenizer.h>

Collaboration diagram for ember::ChatFormat:
Collaboration graph

Public Member Functions

bool is_eos (int id) const
 
bool is_stop (int id) const
 
std::vector< int > stop_ids () const
 
bool v1_markers () const
 
std::string prompt (const std::vector< std::pair< std::string, std::string > > &turns, size_t from, const std::string &user, const std::string *system=nullptr, int thinking=-1) const
 

Static Public Member Functions

static ChatFormat of (const chat::Template &t, const std::vector< std::string > &tokens, std::string why)
 

Public Attributes

chat::Template tmpl
 
std::string name
 
std::string why
 
std::vector< int > eos_ids
 
std::vector< int > turn_ids
 
int tool_call_id = -1
 

Detailed Description

Definition at line 53 of file chat_tokenizer.h.

Member Function Documentation

◆ is_eos()

bool ember::ChatFormat::is_eos ( int  id) const
inline

Definition at line 89 of file chat_tokenizer.h.

89 {
90 for (int e : eos_ids)
91 if (e == id) return true;
92 return false;
93 }
size_t j
std::vector< int > eos_ids

References eos_ids, and j.

Referenced by is_stop().

◆ is_stop()

bool ember::ChatFormat::is_stop ( int  id) const
inline

Definition at line 94 of file chat_tokenizer.h.

94 {
95 if (is_eos(id)) return true;
96 for (int t : turn_ids)
97 if (t == id) return true;
98 return false;
99 }
std::vector< int > turn_ids
bool is_eos(int id) const

References is_eos(), j, and turn_ids.

◆ of()

static ChatFormat ember::ChatFormat::of ( const chat::Template &  t,
const std::vector< std::string > &  tokens,
std::string  why 
)
inlinestatic

Definition at line 61 of file chat_tokenizer.h.

61 {
62 ChatFormat f;
63 f.tmpl = t;
64 f.name = t.id();
65 f.why = std::move(why);
66 auto ids = [&](const std::vector<std::string>& stops, const char* field) {
67 std::vector<int> v;
68 for (const auto& s : stops) {
69 const int id = token_id(tokens, s);
70 if (id < 0)
71 throw chat::Error("chat template " + t.id() + ": stop." + field + " '" + s +
72 "' is not a single token of this vocabulary (ember stops on single tokens only)");
73 v.push_back(id);
74 }
75 return v;
76 };
77 f.eos_ids = ids(t.stop_eos, "eos");
78 f.turn_ids = ids(t.stop_turn, "turn");
79 if (f.eos_ids.empty()) throw chat::Error("chat template " + t.id() + " has no stop.eos token");
80 if (t.has_tool) {
81 f.tool_call_id = token_id(tokens, t.tool_call_open);
82 if (f.tool_call_id < 0)
83 throw chat::Error("chat template " + t.id() + ": tool_call.open '" + t.tool_call_open +
84 "' is not a single token of this vocabulary");
85 }
86 return f;
87 }
std::vector< float > v
int token_id(const std::vector< std::string > &tokens, const std::string &text)

References j, microtorch::chat::Template::name, tmpl, ember::token_id(), v, and why.

Referenced by ember::make_chat_setup().

◆ prompt()

std::string ember::ChatFormat::prompt ( const std::vector< std::pair< std::string, std::string > > &  turns,
size_t  from,
const std::string &  user,
const std::string *  system = nullptr,
int  thinking = -1 
) const
inline

Definition at line 119 of file chat_tokenizer.h.

120 {
121 const std::vector<std::pair<std::string, std::string>> kept(turns.begin() + static_cast<std::ptrdiff_t>(from),
122 turns.end());
123 return chat::render(chat::from_pairs(kept, &user, system), tmpl, /*generation_prompt=*/true, thinking);
124 }
std::string render(const std::vector< Message > &m, const Template &t, bool generation_prompt, int thinking=-1, const std::vector< json > &tools={})
std::vector< Message > from_pairs(const std::vector< std::pair< std::string, std::string > > &turns, const std::string *user=nullptr, const std::string *system=nullptr)
chat::Template tmpl

References microtorch::chat::from_pairs(), j, microtorch::chat::render(), and tmpl.

◆ stop_ids()

std::vector< int > ember::ChatFormat::stop_ids ( ) const
inline

Definition at line 100 of file chat_tokenizer.h.

100 {
101 std::vector<int> v = eos_ids;
102 v.insert(v.end(), turn_ids.begin(), turn_ids.end());
103 return v;
104 }

References eos_ids, turn_ids, and v.

◆ v1_markers()

bool ember::ChatFormat::v1_markers ( ) const
inline

Definition at line 107 of file chat_tokenizer.h.

107 {
108 const chat::Template v = chat::v1();
109 return tmpl.name == v.name && tmpl.version == v.version && tmpl.bos.empty() && tmpl.content == "raw" &&
110 tmpl.user_open == v.user_open && tmpl.user_close == v.user_close &&
111 tmpl.assistant_open == v.assistant_open && tmpl.assistant_close == v.assistant_close &&
112 tmpl.generation_prompt == v.generation_prompt && tmpl.system_mode != "turn" &&
113 tmpl.stop_eos == v.stop_eos && tmpl.stop_turn == v.stop_turn;
114 }
std::vector< std::string > stop_eos
std::vector< std::string > stop_turn

References microtorch::chat::Template::assistant_close, microtorch::chat::Template::assistant_open, microtorch::chat::Template::bos, microtorch::chat::Template::content, microtorch::chat::Template::generation_prompt, microtorch::chat::Template::name, microtorch::chat::Template::stop_eos, microtorch::chat::Template::stop_turn, microtorch::chat::Template::system_mode, tmpl, microtorch::chat::Template::user_close, microtorch::chat::Template::user_open, v, microtorch::chat::v1(), and microtorch::chat::Template::version.

Member Data Documentation

◆ eos_ids

std::vector<int> ember::ChatFormat::eos_ids

Definition at line 57 of file chat_tokenizer.h.

Referenced by is_eos(), main(), and stop_ids().

◆ name

std::string ember::ChatFormat::name

Definition at line 55 of file chat_tokenizer.h.

◆ tmpl

chat::Template ember::ChatFormat::tmpl

Definition at line 54 of file chat_tokenizer.h.

Referenced by of(), prompt(), and v1_markers().

◆ tool_call_id

int ember::ChatFormat::tool_call_id = -1

Definition at line 58 of file chat_tokenizer.h.

◆ turn_ids

std::vector<int> ember::ChatFormat::turn_ids

Definition at line 57 of file chat_tokenizer.h.

Referenced by is_stop(), and stop_ids().

◆ why

std::string ember::ChatFormat::why

Definition at line 56 of file chat_tokenizer.h.

Referenced by of().


The documentation for this struct was generated from the following file: