Prompt Tools 0.3.2
Loading...
Searching...
No Matches
openai_tokenize.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "encoding.h"
5
6namespace prompt
7{
8
15{
16public:
23
31 virtual void initialize(rclcpp::Node::SharedPtr node) override
32 {
33 // initialize base class
34 initialize_tokenize_base(node, "OpenAITokenize");
35 }
36
37protected:
48 {
49 // Initialize the reponse
51
52 //find the 'model' option from the modeloptions
53 std::string model_name;
54
55 for (const auto& option : input.options)
56 {
57 if (option.key == "model")
58 {
59 model_name = option.value;
60 break;
61 }
62 }
63
64 if (model_name=="O200K_BASE")
65 {
66 encoder_ = GptEncoding::get_encoding(LanguageModel::O200K_BASE);
67 }
68 else if ((model_name=="CL100K_BASE"))
69 {
70 encoder_ = GptEncoding::get_encoding(LanguageModel::CL100K_BASE);
71 }
72 else if (model_name=="R50K_BASE")
73 {
74 encoder_ = GptEncoding::get_encoding(LanguageModel::R50K_BASE);
75 }
76 else if (model_name=="P50K_BASE")
77 {
78 encoder_ = GptEncoding::get_encoding(LanguageModel::P50K_BASE);
79 }
80 else if (model_name=="P50K_EDIT")
81 {
82 encoder_ = GptEncoding::get_encoding(LanguageModel::P50K_EDIT);
83 }
84 else
85 {
86 // Add as error log
87 res.success = false;
88 res.error = "OpenAITokenize: Unsupported model for tokenization: " + model_name;
89
90 RCLCPP_ERROR(node_->get_logger(), res.error.c_str());
91 throw std::runtime_error(res.error);
92 }
93
94 if (input.encode)
95 {
96 // Tokenize the input text
97 res.tokens = encoder_->encode(input.text);
98 res.success = true;
99 }
100 else
101 {
102 // Decode the input tokens
103 res.text = encoder_->decode(input.tokens);
104 res.success = true;
105 }
106
107 return res;
108 }
109
110 std::shared_ptr<GptEncoding> encoder_;
111};
112
113} // namespace prompt
rclcpp::Node::SharedPtr node_
Node shared pointer.
Definition base_class.hpp:105
OpenAITokenize.
Definition openai_tokenize.hpp:15
virtual prompt::TokenResponse process(prompt::TokenRequest &input) override
Convert a tokenize request to a JSON object.
Definition openai_tokenize.hpp:47
OpenAITokenize()
Construct a new OpenAI Tokenize Provider object.
Definition openai_tokenize.hpp:20
virtual void initialize(rclcpp::Node::SharedPtr node) override
Initialize the OpenAITokenize.
Definition openai_tokenize.hpp:31
std::shared_ptr< GptEncoding > encoder_
Definition openai_tokenize.hpp:110
TokenizeBaseClass.
Definition tokenize_base_class.hpp:16
virtual void initialize_tokenize_base(rclcpp::Node::SharedPtr node, std::string plugin_name="TokenizeBaseClass")
Initialize the REST base class.
Definition tokenize_base_class.hpp:44
Definition base_class.hpp:8
Definition structs.hpp:71
std::string text
Definition structs.hpp:72
std::vector< PromptOption > options
Definition structs.hpp:76
bool encode
Definition structs.hpp:74
std::vector< int > tokens
Definition structs.hpp:73
Definition structs.hpp:80
std::vector< int > tokens
Definition structs.hpp:81
bool success
Definition structs.hpp:83
std::string error
Definition structs.hpp:84
std::string text
Definition structs.hpp:82