YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Llama2Device.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef YARP_LLAMA2DEVICE_H
7#define YARP_LLAMA2DEVICE_H
8//https://github.com/robotology/yarp/tree/master/src/libYARP_dev/src/idl_generated_code/yarp/dev
9#include <yarp/dev/ILLM.h>
13#include <llama.h>
14#include <common.h>
15#include "sampling.h"
16#include <vector>
17#include <string>
18#include <cstdio>
19#include <cstring>
20#include <iostream>
21
22enum class Author
23{
24 User,
25 Model
26};
27
28using Content = std::string;
29using Question = std::string;
31
35 public yarp::dev::ILLM
36{
37public:
39 Llama2Device(const Llama2Device&) = delete;
40 Llama2Device(Llama2Device&&) noexcept = delete;
41 Llama2Device& operator=(const Llama2Device&) = delete;
42 Llama2Device& operator=(Llama2Device&&) noexcept = delete;
43 ~Llama2Device() override = default;
44
45 // Rpc methods
46 yarp::dev::ReturnValue setPrompt(const std::string &prompt) override;
47
48 yarp::dev::ReturnValue readPrompt(std::string &oPrompt) override;
49
50 yarp::dev::ReturnValue ask(const std::string &question, yarp::dev::LLM_Message &oAnswer) override;
51
52 yarp::dev::ReturnValue getConversation(std::vector<yarp::dev::LLM_Message> &oConversation) override;
53
54 yarp::dev::ReturnValue deleteConversation() noexcept override;
55
56 yarp::dev::ReturnValue refreshConversation() noexcept override;
57
58 // ILLM methods
59 yarp::dev::ReturnValue init_LLM(const std::string &model_path);
60
61 // DeviceDriver
62 bool open(yarp::os::Searchable& config) override;
63 bool close() override;
64 void help();
65
66 void convAdd(std::vector<yarp::dev::LLM_Message>& conversation, const std::string& type, const std::string& content);
67
68private:
69 std::vector<llama_token> tokens_list;
70 std::string model_path;
71 llama_model *model;
72 std::vector<std::pair<Author, Content>> conversation_log;
73 llama_model_params model_params;
74 std::vector<common_chat_msg> chat_msgs;
75 std::string conversation;
76 std::string model_question;
77 std::string ask_question;
78 bool prompt_set = false;
79 bool first_prompt_set = false;
80 bool initialized = false;
81 std::vector<yarp::dev::LLM_Message> m_conversation;
82 yarp::dev::LLM_Message m_prompt;
83};
84
85
86
87#endif // YARP_LLAMA2DEVICE_H
std::string Question
Author
std::string Content
This class is the parameters parser for class Llama2Device.
Llama2Device(const Llama2Device &)=delete
yarp::dev::ReturnValue refreshConversation() noexcept override
Refresh the conversation.
yarp::dev::ReturnValue ask(const std::string &question, yarp::dev::LLM_Message &oAnswer) override
Performs a question.
bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue init_LLM(const std::string &model_path)
void convAdd(std::vector< yarp::dev::LLM_Message > &conversation, const std::string &type, const std::string &content)
yarp::dev::ReturnValue readPrompt(std::string &oPrompt) override
Retrieves the provided prompt.
yarp::dev::ReturnValue setPrompt(const std::string &prompt) override
Performs a question.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::dev::ReturnValue getConversation(std::vector< yarp::dev::LLM_Message > &oConversation) override
Retrieves the whole conversation.
Llama2Device(Llama2Device &&) noexcept=delete
yarp::dev::ReturnValue deleteConversation() noexcept override
Delete the conversation and clear the system context from any internally stored context.
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