From 45a45ca6218ee7618cd253d697ac52eb559bcca8 Mon Sep 17 00:00:00 2001 From: Saifeddine ALOUI Date: Sun, 17 Sep 2023 17:48:00 +0200 Subject: [PATCH] an working c++ example --- examples/cpp_client_example/.gitignore | 1 + examples/cpp_client_example/CMakeLists.txt | 15 ++-- examples/cpp_client_example/main.cpp | 94 +++++++++++++++++----- lollms/terminal.py | 2 +- 4 files changed, 82 insertions(+), 30 deletions(-) diff --git a/examples/cpp_client_example/.gitignore b/examples/cpp_client_example/.gitignore index 7c63aaf..44d7b2f 100644 --- a/examples/cpp_client_example/.gitignore +++ b/examples/cpp_client_example/.gitignore @@ -1,2 +1,3 @@ # Ignore the cloned Socket.IO Client C++ library socket.io-client-cpp/ +build \ No newline at end of file diff --git a/examples/cpp_client_example/CMakeLists.txt b/examples/cpp_client_example/CMakeLists.txt index 7452d3a..856d741 100644 --- a/examples/cpp_client_example/CMakeLists.txt +++ b/examples/cpp_client_example/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.12) -project(MySocketIOApp) +project(LollmsClientApp) # Include ExternalProject module include(ExternalProject) @@ -10,20 +10,21 @@ ExternalProject_Add( PREFIX ${CMAKE_BINARY_DIR}/external GIT_REPOSITORY https://github.com/socketio/socket.io-client-cpp.git GIT_TAG master # You can change this to a specific release/tag if needed - CMAKE_ARGS -DCMAKE_INSTALL_PREFIX= + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX= -DSIO_DISABLE_LOGGING=ON # Add this line to disable logging ) # Add your project's executable or library here -add_executable(MySocketIOApp main.cpp) +add_executable(LollmsClientApp main.cpp src/ASCIIColors.cpp) # Include the socket.io-client-cpp header files -target_include_directories(MySocketIOApp PRIVATE +target_include_directories(LollmsClientApp PRIVATE ${CMAKE_BINARY_DIR}/external/include ${CMAKE_BINARY_DIR}/external/include/socket.io-client-cpp # Add this line ) # This is required for googletest find_package(sioclient) # Link your project with the socket.io-client-cpp library -target_include_directories(MySocketIOApp PRIVATE ${CMAKE_BINARY_DIR}/external/include) -target_link_directories(MySocketIOApp PRIVATE ${CMAKE_BINARY_DIR}/external/lib) -target_link_libraries(MySocketIOApp PRIVATE sioclient) +target_include_directories(LollmsClientApp PRIVATE ${CMAKE_BINARY_DIR}/external/include) +target_link_directories(LollmsClientApp PRIVATE ${CMAKE_BINARY_DIR}/external/lib) +target_include_directories(LollmsClientApp PRIVATE ./inc) +target_link_libraries(LollmsClientApp PRIVATE sioclient) diff --git a/examples/cpp_client_example/main.cpp b/examples/cpp_client_example/main.cpp index f3475a5..33771b4 100644 --- a/examples/cpp_client_example/main.cpp +++ b/examples/cpp_client_example/main.cpp @@ -4,24 +4,32 @@ #include #include #include - +#include using namespace sio; -class SocketIOClient { +class lollmsClient { public: - SocketIOClient(const std::string& serverUrl) : connected_(false) + lollmsClient(const std::string& serverUrl) : connected_(false) { // Set up event listeners setupEventListeners(); + std::cout<<"Built listeners"<get_map()["prompt"]=sio::string_message::create(prompt); + om->get_map()["n_predicts"]=sio::int_message::create(n_predicts); + client_.socket()->emit("generate_text", om); + // client_.socket()->emit("generate_text", sio::object_message(*message)); + } else { std::cerr << "Not connected to the server. Cannot generate text." << std::endl; } @@ -50,40 +58,68 @@ private: void setupEventListeners() { + std::cout<<"Adding open listener"<on("text_chunk", [&](const sio::event& event) { - const std::string chunk = event.get_message()->get_string(); - std::cout << "Received chunk: " << chunk << std::endl; - // Append the chunk to the output or perform any other required actions - // ... + sio::message::ptr message = event.get_message(); + if (message->get_map().find("chunk") != message->get_map().end()) { + sio::message::ptr chunkMessage = message->get_map()["chunk"]; + if (chunkMessage->get_flag() == sio::message::flag_string) { + std::string chunk = chunkMessage->get_string(); + std::cout << chunk; + // Append the chunk to the output or perform any other required actions + // ... + } else { + std::cerr << "Received 'chunk' data is not a string." << std::endl; + } + } else { + std::cerr << "Received event 'text_chunk' without 'chunk' data." << std::endl; + } }); // Event handler for receiving generated text client_.socket()->on("text_generated", [&](const sio::event& event) { - const std::string text = event.get_message()->get_string(); - std::cout << "Text generated: " << text << std::endl; - // Toggle button visibility or perform any other required actions - // ... + sio::message::ptr message = event.get_message(); + if (message->get_map().find("text") != message->get_map().end()) { + sio::message::ptr chunkMessage = message->get_map()["text"]; + if (chunkMessage->get_flag() == sio::message::flag_string) { + std::string chunk = chunkMessage->get_string(); + std::cout << chunk; + // Append the chunk to the output or perform any other required actions + // ... + } else { + std::cerr << "Received 'text' data is not a string." << std::endl; + } + } else { + std::cerr << "Received event 'text_generated' without 'text' data." << std::endl; + } }); // Event handler for error during text generation @@ -97,7 +133,7 @@ private: // Event handler for generation cancellation client_.socket()->on("generation_canceled", [&](const sio::event& event) { // Toggle button visibility or perform any other required actions - // ... + std::cout << "Generation canceled" << std::endl; }); } @@ -138,15 +174,29 @@ private: // Perform actions upon socket closure // ... } - - - }; int main() { - // Create a SocketIOClient instance and connect to the server - SocketIOClient client("http://localhost:9601"); + ASCIIColors::red(R"( ___ ___ ___ ___ ___ ___ )"); + ASCIIColors::red(R"( /\__\ /\ \ /\__\ /\__\ /\__\ /\ \ )"); + ASCIIColors::red(R"( /:/ / /::\ \ /:/ / /:/ / /::| | /::\ \ )"); + ASCIIColors::red(R"( /:/ / /:/\:\ \ /:/ / /:/ / /:|:| | /:/\ \ \ )"); + ASCIIColors::red(R"( /:/ / /:/ \:\ \ /:/ / /:/ / /:/|:|__|__ _\:\~\ \ \ )"); + ASCIIColors::red(R"( /:/__/ /:/__/ \:\__\ /:/__/ /:/__/ /:/ |::::\__\ /\ \:\ \ \__\ )"); + ASCIIColors::red(R"( \:\ \ \:\ \ /:/ / \:\ \ \:\ \ \/__/~~/:/ / \:\ \:\ \/__/ )"); + ASCIIColors::red(R"( \:\ \ \:\ /:/ / \:\ \ \:\ \ /:/ / \:\ \:\__\ )"); + ASCIIColors::red(R"( \:\ \ \:\/:/ / \:\ \ \:\ \ /:/ / \:\/:/ / )"); + ASCIIColors::red(R"( \:\__\ \::/ / \:\__\ \:\__\ /:/ / \::/ / )"); + ASCIIColors::red(R"( \/__/ \/__/ \/__/ \/__/ \/__/ \/__/ )"); + + ASCIIColors::yellow("By ParisNeo"); + + + + ASCIIColors::red("Lollms c++ Client V 1.0"); + // Create a lollmsClient instance and connect to the server + lollmsClient client("http://localhost:9600"); std::cout<<"Created"<=0: if self.lollms_app.select_personality(choice): ASCIIColors.success(f"Selected personality: {self.lollms_app.personality.name}") except Exception as ex: