From e504d7a8ef3e32c382fb262281b756d0923711b3 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Tue, 23 Jan 2024 23:05:55 +0100 Subject: [PATCH] refactor and prep rpbot --- plugins/CMakeLists.txt | 9 +++ plugins/plugin_llama-cpp-web.cpp | 2 +- plugins/plugin_rpbot.cpp | 61 +++++++++++++++++++ src/CMakeLists.txt | 20 +++++- .../llama-cpp-web/llama_cpp_web_impl.cpp | 5 +- .../llama-cpp-web/llama_cpp_web_impl.hpp | 6 +- ...face.hpp => text_completion_interface.hpp} | 6 +- src/solanaceae/rpbot/rpbot.cpp | 12 ++++ src/solanaceae/rpbot/rpbot.hpp | 18 ++++++ src/test1.cpp | 2 +- 10 files changed, 129 insertions(+), 12 deletions(-) create mode 100644 plugins/plugin_rpbot.cpp rename src/solanaceae/llama-cpp-web/{llama_cpp_web_interface.hpp => text_completion_interface.hpp} (79%) create mode 100644 src/solanaceae/rpbot/rpbot.cpp create mode 100644 src/solanaceae/rpbot/rpbot.hpp diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 1fcfd32..38a0357 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -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 +) + diff --git a/plugins/plugin_llama-cpp-web.cpp b/plugins/plugin_llama-cpp-web.cpp index 878d449..a5a1c7b 100644 --- a/plugins/plugin_llama-cpp-web.cpp +++ b/plugins/plugin_llama-cpp-web.cpp @@ -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; diff --git a/plugins/plugin_rpbot.cpp b/plugins/plugin_rpbot.cpp new file mode 100644 index 0000000..fd7b2f6 --- /dev/null +++ b/plugins/plugin_rpbot.cpp @@ -0,0 +1,61 @@ +#include + +#include +#include +#include + +#include +#include +#include + +static std::unique_ptr 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(*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 + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2fd73f2..5692458 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 +) + diff --git a/src/solanaceae/llama-cpp-web/llama_cpp_web_impl.cpp b/src/solanaceae/llama-cpp-web/llama_cpp_web_impl.cpp index d68f99a..64da408 100644 --- a/src/solanaceae/llama-cpp-web/llama_cpp_web_impl.cpp +++ b/src/solanaceae/llama-cpp-web/llama_cpp_web_impl.cpp @@ -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 {}; } diff --git a/src/solanaceae/llama-cpp-web/llama_cpp_web_impl.hpp b/src/solanaceae/llama-cpp-web/llama_cpp_web_impl.hpp index ed9391b..9f23ea7 100644 --- a/src/solanaceae/llama-cpp-web/llama_cpp_web_impl.hpp +++ b/src/solanaceae/llama-cpp-web/llama_cpp_web_impl.hpp @@ -1,19 +1,19 @@ #pragma once -#include "./llama_cpp_web_interface.hpp" +#include "./text_completion_interface.hpp" #include #include #include -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& possible) override; std::string completeLine(const std::string_view prompt) override; diff --git a/src/solanaceae/llama-cpp-web/llama_cpp_web_interface.hpp b/src/solanaceae/llama-cpp-web/text_completion_interface.hpp similarity index 79% rename from src/solanaceae/llama-cpp-web/llama_cpp_web_interface.hpp rename to src/solanaceae/llama-cpp-web/text_completion_interface.hpp index 7805604..e396969 100644 --- a/src/solanaceae/llama-cpp-web/llama_cpp_web_interface.hpp +++ b/src/solanaceae/llama-cpp-web/text_completion_interface.hpp @@ -4,10 +4,10 @@ #include #include -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 diff --git a/src/solanaceae/rpbot/rpbot.cpp b/src/solanaceae/rpbot/rpbot.cpp new file mode 100644 index 0000000..2c4add7 --- /dev/null +++ b/src/solanaceae/rpbot/rpbot.cpp @@ -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; +} + diff --git a/src/solanaceae/rpbot/rpbot.hpp b/src/solanaceae/rpbot/rpbot.hpp new file mode 100644 index 0000000..ef469b7 --- /dev/null +++ b/src/solanaceae/rpbot/rpbot.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +struct RPBot { + TextCompletionI& _completion; + ConfigModelI& _conf; + + public: + RPBot( + TextCompletionI& completion, + ConfigModelI& conf + ); + + float tick(float time_delta); +}; + diff --git a/src/test1.cpp b/src/test1.cpp index 139afad..5020132 100644 --- a/src/test1.cpp +++ b/src/test1.cpp @@ -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; }