2 #ifndef _NNDEPLOY_LLM_STREAM_OUT_H_
3 #define _NNDEPLOY_LLM_STREAM_OUT_H_
43 StreamOut(
const std::string& name, std::vector<dag::Edge*> inputs,
44 std::vector<dag::Edge*> outputs)
45 : dag::Node(name, inputs, outputs) {
46 key_ =
"nndeploy::llm::StreamOut";
47 desc_ =
"StreamOut: Stream output node";
48 this->setDynamicOutput(
true);
49 this->setInputTypeInfo<tokenizer::TokenizerText>(
"input_text");
50 this->setOutputTypeInfo<tokenizer::TokenizerText>(
"output_text");
63 delete stream_output_;
70 std::string text = *stream_output_;
71 std::string last_text =
"";
72 size_t last_space = text.find_last_of(
' ');
73 if (last_space != std::string::npos) {
74 last_text = text.substr(last_space + 1);
78 return std::find(stop_texts_.begin(), stop_texts_.end(), last_text) !=
86 stream_output_ =
new std::string();
91 output_text_->texts_.resize(input_text->
texts_.size());
95 *stream_output_ = input_text->
texts_[0];
98 outputs_[0]->set(output_text_,
true);
103 output_text_->texts_[0] += (*stream_output_);
104 if (enable_stream_) {
109 if (outputs_.size() > 1) {
110 outputs_[1]->set(stream_output_,
true);
117 rapidjson::Value& json,
118 rapidjson::Document::AllocatorType& allocator)
override {
123 json.AddMember(
"enable_stream_", enable_stream_, allocator);
132 if (json.HasMember(
"enable_stream_") && json[
"enable_stream_"].IsBool()) {
133 enable_stream_ = json[
"enable_stream_"].GetBool();
140 bool enable_stream_ =
true;
142 bool is_first_ =
true;
145 std::string* stream_output_ =
nullptr;
147 std::vector<std::string> stop_texts_ = {
148 "<|endoftext|>",
"<|im_end|>",
"</s>",
"<|end|>",
"<|eot_id|>",
"[DONE]"};
virtual std::string serialize()
Serialize to JSON string.
virtual base::Status deserialize(rapidjson::Value &json)
Deserialize from JSON.
virtual base::Status init() override
Initialize node.
virtual base::Status run() override
Run node (pure virtual function)
virtual base::Status serialize(rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator) override
Serialize to JSON.
virtual base::Status deserialize(rapidjson::Value &json) override
Deserialize from JSON.
StreamOut(const std::string &name, std::vector< dag::Edge * > inputs, std::vector< dag::Edge * > outputs)
virtual base::Status deinit() override
Deinitialize node.
std::vector< std::string > texts_
#define NNDEPLOY_PRINTF(fmt,...)
#define NNDEPLOY_CC_API
api