Skip to main content
Elixir SDK / Inference Moss.Inference.Chat.Completions.create/3 sends OpenAI’s chat completions body, as a map, to Moss Inference, which searches the indexes you name, adds what it finds to the prompt and replies with OpenAI’s field names as string keys. Code written for OpenAI keeps its body and adds :indexes.
Requires moss 1.10.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 process. It is plain data with nothing to close.

Get a reply

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

Stream a reply

Read the stream once, in the process that called create/3. To stop early, halt it, for example with Enum.take/2, or call close/1 from any process: either closes the connection, which stops the model.
Only the last chunk carries usage, and it has no choices, which is why the comprehension above skips it.

Errors

A failed call gives a Moss.Inference.Error: new/3 raises it, create/3 returns it as {:error, error}, and a stream raises it while you read it. 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", request_id is the response’s X-Moss-Request-Id, and retry_after is the wait in milliseconds that a Retry-After header asks for. A field the failure lacks is nil. The SDK sets these types itself, with status nil: Every other error reports what Moss Inference sent. See Errors, and Limits for a 429.

Tool calls

tools, tool_choice and parallel_tool_calls go in params as in OpenAI’s API, and your code runs the tools. With stream: true, each call arrives in pieces in choice["delta"]["tool_calls"]. Without it, create/3 joins them into the reply’s message, with "" for a missing id, name or arguments. Append that message to messages as it is, add one tool message per call, and call again, as in Tool calls.
lookup_order/1 stands for your own code. It gets the arguments as a JSON string and returns the result as a string.

MCP servers

A Moss.Inference.McpServer in tools lets Moss Inference call that server’s tools itself during the turn.
Steps arrive in chunk["moss_tool_step"] and completion["moss_tool_steps"], and MCP failures give a Moss.Inference.Error with status, type, code and retry_after, as in MCP servers.

Options

Moss.Inference.new(project_id, project_key, opts): Moss.Inference.Chat.Completions.create(client, params, opts):