44
55#include < cctype> // std::isspace
66#include < cstdlib> // std::free
7- #include < cstring> // ::strdup (POSIX standard, but not C standard)
7+ #include < cstring> // std::strcmp, ::strdup (POSIX standard, but not C standard)
88
99#include < algorithm> // std::find_if, std::transform
1010#include < iterator> // std::back_inserter
@@ -44,9 +44,9 @@ yarp::dev::ReturnValue LlamaGPT::setPrompt(const std::string & prompt)
4444bool LlamaGPT::setPrompt (const std::string & prompt)
4545#endif
4646{
47- if (!m_prompt .empty ())
47+ if (!conversation .empty ())
4848 {
49- yCError (LLAMA) << " Prompt already set" ;
49+ yCError (LLAMA) << " Conversation has started or the prompt was already set, you must delete the conversation first " ;
5050#if YARP_VERSION_COMPARE(>=, 3, 12, 0)
5151 return yarp::dev::ReturnValue::return_code::return_value_error_method_failed;
5252#else
@@ -58,14 +58,15 @@ bool LlamaGPT::setPrompt(const std::string & prompt)
5858 ltrim (temp);
5959 rtrim (temp);
6060
61- if (temp.empty ())
61+ if (! temp.empty ())
6262 {
63- yCWarning (LLAMA) << " Requested prompt is empty" ;
63+ yCInfo (LLAMA) << " Setting prompt:" << temp;
64+ conversation.push_back ({" system" , ::strdup (temp.c_str ())});
65+ }
66+ else
67+ {
68+ yCWarning (LLAMA) << " Requested prompt is empty, not setting it" ;
6469 }
65-
66- yCInfo (LLAMA) << " Setting prompt:" << temp;
67- m_prompt = temp;
68- conversation.push_back ({" system" , ::strdup (m_prompt.c_str ())});
6970
7071#if YARP_VERSION_COMPARE(>=, 3, 12, 0)
7172 return yarp::dev::ReturnValue::return_code::return_value_ok;
@@ -82,7 +83,16 @@ yarp::dev::ReturnValue LlamaGPT::readPrompt(std::string & oPrompt)
8283bool LlamaGPT::readPrompt (std::string & oPrompt)
8384#endif
8485{
85- oPrompt = m_prompt;
86+ if (!conversation.empty () && conversation.front ().role == " system" )
87+ {
88+ oPrompt = conversation.front ().content ;
89+ }
90+ else
91+ {
92+ yCWarning (LLAMA) << " No prompt set, returning empty string" ;
93+ oPrompt.clear ();
94+ }
95+
8696#if YARP_VERSION_COMPARE(>=, 3, 12, 0)
8797 return yarp::dev::ReturnValue::return_code::return_value_ok;
8898#else
@@ -101,7 +111,12 @@ bool LlamaGPT::ask(const std::string & question, yarp::dev::LLM_Message & answer
101111 yCInfo (LLAMA) << " Asking:" << question;
102112 conversation.push_back ({" user" , ::strdup (question.c_str ())});
103113
104- auto prompt = m_prompt;
114+ std::string prompt;
115+
116+ if (!conversation.empty () && conversation.front ().role == " system" )
117+ {
118+ prompt = conversation.front ().content ;
119+ }
105120
106121 if (!prompt.empty ())
107122 {
@@ -276,7 +291,6 @@ bool LlamaGPT::deleteConversation()
276291 }
277292
278293 conversation.clear ();
279- m_prompt.clear ();
280294#if YARP_VERSION_COMPARE(>=, 3, 12, 0)
281295 return yarp::dev::ReturnValue::return_code::return_value_ok;
282296#else
@@ -292,14 +306,26 @@ yarp::dev::ReturnValue LlamaGPT::refreshConversation()
292306bool LlamaGPT::refreshConversation ()
293307#endif
294308{
295- yCInfo (LLAMA) << " Deleting conversation while keeping the prompt" ;
309+ yCInfo (LLAMA) << " Deleting conversation while keeping the prompt (if any) " ;
296310
297- for ( auto & msg : conversation)
311+ if (! conversation. empty () )
298312 {
299- std::free (const_cast <char *>(msg.content ));
300- }
313+ auto first = conversation.end ();
301314
302- conversation.clear ();
315+ for (auto it = conversation.begin (); it != conversation.end (); ++it)
316+ {
317+ if (std::strcmp (it->role , " system" ) != 0 )
318+ {
319+ std::free (const_cast <char *>(it->content ));
320+ }
321+ else if (first == conversation.end ())
322+ {
323+ first = it;
324+ }
325+ }
326+
327+ conversation.erase (first, conversation.end ());
328+ }
303329#if YARP_VERSION_COMPARE(>=, 3, 12, 0)
304330 return yarp::dev::ReturnValue::return_code::return_value_ok;
305331#else
0 commit comments