refactor and prep rpbot

This commit is contained in:
Green Sky 2024-01-23 23:05:55 +01:00
parent c497b19b20
commit e504d7a8ef
No known key found for this signature in database
10 changed files with 129 additions and 12 deletions

View File

@ -9,3 +9,12 @@ target_link_libraries(plugin_llama-cpp-web PUBLIC
solanaceae_llama-cpp-web
)
add_library(plugin_rpbot SHARED
./plugin_rpbot.cpp
)
target_link_libraries(plugin_rpbot PUBLIC
solanaceae_plugin
solanaceae_rpbot
)

View File

@ -36,7 +36,7 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api)
// register types
PLUG_PROVIDE_INSTANCE(LlamaCppWeb, plugin_name, g_lcw.get());
PLUG_PROVIDE_INSTANCE(LlamaCppWebI, plugin_name, g_lcw.get());
PLUG_PROVIDE_INSTANCE(TextCompletionI, plugin_name, g_lcw.get());
} catch (const ResolveException& e) {
std::cerr << "PLUGIN " << plugin_name << " " << e.what << "\n";
return 2;

61
plugins/plugin_rpbot.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <solanaceae/plugin/solana_plugin_v1.h>
#include <solanaceae/util/config_model.hpp>
#include <solanaceae/llama-cpp-web/text_completion_interface.hpp>
#include <solanaceae/rpbot/rpbot.hpp>
#include <memory>
#include <iostream>
#include <limits>
static std::unique_ptr<RPBot> g_rpbot = nullptr;
constexpr const char* plugin_name = "RPBot";
extern "C" {
SOLANA_PLUGIN_EXPORT const char* solana_plugin_get_name(void) {
return plugin_name;
}
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) {
return SOLANA_PLUGIN_VERSION;
}
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) {
std::cout << "PLUGIN " << plugin_name << " START()\n";
if (solana_api == nullptr) {
return 1;
}
try {
auto* completion = PLUG_RESOLVE_INSTANCE(TextCompletionI);
auto* conf = PLUG_RESOLVE_INSTANCE(ConfigModelI);
// static store, could be anywhere tho
// construct with fetched dependencies
g_rpbot = std::make_unique<RPBot>(*completion, *conf);
// register types
PLUG_PROVIDE_INSTANCE(RPBot, plugin_name, g_rpbot.get());
} catch (const ResolveException& e) {
std::cerr << "PLUGIN " << plugin_name << " " << e.what << "\n";
return 2;
}
return 0;
}
SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void) {
std::cout << "PLUGIN " << plugin_name << " STOP()\n";
g_rpbot.reset();
}
SOLANA_PLUGIN_EXPORT float solana_plugin_tick(float delta) {
return g_rpbot->tick(delta);
}
} // extern C

View File

@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.9...3.24 FATAL_ERROR)
project(solanaceae)
add_library(solanaceae_llama-cpp-web
./solanaceae/llama-cpp-web/llama_cpp_web_interface.hpp
./solanaceae/llama-cpp-web/text_completion_interface.hpp
./solanaceae/llama-cpp-web/llama_cpp_web_impl.hpp
./solanaceae/llama-cpp-web/llama_cpp_web_impl.cpp
)
@ -16,7 +16,6 @@ target_link_libraries(solanaceae_llama-cpp-web PUBLIC
nlohmann_json::nlohmann_json
solanaceae_util
solanaceae_message3
)
########################################
@ -29,3 +28,20 @@ target_link_libraries(test1 PUBLIC
solanaceae_llama-cpp-web
)
########################################
add_library(solanaceae_rpbot
./solanaceae/rpbot/rpbot.hpp
./solanaceae/rpbot/rpbot.cpp
)
target_include_directories(solanaceae_rpbot PUBLIC .)
target_compile_features(solanaceae_rpbot PRIVATE cxx_std_20)
target_compile_features(solanaceae_rpbot INTERFACE cxx_std_17)
target_link_libraries(solanaceae_rpbot PUBLIC
solanaceae_util
solanaceae_message3
solanaceae_llama-cpp-web
)

View File

@ -19,7 +19,7 @@ static std::string convertToSafeGrammarString(std::string_view input) {
LlamaCppWeb::~LlamaCppWeb(void) {
}
bool LlamaCppWeb::isHealthy(void) {
bool LlamaCppWeb::isGood(void) {
auto res = _cli.Get("/health");
if (
res.error() != httplib::Error::Success ||
@ -123,7 +123,8 @@ std::string LlamaCppWeb::completeLine(const std::string_view prompt) {
}
nlohmann::json LlamaCppWeb::complete(const nlohmann::json& request_j) {
if (!isHealthy()) {
// TODO: dont check ourself
if (!isGood()) {
return {};
}

View File

@ -1,19 +1,19 @@
#pragma once
#include "./llama_cpp_web_interface.hpp"
#include "./text_completion_interface.hpp"
#include <httplib.h>
#include <nlohmann/json_fwd.hpp>
#include <random>
struct LlamaCppWeb : public LlamaCppWebI {
struct LlamaCppWeb : public TextCompletionI {
httplib::Client _cli{"http://localhost:8080"};
std::minstd_rand _rng{std::random_device{}()};
~LlamaCppWeb(void);
bool isHealthy(void) override;
bool isGood(void) override;
int64_t completeSelect(const std::string_view prompt, const std::vector<std::string_view>& possible) override;
std::string completeLine(const std::string_view prompt) override;

View File

@ -4,10 +4,10 @@
#include <string_view>
#include <vector>
struct LlamaCppWebI {
virtual ~LlamaCppWebI(void) {}
struct TextCompletionI {
virtual ~TextCompletionI(void) {}
virtual bool isHealthy(void) = 0;
virtual bool isGood(void) = 0;
// TODO: add more complex api

View File

@ -0,0 +1,12 @@
#include "./rpbot.hpp"
RPBot::RPBot(
TextCompletionI& completion,
ConfigModelI& conf
) : _completion(completion), _conf(conf) {
}
float RPBot::tick(float time_delta) {
return 10.f;
}

View File

@ -0,0 +1,18 @@
#pragma once
#include <solanaceae/util/config_model.hpp>
#include <solanaceae/llama-cpp-web/text_completion_interface.hpp>
struct RPBot {
TextCompletionI& _completion;
ConfigModelI& _conf;
public:
RPBot(
TextCompletionI& completion,
ConfigModelI& conf
);
float tick(float time_delta);
};

View File

@ -11,7 +11,7 @@
int main(void) {
LlamaCppWeb lcw;
if (!lcw.isHealthy()) {
if (!lcw.isGood()) {
std::cerr << lcw._cli.host() << " " << lcw._cli.port() << " endpoint not healthy\n";
return 1;
}