YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
ConversationModel.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 <ConversationModel.h>
7#include <iostream>
8
9void ConversationModel::setRemoteRpc(const std::string& remoteRpc)
10{
11 m_prop.put("remote", remoteRpc);
12}
13
14bool ConversationModel::setRemoteStreamingPort(const std::string& remote_streaming_port_name)
15{
16
17 bool ret = false;
18
19 m_conversation_port.useCallback(m_callback);
20
21 if (!m_conversation_port.open(m_conversation_port_name)) {
22 yWarning() << "Cannot open local streaming port " + m_conversation_port_name;
23 return ret;
24 }
25 ret = yarp::os::Network::connect(remote_streaming_port_name, m_conversation_port_name);
27
28 return ret;
29}
30
31bool ConversationModel::setInterface()
32{
33 if (!m_poly.open(m_prop)) {
34 yWarning() << "Cannot open LLM_nwc_yarp";
35 return false;
36 }
37
38 if (!m_poly.view(m_iLlm)) {
39 yWarning() << "Cannot open interface from driver";
40 return false;
41 }
42
43 return true;
44}
45
46bool ConversationModel::configure(const std::string& remote_rpc, const std::string& streaming_port_name)
47{
48 bool ret = false;
49
50 if (!remote_rpc.empty()) {
51 setRemoteRpc(remote_rpc);
52 ret = setInterface();
53 }
54
55 if (!streaming_port_name.empty()) {
56 if (!m_conversation_port.isClosed()) {
57 m_conversation_port.close();
58 }
59 ret = setRemoteStreamingPort(streaming_port_name);
60 }
61
62 // We set at least the remote_rpc or streaming_port_name so we try to refresh.
63 // This is consistent with what the user expects when making a succesfull configuration.
64 if (ret) {
66 }
67
68 return ret;
69}
70
71void ConversationModel::configure(const QString& remote_rpc, const QString& streaming_port_name)
72{
73 configure(remote_rpc.toStdString(), streaming_port_name.toStdString());
74}
75
76QVariant ConversationModel::data(const QModelIndex& index, int role) const
77{
78 if (index.row() < 0 || index.row() >= m_conversation.count()) {
79 return QVariant();
80 }
81
82 const Message& message = m_conversation[index.row()];
83 if (role == TypeRole) {
84 return message.type();
85 }
86 else if (role == ContentRole) {
87 return message.content();
88 }
89
90 return QVariant();
91}
92
93int ConversationModel::rowCount(const QModelIndex& parent) const
94{
95 Q_UNUSED(parent);
96 return m_conversation.count();
97}
98
99QHash<int, QByteArray> ConversationModel::roleNames() const
100{
101 QHash<int, QByteArray> roles;
102 roles[TypeRole] = "type";
103 roles[ContentRole] = "content";
104 return roles;
105}
106
107void ConversationModel::eraseConversation()
108{
109 beginRemoveRows(QModelIndex(), 0, rowCount());
110 m_conversation.clear();
111 endRemoveRows();
112}
113
115{
116 if (!m_iLlm) {
117 yError() << m_no_device_message;
118 return;
119 }
120
121 m_iLlm->deleteConversation();
123}
124
126{
127
128 this->eraseConversation();
129
130 if (!m_iLlm) {
131 yError() << m_no_device_message;
132 return;
133 }
134
135 std::vector<yarp::dev::LLM_Message> conversation;
136
137 if (!m_iLlm->getConversation(conversation)) {
138 yError() << "Unable to get conversation";
139 return;
140 }
141
142 for (const auto& message : conversation) {
143 addMessage(Message(QString::fromStdString(message.type), QString::fromStdString(message.content)));
144 }
145}
146
147void ConversationModel::addMessage(const Message& message)
148{
149 beginInsertRows(QModelIndex(), rowCount(), rowCount());
150 m_conversation << message;
151 endInsertRows();
152}
153
154void ConversationModel::sendMessage(const QString& message)
155{
156
158
159 if (!m_iLlm) {
160 yError() << m_no_device_message;
161 addMessage(Message(QString::fromStdString("User"), QString::fromStdString(m_no_device_message)));
162 addMessage(Message(QString::fromStdString("User"), QString::fromStdString("Please use the menu from the gui to add a valid connection to an existing LLM_nws")));
163 return;
164 }
165
166 if (rowCount() == 0) {
167 m_iLlm->setPrompt(message.toStdString());
168 } else {
169 m_iLlm->ask(message.toStdString(), answer);
170 }
171
172 this->refreshConversation();
173}
174
176{
177 beginRemoveRows(QModelIndex(), index, index);
178 m_conversation.removeAt(index);
179 endRemoveRows();
180}
bool ret
#define yError(...)
Definition Log.h:361
#define yWarning(...)
Definition Log.h:340
Q_INVOKABLE void sendMessage(const QString &message)
Sends a message.
void refreshConversation()
Deletes and reuploads the conversation.
QVariant data(const QModelIndex &index, int role) const override
Q_INVOKABLE void eraseMessage(const int &index)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
bool configure(const std::string &remote_rpc, const std::string &streaming_port_name)
Configures the remote rpc and streaming port.
Q_INVOKABLE void deleteConversation()
Deletes the conversation.
QHash< int, QByteArray > roleNames() const override
Inner class for message representation in ConversationModel.
QString type() const
QString content() const
bool view(T *&x)
Get an interface to the device driver.
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 ask(const std::string &question, yarp::dev::LLM_Message &answer)=0
Performs a question.
bool open(const std::string &txt)
Construct and configure a device by its common name.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
bool isClosed() override
Returns whether the port associated with this reader has been closed.
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition Network.cpp:682
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:987