YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
GPTDevice.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023-2024 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef YARP_GPTDevice_H
7#define YARP_GPTDevice_H
8
9#include <yarp/dev/ILLM.h>
12
13#include <liboai.h>
14#include <nlohmann/json.hpp>
15#include <map>
16
18
19using json = nlohmann::json;
20
21using Question = std::string;
23// using Answer = std::tuple<std::string,std::vector<std::string>,std::vector<std::string>>;
24
39{
40public:
41 GPTDevice() : ILLM(), m_convo{std::make_unique<liboai::Conversation>()},
42 m_function_called{},
43 m_functions{std::make_unique<liboai::Functions>()},
44 m_convo_length{1},
45 m_offline{false}
46 {
47 }
48
49 GPTDevice(const GPTDevice &) = delete;
50 GPTDevice(GPTDevice &&) noexcept = delete;
51 GPTDevice &operator=(const GPTDevice &) = delete;
52 GPTDevice &operator=(GPTDevice &&) noexcept = delete;
53
54 ~GPTDevice() override = default;
55
56 // Rpc methods
57 yarp::dev::ReturnValue setPrompt(const std::string &prompt) override;
58
59 yarp::dev::ReturnValue readPrompt(std::string &oPrompt) override;
60
61 yarp::dev::ReturnValue ask(const std::string &question, yarp::dev::LLM_Message &oAnswer) override;
62
63 yarp::dev::ReturnValue getConversation(std::vector<yarp::dev::LLM_Message> &oConversation) override;
64
65 yarp::dev::ReturnValue deleteConversation() noexcept override;
66
67 yarp::dev::ReturnValue refreshConversation() noexcept override;
68
69 // Device initialization
70 bool open(yarp::os::Searchable &config) override;
71
72 bool close() override;
73
74private:
75 // data
76 std::unique_ptr<liboai::Conversation> m_convo;
77 std::size_t m_convo_length; //How many messages are in conversation
78
79 // function_call data. Mostly kept to display the full conversation
80 std::map<std::size_t,Answer> m_function_called;
81
82 // functions
83 std::unique_ptr<liboai::Functions> m_functions;
84
85 // configuration
86 char *azure_resource;
87 char *azure_deployment_id;
88 liboai::OpenAI oai;
89
90 // offline mode for testing purpouses
91 bool m_offline;
92
93 // model
94 std::string m_model;
95
96 // sets OpenAI function
97 bool setFunctions(const json& function_json);
98
99 const std::string module_name = "GPTDevice";
100
101};
102
103#endif // YARP_GPTDevice_H
nlohmann::json json
Definition GPTDevice.cpp:14
std::string Question
Definition GPTDevice.h:21
nlohmann::json json
Definition GPTDevice.h:19
This class is the parameters parser for class GPTDevice.
GPTDevice: A yarp device for interacting with the GPT model
Definition GPTDevice.h:39
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 getConversation(std::vector< yarp::dev::LLM_Message > &oConversation) override
Retrieves the whole conversation.
yarp::dev::ReturnValue refreshConversation() noexcept override
Refresh the conversation.
yarp::dev::ReturnValue readPrompt(std::string &oPrompt) override
Retrieves the provided prompt.
GPTDevice(GPTDevice &&) noexcept=delete
yarp::dev::ReturnValue ask(const std::string &question, yarp::dev::LLM_Message &oAnswer) override
Performs a question.
Definition GPTDevice.cpp:90
bool close() override
Close the DeviceDriver.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition GPTDevice.cpp:16
GPTDevice(const GPTDevice &)=delete
Interface implemented by all device drivers.
ILLM interface.
Definition ILLM.h:26
STL namespace.
The main, catch-all namespace for YARP.
Definition dirs.h:16