~/bend-docscommunity

llm.bend relies on unsafe/foreign

raw source on the hub · import bend-kit-llm@0.15.0.0/llm.bend as Llm

LLM client for the Anthropic Messages and OpenAI Chat Completions APIs, with SSE streaming. Source: https://github.com/paymog/bend-kit/tree/main/llm

8 imports
import Base
import ./sse.bend as Sse
import bend-kit-hairpin@0.15.0.0/hairpin.bend as Hairpin
import bend-kit-retry@0.3.0.0/retry.bend as Retry
import bend-kit-http@0.39.2.0/http.bend as Http
import bend-kit-json@0.7.3.0/json.bend as Json
import bend-kit-bytes@0.3.3.1/bytes.bend as Bytes
import bend-kit-encoding@0.5.0.1/encoding.bend as Enc

Types

type Api source · line 12 · raw

Data

Anthropic: POST {base}/v1/messages. OpenAI: POST {base}/chat/completions, which many other providers serve too.

type Role source · line 16 · raw

Data

type Msg source · line 20 · raw

Data

type Req source · line 24 · raw

Data

system: "" for none. max: the most tokens to write.

type Reply source · line 28 · raw

Data

stop: the API's reason, as "end_turn" or "stop". input and output count tokens.

type Err source · line 33 · raw

Data

ErrNet: the request did not complete, or Hairpin's breaker admitted no attempt. ErrApi: the API refused, with its status and error text; an error event in a stream has status 200. ErrReply: a reply that is not the API's JSON, with its text.

type Client source · line 39 · raw

Type

headers go on every request. ms: the step timeout. retries: extra tries after a retryable failure.

type Leaf source · line 102 · raw

Data

A JSON leaf: its path, as "content.0.text", and its text.

type Ev source · line 216 · raw

Data

A text piece, the end, an error, or an event with no text.

type Stream source · line 419 · raw

Type

type Rn source · line 538 · raw

Type

The reader after the next event, or the stream's result.

Definitions

def str source · line 44 · raw

@s:String -> 0x419ec39802735c4664960ab4b9d962e5/json.Val

def kv source · line 47 · raw

@k:String -> @v:0x419ec39802735c4664960ab4b9d962e5/json.Val -> Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0x419ec39802735c4664960ab4b9d962e5/json.Val)

def num source · line 50 · raw

@+n:U32 -> 0x419ec39802735c4664960ab4b9d962e5/json.Val

def obj source · line 53 · raw

@kvs:List<&1, Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0x419ec39802735c4664960ab4b9d962e5/json.Val)> -> 0x419ec39802735c4664960ab4b9d962e5/json.Val

def when source · line 56 · raw

@on:Bool -> @x:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0x419ec39802735c4664960ab4b9d962e5/json.Val) -> @xs:List<&1, Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0x419ec39802735c4664960ab4b9d962e5/json.Val)> -> List<&1, Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0x419ec39802735c4664960ab4b9d962e5/json.Val)>

def role source · line 63 · raw

@r:Role -> String

def msg source · line 70 · raw

@+r:String -> @t:String -> 0x419ec39802735c4664960ab4b9d962e5/json.Val

def msgs source · line 73 · raw

@xs:List<&2, Msg> -> List<&1, 0x419ec39802735c4664960ab4b9d962e5/json.Val>

def sys source · line 80 · raw

@on:Bool -> @s:String -> @xs:List<&1, 0x419ec39802735c4664960ab4b9d962e5/json.Val> -> List<&1, 0x419ec39802735c4664960ab4b9d962e5/json.Val>

def body source · line 88 · raw

@api:Api -> @req:Req -> @stream:Bool -> 0x419ec39802735c4664960ab4b9d962e5/json.Val

The request body for api. stream asks for SSE.

def path source · line 105 · raw

@+p:String -> @+k:String -> String

def push.arr source · line 108 · raw

@xs:List<&1, 0x419ec39802735c4664960ab4b9d962e5/json.Val> -> @+p:String -> @+i:U32 -> @t:List<&1, Pair(String, 0x419ec39802735c4664960ab4b9d962e5/json.Val)> -> List<&1, Pair(String, 0x419ec39802735c4664960ab4b9d962e5/json.Val)>

def push.obj source · line 115 · raw

@kvs:List<&1, Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0x419ec39802735c4664960ab4b9d962e5/json.Val)> -> @+p:String -> @t:List<&1, Pair(String, 0x419ec39802735c4664960ab4b9d962e5/json.Val)> -> List<&1, Pair(String, 0x419ec39802735c4664960ab4b9d962e5/json.Val)>

def flat source · line 123 · raw

@f:Nat -> @stack:List<&1, Pair(String, 0x419ec39802735c4664960ab4b9d962e5/json.Val)> -> @acc:List<&2, Leaf> -> List<&2, Leaf>

The leaves under the stack's values, last first. f: at least the values left.

def doc.of source · line 146 · raw

@+n:U32 -> @m:Maybe<&1, 0x419ec39802735c4664960ab4b9d962e5/json.Val> -> Maybe<&2, List<&2, Leaf>>

def doc source · line 155 · raw

@b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Maybe<&2, List<&2, Leaf>>

Every non-null leaf of a JSON text in document order, keyed by its path, as ("content.0.text", "Hi"). Strings are decoded; numbers keep their text. None when b is not JSON. Each value takes a byte, so n + 1 steps suffice.

def look source · line 160 · raw

@d:List<&2, Leaf> -> @+k:String -> String

The first value at path k, or "".

def count source · line 167 · raw

@s:String -> U32

def dots source · line 170 · raw

@s:String -> @+n:U32 -> U32

def block source · line 178 · raw

@+p:String -> Bool

Is p the text of a content block, "content.<i>.text"?

def blocks source · line 182 · raw

@d:List<&2, Leaf> -> String

The text blocks of an Anthropic message, joined.

def reply.of source · line 189 · raw

@api:Api -> @+d:List<&2, Leaf> -> Reply

def reply.got source · line 198 · raw

@api:Api -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @m:Maybe<&2, List<&2, Leaf>> -> Result<&1, &1, Err, Reply>

def reply.copy source · line 205 · raw

@api:Api -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> Result<&1, &1, Err, Reply>

def reply source · line 210 · raw

@api:Api -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Result<&1, &1, Err, Reply>

A 2xx response body as a Reply.

def ev.text source · line 222 · raw

@+s:String -> Ev

def ev.doc source · line 225 · raw

@m:Maybe<&2, List<&2, Leaf>> -> List<&2, Leaf>

def ev.anthropic source · line 228 · raw

@+name:String -> @data:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Ev

def ev.openai source · line 234 · raw

@+s:String -> Ev

def ev source · line 241 · raw

@api:Api -> @e:0x5cebe4326f2cbc09ae7d69e7299d3257/sse.Message -> Ev

What one SSE event of api means.

def ok source · line 252 · raw

@+s:U32 -> Bool

def again.status source · line 256 · raw

@+s:U32 -> Bool

Statuses a later try may clear: 408, 409, 429, and 5xx, which holds Anthropic's 529.

def again.err source · line 260 · raw

@+e:0x38a6a43217c5976ca9d95ed542a22e7a/http.Err -> Maybe<&2, String>

A refused connect or a timeout may clear.

def again.of source · line 269 · raw

@+status:U32 -> @+h:Map<&2, List<&2, String>> -> Maybe<&2, String>

def verdict source · line 273 · raw

@m:Maybe<&2, String> -> @settled:0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Verdict -> 0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Verdict

Again when another try may help; otherwise settled, as Hairpin.judge would settle it.

def again source · line 281 · raw

@r:Result<&1, &1, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Res> -> Pair(Result<&1, &1, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Res>, 0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Verdict)

Hairpin's judge for every call.

def body.ok source · line 289 · raw

@good:Bool -> @+status:U32 -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Result<&1, &1, Err, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>

def body.of source · line 297 · raw

@r:Result<&1, &1, 0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Res> -> Result<&1, &1, Err, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>

The 2xx body, or why there is none.

def url source · line 305 · raw

@api:Api -> @+base:String -> String

def new source · line 314 · raw

@+api:Api -> @base:String -> @headers:Map<&2, List<&2, String>> -> Client

def json source · line 317 · raw

@h:Map<&2, List<&2, String>> -> Map<&2, List<&2, String>>

def anthropic source · line 321 · raw

@key:String -> Client

base https://api.anthropic.com, 10 min steps, 2 retries.

def openai source · line 325 · raw

@key:String -> Client

base https://api.openai.com/v1, 10 min steps, 2 retries.

def base source · line 329 · raw

@c:Client -> @url:String -> Client

Another base URL, as for a provider that serves the same API.

def timeout source · line 338 · raw

@c:Client -> @+ms:U32 -> Client

def retry source · line 342 · raw

@c:Client -> @n:U32 -> Client

def close source · line 346 · raw

@c:Client -> IO(Unit)

def call.done source · line 350 · raw

@+api:Api -> @base:String -> @headers:Map<&2, List<&2, String>> -> @+ms:U32 -> @+n:U32 -> @x:Pair(0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client, Pair(0x14e48621ef66156b0eb652524b8594d4/retry.Budget, Result<&1, &1, 0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Res>)) -> Pair(Client, Result<&1, &1, Err, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>)

def call.budget source · line 355 · raw

@+api:Api -> @+base:String -> @+headers:Map<&2, List<&2, String>> -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @y:Pair(0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client, 0x14e48621ef66156b0eb652524b8594d4/retry.Budget) -> IO(Pair(0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client, Pair(0x14e48621ef66156b0eb652524b8594d4/retry.Budget, Result<&1, &1, 0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Res>)))

def call source · line 364 · raw

@c:Client -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(Client, Result<&1, &1, Err, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>))

POST body to the API with n retries on a budget of n + 1; the 2xx body comes back. These POSTs are safe to repeat, so Hairpin retries them as again judges.

def raw.got source · line 371 · raw

@b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @m:Maybe<&1, 0x419ec39802735c4664960ab4b9d962e5/json.Val> -> Result<&1, &1, Err, 0x419ec39802735c4664960ab4b9d962e5/json.Val>

def raw.copy source · line 378 · raw

@r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> Result<&1, &1, Err, 0x419ec39802735c4664960ab4b9d962e5/json.Val>

def raw.of source · line 382 · raw

@r:Result<&1, &1, Err, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes> -> Result<&1, &1, Err, 0x419ec39802735c4664960ab4b9d962e5/json.Val>

def raw.done source · line 389 · raw

@x:Pair(Client, Result<&1, &1, Err, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>) -> Pair(Client, Result<&1, &1, Err, 0x419ec39802735c4664960ab4b9d962e5/json.Val>)

def send.raw source · line 394 · raw

@c:Client -> @v:0x419ec39802735c4664960ab4b9d962e5/json.Val -> IO(Pair(Client, Result<&1, &1, Err, 0x419ec39802735c4664960ab4b9d962e5/json.Val>))

Any request body, as for tools or images, and the reply as JSON.

def send.of source · line 399 · raw

@r:Result<&1, &1, Err, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes> -> @+api:Api -> Result<&1, &1, Err, Reply>

def send.done source · line 406 · raw

@+api:Api -> @x:Pair(Client, Result<&1, &1, Err, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>) -> Pair(Client, Result<&1, &1, Err, Reply>)

def send source · line 411 · raw

@c:Client -> @req:Req -> IO(Pair(Client, Result<&1, &1, Err, Reply>))

One whole reply.

def open.res source · line 422 · raw

@x:Pair(0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Res) -> Pair(Result<&1, &1, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream>, 0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Verdict)

def open.judge source · line 427 · raw

@r:Result<&1, &1, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream> -> Pair(Result<&1, &1, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream>, 0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Verdict)

def drain source · line 435 · raw

@f:Nat -> @c:0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client -> @x:Pair(0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream, Result<&1, &1, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Err, Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>>) -> @acc:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes))

A refused stream's error body, at most f more pieces; only a complete body returns its connection.

def start.error source · line 460 · raw

@+status:U32 -> @x:Pair(0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> Pair(0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client, Result<&1, &1, Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream>)

def start.refused source · line 464 · raw

@+status:U32 -> @c:0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client -> @h:0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream -> IO(Pair(0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client, Result<&1, &1, Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream>))

def start.ok source · line 470 · raw

@good:Bool -> @+status:U32 -> @c:0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client -> @h:0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream -> IO(Pair(0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client, Result<&1, &1, Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream>))

def start.res source · line 477 · raw

@c:0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client -> @x:Pair(0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Res) -> IO(Pair(0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client, Result<&1, &1, Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream>))

def start source · line 482 · raw

@c:0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client -> @r:Result<&1, &1, 0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream> -> IO(Pair(0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client, Result<&1, &1, Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream>))

def stream.of source · line 489 · raw

@r:Result<&1, &1, Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream> -> @+api:Api -> Result<&1, &1, Err, Stream>

def stream.result source · line 496 · raw

@+api:Api -> @base:String -> @headers:Map<&2, List<&2, String>> -> @+ms:U32 -> @+n:U32 -> @x:Pair(0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client, Result<&1, &1, Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream>) -> Pair(Client, Result<&1, &1, Err, Stream>)

def stream.done source · line 501 · raw

@+api:Api -> @base:String -> @headers:Map<&2, List<&2, String>> -> @+ms:U32 -> @+n:U32 -> @x:Pair(0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client, Pair(0x14e48621ef66156b0eb652524b8594d4/retry.Budget, Result<&1, &1, 0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream>)) -> IO(Pair(Client, Result<&1, &1, Err, Stream>))

def open.budget source · line 508 · raw

@+api:Api -> @+base:String -> @+headers:Map<&2, List<&2, String>> -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @y:Pair(0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client, 0x14e48621ef66156b0eb652524b8594d4/retry.Budget) -> IO(Pair(0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Client, Pair(0x14e48621ef66156b0eb652524b8594d4/retry.Budget, Result<&1, &1, 0xc4e34def1933b85a360919d3b5a45ec1/hairpin.Err, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream>)))

def stream.raw source · line 517 · raw

@c:Client -> @v:0x419ec39802735c4664960ab4b9d962e5/json.Val -> IO(Pair(Client, Result<&1, &1, Err, Stream>))

Any request body that asks for SSE, as for tools. The open retries as call does, on a budget of n + 1. A stream opens on the client's pool. Close it with stream.close to give its connection back.

def stream source · line 525 · raw

@c:Client -> @req:Req -> IO(Pair(Client, Result<&1, &1, Err, Stream>))

The reply as it is written: read it with next.

def stream.close source · line 530 · raw

@c:Client -> @s:Stream -> IO(Client)

Close a stream. A stream read to its end gives its connection to the client's pool.

def event.read source · line 542 · raw

@rd:0x5cebe4326f2cbc09ae7d69e7299d3257/sse.Reader -> @x:Pair(0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream, Result<&1, &1, 0x38a6a43217c5976ca9d95ed542a22e7a/http.Err, Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>>) -> Rn

def event.loop source · line 555 · raw

@f:Nat -> @st:Rn -> IO(Pair(0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream, Pair(0x5cebe4326f2cbc09ae7d69e7299d3257/sse.Reader, Result<&1, &1, Err, Maybe<&1, 0x5cebe4326f2cbc09ae7d69e7299d3257/sse.Message>>)))

f: pieces left to read before the stream counts as stalled.

def event.done source · line 578 · raw

@+api:Api -> @x:Pair(0x38a6a43217c5976ca9d95ed542a22e7a/http.Stream, Pair(0x5cebe4326f2cbc09ae7d69e7299d3257/sse.Reader, Result<&1, &1, Err, Maybe<&1, 0x5cebe4326f2cbc09ae7d69e7299d3257/sse.Message>>)) -> Pair(Stream, Result<&1, &1, Err, Maybe<&1, 0x5cebe4326f2cbc09ae7d69e7299d3257/sse.Message>>)

def event source · line 583 · raw

@s:Stream -> IO(Pair(Stream, Result<&1, &1, Err, Maybe<&1, 0x5cebe4326f2cbc09ae7d69e7299d3257/sse.Message>>))

The next SSE event, or None when the stream ends. An event the end cuts off is dropped.

def next.ev source · line 589 · raw

@r:Result<&1, &1, Err, Maybe<&1, 0x5cebe4326f2cbc09ae7d69e7299d3257/sse.Message>> -> @+api:Api -> Ev

def next.of source · line 600 · raw

@x:Pair(Stream, Result<&1, &1, Err, Maybe<&1, 0x5cebe4326f2cbc09ae7d69e7299d3257/sse.Message>>) -> Pair(Stream, Ev)

def next.loop source · line 606 · raw

@f:Nat -> @x:Pair(Stream, Ev) -> IO(Pair(Stream, Result<&1, &1, Err, Maybe<&2, String>>))

f: events left to skip.

def next source · line 626 · raw

@s:Stream -> IO(Pair(Stream, Result<&1, &1, Err, Maybe<&2, String>>))

The next piece of reply text, or None at the end.