nndeploy C++ API  0.2.0
nndeploy C++ API
prompt.h
Go to the documentation of this file.
1 #ifndef _NNDEPLOY_LLM_PROMPT_H_
2 #define _NNDEPLOY_LLM_PROMPT_H_
3 
4 #include "nndeploy/base/any.h"
5 #include "nndeploy/base/common.h"
7 #include "nndeploy/base/log.h"
8 #include "nndeploy/base/macro.h"
9 #include "nndeploy/base/object.h"
11 #include "nndeploy/base/param.h"
12 #include "nndeploy/base/status.h"
13 #include "nndeploy/base/string.h"
15 #include "nndeploy/dag/edge.h"
16 #include "nndeploy/dag/graph.h"
17 #include "nndeploy/dag/loop.h"
18 #include "nndeploy/dag/node.h"
19 #include "nndeploy/device/buffer.h"
20 #include "nndeploy/device/device.h"
22 #include "nndeploy/device/tensor.h"
24 #include "nndeploy/tokenizer/tokenizer_cpp/tokenizer_cpp.h"
25 
26 namespace nndeploy {
27 namespace llm {
28 
30  public:
31  std::string prompt_template_ =
32  "<|im_start|>user\n%s<|im_end|>\n<|im_start|>assistant\n";
33  std::string user_content_;
34 
35  public:
36  base::Status serialize(rapidjson::Value& json,
37  rapidjson::Document::AllocatorType& allocator);
38  base::Status deserialize(rapidjson::Value& json);
39 
42 };
43 
45  public:
46  Prompt(const std::string& name, std::vector<dag::Edge*> inputs,
47  std::vector<dag::Edge*> outputs)
48  : Node(name, inputs, outputs) {
49  key_ = "nndeploy::llm::Prompt";
50  desc_ =
51  "Generate TokenizerText from prompt string using optional template.";
52  param_ = std::make_shared<PromptParam>();
53  this->setOutputTypeInfo<tokenizer::TokenizerText>();
54  node_type_ = dag::NodeType::kNodeTypeInput;
55  this->setIoType(dag::IOType::kIOTypeString);
56  }
57  virtual ~Prompt() {}
58  virtual base::Status run();
59 
61  if (index_ < size_) {
63  } else {
64  if (size_ == 0) {
66  } else {
68  }
69  }
70  }
71 
72  void setSize(int size) {
73  if (size > 0) {
74  size_ = size;
75  }
76  }
77  int getSize() { return size_; }
78 
79  protected:
80  std::string applyTemplate(std::string prompt_template,
81  const std::string& content,
82  const std::string& role = "");
83 
84  private:
85  int index_ = 0;
86  int size_ = 1;
87 };
88 
89 } // namespace llm
90 } // namespace nndeploy
91 
92 #endif // _NNDEPLOY_LLM_PROMPT_H_
Node base class.
Definition: node.h:171
base::Status serialize(rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator)
base::Status deserialize(rapidjson::Value &json)
std::string user_content_
Definition: prompt.h:33
virtual base::Status run()
Run node (pure virtual function)
Prompt(const std::string &name, std::vector< dag::Edge * > inputs, std::vector< dag::Edge * > outputs)
Definition: prompt.h:46
virtual base::EdgeUpdateFlag updateInput()
Update input.
Definition: prompt.h:60
void setSize(int size)
Definition: prompt.h:72
virtual ~Prompt()
Definition: prompt.h:57
std::string applyTemplate(std::string prompt_template, const std::string &content, const std::string &role="")
#define NNDEPLOY_CC_API
api
Definition: macro.h:29
@ kEdgeUpdateFlagComplete
Definition: common.h:366
@ kEdgeUpdateFlagTerminate
Definition: common.h:367
#define PARAM_COPY_TO(param_type)
Definition: param.h:25
#define PARAM_COPY(param_type)
Definition: param.h:16