pure.bend checks
raw source on the hub · import bend-kit-llm@0.19.0.1/pure.bend as Pure
5 imports
import Base import ./sse.bend as Sse import bend-kit-json@0.7.3.1/json.bend as Json import bend-kit-bytes@0.3.3.2/bytes.bend as Bytes import bend-kit-encoding@0.5.0.2/encoding.bend as Enc
Types
type Api source · line 8 · raw
Data
Anthropic: POST {base}/v1/messages. OpenAI: POST {base}/chat/completions, which many other providers serve too.
AnthropicApi
OpenAIApi
type Role source · line 12 · raw
Data
UserRole
AssistantRole
type Msg source · line 16 · raw
Data
Msg@role:Role -> @text:String -> Msg
type Req source · line 20 · raw
Data
system: "" for none. max: the most tokens to write.
Req@model:String -> @system:String -> @msgs:List<&2, Msg> -> @max:U32 -> Req
type Reply source · line 24 · raw
Data
stop: the API's reason, as "end_turn" or "stop". input and output count tokens.
Reply@id:String -> @model:String -> @text:String -> @stop:String -> @input:U32 -> @output:U32 -> Reply
type Err source · line 27 · raw
Data
ErrApi@status:U32 -> @body:String -> Err
ErrReply@body:String -> Err
type Leaf source · line 91 · raw
Data
A JSON leaf: its path, as "content.0.text", and its text.
Leaf@p:String -> @v:String -> Leaf
type Ev source · line 205 · raw
Data
A text piece, the end, an error, or an event with no text.
EText@s:String -> Ev
EEndEv
EFail@e:Err -> Ev
ESkipEv
Definitions
def str source · line 33 · raw
@s:String -> 0x24fdc97c15da7903c115e8c30af2a72d/json.Val
def kv source · line 36 · raw
@k:String -> @v:0x24fdc97c15da7903c115e8c30af2a72d/json.Val -> Pair(0x166a26e38c43ec5f8fe3323f5850e3d8/bytes.Bytes, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val)
def num source · line 39 · raw
@+n:U32 -> 0x24fdc97c15da7903c115e8c30af2a72d/json.Val
def obj source · line 42 · raw
@kvs:List<&1, Pair(0x166a26e38c43ec5f8fe3323f5850e3d8/bytes.Bytes, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val)> -> 0x24fdc97c15da7903c115e8c30af2a72d/json.Val
def when source · line 45 · raw
@on:Bool -> @x:Pair(0x166a26e38c43ec5f8fe3323f5850e3d8/bytes.Bytes, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val) -> @xs:List<&1, Pair(0x166a26e38c43ec5f8fe3323f5850e3d8/bytes.Bytes, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val)> -> List<&1, Pair(0x166a26e38c43ec5f8fe3323f5850e3d8/bytes.Bytes, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val)>
def role source · line 52 · raw
@r:Role -> String
def msg source · line 59 · raw
@+r:String -> @t:String -> 0x24fdc97c15da7903c115e8c30af2a72d/json.Val
def msgs source · line 62 · raw
@xs:List<&2, Msg> -> List<&1, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val>
def sys source · line 69 · raw
@on:Bool -> @s:String -> @xs:List<&1, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val> -> List<&1, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val>
def body source · line 77 · raw
@api:Api -> @req:Req -> @stream:Bool -> 0x24fdc97c15da7903c115e8c30af2a72d/json.Val
The request body for api. stream asks for SSE.
def path source · line 94 · raw
@+p:String -> @+k:String -> String
def push.arr source · line 97 · raw
@xs:List<&1, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val> -> @+p:String -> @+i:U32 -> @t:List<&1, Pair(String, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val)> -> List<&1, Pair(String, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val)>
def push.obj source · line 104 · raw
@kvs:List<&1, Pair(0x166a26e38c43ec5f8fe3323f5850e3d8/bytes.Bytes, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val)> -> @+p:String -> @t:List<&1, Pair(String, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val)> -> List<&1, Pair(String, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val)>
def flat source · line 112 · raw
@f:Nat -> @stack:List<&1, Pair(String, 0x24fdc97c15da7903c115e8c30af2a72d/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 135 · raw
@+n:U32 -> @m:Maybe<&1, 0x24fdc97c15da7903c115e8c30af2a72d/json.Val> -> Maybe<&2, List<&2, Leaf>>
def doc source · line 144 · raw
@b:0x166a26e38c43ec5f8fe3323f5850e3d8/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 149 · raw
@d:List<&2, Leaf> -> @+k:String -> String
The first value at path k, or "".
def count source · line 156 · raw
@s:String -> U32
def dots source · line 159 · raw
@s:String -> @+n:U32 -> U32
def block source · line 167 · raw
@+p:String -> Bool
Is p the text of a content block, "content.<i>.text"?
def blocks source · line 171 · raw
@d:List<&2, Leaf> -> String
The text blocks of an Anthropic message, joined.
def reply.of source · line 178 · raw
@api:Api -> @+d:List<&2, Leaf> -> Reply
def reply.got source · line 187 · raw
@api:Api -> @b:0x166a26e38c43ec5f8fe3323f5850e3d8/bytes.Bytes -> @m:Maybe<&2, List<&2, Leaf>> -> Result<&1, &1, Err, Reply>
def reply.copy source · line 194 · raw
@api:Api -> @r:Pair(0x166a26e38c43ec5f8fe3323f5850e3d8/bytes.Bytes, 0x166a26e38c43ec5f8fe3323f5850e3d8/bytes.Bytes) -> Result<&1, &1, Err, Reply>
def reply source · line 199 · raw
@api:Api -> @b:0x166a26e38c43ec5f8fe3323f5850e3d8/bytes.Bytes -> Result<&1, &1, Err, Reply>
A 2xx response body as a Reply.
def ev.text source · line 211 · raw
@+s:String -> Ev
def ev.doc source · line 214 · raw
@m:Maybe<&2, List<&2, Leaf>> -> List<&2, Leaf>
def ev.anthropic source · line 217 · raw
@+name:String -> @data:0x166a26e38c43ec5f8fe3323f5850e3d8/bytes.Bytes -> Ev
def ev.openai source · line 223 · raw
@+s:String -> Ev
def ev source · line 230 · raw
@api:Api -> @e:0x258546a79fa615a80a19ab9a35d86e5a/sse.Message -> Ev
What one SSE event of api means.
def ok source · line 241 · raw
@+s:U32 -> Bool
def again.status source · line 245 · raw
@+s:U32 -> Bool
Statuses a later try may clear: 408, 409, 429, and 5xx, which holds Anthropic's 529.