YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
ConversationModel.h
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#ifndef CONVERSATIONMODEL_H
7#define CONVERSATIONMODEL_H
8
9#include <yarp/os/Bottle.h>
11#include <yarp/os/Log.h>
12#include <yarp/os/LogStream.h>
13#include <yarp/os/Property.h>
14
15#include <yarp/dev/ILLM.h>
17#include <yarp/dev/PolyDriver.h>
18
20#include <QAbstractListModel>
21
26{
27public:
28 Message(const QString& type, const QString& content) :
29 m_type {type}, m_content {content}
30 {
31 }
32
33 QString type() const
34 {
35 return m_type;
36 }
37 QString content() const
38 {
39 return m_content;
40 }
41
42private:
43 QString m_type;
44 QString m_content;
45};
46
59class ConversationModel : public QAbstractListModel
60{
61 Q_OBJECT
62
63public:
65 {
66 TypeRole = Qt::UserRole + 1,
68 };
69
70 explicit ConversationModel(QObject* parent = nullptr) :
71 QAbstractListModel(parent)
72 {
73 m_prop.put("device", "LLM_nwc_yarp");
74 m_prop.put("local", m_local_rpc_port);
75 }
76
78 {
79 m_conversation_port.close();
80 m_poly.close();
81 }
82
90 bool configure(const std::string& remote_rpc, const std::string& streaming_port_name);
91
92 // QAbstractListModel overrides
93 QVariant data(const QModelIndex& index, int role) const override;
94 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
95
99 Q_INVOKABLE void deleteConversation();
100 Q_INVOKABLE void eraseMessage(const int& index);
101
106 Q_INVOKABLE void sendMessage(const QString& message);
107
113 Q_INVOKABLE void configure(const QString& remote_rpc, const QString& streaming_port_name);
114
115public slots:
116
121 void refreshConversation();
122
123protected:
124 QHash<int, QByteArray> roleNames() const override;
125
126private:
128 yarp::os::Property m_prop;
129
130 yarp::dev::ILLM* m_iLlm = nullptr;
131 ConversationCallback m_callback;
132 QList<Message> m_conversation;
134 const std::string m_conversation_port_name = "/llmgui/conv:i";
135 const std::string m_local_rpc_port = "/yarpllmgui/rpc";
136 const std::string m_no_device_message = "yarpLLMGui: no LLM_nwc was found. Operation is not allowed.";
137
138 void setRemoteRpc(const std::string& remoteRpc);
139 bool setRemoteStreamingPort(const std::string& remote_streaming_port_name);
140 bool setInterface();
141 void addMessage(const Message& message);
142 void eraseConversation();
143};
144
145#endif // CONVERSATIONMODEL_H
The model of the conversation data in the ui.
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.
ConversationModel(QObject *parent=nullptr)
Q_INVOKABLE void deleteConversation()
Deletes the conversation.
QHash< int, QByteArray > roleNames() const override
Inner class for message representation in ConversationModel.
QString type() const
Message(const QString &type, const QString &content)
QString content() const
ILLM interface.
Definition ILLM.h:26
A container for a device driver.
Definition PolyDriver.h:23
bool close() override
Close the DeviceDriver.
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
A class for storing options and configuration information.
Definition Property.h:33
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:987