YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
FakeLLMDevice.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "FakeLLMDevice.h"
7#include <yarp/os/Log.h>
8#include <yarp/os/LogStream.h>
9
10bool FakeLLMDevice::setPrompt(const std::string &prompt)
11{
12 if(!m_conversation.empty())
13 {
14 yError() << "The conversation is already ongoing. You must first delete the whole conversation and start from scratch.";
15 return false;
16 }
17
18 m_conversation.push_back(yarp::dev::LLM_Message("system", prompt,{},{}));
19 return true;
20}
21
22bool FakeLLMDevice::readPrompt(std::string &oPrompt)
23{
24 for (const auto &message : m_conversation)
25 {
26 if (message.type == "system")
27 {
28 oPrompt = message.content;
29 return true;
30 }
31 }
32
33 return false;
34}
35
36bool FakeLLMDevice::ask(const std::string &question, yarp::dev::LLM_Message &oAnswer)
37{
38 // In the fake device we ignore the question
40 if(question == "function")
41 {
42 std::string function_name = "FakeFunction";
43 std::vector<std::string> function_params = {"arg1","arg2"};
44 std::vector<std::string> function_args = {"yes","no"};
45 answer = yarp::dev::LLM_Message("function",function_name,function_params,function_args);
46 }
47 else
48 {
49 std::string answer_content = "Fatti non foste per viver come bruti ma per seguir virtute e canoscenza";
50 answer = yarp::dev::LLM_Message("assistant", answer_content,{},{});
51 }
52 m_conversation.push_back(yarp::dev::LLM_Message("user", question,{},{}));
53 m_conversation.push_back(answer);
54 oAnswer = answer;
55 return true;
56}
57
58bool FakeLLMDevice::getConversation(std::vector<yarp::dev::LLM_Message>& oConversation)
59{
60 oConversation = m_conversation;
61 return true;
62}
63
65{
66 m_conversation.clear();
67 return true;
68}
69
71{
72 std::string current_prompt;
73
74 if(!this->readPrompt(current_prompt))
75 {
76 yError() << "No prompt found in the conversation. Cannot refresh.";
77 return false;
78 }
79
80 this->deleteConversation();
81
82 if(!this->setPrompt(current_prompt))
83 {
84 yError() << "Failed to refresh the conversation.";
85 return false;
86 }
87
88 return true;
89}
90
92{
93 if (!this->parseParams(config)) {return false;}
94 return true;
95}
96
98{
99 return true;
100}
#define yError(...)
Definition Log.h:361
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool refreshConversation() noexcept override
Refresh the conversation.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool close() override
Close the DeviceDriver.
bool setPrompt(const std::string &prompt) override
Performs a question.
bool ask(const std::string &question, yarp::dev::LLM_Message &oAnswer) override
Performs a question.
bool deleteConversation() noexcept override
Delete the conversation and clear the system context from any internally stored context.
bool readPrompt(std::string &oPromp) override
Retrieves the provided prompt.
bool getConversation(std::vector< yarp::dev::LLM_Message > &oConversation) override
Retrieves the whole conversation.
A base class for nested structures that can be searched.
Definition Searchable.h:31