| 54 | } |
| 55 | json tools; |
| 56 | bool GPT_OSS::insert(chat_meta_info_t& meta_info, lm_uniform_input_t& input, std::function<bool()> is_cancelled) |
| 57 | { |
| 58 | // preprocess |
| 59 | this->profiler_list[TKOEN_ENCODE_TIME].start(); |
| 60 | std::string templated_text; |
| 61 | if (input.messages.empty() && input.prompt.empty()) { |
| 62 | header_print("WARNING", "No messages or prompt provided"); |
| 63 | return false; |
| 64 | } |
| 65 | if (!input.messages.empty()) { // already a formated messages, usually from REST API |
| 66 | tools = input.tools; |
| 67 | templated_text = this->apply_chat_template(input.messages); |
| 68 | //templated_text = this->apply_chat_template(input.messages, input.tools); |
| 69 | } |
| 70 | else if (!input.prompt.empty()) { // a pure text, usually from the cli |
| 71 | nlohmann::ordered_json messages; |
| 72 | |
| 73 | messages.push_back({ {"role", "user"}, {"content", input.prompt} }); |
| 74 | templated_text = this->apply_chat_template(messages); |
| 75 | } |
| 76 | |
| 77 | std::vector<int> tokens = this->tokenizer->encode(templated_text); |
| 78 | |
| 79 | this->profiler_list[TKOEN_ENCODE_TIME].stop(tokens.size()); |
| 80 | |
| 81 | // hardware |
| 82 | int restore_idx = -1; |
| 83 | gpt_oss_npu *gpt_oss_engine = dynamic_cast<gpt_oss_npu*>(this->lm_engine.get()); |
| 84 | if (meta_info.restore_allowed) { |
| 85 | restore_idx = gpt_oss_engine->restore(); |
| 86 | this->total_tokens = restore_idx; |
| 87 | this->token_history = checkpoint_his; // restore the token history to be consistent with the restored KV cache, which is crucial for correct functioning of _shared_insert's prefix-matching logic |
| 88 | } |
| 89 | bool success = this->_shared_insert(meta_info, tokens, is_cancelled, nullptr); |
| 90 | |
| 91 | checkpoint_his = token_history; |
| 92 | int checkpoint_idx = gpt_oss_engine->checkpoint(); |
| 93 | |
| 94 | return success; |
| 95 | } |
| 96 | |
| 97 | std::string GPT_OSS::generate(chat_meta_info_t& meta_info, int length_limit, std::ostream& os, std::function<bool()> is_cancelled) { |
| 98 | os << "<|start|>" << std::flush; |
no test coverage detected