char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
c264f65ff
char/aliceai-t5-moe
1#pragma once 2 3#include <thread> 4 5#include "http.h" 6 7// llama_server will be available as a dynamic library symbol 8int llama_server (common_params & params ,int argc ,char ** argv ); 9void llama_server_terminate (); 10 11struct cli_server { 12std ::thread th ; 13int port = -1 ; 14std ::atomic < bool > is_alive = false; 15std ::atomic < bool > is_stopping = false; 16 17 ~cli_server () { 18stop (); 19 } 20 21void stop () { 22if (is_stopping .exchange (true)) { 23return ; 24 } 25if (alive ()) { 26llama_server_terminate (); 27 } 28if (th .joinable ()) { 29th .join (); 30 } 31 } 32 33// spawn llama-server in a thread and interact with it via a random port 34bool start (common_params & params ) { 35port = common_http_get_free_port (); 36if (port <=0 ) { 37fprintf (stderr ,"failed to get a free port\n" ); 38exit (1 ); 39 } 40 41is_alive .store (true,std ::memory_order_release ); 42 43common_params server_params = params ;// copy 44server_params .port = port ; 45 46th = std ::thread ([this ,server_params ]()mutable { 47// argc / argv are only used in router mode, we can skip them for now 48int res = llama_server (server_params ,0 ,nullptr ); 49if (res != 0 ) { 50fprintf (stderr ,"llama_server exited with code %d\n" ,res ); 51 } 52is_alive .store (false,std ::memory_order_release ); 53 }); 54 55return true; 56 } 57 58std ::string address ()const { 59return "http://127.0.0.1:" + std ::to_string (port ); 60 } 61 62bool wait_ready (std ::function < bool ()> should_stop ) { 63if (!alive ()) { 64return false; 65 } 66while (!should_stop ()) { 67 auto [cli ,parts ]= common_http_client (address ()); 68cli .set_connection_timeout (1 ,0 ); 69 autores = cli. Get ( "/health" ); 70if (res) { 71if (res -> status == 200 ) { 72return true; 73} 74// any other status means the server is up but not ready yet 75// (e.g. 503 while the model is still loading) 76} 77if (! alive ()) { 78// in case server die permanently 79return false; 80} 81std :: this_thread :: sleep_for ( std :: chrono :: milliseconds ( 200 )); 82} 83return true; 84} 85 86bool alive () const { 87return is_alive. load (std::memory_order_acquire ); 88 } 89};