mirror of
https://github.com/ParisNeo/lollms.git
synced 2025-04-19 16:10:46 +00:00
an working c++ example
This commit is contained in:
parent
e04d2269bd
commit
45a45ca621
1
examples/cpp_client_example/.gitignore
vendored
1
examples/cpp_client_example/.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
# Ignore the cloned Socket.IO Client C++ library
|
||||
socket.io-client-cpp/
|
||||
build
|
@ -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=<INSTALL_DIR>
|
||||
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR> -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)
|
||||
|
@ -4,24 +4,32 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#include <ASCIIColors.h>
|
||||
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"<<std::endl;
|
||||
client_.set_logs_quiet();
|
||||
// Connect to the server
|
||||
client_.connect(serverUrl);
|
||||
}
|
||||
|
||||
void generateText(const std::string& prompt)
|
||||
void generateText(const std::string& prompt, int n_predicts=128)
|
||||
{
|
||||
if (connected_) {
|
||||
object_message::ptr om = object_message::create();
|
||||
om->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"<<std::endl;
|
||||
client_.set_open_listener([&]() {
|
||||
onConnected();
|
||||
});
|
||||
|
||||
std::cout<<"Adding close listener"<<std::endl;
|
||||
client_.set_close_listener([&](sio::client::close_reason const& reason) {
|
||||
onDisconnected();
|
||||
});
|
||||
|
||||
std::cout<<"Adding fail listener"<<std::endl;
|
||||
client_.set_fail_listener([&]() {
|
||||
onConnectionFailed();
|
||||
});
|
||||
|
||||
std::cout<<"Adding reconnect listener"<<std::endl;
|
||||
client_.set_reconnect_listener([&](unsigned int reconnectionAttempts, unsigned int delay) {
|
||||
onReconnecting(reconnectionAttempts, delay);
|
||||
});
|
||||
|
||||
/*
|
||||
std::cout<<"Adding socket close listener"<<std::endl;
|
||||
client_.set_socket_close_listener((const sio::client::socket_listener &)[&]() {
|
||||
onSocketClosed();
|
||||
});
|
||||
|
||||
*/
|
||||
|
||||
std::cout<<"Adding lollms server listener"<<std::endl;
|
||||
// Event handler for receiving generated text chunks
|
||||
client_.socket()->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"<<std::endl;
|
||||
// Wait for the connection to be established before sending events
|
||||
while (!client.getClient().opened())
|
||||
@ -156,11 +206,11 @@ int main()
|
||||
std::cout<<"Opened"<<std::endl;
|
||||
|
||||
// Trigger the "generate_text" event when needed
|
||||
std::string prompt = "Enter your prompt here";
|
||||
client.generateText(prompt);
|
||||
std::string prompt = "SYSTEM:Act as assiatant, a great and helpful AI agent that does multiple tasks. Help user do what he needs to do.\nUser:write a python hello word code\nAssistant:";
|
||||
client.generateText(prompt,1024);
|
||||
|
||||
// Trigger the "cancel_generation" event when needed
|
||||
client.cancelGeneration();
|
||||
// client.cancelGeneration();
|
||||
|
||||
// Run the event loop to keep the connection alive
|
||||
while (true)
|
||||
|
@ -373,7 +373,7 @@ class MainMenu(Menu):
|
||||
entries = self.lollms_app.config['personalities']+["Back"]
|
||||
try:
|
||||
choice = int(self.show_menu(entries, self.lollms_app.config['active_personality_id']))-1
|
||||
if choice<len(entries)-1:
|
||||
if choice<len(entries)-1 and choice>=0:
|
||||
if self.lollms_app.select_personality(choice):
|
||||
ASCIIColors.success(f"Selected personality: {self.lollms_app.personality.name}")
|
||||
except Exception as ex:
|
||||
|
Loading…
x
Reference in New Issue
Block a user