YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
ILLMServerImpl.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
7#include <yarp/os/LogStream.h>
8
9#include <ILLMServerImpl.h>
10
11using namespace yarp::dev;
12
13namespace {
14YARP_LOG_COMPONENT(LLMSERVER, "yarp.device.LLMServer")
15}
16
17ReturnValue ILLMRPCd::setPrompt(const std::string& prompt)
18{
19 if (m_iLlm == nullptr) {
20 yCError(LLMSERVER, "Invalid interface");
21 return ReturnValue::return_code::return_value_error_not_ready;
22 }
23
24 auto ret = m_iLlm->setPrompt(prompt);
25 if (!ret) {
26 yCError(LLMSERVER, "setPrompt failed");
27 return ret;
28 }
29
30 m_stream_conversation();
31
32 return ret;
33}
34
36{
37
39
40 if (m_iLlm == nullptr) {
41 yCError(LLMSERVER, "Invalid interface");
42 ret.ret = ReturnValue::return_code::return_value_error_not_ready;
43 return ret;
44 }
45
46 ret.ret = m_iLlm->readPrompt(ret.prompt);
47
48 return ret;
49}
50
51yarp::dev::llm::return_ask ILLMRPCd::ask(const std::string& question)
52{
54
55 if (m_iLlm == nullptr) {
56 yCError(LLMSERVER, "Invalid interface");
57 ret.ret = ReturnValue::return_code::return_value_error_not_ready;
58 return ret;
59 }
60
61 ret.ret = m_iLlm->ask(question,ret.answer);
62
63 if (ret.ret) {
64 m_stream_conversation();
65 }
66
67 return ret;
68}
69
70void ILLMRPCd::m_stream_conversation()
71{
72 std::vector<yarp::dev::LLM_Message> conversation;
73 if (!m_iLlm->getConversation(conversation)) {
74 yCError(LLMSERVER, "Unable to retrieve the conversation");
75 return;
76 }
77
78 auto& bot = m_streaming_port.prepare();
79 bot.clear();
80 auto& list = bot.addList();
81 for (const auto& message : conversation) {
82 auto& message_bot = list.addList();
83 message_bot.addString(message.type);
84 message_bot.addString(message.content);
85 auto& params_bot = message_bot.addList();
86 for(const auto& params: message.parameters)
87 {
88 params_bot.addString(params);
89 }
90
91 auto& args_bot = message_bot.addList();
92 for(const auto& args: message.arguments)
93 {
94 args_bot.addString(args);
95 }
96 }
97
98 m_streaming_port.write();
99}
100
101
103{
105
106 if (m_iLlm == nullptr) {
107 yCError(LLMSERVER, "Invalid interface");
108 ret.ret = ReturnValue::return_code::return_value_error_not_ready;
109 return ret;
110 }
111
112 std::vector<yarp::dev::LLM_Message> conversation;
113 ret.ret = m_iLlm->getConversation(conversation);
114 ret.conversation = conversation;
115
116 return ret;
117}
118
120{
121 if (m_iLlm == nullptr) {
122 yCError(LLMSERVER, "Invalid interface");
123 return ReturnValue::return_code::return_value_error_not_ready;
124 }
125
126 auto ret = m_iLlm->deleteConversation();
127
128 if (ret) {
129 m_stream_conversation();
130 }
131
132 return ret;
133}
134
136{
137 if (m_iLlm == nullptr) {
138 yCError(LLMSERVER, "Invalid interface");
139 return ReturnValue::return_code::return_value_error_not_ready;
140 }
141
142 auto ret = m_iLlm->refreshConversation();
143
144 if (ret) {
145 m_stream_conversation();
146 }
147
148 return ret;
149}
bool ret
yarp::dev::ReturnValue setPrompt(const std::string &prompt) override
yarp::dev::llm::return_ask ask(const std::string &question) override
yarp::dev::ReturnValue refreshConversation() override
yarp::dev::llm::return_getConversation getConversation() override
yarp::dev::ReturnValue deleteConversation() override
yarp::dev::llm::return_readPrompt readPrompt() override
virtual yarp::dev::ReturnValue deleteConversation()=0
Delete the conversation and clear the system context from any internally stored context.
virtual yarp::dev::ReturnValue setPrompt(const std::string &prompt)=0
Performs a question.
virtual yarp::dev::ReturnValue getConversation(std::vector< yarp::dev::LLM_Message > &conversation)=0
Retrieves the whole conversation.
virtual yarp::dev::ReturnValue refreshConversation()=0
Refresh the conversation.
virtual yarp::dev::ReturnValue readPrompt(std::string &oPrompt)=0
Retrieves the provided prompt.
virtual yarp::dev::ReturnValue ask(const std::string &question, yarp::dev::LLM_Message &answer)=0
Performs a question.
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.