An adapter turns gptr's request context into one provider's wire format and its stream back
into gptr events. HTTP and process adapters give build() and parse(); inprocess adapters
give stream(), a generator factory; classifier adapters give classify.
Arguments
- api
The wire api this adapter implements (the name providers bind to).
- transport
"http_sse","http_ndjson","http_json","process_jsonl"or"inprocess".- build
function(model, context, opts)returning a request spec.- parse
function(model, opts)returning a stream normalizer.- stream
function(model, context, opts)returning a generator (inprocess).- classify
Named list for classifier adapters:
build(model, state, questions, opts)andparse(model, status, headers, body, questions), orrun(model, state, questions, opts)forinprocess. Classifier response parsing has a separate signature from the ordinary chat streamparse(model, opts).- capabilities
Named list of adapter capabilities.
Callback results
HTTP build() returns a named list with an absolute HTTP(S) url, optional method
(POST with a body, otherwise GET), named headers (each a string, an origin-bound secret
handle such as opts$credential, or an unnamed list of strings and handles joined in order,
for example list("Bearer ", opts$credential)), body (UTF-8 text or raw bytes), stream
("sse", "ndjson", "json" or "raw"; the transport supplies the default) and optional
sign, function(method, url, body, value), called once per attempt with the raw body, where
value(handle) reads a handle bound to the origin of the URL; it returns a named list of extra
headers, for request signing. Never paste a handle into a string.
For process_jsonl, build() returns start, a process specification with command (or
NULL to reuse the session child), optional send, an unnamed list of objects to write as
JSON lines (default empty), and optional close_stdin.
parse() returns a list of functions: push(unit) consumes a decoded unit, finish()
ends input and returns the final assistant message, fail(cnd) handles a transport failure,
and message() returns the partial assistant message. The normalizer emits events through
opts$emit and ends with one done or error event carrying the final or partial message.
stream() returns a zero-argument generator. Each step returns
list(events = list(...), wait = 0); wait is seconds before the next step. Emit a terminal
done or error event before returning NULL. Steps must return promptly (within 50 ms)
so other work on the shared reactor can progress.
Examples
gptr_adapter("echo", transport = "inprocess",
stream = function(model, context, opts) {
state = new.env()
state$done = FALSE
function() {
if (state$done) return(NULL)
state$done = TRUE
message = list(role = "assistant", content = list(), api = model$api,
provider = model$provider, model = model$id,
stop_reason = "stop")
list(events = list(
list(type = "start", api = model$api, provider = model$provider,
model = model$id),
list(type = "done", reason = "stop", message = message)))
}
})
#> <gptr_adapter echo>
#> api: echo
#> transport: inprocess
#> stream: <fn>
#> capabilities: <list of 0>
#> api_version: 1.0