- C++ 99.7%
- Makefile 0.3%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| .vscode | ||
| .gitignore | ||
| channel.hpp | ||
| client.cpp | ||
| common.h | ||
| Makefile | ||
| manager.hpp | ||
| models.hpp | ||
| quic_transport.hpp | ||
| README.md | ||
| rpc.hpp | ||
| server.cpp | ||
| test.cpp | ||
| wire.hpp | ||
Model RPC over QUIC (ngtcp2)
Files
| File | Purpose | Edit it? |
|---|---|---|
models.hpp |
Model types, capabilities, methods, Server and Client classes |
Yes - this is the one |
manager.hpp |
Manager: several clients, auto-reconnect, fail-over across (client, model) pairs |
Yes - If large changes are made in models.hpp |
rpc.hpp |
Calls, streaming, cancellation, model-list updates | No |
quic_transport.hpp |
All ngtcp2 / GnuTLS / socket code | No |
wire.hpp |
Binary serialization (+ Kwargs) |
Rarely |
channel.hpp |
Your Channel<T> (unchanged) |
- |
example_server.cpp, example_client.cpp |
Runnable demo with fake models | copy from these |
test.cpp |
Self-checking test of everything | - |
Install
Debian 13 (trixie; Debian 12 ships ngtcp2 0.12 whose API is different):
sudo apt install g++ make pkg-config libngtcp2-dev libngtcp2-crypto-gnutls-dev libgnutls28-dev
MSYS2 (mingw64 shell):
pacman -S make mingw-w64-x86_64-gcc mingw-w64-x86_64-pkgconf mingw-w64-x86_64-ngtcp2 mingw-w64-x86_64-gnutls
Needs C++23 (std::expected). Build with make, test with make test.
Using it
Server:
Server server({.port = 4433}); // no cert given -> self-signed
server.add_llm("llm1", std::make_shared<MyLlm>()); // MyLlm derives from LlmModel
server.start();
// any thread, any time: server.add_tts(...); server.remove_llm("llm1");
Client:
Client client({.host = "192.168.1.5", .port = 4433});
client.connect(); // returns once the model list arrived
auto& llm = client.llmModels["llm1"]; // handle with .capabilities
auto r = llm.count_tokens({"some text"}); // Result<Final> (std::expected)
auto call = llm.generate({"prompt", 256, 0.7f}); // streaming output
for (auto& tok : call->output()) ...; // iterate the Channel
auto fin = call->final(); // blocks until done
call->cancel(); // final still arrives
Input streaming: pass a std::shared_ptr<Channel<T>>, send into it, close() it when done.
Tests / several servers on one machine: Server server({.port = 0}); binds any free port;
server.port() returns it after start().
Cancelling outside I/O (server side): a model blocked on an HTTP request, websocket connect, ...
can register a hook that runs as soon as the call is cancelled, instead of polling cancelled():
auto hook = call.on_cancel([&] { http.abort(); }); // removed when `hook` is destroyed
Tools (LLM): a tool is a name, a description and a function; it runs on the client when the
model on the server asks for it. Put tools in the params of any llm method (run, run_stream,
run_dual_stream, and the Manager versions):
llm::Tool get_time("get_time", "Returns the current local time", [] { return now_as_text(); });
llm::Tool add("add", "Adds a and b", [](const std::string& args_json) { ... },
R"({"type":"object","properties":{"a":{"type":"number"},"b":{"type":"number"}}})");
auto call = llm.run_stream({history, 256, 0.7f, {get_time, add}});
for (auto& item : call->output()) ...; // text, tool calls and tool results
auto fin = call->final(); // covers the whole exchange
history.insert(history.end(), fin->added_history.begin(), fin->added_history.end());
The call runs every tool the model asks for and gives it the results (for run/run_stream by
calling the model again with them appended to the history, at most RunParams::max_tool_rounds
times; for run_dual_stream in the input stream). On the server, a model reads call.params.tools
(name, description, parameters), sends {"assistant", "", {llm::ToolCall{id, name, args_json}}}
and returns RunFinal{"tool_calls"} (see FakeLlm in server.cpp).
ASR streaming: asr::run_stream sends asr::Transcript{text, final, replace} events
(final text, partials, replacements); asr::TranscriptBuilder turns them into text.
Behaviour worth knowing
- Reconnection: QUIC itself rides out outages shorter than the idle timeout
(
kIdleTimeout, 10 s; keep-alive pings every 3 s). Longer outages close the connection: running calls fail withErrorCode::Disconnected, the*Modelsmaps empty andclient.on_disconnectedfires. A plainClientdoes not reconnect by itself - callconnect()again, or useManager(manager.hpp), which keeps retrying every client in the background. - Security: TLS 1.3 always. By default the client does NOT verify the server certificate
(
ClientOptions::ca_fileempty). To verify, give the servercert_file/key_file(CN/SAN must match the host you connect to) and give the client that cert asca_file. There is no client authentication yet. - Big data: one message may be up to 64 MB, but send audio/video in chunks (tens of KB to ~1 MB) so playback starts early and cancel reacts quickly.
- Backpressure: server
call.send()blocks when the client is slower than the model. The server's input channel is unbounded, so a very fast client can queue memory if a model reads input slowly. - Threads: one thread per running call on the server; callbacks (
on_added, ...) run on the I/O thread, keep them short and don't calldisconnect()from them. - Capabilities are read once when a model is added; to change them, remove and re-add the model.