Skip to contents

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.

Usage

gptr_adapter(
  api,
  transport = c("http_sse", "http_ndjson", "http_json", "process_jsonl", "inprocess"),
  build = NULL,
  parse = NULL,
  stream = NULL,
  classify = NULL,
  capabilities = list()
)

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) and parse(model, status, headers, body, questions), or run(model, state, questions, opts) for inprocess. Classifier response parsing has a separate signature from the ordinary chat stream parse(model, opts).

capabilities

Named list of adapter capabilities.

Value

A spec of class c("gptr_adapter", "gptr_spec").

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
                 list(events = list())
               }
             })
#> <gptr_adapter echo>
#>   api: echo
#>   transport: inprocess
#>   stream: <fn>
#>   capabilities: <list of 0>
#>   api_version: 1.0