char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
ea1f7bbb5
char/aliceai-t5-moe
1#pragma once 2 3#include <atomic> 4#include <functional> 5#include <map> 6#include <memory> 7#include <string> 8#include <thread> 9#include <vector> 10#include <cstdint> 11#include <unordered_map> 12 13struct common_params ; 14 15// generator-like API for HTTP response generation 16// this object response with one of the 2 modes: 17// 1) normal response: `data` contains the full response body 18// 2) streaming response: each call to next(output) generates the next chunk 19// when next(output) returns false, no more data after the current chunk 20// note: some chunks can be empty, in which case no data is sent for that chunk 21struct server_http_res { 22std ::string content_type = "application/json; charset=utf-8" ; 23int status = 200 ; 24std ::string data ; 25std ::map < std ::string ,std ::string > headers ; 26 27std ::function < bool (std ::string & )> next = nullptr ; 28bool is_stream ()const { 29return next != nullptr ; 30 } 31 32// fired before req and res are destroyed 33virtual void on_complete () {} 34 35virtual ~server_http_res ()= default ; 36}; 37 38// unique pointer, used by set_chunked_content_provider 39// httplib requires the stream provider to be stored in heap 40using server_http_res_ptr = std ::unique_ptr < server_http_res > ; 41using raw_buffer = std ::vector < uint8_t > ; 42 43struct uploaded_file { 44raw_buffer data ; 45std ::string filename ; 46std ::string content_type ; 47}; 48 49struct server_http_req { 50std ::map < std ::string ,std ::string > params ;// path_params + query_params 51std ::map < std ::string ,std ::string > headers ;// used by MCP proxy 52std ::string path ; 53std ::string query_string ;// query parameters string (e.g. "action=save") 54std ::string body ; 55std ::map < std ::string ,uploaded_file > files ;// used for file uploads (form data) 56const std ::function < bool ()> & should_stop ; 57 58std ::string get_param (const std ::string & key ,const std ::string & def = "" )const { 59 autoit = params .find (key ); 60if (it != params .end ()) { 61return it -> second ; 62 } 63return def ; 64 } 65}; 66 67struct server_http_context { 68class Impl ; 69std ::unique_ptr < Impl > pimpl ; 70 71std ::thread thread ;// server thread 72std ::atomic < bool > is_ready = false; 73 74// note: the handler should never throw exceptions 75using handler_t = std ::function < server_http_res_ptr (const server_http_req & req )> ; 76mutable std ::unordered_map < std ::string ,handler_t > handlers ; 77 78std ::string path_prefix ; 79std ::string hostname ; 80int port = 8080 ; 81bool is_ssl = false; 82 83server_http_context (); 84~ server_http_context (); 85 86bool init ( const common_params & params ); 87bool start (); 88void stop () const ; 89 90void get ( const std:: string & path , const handler_t & handler ) const ; 91void post ( const std:: string & path , const handler_t & handler ) const ; 92void del ( const std:: string & path , const handler_t & handler ) const ; 93 94// Register the Google Cloud Platform (Vertex AI) compat (AIP_PREDICT_ROUTE env var, or /predict) 95// Must be called AFTER all other API routes are registered 96void register_gcp_compat () const ; 97 98// for debugging 99std:: string listening_address ; 100};