6#ifndef _USE_MATH_DEFINES
7#define _USE_MATH_DEFINES
40 std::string model_path;
55 if(has_prompt_context)
63 auto stream = std::ifstream(prompt_file_fullpath);
70 std::ostringstream sstr;
71 sstr << stream.rdbuf();
93 nlohmann::json request_json;
94 request_json[
"model"] =
"default";
98 nlohmann::json messages_array = nlohmann::json::array();
99 for (
const auto& msg : m_conversation) {
100 messages_array.push_back({{
"role", msg.type}, {
"content", msg.content}});
104 messages_array.push_back({{
"role",
"user"}, {
"content", question}});
107 request_json[
"messages"] = messages_array;
117 message.
type =
"user";
119 m_conversation.push_back(message);
125 message.
type =
"assitant";
126 message.
content = final_output;
127 m_conversation.push_back(message);
130 oAnswer.
type =
"assistant";
131 oAnswer.
content = final_output;
140 if(prompt_set ==
true){
146 first_prompt_set =
true;
148 m_prompt.
type =
"system";
153 message.
type =
"system";
155 m_conversation.push_back(message);
164 if(prompt_set ==
false){
178 if(m_conversation.empty()){
184 for (
const auto& msg : oConversation) {
191 oConversation = m_conversation;
199 if (m_conversation.empty()){
204 m_conversation.clear();
206 first_prompt_set =
false;
224 std::cout <<
"Llama2Device Methods:" << std::endl;
225 std::cout <<
"1. setPrompt" << std::endl;
226 std::cout <<
" - Set the initial prompt for the model." << std::endl;
227 std::cout <<
"2. ask" << std::endl;
228 std::cout <<
" - Ask a question to the model and get an answer." << std::endl;
229 std::cout <<
"3. getConversation" << std::endl;
230 std::cout <<
" - Prints the current conversation with the model." << std::endl;
231 std::cout <<
"4. deleteConversation" << std::endl;
232 std::cout <<
" - Deletes the current conversation" << std::endl;
233 std::cout <<
"5. readPrompt" << std::endl;
234 std::cout <<
" - Prints the last prompt provided to the model" << std::endl;
235 std::cout <<
"6. help" << std::endl;
236 std::cout <<
" - Prints the list of possible commands and their description." << std::endl;
const yarp::os::LogComponent & LLAMA2DEVICE()
std::string sendPostRequest(const std::string &jsonData)
void startLlama2Server(const std::string &model_path, int m_context, int m_gpulayers)
std::string extractContent(const std::string &jsonResponse)
std::string m_model_context
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
std::string m_prompt_file
std::string m_prompt_context
yarp::dev::ReturnValue refreshConversation() noexcept override
Refresh the conversation.
yarp::dev::ReturnValue ask(const std::string &question, yarp::dev::LLM_Message &oAnswer) override
Performs a question.
bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue init_LLM(const std::string &model_path)
yarp::dev::ReturnValue readPrompt(std::string &oPrompt) override
Retrieves the provided prompt.
yarp::dev::ReturnValue setPrompt(const std::string &prompt) override
Performs a question.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::dev::ReturnValue getConversation(std::vector< yarp::dev::LLM_Message > &oConversation) override
Retrieves the whole conversation.
yarp::dev::ReturnValue deleteConversation() noexcept override
Delete the conversation and clear the system context from any internally stored context.
std::string type
type of message (can be prompt, assistant, function, ...)
std::string content
content of the message
@ return_value_error_generic
Method was successfully executed.
Helper class for finding config files and other external resources.
bool setDefaultContext(const std::string &contextName)
Sets the context for the current ResourceFinder object.
std::string findFile(const std::string &name)
Find the full path to a file.
A base class for nested structures that can be searched.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.