Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

1.7 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import { authFetch } from "@/features/auth";
import { readFastApiError } from "@/lib/format-fastapi-error";
import {
  type LlamaBackend,
  type LlamaBackendStatus,
  type LlamaBackendSwitchStarted,
  parseLlamaBackendStatus,
  parseLlamaBackendSwitchStarted,
} from "./llama-backend-payload";

export {
  LLAMA_BACKENDS,
  isLlamaBackend,
  llamaBackendSelectionNeedsApply,
  visibleLlamaBackendOptions,
} from "./llama-backend-payload";
export type {
  LlamaBackend,
  LlamaBackendJob,
  LlamaBackendOption,
  LlamaBackendStatus,
  LlamaBackendSwitchStarted,
  LlamaEffectiveBackend,
} from "./llama-backend-payload";

/** Always refetches: the install on disk and the in-flight job change under it. */
export async function loadLlamaBackendStatus(
  forceRefresh = false,
): Promise<LlamaBackendStatus> {
  const res = await authFetch(
    `/api/llama/backend${forceRefresh ? "?force_refresh=true" : ""}`,
  );
  if (!res.ok) {
    throw new Error(
      await readFastApiError(res, "Failed to load llama.cpp backend status"),
    );
  }
  return parseLlamaBackendStatus(await res.json());
}

/** Progress arrives through loadLlamaBackendStatus().job. */
export async function switchLlamaBackend(
  backend: LlamaBackend,
): Promise<LlamaBackendSwitchStarted> {
  const res = await authFetch("/api/llama/backend", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ backend }),
  });
  if (!res.ok) {
    throw new Error(
      await readFastApiError(res, "Failed to switch the llama.cpp backend"),
    );
  }
  return parseLlamaBackendSwitchStarted(await res.json());
}