Skip to main content
MossInference is a Kotlin client for Moss Inference, which searches your indexes in the cloud and answers in OpenAI’s chat completions shape, with camelCase fields. chat.completions.create returns the whole reply, and createStreaming streams it.
Requires the moss-android AAR 0.28.0 or later, installed as in Install.

Create a client

The project id and key come from the Moss portal. Build one client and reuse it for every call, from any thread. Calls and stream reads block, and throw MossException on the main thread, so run them from Dispatchers.IO.

Get a reply

completion.requestId is the response’s X-Moss-Request-Id, and so is stream.requestId on a stream. Include it when you report a problem. Both are null when the response has none.

Stream a reply

To stop early, leave use with a break or return: that closes the connection, which stops the model. Cancelling the coroutine alone does not, so the loop calls ensureActive(), whose throw leaves use the same way. Only the last chunk carries usage, and it has no choices, which is why the loop reads choices.firstOrNull().

Errors

A failed call throws InferenceError, from the for loop once a stream has started. status is the HTTP status of a refused request, type names the failure, and message describes it. code is the error code Moss Inference sent, such as "rate_limit_exceeded", requestId is the response’s X-Moss-Request-Id, and retryAfterMs is the wait in milliseconds that a Retry-After header asks for. A field the failure lacks is null. The SDK raises these itself, the last three as an InferenceError with that type and status null: Every other error reports what Moss Inference sent. See Errors, and Limits for a 429.

Tool calls

Pass tools, and the model may answer with toolCalls. create reads the whole stream and joins each call’s arguments. Append the reply with ChatMessage.assistant(reply), which sends it back unchanged, then one ChatMessage.tool(callId, content) per call, and call again, as in Tool calls:
lookupOrder is your own function, which takes the call’s JSON arguments and returns the result as text. createStreaming streams each call in pieces, in delta.toolCalls, so use create for a reply you send back.

MCP servers

A Tool.mcp entry names an MCP server whose tools Moss Inference runs for the model during the same call, so your code sends no ChatMessage.tool for them:
McpServer also takes allowedTools and transport, McpServer.Transport.STREAMABLE_HTTP or SSE. Steps arrive in chunk.toolStep and completion.toolSteps, with camelCase fields such as isError, and MCP failures throw InferenceError with status, type, code and retryAfterMs, as in MCP servers.

Options

MossInference(...): chat.completions.create and createStreaming take the same parameters, messages first: