package main // Exhibit 14. The zoo has no model key. It asks the ai app, over HTTPS, for the signed-in person: // POST rc.app_url("ai") + "/?api=complete" with rc.app_headers("ai", true) // body: {"model": "fast", "max_tokens": 400, "messages": [{"role": "user", "content": "..."}]} // The answer is the Messages shape plus rc: {via, app, user}, which is what the card prints. import "core:fmt" import "core:strings" import "core:unicode/utf8" import "ratcloud:rc" Answer :: struct { answer: string `json:"answer"`, via: string `json:"via"`, for_: string `json:"for"`, model: string `json:"model"`, tokens: i64 `json:"tokens"`, } ask_ai :: proc(question: string, max_tokens := 400) -> (Answer, int, string) { body := fmt.tprintf(`{{"model":"fast","max_tokens":%d,"messages":[{{"role":"user","content":%s}}]}}`, max_tokens, jq(question)) a := call_app("ai", "/?api=complete", true, "POST", body, "application/json", 28_000) r := parse(a.body) if a.status != 200 || r == nil { msg := jstr(r, "error", "message") if msg == "" do msg = jstr(r, "error") if msg == "" do msg = fmt.tprintf("ai answered %d%s", a.status, cat(": ", a.err) if a.err != "" else "") return {}, 429 if a.status == 429 else 502, msg } text := strings.builder_make() for b in jarr(r, "content") do if jstr(b, "type") == "text" do strings.write_string(&text, jstr(b, "text")) return {strings.trim_space(strings.to_string(text)), jstr(r, "rc", "via"), jstr(r, "rc", "app"), jstr(r, "model"), jint(r, "usage", "input_tokens") + jint(r, "usage", "output_tokens")}, 200, "" } // Nothing key-shaped may sit in what the zoo serves: every file it is built from (exhibits' "how" // files and the page's assets), searched for an API key's or a private key's shape. KEY_SHAPES :: []string{"sk-", "-----BEGIN"} key_shaped :: proc() -> string { files := make([dynamic]Src) for &e in exhibits() do for f in how_files(&e) do append(&files, f) append(&files, Src{"public/zoo.js", ZOO_JS}, Src{"public/zoo.css", ZOO_CSS}, Src{"web/page.html", PAGE_HTML}) for f in files { t := f.text for i := 0; i + 20 < len(t); i += 1 { // sk-<20+ key characters> if t[i] == 's' && t[i + 1] == 'k' && t[i + 2] == '-' && (i == 0 || !is_word(t[i - 1])) { n := 0 for j := i + 3; j < len(t) && (is_word(t[j]) || t[j] == '-'); j += 1 do n += 1 if n >= 20 do return f.path } // <32 hex>.<12+ alnum> (a z.ai key) if is_hex(t[i]) && (i == 0 || !is_word(t[i - 1])) && i + 45 <= len(t) { k := 0 for k < 32 && is_hex(t[i + k]) do k += 1 if k == 32 && t[i + 32] == '.' { m := 0 for j := i + 33; j < len(t) && is_alnum(t[j]); j += 1 do m += 1 if m >= 12 do return f.path } } if t[i] == '-' && strings.has_prefix(t[i:], "-----BEGIN ") { end := strings.index(t[i + 11:], "-----") if end > 0 && end < 40 && strings.has_suffix(t[i + 11:i + 11 + end], "PRIVATE KEY") do return f.path } } } return "" } is_hex :: proc(c: u8) -> bool {return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')} is_alnum :: proc(c: u8) -> bool {return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')} is_word :: proc(c: u8) -> bool {return is_alnum(c) || c == '_'} e14 :: proc() -> Exhibit { return { n = 14, try_ = "ask the model a question", wing = "Intelligence", title = "Ask a model", promise = "Any app can ask a language model a question without holding a key.", block = `The ai app holds the key; an app calls it with rc.app_headers("ai", true) and gets an answer marked who asked, for whom.`, src = {"e14_ask_a_model.odin", #load("e14_ask_a_model.odin", string)}, script = {"web/e14.js", #load("web/e14.js", string)}, show = proc(me: ^rc.User) -> string { if me == nil do return signin_line("to ask the model something.") return `
One question at a time, up to 500 characters; each answer goes on your ledger (exhibit 15).
` }, api = proc(a: ^Api) -> bool { if a.do_ != "ask" || !a.post do return false if a.me == nil {reply_err(a, 401, "sign in first"); return true} q := in_str(a, "question") if utf8.rune_count_in_string(q) > 500 { n := 0 for _, i in q {if n == 500 {q = q[:i]; break}; n += 1} } q = strings.trim_space(q) if q == "" {reply_err(a, 400, "ask something"); return true} mt := in_int(a, "max_tokens") ans, status, err := ask_ai(q, int(min(400, max(1, mt if mt != 0 else 400)))) if err != "" {reply_err(a, status, err); return true} reply(a, ans) return true }, // The robot asks a fixed cheap question; and nothing key-shaped may sit in what we serve. check = proc() -> Check { b, err := robot_at() if err != "" do return fail(err) b.timeout_ms = 30_000 r := post(b, self_url("/api/14/ask"), `{"question":"Reply with the single word: pong","max_tokens":20}`) b.timeout_ms = 15_000 j: Answer if r.status != 200 || !json_into(r.body, &j) do return fail(fmt.tprintf("the question failed (%d): %s", r.status, jstr(r.j, "error") if r.j != nil else clip(r.body, 120))) if j.answer == "" do return fail("ai answered, but with nothing") if j.via != "ai" || j.for_ != "zoo" do return fail(fmt.tprintf("the answer is not marked 'via ai, for zoo' (via '%s', for '%s')", j.via, j.for_)) if f := key_shaped(); f != "" do return fail(cat("something key-shaped is in ", f)) return pass(fmt.tprintf(`asked the fast model through ai as the robot: "%s" (%d tokens); no key in the zoo's files`, clip(j.answer, 40), j.tokens), struct { tokens: i64 `json:"tokens"`, }{j.tokens}) }, } }