14. Ask a model
Any app can ask a language model a question without holding a key.
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.
This is the code serving the zoo right now: the bytes this very program was built from (each file is embedded when the zoo is compiled), at commit
55c71513c5 (live). The zoo's own self-check fetches this page and compares it byte for byte with the file it runs.
e14_ask_a_model.odin sha256 8a164ecc6864 · raw
1package main 2 3// Exhibit 14. The zoo has no model key. It asks the ai app, over HTTPS, for the signed-in person: 4// POST rc.app_url("ai") + "/?api=complete" with rc.app_headers("ai", true) 5// body: {"model": "fast", "max_tokens": 400, "messages": [{"role": "user", "content": "..."}]} 6// The answer is the Messages shape plus rc: {via, app, user}, which is what the card prints. 7 8import "core:fmt" 9import "core:strings" 10import "core:unicode/utf8" 11import "ratcloud:rc" 12 13Answer :: struct { 14 answer: string `json:"answer"`, 15 via: string `json:"via"`, 16 for_: string `json:"for"`, 17 model: string `json:"model"`, 18 tokens: i64 `json:"tokens"`, 19} 20 21ask_ai :: proc(question: string, max_tokens := 400) -> (Answer, int, string) { 22 body := fmt.tprintf(`{{"model":"fast","max_tokens":%d,"messages":[{{"role":"user","content":%s}}]}}`, max_tokens, jq(question)) 23 a := call_app("ai", "/?api=complete", true, "POST", body, "application/json", 28_000) 24 r := parse(a.body) 25 if a.status != 200 || r == nil { 26 msg := jstr(r, "error", "message") 27 if msg == "" do msg = jstr(r, "error") 28 if msg == "" do msg = fmt.tprintf("ai answered %d%s", a.status, cat(": ", a.err) if a.err != "" else "") 29 return {}, 429 if a.status == 429 else 502, msg 30 } 31 text := strings.builder_make() 32 for b in jarr(r, "content") do if jstr(b, "type") == "text" do strings.write_string(&text, jstr(b, "text")) 33 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, "" 34} 35 36// Nothing key-shaped may sit in what the zoo serves: every file it is built from (exhibits' "how" 37// files and the page's assets), searched for an API key's or a private key's shape. 38KEY_SHAPES :: []string{"sk-", "-----BEGIN"} 39key_shaped :: proc() -> string { 40 files := make([dynamic]Src) 41 for &e in exhibits() do for f in how_files(&e) do append(&files, f) 42 append(&files, Src{"public/zoo.js", ZOO_JS}, Src{"public/zoo.css", ZOO_CSS}, Src{"web/page.html", PAGE_HTML}) 43 for f in files { 44 t := f.text 45 for i := 0; i + 20 < len(t); i += 1 { 46 // sk-<20+ key characters> 47 if t[i] == 's' && t[i + 1] == 'k' && t[i + 2] == '-' && (i == 0 || !is_word(t[i - 1])) { 48 n := 0 49 for j := i + 3; j < len(t) && (is_word(t[j]) || t[j] == '-'); j += 1 do n += 1 50 if n >= 20 do return f.path 51 } 52 // <32 hex>.<12+ alnum> (a z.ai key) 53 if is_hex(t[i]) && (i == 0 || !is_word(t[i - 1])) && i + 45 <= len(t) { 54 k := 0 55 for k < 32 && is_hex(t[i + k]) do k += 1 56 if k == 32 && t[i + 32] == '.' { 57 m := 0 58 for j := i + 33; j < len(t) && is_alnum(t[j]); j += 1 do m += 1 59 if m >= 12 do return f.path 60 } 61 } 62 if t[i] == '-' && strings.has_prefix(t[i:], "-----BEGIN ") { 63 end := strings.index(t[i + 11:], "-----") 64 if end > 0 && end < 40 && strings.has_suffix(t[i + 11:i + 11 + end], "PRIVATE KEY") do return f.path 65 } 66 } 67 } 68 return "" 69} 70is_hex :: proc(c: u8) -> bool {return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')} 71is_alnum :: proc(c: u8) -> bool {return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')} 72is_word :: proc(c: u8) -> bool {return is_alnum(c) || c == '_'} 73 74e14 :: proc() -> Exhibit { 75 return { 76 n = 14, try_ = "ask the model a question", wing = "Intelligence", 77 title = "Ask a model", 78 promise = "Any app can ask a language model a question without holding a key.", 79 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.`, 80 src = {"e14_ask_a_model.odin", #load("e14_ask_a_model.odin", string)}, 81 script = {"web/e14.js", #load("web/e14.js", string)}, 82 show = proc(me: ^rc.User) -> string { 83 if me == nil do return signin_line("to ask the model something.") 84 return `<p class="muted small">One question at a time, up to 500 characters; each answer goes on your ledger (exhibit 15).</p><form class="row" id="ask-form"><input name="q" maxlength="500" placeholder="Try: name three Swedish lakes" required><button class="primary">Ask</button></form><div id="ask-out" class="note"></div>` 85 }, 86 api = proc(a: ^Api) -> bool { 87 if a.do_ != "ask" || !a.post do return false 88 if a.me == nil {reply_err(a, 401, "sign in first"); return true} 89 q := in_str(a, "question") 90 if utf8.rune_count_in_string(q) > 500 { 91 n := 0 92 for _, i in q {if n == 500 {q = q[:i]; break}; n += 1} 93 } 94 q = strings.trim_space(q) 95 if q == "" {reply_err(a, 400, "ask something"); return true} 96 mt := in_int(a, "max_tokens") 97 ans, status, err := ask_ai(q, int(min(400, max(1, mt if mt != 0 else 400)))) 98 if err != "" {reply_err(a, status, err); return true} 99 reply(a, ans) 100 return true 101 }, 102 // The robot asks a fixed cheap question; and nothing key-shaped may sit in what we serve. 103 check = proc() -> Check { 104 b, err := robot_at() 105 if err != "" do return fail(err) 106 b.timeout_ms = 30_000 107 r := post(b, self_url("/api/14/ask"), `{"question":"Reply with the single word: pong","max_tokens":20}`) 108 b.timeout_ms = 15_000 109 j: Answer 110 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))) 111 if j.answer == "" do return fail("ai answered, but with nothing") 112 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_)) 113 if f := key_shaped(); f != "" do return fail(cat("something key-shaped is in ", f)) 114 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 { 115 tokens: i64 `json:"tokens"`, 116 }{j.tokens}) 117 }, 118 } 119}
web/e14.js sha256 a34fbbeadc97 · raw
1document.getElementById("ask-form")?.addEventListener("submit", async (ev) => { 2 ev.preventDefault(); 3 const out = document.getElementById("ask-out"), btn = ev.target.querySelector("button"); 4 btn.disabled = true; out.textContent = "thinking…"; 5 const j = await zoo.call("/api/14/ask", { question: ev.target.q.value }); 6 btn.disabled = false; 7 if (j.error) { out.textContent = j.error; return; } 8 out.innerHTML = '<div class="answer">' + zoo.md(j.answer) + '</div><p class="muted">answered via ' + zoo.esc(j.via) + ', for ' + zoo.esc(j.for) + ', on your behalf (' + zoo.esc(j.model) + ', ' + j.tokens + ' tokens)</p>'; 9 window.dispatchEvent(new Event("zoo-asked")); 10});