Prompt Tools 0.3.2
Loading...
Searching...
No Matches
prompt_base_class.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <string>
6
7namespace prompt
8{
9
17{
18public:
25 {
26 }
27
33 virtual ~PromptBaseClass() = default;
34
45 virtual void initialize_prompt_base(rclcpp::Node::SharedPtr node, std::string plugin_name = "PromptBaseClass",
46 std::string api_key_name = "")
47 {
48 // initialize base class
49 initialize_rest_base(node, plugin_name, api_key_name);
50
51 // declare parameters only if not already declared (idempotent init)
52 if (!node_->has_parameter(plugin_name_ + ".rest.uri"))
53 {
54 node_->declare_parameter(plugin_name_ + ".rest.uri", "");
55 }
56 if (!node_->has_parameter(plugin_name_ + ".rest.chat_uri"))
57 {
58 node_->declare_parameter(plugin_name_ + ".rest.chat_uri", "");
59 }
60
61 // get parameters from the parameter server
62 uri_ = node_->get_parameter(plugin_name_ + ".rest.uri").as_string();
63 chat_uri_ = node_->get_parameter(plugin_name_ + ".rest.chat_uri").as_string();
64
65 // log the parameters
66 RCLCPP_INFO(node_->get_logger(), "%s Single URI: %s", plugin_name_.c_str(), uri_.c_str());
67 RCLCPP_INFO(node_->get_logger(), "%s Chat URI: %s", plugin_name_.c_str(), chat_uri_.c_str());
68 }
69
79 {
80 // verify and add required model options
82
83 // prepare request body
84 Poco::JSON::Object body_json = toJson(req);
85
86 // process the prompt
87 Poco::JSON::Object::Ptr object = process(body_json, uri_);
88
89 // create prompt provider response container
90 prompt::PromptResponse res = fromJson(object);
91
92 return res;
93 }
94
105 std::vector<PromptDialogue>& conversation)
106 {
107 // verify and add required model options
109
110 // prepare request body
111 Poco::JSON::Object body_json = toJsonConversation(latest_request, conversation);
112
113 // process the prompt
114 Poco::JSON::Object::Ptr object = process(body_json, chat_uri_);
115
116 // create prompt provider response container
118
119 return res;
120 }
121
122protected:
132 virtual Poco::JSON::Object toJson(prompt::PromptRequest& prompt) = 0;
133
144 virtual Poco::JSON::Object toJsonConversation(prompt::PromptRequest& latest_request,
145 std::vector<PromptDialogue>& conversation) = 0;
146
156 virtual prompt::PromptResponse fromJson(const Poco::JSON::Object::Ptr object) = 0;
157
168 virtual prompt::PromptResponse fromJsonConversation(const Poco::JSON::Object::Ptr object) = 0;
169
173 std::string uri_;
174
178 std::string chat_uri_;
179};
180
181} // namespace prompt
rclcpp::Node::SharedPtr node_
Node shared pointer.
Definition base_class.hpp:105
std::string plugin_name_
Plugin name.
Definition base_class.hpp:112
PromptBaseClass.
Definition prompt_base_class.hpp:17
virtual prompt::PromptResponse fromJson(const Poco::JSON::Object::Ptr object)=0
Convert a JSON object to a prompt response.
PromptBaseClass()
Constructor.
Definition prompt_base_class.hpp:24
virtual prompt::PromptResponse sendPrompt(prompt::PromptRequest &req)
sendPrompt send a prompt to a prompt provider using REST
Definition prompt_base_class.hpp:78
std::string uri_
URI for single prompt requests.
Definition prompt_base_class.hpp:173
virtual prompt::PromptResponse sendConversation(prompt::PromptRequest &latest_request, std::vector< PromptDialogue > &conversation)
sendConversation send a prompt with conversation history to a prompt provider using REST
Definition prompt_base_class.hpp:104
virtual ~PromptBaseClass()=default
Destructor.
std::string chat_uri_
URI for chat/conversation requests.
Definition prompt_base_class.hpp:178
virtual void initialize_prompt_base(rclcpp::Node::SharedPtr node, std::string plugin_name="PromptBaseClass", std::string api_key_name="")
Initialize the REST base class.
Definition prompt_base_class.hpp:45
virtual Poco::JSON::Object toJson(prompt::PromptRequest &prompt)=0
Convert a prompt request to a JSON object.
virtual prompt::PromptResponse fromJsonConversation(const Poco::JSON::Object::Ptr object)=0
Convert a JSON object to a prompt response with conversation history.
virtual Poco::JSON::Object toJsonConversation(prompt::PromptRequest &latest_request, std::vector< PromptDialogue > &conversation)=0
Convert a prompt request with conversation history to a JSON object.
RestBaseClass.
Definition rest_base_class.hpp:31
Poco::JSON::Object::Ptr process(Poco::JSON::Object &body_json, std::string &uri)
Process the HTTP request and return the response as a JSON object.
Definition rest_base_class.hpp:170
std::vector< prompt::PromptOption > required_options_
Model options.
Definition rest_base_class.hpp:371
virtual void initialize_rest_base(rclcpp::Node::SharedPtr node, std::string plugin_name="RestBaseClass", std::string api_key_name="")
Initialize the REST base class.
Definition rest_base_class.hpp:59
Definition base_class.hpp:8
void ensure_model_options(rclcpp::Node::SharedPtr node, std::vector< prompt::PromptOption > &options, std::vector< prompt::PromptOption > &required_options)
Check and add required model options to the prompt options.
Definition prompt_options.hpp:149
Definition structs.hpp:24
std::vector< PromptOption > options
Definition structs.hpp:30
Definition structs.hpp:35