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
17using json = nlohmann::json;
18
19using Question = std::string;
21// using Answer = std::tuple<std::string,std::vector<std::string>,std::vector<std::string>>;
22
25{
26public:
27 GPTDevice() : ILLM(), m_convo{std::make_unique<liboai::Conversation>()},
28 m_function_called{},
29 m_functions{std::make_unique<liboai::Functions>()},
30 m_convo_length{1},
31 m_offline{false}
32 {
33 }
34
35 GPTDevice(const GPTDevice &) = delete;
36 GPTDevice(GPTDevice &&) noexcept = delete;
37 GPTDevice &operator=(const GPTDevice &) = delete;
38 GPTDevice &operator=(GPTDevice &&) noexcept = delete;
39
40 ~GPTDevice() override = default;
41
42 // Rpc methods
43 yarp::dev::ReturnValue setPrompt(const std::string &prompt) override;
44
45 yarp::dev::ReturnValue readPrompt(std::string &oPrompt) override;
46
47 yarp::dev::ReturnValue ask(const std::string &question, yarp::dev::LLM_Message &oAnswer) override;
48
49 yarp::dev::ReturnValue getConversation(std::vector<yarp::dev::LLM_Message> &oConversation) override;
50
51 yarp::dev::ReturnValue deleteConversation() noexcept override;
52
53 yarp::dev::ReturnValue refreshConversation() noexcept override;
54
55 // Device initialization
56 bool open(yarp::os::Searchable &config) override;
57
58 bool close() override;
59
60private:
61 // data
62 std::unique_ptr<liboai::Conversation> m_convo;
63 std::size_t m_convo_length; //How many messages are in conversation
64
65 // function_call data. Mostly kept to display the full conversation
66 std::map<std::size_t,Answer> m_function_called;
67
68 // functions
69 std::unique_ptr<liboai::Functions> m_functions;
70
71 // configuration
72 char *azure_resource;
73 char *azure_deployment_id;
74 std::string azure_api_version;
75 liboai::OpenAI oai;
76
77 // offline mode for testing purpouses
78 bool m_offline;
79
80 // model
81 std::string m_model;
82
83 // sets OpenAI function
84 bool setFunctions(const json& function_json);
85
86 const std::string module_name = "GPTDevice";
87
88};
89
90#endif // YARP_GPTDevice_H
nlohmann::json json
Definition GPTDevice.cpp:12
std::string Question
Definition GPTDevice.h:19
nlohmann::json json
Definition GPTDevice.h:17
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:85
bool close() override
Close the DeviceDriver.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition GPTDevice.cpp:14
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