// 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
pip install krino-worldPython 3.10 or newer; its only dependency is httpx.
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)| Argument | Description |
|---|---|
region | "cn" for the mainland China site, "global" for the international one. Their accounts are separate |
base_url | Your own server, such as http://127.0.0.1:8630 (overrides region) |
api_key | A key from Settings → API keys (starts with km_). If omitted, read from the KRINO_API_KEY environment variable |
token | A login token you already have (from a password login), so you needn't log in again |
timeout | Request 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) -> AnyCall 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) -> dictRegister and sign in; the token is kept on the client. Returns your account.
login(username: str, password: str) -> dictSign 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() -> NoneSign out and revoke the token.
me() -> dictYour account and settings.
health() -> dictWhether the service is up.
Characters
characters() -> list[dict]Your characters, newest first. A new account already has the official characters.
character(character_id: str) -> dictOne character in full.
export_character(character_id: str) -> dictThe character as a krino-persona/1 package.
Chat
sessions(character_id: str) -> list[dict]Your conversations with a character.
start_session(character_id: str) -> dictStart a conversation; the result carries the greeting.
session(session_id: str) -> dictA conversation with all its messages.
send(session_id: str, text: str, image: str | None=None, source: str='app') -> dictSay 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) -> dictMake a gesture (wave, pat…); the character decides how to respond.
end_session(session_id: str) -> dictClose the conversation; the character writes a summary.
delete_session(session_id: str) -> NoneDelete 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) -> dictTell the character something to remember. type: fact, preference, event or commitment.
edit_memory(memory_id: str, **fields) -> dictChange a memory; pass only the fields you change.
forget(memory_id: str) -> dictForget a memory; restore() brings it back.
restore(memory_id: str) -> dictBring a forgotten memory back.
delete_memory(memory_id: str) -> NoneDelete 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.
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.
from krino_world import Client, KrinoError
try:
kw.send(session["id"], "")
except KrinoError as e:
print(e.status, e.message) # 400 Message is empty