Chat API for LLM text generation via the Sogni Supernet.

Provides OpenAI-compatible chat completion interface using Sogni's decentralized LLM worker network.

Usage:

// Streaming
const stream = await sogni.chat.completions.create({
model: 'qwen3.6-35b-a3b-gguf-iq4xs',
messages: [{ role: 'user', content: 'Hello!' }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.content);
}

// Non-streaming
const result = await sogni.chat.completions.create({
model: 'qwen3.6-35b-a3b-gguf-iq4xs',
messages: [{ role: 'user', content: 'Hello!' }],
});
console.log(result.content);

Hierarchy (View Summary)

Constructors

Properties

client: ApiClient
completions: {
    create: (
        params: ChatCompletionParams & { stream: true },
    ) => Promise<ChatStream> & (
        params: ChatCompletionParams & { stream?: false },
    ) => Promise<ChatCompletionResult> & (
        params: ChatCompletionParams,
    ) => Promise<ChatCompletionResult | ChatStream>;
}
eip712: EIP712Helper
hosted: {
    create: (
        params: HostedChatCompletionParams,
    ) => Promise<HostedChatCompletionResult>;
}

Hosted REST chat completion (POST /v1/chat/completions). The API executes any eligible hosted tools server-side and returns the final result within the request lifetime.

listeners: {
    completed?: (data: ChatCompletionResult) => void[];
    error?: (
        data: {
            error: string;
            jobID: string;
            message: string;
            workerName?: string;
        },
    ) => void[];
    jobState?: (data: ChatJobStateEvent) => void[];
    modelsUpdated?: (data: Record) => void[];
    token?: (data: ChatCompletionChunk) => void[];
} = {}

Type declaration

  • Optionalcompleted?: (data: ChatCompletionResult) => void[]

    Emitted when a chat completion finishes

  • Optionalerror?: (
        data: {
            error: string;
            jobID: string;
            message: string;
            workerName?: string;
        },
    ) => void[]

    Emitted when a chat completion fails

  • OptionaljobState?: (data: ChatJobStateEvent) => void[]

    Emitted when the job state changes (queued, assigned to worker, started, etc.)

  • OptionalmodelsUpdated?: (data: Record) => void[]

    Emitted when the available LLM models list is updated from the network

  • Optionaltoken?: (data: ChatCompletionChunk) => void[]

    Emitted for each token chunk received during streaming

runs: {
    cancel: (runId: string, reason?: string) => Promise<ChatRunRecord>;
    create: (params: StartChatRunParams) => Promise<ChatRunRecord>;
    get: (runId: string) => Promise<ChatRunRecord>;
    streamEvents: (
        runId: string,
        options?: StreamChatRunEventsOptions,
    ) => AsyncIterableIterator<ChatRunEvent>;
}

Durable hosted chat runs (POST /v1/chat/runs). A run is submitted and persisted server-side; the executor drives the LLM/tool loop without requiring the client to stay connected. Clients can disconnect and reattach via SSE Last-Event-ID replay or fetch the final snapshot.

  • create returns the persisted run record immediately (202).
  • get reads the current run state.
  • cancel flips the run to cancelled and aborts in-flight work.
  • streamEvents yields ChatRunEvents via SSE.

Tool execution API for Sogni platform tools (image, video, music generation).

// Execute a single Sogni tool call
const result = await sogni.chat.tools.execute(toolCall);

// Execute all tool calls from a completion
const results = await sogni.chat.tools.executeAll(toolCalls, {
onToolCall: async (tc) => customHandler(tc), // for non-Sogni tools
});

Accessors

Methods

  • Estimate the cost of a chat completion request before submitting it.

    Uses the same token estimation formula as the server: input tokens ≈ ceil(JSON.stringify(messages).length / 4)

    Parameters

    • params: {
          max_tokens?: number;
          messages: ChatMessage[];
          model: string;
          taskProfile?: "general" | "coding" | "reasoning";
          think?: boolean;
          tokenType?: "sogni" | "spark";
      }

    Returns Promise<LLMCostEstimation>

    const estimate = await sogni.chat.estimateCost({
    model: 'qwen3.6-35b-a3b-gguf-iq4xs',
    messages: [{ role: 'user', content: 'Hello!' }],
    think: true,
    taskProfile: 'reasoning',
    });
    console.log(`Estimated cost: ${estimate.costInToken.toFixed(6)}`);
  • Wait for available LLM models to be received from the network. Resolves immediately if models are already available.

    Parameters

    • timeout: number = 10000

      timeout in milliseconds until the promise is rejected (default: 10000)

    Returns Promise<Record<string, LLMModelInfo>>