KRINO MATRIX

// PYTHON SDK

Python SDK

krino-world wraps the API in a few dozen methods that return plain dicts and lists. v0.2.0 · PyPI · Source

Install

Shell
pip install krino-world

Python 3.10 or newer; its only dependency is httpx.

Python
from krino_world import Client

with Client(region="global") as kw:     # reads the key from KRINO_API_KEY; or pass api_key='km_...'
    character = kw.characters()[0]           # a new account already has the official characters
    session = kw.start_session(character["id"])
    print(session["greeting"])

    turn = kw.send(session["id"], "How was your day?")
    print(turn["reply"]["content"])

Client

Client(region="global", base_url=None, api_key=None, token=None, timeout=120)
ArgumentDescription
region"cn" for the mainland China site, "global" for the international one. Their accounts are separate
base_urlYour own server, such as http://127.0.0.1:8630 (overrides region)
api_keyA key from Settings → API keys (starts with km_). If omitted, read from the KRINO_API_KEY environment variable
tokenA login token you already have (from a password login), so you needn't log in again
timeoutRequest timeout in seconds

with closes the connection when you're done; otherwise call close(). Without an API key you can kw.login(username, password) instead; that token expires after a week.

Any endpoint

request(method: str, path: str, **kwargs) -> Any

Call any endpoint under /api/ that has no method of its own. Returns the decoded JSON; extra arguments go to httpx.

Account

register(username: str, password: str, display_name: str | None=None) -> dict

Register and sign in; the token is kept on the client. Returns your account.

login(username: str, password: str) -> dict

Sign in with a password when you have no API key; the token is kept on the client and expires after a week. Returns your account.

logout() -> None

Sign out and revoke the token.

me() -> dict

Your account and settings.

health() -> dict

Whether the service is up.

Characters

characters() -> list[dict]

Your characters, newest first. A new account already has the official characters.

character(character_id: str) -> dict

One character in full.

export_character(character_id: str) -> dict

The character as a krino-persona/1 package.

Chat

sessions(character_id: str) -> list[dict]

Your conversations with a character.

start_session(character_id: str) -> dict

Start a conversation; the result carries the greeting.

session(session_id: str) -> dict

A conversation with all its messages.

send(session_id: str, text: str, image: str | None=None, source: str='app') -> dict

Say something and wait for the whole reply, at turn["reply"]["content"].

stream(session_id: str, text: str='', image: str | None=None, audio: str | None=None, source: str='app') -> Iterator[Event]

Say something and get the reply in pieces: yields Events.

gesture(session_id: str, gesture: str) -> dict

Make a gesture (wave, pat…); the character decides how to respond.

end_session(session_id: str) -> dict

Close the conversation; the character writes a summary.

delete_session(session_id: str) -> None

Delete a conversation.

Memory

memories(character_id: str, status: str='active', type: str | None=None, q: str | None=None) -> list[dict]

What the character remembers about you, filtered by status, type or search text.

remember(character_id: str, content: str, type: str='fact', reality: str='real', importance: int=3, **fields) -> dict

Tell the character something to remember. type: fact, preference, event or commitment.

edit_memory(memory_id: str, **fields) -> dict

Change a memory; pass only the fields you change.

forget(memory_id: str) -> dict

Forget a memory; restore() brings it back.

restore(memory_id: str) -> dict

Bring a forgotten memory back.

delete_memory(memory_id: str) -> None

Delete a memory for good.

Voice

speak(text: str, voice: str | None=None, rate: float=1.0) -> tuple[bytes, str]

Synthesize speech: returns (audio bytes, media type).

Event

What stream() yields: name and data. name is decision, delta, done or error; see Streaming.

Python
for event in kw.stream(session["id"], "Tell me a story"):
    if event.name == "delta":
        print(event.data["text"], end="", flush=True)
    elif event.name == "error":
        print("\n", event.data["message"])

KrinoError

Raised when the server refuses a request: .status is the HTTP status, .message the server's explanation.

Python
from krino_world import Client, KrinoError

try:
    kw.send(session["id"], "")
except KrinoError as e:
    print(e.status, e.message)   # 400 Message is empty