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-30b-a3b-gptq-int4',
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-30b-a3b-gptq-int4',
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
listeners: {
    completed?: (data: ChatCompletionResult) => void[];
    error?: (
        data: {
            error: string;
            jobID: string;
            message: string;
            workerName?: string;
        },
    ) => void[];
    jobState?: (data: ChatJobStateEvent) => void[];
    modelsUpdated?: (data: Record<string, LLMModelInfo>) => 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<string, LLMModelInfo>) => 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

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;
          tokenType?: "sogni" | "spark";
      }

    Returns Promise<LLMCostEstimation>

    const estimate = await sogni.chat.estimateCost({
    model: 'qwen3-30b-a3b-gptq-int4',
    messages: [{ role: 'user', content: 'Hello!' }],
    max_tokens: 1024,
    });
    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>>