Merge branch 'main' of https://github.com/ParisNeo/lollms into main

This commit is contained in:
saloui 2023-07-05 17:54:32 +02:00
commit 21852f813e
4 changed files with 214 additions and 0 deletions

View File

@ -0,0 +1,2 @@
# Ignore the cloned Socket.IO Client C++ library
socket.io-client-cpp/

View File

@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.1)
project(SocketIOClientExample)
set(CMAKE_CXX_STANDARD 11)
# Set the path to the Socket.IO Client C++ library
set(SOCKET_IO_CLIENT_CPP_PATH /path/to/socket.io-client-cpp)
# Add the Socket.IO Client C++ library include directory
include_directories(${SOCKET_IO_CLIENT_CPP_PATH}/src)
# Add the Socket.IO Client C++ library source files
set(SOCKET_IO_CLIENT_CPP_SOURCE_FILES
${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet.h
${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet.cpp
${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet_op.h
${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet_op.cpp
${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet_socket.h
${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet_socket.cpp
${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_streambuf.h
${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_streambuf.cpp
${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_socket.h
${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_socket.cpp
${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_socket_impl.h
${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_socket_impl.cpp
${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_client_impl.h
${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_client_impl.cpp
${SOCKET_IO_CLIENT_CPP_PATH}/src/sio_client.h
${SOCKET_IO_CLIENT_CPP_PATH}/src/sio_client.cpp
)
# Add the executable target
add_executable(SocketIOClientExample main.cpp ${SOCKET_IO_CLIENT_CPP_SOURCE_FILES})

View File

@ -0,0 +1,48 @@
import os
import subprocess
import platform
repo_dir = 'socket.io-client-cpp'
# Check if the repository directory exists
if os.path.exists(repo_dir):
# If it exists, perform a git pull to update the repository
os.chdir(repo_dir)
subprocess.run(['git', 'pull'])
else:
# If it doesn't exist, clone the Socket.IO Client C++ library
subprocess.run(['git', 'clone', 'https://github.com/socketio/socket.io-client-cpp.git', repo_dir])
# Build the Socket.IO Client C++ library
os.chdir(repo_dir)
if platform.system() == 'Windows':
os.makedirs('build', exist_ok=True)
os.chdir('build')
# Detect if running on Windows Command Prompt (cmd) or PowerShell
shell = os.environ.get('SHELL')
if shell and 'powershell' in shell.lower():
subprocess.run(['cmake', '..'])
subprocess.run(['cmake', '--build', '.', '--config', 'Release'])
else:
# Use the Visual Studio build tools
subprocess.run(['cmake', '-G', 'Visual Studio 16 2019', '..'])
subprocess.run(['cmake', '--build', '.', '--config', 'Release'])
else:
subprocess.run(['mkdir', 'build'])
os.chdir('build')
subprocess.run(['cmake', '..'])
subprocess.run(['make'])
# Compile the C++ code
os.chdir('../..')
if platform.system() == 'Windows':
subprocess.run(['cl', '/EHsc', '/Isocket.io-client-cpp/src',
'main.cpp', 'socket.io-client-cpp/build/Release/sioclient.lib', '/FeSocketIOClientExample.exe'])
else:
subprocess.run(['g++', '-std=c++11', '-Isocket.io-client-cpp/src',
'main.cpp', 'socket.io-client-cpp/build/libsioclient.a', '-o', 'SocketIOClientExample'])
print('Compilation completed successfully.')

View File

@ -0,0 +1,131 @@
#include <iostream>
#include <string>
#include <socket.io-client-cpp/src/sio_client.h>
class SocketIOClient {
public:
SocketIOClient(const std::string& serverUrl) : client_(sio::socket::create())
{
client_->set_open_listener([&]() {
onConnected();
});
client_->set_close_listener([&](sio::client::close_reason const& reason) {
onDisconnected();
});
client_->set_fail_listener([&]() {
onConnectionFailed();
});
client_->set_reconnect_listener([&](unsigned int reconnectionAttempts, unsigned int delay) {
onReconnecting(reconnectionAttempts, delay);
});
client_->set_socket_close_listener([&]() {
onSocketClosed();
});
client_->connect(serverUrl);
}
void generateText(const std::string& prompt)
{
sio::message::list messages;
messages.push(sio::string_message::create(prompt));
messages.push(sio::int_message::create(-1));
messages.push(sio::int_message::create(1024));
client_->socket()->emit("generate_text", messages);
}
void cancelGeneration()
{
client_->socket()->emit("cancel_generation");
}
void onConnected()
{
std::cout << "Connected to the LoLLMs server" << std::endl;
// Perform actions upon successful connection
// ...
}
void onDisconnected()
{
std::cout << "Disconnected from the server" << std::endl;
// Perform actions upon disconnection
// ...
}
void onConnectionFailed()
{
std::cout << "Connection to the server failed" << std::endl;
// Perform actions upon connection failure
// ...
}
void onReconnecting(unsigned int reconnectionAttempts, unsigned int delay)
{
std::cout << "Reconnecting to the server (attempt " << reconnectionAttempts << ") in " << delay << " milliseconds" << std::endl;
// Perform actions upon reconnection attempt
// ...
}
void onSocketClosed()
{
std::cout << "Socket closed" << std::endl;
// Perform actions upon socket closure
// ...
}
private:
sio::client client_;
};
int main()
{
// Create a SocketIOClient instance and connect to the server
SocketIOClient client("http://localhost:9601");
// Event handler for receiving generated text chunks
client.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
// ...
});
// Event handler for receiving generated text
client.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
// ...
});
// Event handler for error during text generation
client.client_->socket()->on("buzzy", [&](const sio::event& event) {
const std::string error = event.get_message()->get_string();
std::cerr << "Server is busy. Wait for your turn. Error: " << error << std::endl;
// Handle the error or perform any other required actions
// ...
});
// Event handler for generation cancellation
client.client_->socket()->on("generation_canceled", [&](const sio::event& event) {
// Toggle button visibility or perform any other required actions
// ...
});
// Trigger the "generate_text" event when needed
std::string prompt = "Enter your prompt here";
client.generateText(prompt);
// Trigger the "cancel_generation" event when needed
client.cancelGeneration();
// Run the event loop
client.client_->sync_close();
return 0;
}