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
10using namespace yarp::dev;
11
12ReturnValue FakeLLMDevice::setPrompt(const std::string &prompt)
13{
14 if(!m_conversation.empty())
15 {
16 yError() << "The conversation is already ongoing. You must first delete the whole conversation and start from scratch.";
17 return ReturnValue::return_code::return_value_error_method_failed;
18 }
19
20 m_conversation.push_back(yarp::dev::LLM_Message("system", prompt,{},{}));
21 return ReturnValue_ok;
22}
23
25{
26 for (const auto &message : m_conversation)
27 {
28 if (message.type == "system")
29 {
30 oPrompt = message.content;
31 return ReturnValue_ok;
32 }
33 }
34
35 return ReturnValue::return_code::return_value_error_method_failed;
36}
37
38ReturnValue FakeLLMDevice::ask(const std::string &question, yarp::dev::LLM_Message &oAnswer)
39{
40 // In the fake device we ignore the question
42 if(question == "function")
43 {
44 std::string function_name = "FakeFunction";
45 std::vector<std::string> function_params = {"arg1","arg2"};
46 std::vector<std::string> function_args = {"yes","no"};
47 answer = yarp::dev::LLM_Message("function",function_name,function_params,function_args);
48 }
49 else
50 {
51 std::string answer_content = "Fatti non foste per viver come bruti ma per seguir virtute e canoscenza";
52 answer = yarp::dev::LLM_Message("assistant", answer_content,{},{});
53 }
54 m_conversation.push_back(yarp::dev::LLM_Message("user", question,{},{}));
55 m_conversation.push_back(answer);
56 oAnswer = answer;
57 return ReturnValue_ok;
58}
59
60ReturnValue FakeLLMDevice::getConversation(std::vector<yarp::dev::LLM_Message>& oConversation)
61{
62 oConversation = m_conversation;
63 return ReturnValue_ok;
64}
65
67{
68 m_conversation.clear();
69 return ReturnValue_ok;
70}
71
73{
74 std::string current_prompt;
75
76 if(!this->readPrompt(current_prompt))
77 {
78 yError() << "No prompt found in the conversation. Cannot refresh.";
79 return ReturnValue::return_code::return_value_error_method_failed;
80 }
81
82 this->deleteConversation();
83
84 if(!this->setPrompt(current_prompt))
85 {
86 yError() << "Failed to refresh the conversation.";
87 return ReturnValue::return_code::return_value_error_method_failed;
88
89 }
90
91 return ReturnValue_ok;
92}
93
95{
96 if (!this->parseParams(config)) {return false;}
97 return true;
98}
99
101{
102 return true;
103}
#define yError(...)
Definition Log.h:361
#define ReturnValue_ok
Definition ReturnValue.h:77
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
yarp::dev::ReturnValue getConversation(std::vector< yarp::dev::LLM_Message > &oConversation) override
Retrieves the whole conversation.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::dev::ReturnValue ask(const std::string &question, yarp::dev::LLM_Message &oAnswer) override
Performs a question.
yarp::dev::ReturnValue refreshConversation() noexcept override
Refresh the conversation.
bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue deleteConversation() noexcept override
Delete the conversation and clear the system context from any internally stored context.
yarp::dev::ReturnValue setPrompt(const std::string &prompt) override
Performs a question.
yarp::dev::ReturnValue readPrompt(std::string &oPromp) override
Retrieves the provided prompt.
A base class for nested structures that can be searched.
Definition Searchable.h:31
For streams capable of holding different kinds of content, check what they actually have.