For programs in languages that have no Grayling client (Python, Go, Ruby, a shell script, a web page of your own). It is a small HTTP/1.1
server on 127.0.0.1 that talks to the engine host as any other client (its name there is http): the same requests, answers and
checks as docs/protocol.md, as JSON. Nothing leaves the computer.
grayling http # 127.0.0.1:4774, a token
grayling http --port 0 # a free port (printed)
grayling http --no-token # no token (see below)
grayling http --origin https://app.example # a web page of that origin may read the answers (CORS)It prints where it listens and the token:
grayling http: listening on http://127.0.0.1:4774
token: 3f0c…
The token is made once and kept in <data>/http-token (mode 0600; ~/.local/share/grayling/http-token on Linux), so it stays the
same from one run to the next; --token T gives one (16 characters or more) and keeps nothing. Send it as Authorization: Bearer <token>.
The server holds a connection to the host while it is asked and lets go after a minute without a request, so that it does not keep
the host from ending when nobody uses it. The first request starts the host if it is not running.
TOKEN=$(cat ~/.local/share/grayling/http-token)
A="Authorization: Bearer $TOKEN"
# The rest of the word at the caret, the next word, and a fix of a word typed wrong.
curl -s -H "$A" -d '{"text": "Dear Anna, I hope you are we"}' http://127.0.0.1:4774/v1/suggest
# {"word":"ll","next":" and","conf":0.93,"conf_next":0.41,"theta":0.1,"show":true,"show_next":true,"plan":"mix","lang":"eng_Latn",…}
# 204 with no body when there is none; the header says why: starting, dropped, memory, failed, off, elsewhere.
curl -si -H "$A" -d '{"text": "Dear"}' http://127.0.0.1:4774/v1/suggest | head -3
# HTTP/1.1 204 No Content
# Grayling-None: starting (loading the models: ask again in a moment)
curl -s -H "$A" -X POST http://127.0.0.1:4774/v1/warm # a text box got the focus: load what is set up
curl -s -H "$A" http://127.0.0.1:4774/v1/state # {"settings": {…}, "status": {…}}
curl -s -H "$A" -X PUT -d '{"llm": "off", "threads": 3}' http://127.0.0.1:4774/v1/settings
curl -s -H "$A" -d '{"counts": {"shown": 12, "taken": 4}}' http://127.0.0.1:4774/v1/usage
curl -s -H "$A" -d '{"word": "teh"}' http://127.0.0.1:4774/v1/keep # never offered as a typo again
curl -s http://127.0.0.1:4774/v1/openapi.json # the description (needs no token)In Python, with nothing installed:
import json, urllib.request
token = open("/home/me/.local/share/grayling/http-token").read().strip()
def suggest(text, **more):
req = urllib.request.Request("http://127.0.0.1:4774/v1/suggest", json.dumps({"text": text, **more}).encode(),
{"Authorization": f"Bearer {token}"})
with urllib.request.urlopen(req) as r:
return json.load(r) if r.status == 200 else None
print(suggest("Thanks for your help, I will definately re"))POST /v1/suggest |
the request (SuggestRequest of docs/protocol.md: text, after, surface, reply, prev, doc, session, fix, precision, words, partial, tab…). A word typed wrong is looked for unless fix is false |
200 the answer, or 204 |
POST /v1/warm |
the state | |
GET /v1/state |
{"settings", "status"} |
|
PUT /v1/settings |
the settings to change ({"llm": "off"}); every field is checked, one bad one changes nothing |
the state |
POST /v1/usage |
{"counts": {…}, "judged": {…}}: counts only, never any text |
{"usage", "thresholds"} |
POST /v1/keep |
{"word": "teh"} |
{"kept": n} |
POST /v1/session |
{"session", "texts": […], "own": […]}: a chat session's earlier texts, for its cache |
{"ok": true} |
GET /v1/openapi.json |
the description of all this (OpenAPI 3) |
Errors are {"error": {"code", "message"}}: 400 bad (a field the host refused: its message names it), 401 token, 403 host or
origin, 404, 405, 409 cannot, 413, 501 (a chunked body: send a Content-Length), 503 the host is not there or is ending.
A request is cancelled when its connection closes: close it (a client's timeout, the person typed on) and the engine stops the
decoding; the next request is not held up. Requests with the same tab drop the one before, as in the protocol.
Anything on the computer that can open a port can ask, so:
- it listens on 127.0.0.1 only;
- the Host header must be
127.0.0.1:<port>orlocalhost:<port>(a web page reaching the port by a name of its own, DNS rebinding, is refused with 403); - every route but the description needs the token; without it (
--no-token) any program on this computer, and any other user of it, may ask: use it for a test, not for a computer that has other people on it; - a request that carries an
Originheader (a web page) is refused unless--originnamed that origin, and then the answers carryAccess-Control-Allow-Originfor it alone; without--originthere is no CORS at all, so a page can neither read the answers nor send what needs a preflight.