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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: include the chat's system prompt in exports and training data (#12913)
Nilay111e160e10f4
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.tsindex 6cdbc167a45..7c223695267 100644--- a/studio/frontend/src/features/chat/api/chat-adapter.ts+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts@@ -2263,7 +2263,7 @@ async function resolveProjectInstructions(return project.instructions?.trim() ?? "";}-async function resolveChatInstructions(+export async function resolveChatInstructions(threadId: string | undefined,systemPrompt: unknown,systemVariables: unknown,diff --git a/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx b/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsxindex be7af3a7dde..c5768ebbfaf 100644--- a/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx+++ b/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx@@ -76,9 +76,13 @@ import {syncStoredChatMessages,} from "../utils/chat-history-storage";import { notifyChatHistoryUpdated } from "../api/chat-api";-import { toolResultModelText } from "../api/chat-adapter";+import { resolveChatInstructions, toolResultModelText } from "../api/chat-adapter";import { toolCallReplayArguments } from "../tool-call-arguments";import { codexLocalToolRoundId, startsNewCodexToolRound } from "../codex-reasoning";+import {+ settleThreadScopedSettingsForCopy,+ threadScopedDefault,+} from "../stores/chat-runtime-store";import { usePlusMenuPrefsStore } from "../stores/plus-menu-prefs-store";import type { ThreadRecord, MessageRecord } from "../types";import {@@ -231,24 +235,55 @@ async function loadConversationMessages(options: {emptyMessage?: string;includeSiblings?: boolean;+ includeInstructions?: boolean;} = {},) {const {emptyMessage = "No messages in this conversation to export.",includeSiblings = true,+ includeInstructions = true,} = options;// Read before the storage await: switching chats meanwhile would point the lookup at another thread.const liveBranch = liveThreadBranch(threadId);- const raw = await listStoredChatMessages(threadId);+ const [raw, instructions] = await Promise.all([+ listStoredChatMessages(threadId),+ includeInstructions ? chatInstructionsTurn(threadId) : [],+ ]);if (raw.length === 0) {toast.info(emptyMessage);return null;}// No parentId = legacy flat thread (already DB createdAt-sorted); walking the chain would invert order.const hasParentIds = raw.some((m) => (m as { parentId?: unknown }).parentId != null);- if (!hasParentIds) return raw;+ if (!hasParentIds) return [...instructions, ...raw];const headId = liveBranchHeadId(liveBranch, raw);- return orderByParentChain(raw, { includeSiblings, headId }) as typeof raw;+ return [+ ...instructions,+ ...orderByParentChain(raw, { includeSiblings, headId }),+ ] as typeof raw;+}++async function chatInstructionsTurn(threadId: string): Promise<MessageRecord[]> {+ // A debounced or held edit is not on the row yet, and the next reply already runs with it.+ await settleThreadScopedSettingsForCopy(threadId);+ const thread = await getStoredChatThread(threadId);+ if (!thread) return [];+ const text = await resolveChatInstructions(+ threadId,+ thread.settings?.systemPrompt ?? threadScopedDefault("systemPrompt"),+ thread.settings?.systemVariables ?? threadScopedDefault("systemVariables"),+ async () => thread,+ );+ if (!text) return [];+ return [+ {+ id: `${threadId}-instructions`,+ threadId,+ role: "system",+ content: [{ type: "text", text }],+ createdAt: thread.createdAt,+ },+ ];}// Newest saved turn of the branch on screen: a reply still generating is not stored yet, and falling back to the newest leaf would export the reply it replaces.@@ -498,7 +533,7 @@ export async function exportConversationCsv(threadId: string): Promise<void> {// One place decides that markdown carries the branch on screen; callers keep their own empty-state wording.const loadDisplayedBranchMessages = (threadId: string,- options: { emptyMessage?: string } = {},+ options: { emptyMessage?: string; includeInstructions?: boolean } = {},) => loadConversationMessages(threadId, { ...options, includeSiblings: false });/** Same markdown the download produces, for the "Copy as Markdown" shortcut. */@@ -526,6 +561,7 @@ async function saveConversationAsProjectSource(): Promise<SaveSourceOutcome> {const messages = await loadDisplayedBranchMessages(threadId, {emptyMessage: "No messages in this conversation to save.",+ includeInstructions: false,});if (!messages) return "skipped";const markdown = buildConversationMarkdown(@@ -963,7 +999,10 @@ export async function buildFineTuneJsonl(let skipped = 0;for (const id of ids) {const liveBranch = liveThreadBranch(id);- const raw = await listStoredChatMessages(id);+ const [raw, instructions] = await Promise.all([+ listStoredChatMessages(id),+ chatInstructionsTurn(id),+ ]);const hasParentIds = raw.some((m) => (m as { parentId?: unknown }).parentId != null,);@@ -974,7 +1013,7 @@ export async function buildFineTuneJsonl(headId: liveBranchHeadId(liveBranch, raw),}) as typeof raw): raw;- const turns = messagesToFineTuneTurns(ordered);+ const turns = messagesToFineTuneTurns([...instructions, ...ordered]);const converted = turns ? turnsToFineTuneLines(turns, format) : [];if (converted.length === 0) {skipped += 1;diff --git a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts b/studio/frontend/src/features/chat/stores/chat-runtime-store.tsindex 323c97deac2..dea9b30d3ea 100644--- a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts+++ b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts@@ -857,6 +857,24 @@ function readThreadScopedSettings(return sanitizeThreadScopedSettings(source);}+/** What a chat whose snapshot omits `key` runs with: applyThreadScopedSettings falls back to these. */+export function threadScopedDefault<K extends ThreadScopedSettingKey>(+ key: K,+): ThreadScopedSettings[K] | undefined {+ // No chat paired, so the store holds the installation's values, except a held edit, which is+ // the pairing chat's: resolved the way applyThreadScopedSettings captures the defaults.+ if (threadScopedSettingsThreadId === null) {+ if (!isHeldThreadScopedField(key)) {+ return readThreadScopedSettings(useChatRuntimeStore.getState())[key];+ }+ if (hydratedDefaultsByHeldField.has(key)) {+ return hydratedDefaultsByHeldField.get(key) as ThreadScopedSettings[K];+ }+ return (pairingWindowDefaults ?? globalThreadScopedDefaults)?.[key];+ }+ return globalThreadScopedDefaults?.[key];+}+// Keeps a model load from re-applying the global default over the pills the chat is running with.export function threadScopedOverride<K extends ThreadScopedSettingKey>(key: K,diff --git a/studio/frontend/tests/chat-export-system-prompt.test.ts b/studio/frontend/tests/chat-export-system-prompt.test.tsnew file mode 100644index 00000000000..988b378d08c--- /dev/null+++ b/studio/frontend/tests/chat-export-system-prompt.test.ts@@ -0,0 +1,316 @@+// 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 assert from "node:assert/strict";+import test from "node:test";+import vm from "node:vm";+import ts from "typescript";++import {+ codexLocalToolRoundId,+ startsNewCodexToolRound,+} from "../src/features/chat/codex-reasoning.ts";+import { stripSearchImageTokens } from "../src/features/chat/search-images/search-images.ts";+import { toolCallReplayArguments } from "../src/features/chat/tool-call-arguments.ts";+import type { ThreadRecord } from "../src/features/chat/types.ts";+import {+ buildConversationMarkdown,+ contentBlocksToMarkdownBlocks,+ CONVERSATION_MARKDOWN_MIME_TYPE,+ renderConversationBlocks,+} from "../src/features/chat/utils/conversation-markdown.ts";+import {+ buildNamedConversationsMarkdown,+ createConversationMarkdownBuilder,+ createConversationMarkdownExporter,+} from "../src/features/chat/utils/conversation-markdown-export.ts";+import { csvDocument, csvEscape, CSV_MIME } from "../src/features/chat/utils/csv-export.ts";+import * as liveThreadHead from "../src/features/chat/utils/live-thread-head.ts";+import { orderByParentChain } from "../src/features/chat/utils/message-order.ts";+import {+ conversationJsonlBody,+ exportFormatIncludesSiblings,+ ndjsonBody,+} from "../src/features/chat/utils/ndjson.ts";+import { unwrapPastedTextContent } from "../src/features/chat/utils/pasted-text.ts";+import { readSrc } from "./helpers/kit.ts";++type Exporters = {+ buildFineTuneJsonl: (format: string) => Promise<{ lines: string[] }>;+ exportConversationRawJsonl: (threadId: string) => Promise<void>;+ exportConversationMessagesJsonl: (threadId: string) => Promise<void>;+ exportConversationShareGPT: (threadId: string) => Promise<void>;+ exportConversationCsv: (threadId: string) => Promise<void>;+ exportConversationMarkdown: (threadId: string) => Promise<void>;+ buildThreadContent: (threadId: string, format: string) => Promise<string | null>;+ saveConversationAsProjectSource: (+ threadId: string,+ projectId: string,+ title: string,+ ) => Promise<string>;+};++const DIALOG = readSrc("features/chat/prompt-storage/prompt-storage-dialog.tsx");+const ADAPTER = readSrc("features/chat/api/chat-adapter.ts");++function slice(source: string, startMarker: string, endMarker: string): string {+ const start = source.indexOf(startMarker);+ const end = source.indexOf(endMarker, start);+ assert.notEqual(start, -1, `${startMarker} must exist`);+ assert.notEqual(end, -1, `${endMarker} must exist`);+ return source.slice(start, end);+}++const THREADS: ThreadRecord[] = [+ {+ id: "support",+ title: "Support",+ modelType: "base",+ projectId: "billing",+ archived: false,+ createdAt: 1,+ settings: {+ systemPrompt: "You answer for the {{team}} team. End with Ticket closed.",+ systemVariables: '{"team":"Billing"}',+ },+ },+ {+ id: "plain",+ title: "Plain",+ modelType: "base",+ projectId: null,+ archived: false,+ createdAt: 2,+ settings: { systemPrompt: " " },+ },+ {+ id: "inherits",+ title: "Inherits",+ modelType: "base",+ projectId: null,+ archived: false,+ createdAt: 3,+ settings: { temperature: 0.2 },+ },+];++const INSTALLATION_DEFAULTS: Record<string, string> = {+ systemPrompt: "Sign off as {{name}}.",+ systemVariables: '{"name":"Ada"}',+};++const PROJECTS: Record<string, { instructions: string; archived: boolean }> = {+ billing: { instructions: "Cite the refund policy.", archived: false },+ openInComposer: { instructions: "Reply in French.", archived: false },+};++const SUPPORT_INSTRUCTIONS =+ "<project_instructions>\nCite the refund policy.\n</project_instructions>\n\n" ++ "You answer for the Billing team. End with Ticket closed.";++function turns(threadId: string) {+ return [+ ["u1", null, "user", "Where is my refund?"],+ ["a1", "u1", "assistant", "It went out today. Ticket closed."],+ ].map(([id, parentId, role, text], index) => ({+ id: `${threadId}-${id}`,+ threadId,+ parentId: parentId ? `${threadId}-${parentId}` : null,+ createdAt: index + 10,+ role,+ content: [{ type: "text", text }],+ }));+}++function loadExporters(+ threadIds: string[],+ downloads: string[],+ sources: string[] = [],+ overrides: Record<string, unknown> = {},+) {+ const javascript = ts.transpileModule(+ [+ slice(ADAPTER, "function parseSystemVariablesMap(", "export const ThreadAutosaveHandle"),+ slice(ADAPTER, "async function resolveProjectInstructions(", "// Answered once per thread"),+ slice(ADAPTER, "export async function resolveProjectId(", "async function resolveSandboxSessionId("),+ slice(DIALOG, "function contentBlocksToText(", "/** A sidebar row as one markdown document."),+ slice(DIALOG, "async function buildThreadContent(", "function csvHeader("),+ slice(DIALOG, "// One JSONL line per conversation", "/** Download the fine-tuning JSONL"),+ "globalThis.__exporters = { buildFineTuneJsonl, exportConversationRawJsonl, exportConversationMessagesJsonl, exportConversationShareGPT, exportConversationCsv, exportConversationMarkdown, buildThreadContent, saveConversationAsProjectSource };",+ ].join("\n"),+ {+ compilerOptions: {+ module: ts.ModuleKind.CommonJS,+ target: ts.ScriptTarget.ES2022,+ },+ },+ ).outputText;+ const context = {+ exports: {},+ toast: { info: () => {}, success: () => {} },+ listStoredChatThreads: async () => threadIds.map((id) => ({ id })),+ listStoredChatMessages: async (id: string) => turns(id),+ getStoredChatThread: async (id: string) =>+ THREADS.find((thread) => thread.id === id),+ getStoredChatProject: async (id: string) => PROJECTS[id] ?? null,+ useChatRuntimeStore: { getState: () => ({ activeProjectId: "openInComposer" }) },+ isThreadIncognito: () => false,+ settleThreadScopedSettingsForCopy: async () => {},+ threadScopedDefault: (key: string) => INSTALLATION_DEFAULTS[key],+ composerProjectByPendingThread: new Map(),+ ...liveThreadHead,+ orderByParentChain,+ unwrapPastedTextContent,+ toolResultModelText: (result: unknown) => result,+ toolCallReplayArguments,+ codexLocalToolRoundId,+ startsNewCodexToolRound,+ contentBlocksToMarkdownBlocks,+ renderConversationBlocks,+ buildConversationMarkdown,+ buildNamedConversationsMarkdown,+ createConversationMarkdownBuilder,+ createConversationMarkdownExporter,+ CONVERSATION_MARKDOWN_MIME_TYPE,+ stripSearchImageTokens,+ conversationJsonlBody,+ exportFormatIncludesSiblings,+ ndjsonBody,+ csvDocument,+ csvEscape,+ CSV_MIME,+ downloadBlob: async (body: string) => {+ downloads.push(body);+ },+ saveMarkdownAsProjectSource: async (_projectId: string, body: string) => {+ sources.push(body);+ return true;+ },+ ...overrides,+ } as Record<string, unknown>;+ vm.runInNewContext(javascript, context);+ return context.__exporters as Exporters;+}++test("chat training data starts with the system prompt the chat ran with", async () => {+ const exporters = loadExporters(["support", "plain"], []);++ const openai = (await exporters.buildFineTuneJsonl("openai")).lines.map(+ (line) => JSON.parse(line).messages,+ );+ assert.deepEqual(openai[0], [+ { role: "system", content: SUPPORT_INSTRUCTIONS },+ { role: "user", content: "Where is my refund?" },+ { role: "assistant", content: "It went out today. Ticket closed." },+ ]);+ assert.deepEqual(openai[1], [+ { role: "user", content: "Where is my refund?" },+ { role: "assistant", content: "It went out today. Ticket closed." },+ ]);++ const sharegpt = (await exporters.buildFineTuneJsonl("sharegpt")).lines.map(+ (line) => JSON.parse(line).conversations,+ );+ assert.deepEqual(sharegpt[0][0], { from: "system", value: SUPPORT_INSTRUCTIONS });+ assert.equal(sharegpt[1][0].from, "human");++ const alpaca = (await exporters.buildFineTuneJsonl("alpaca")).lines.map((line) =>+ JSON.parse(line),+ );+ assert.equal(alpaca[0].input, SUPPORT_INSTRUCTIONS);+ assert.equal(alpaca[1].input, "");+});++test("every chat export format starts with the chat's system prompt", async () => {+ const downloads: string[] = [];+ const exporters = loadExporters(["support"], downloads);++ await exporters.exportConversationRawJsonl("support");+ assert.deepEqual(JSON.parse(downloads[0]).messages[0], {+ role: "system",+ content: SUPPORT_INSTRUCTIONS,+ });++ await exporters.exportConversationMessagesJsonl("support");+ assert.deepEqual(JSON.parse(downloads[1].split("\n")[0]), {+ role: "system",+ content: SUPPORT_INSTRUCTIONS,+ });++ await exporters.exportConversationShareGPT("support");+ assert.deepEqual(JSON.parse(downloads[2]).conversations[0], {+ from: "system",+ value: SUPPORT_INSTRUCTIONS,+ });++ await exporters.exportConversationCsv("support");+ assert.match(downloads[3], /^\W*role,content\r?\n"system","<project_instructions>/);++ await exporters.exportConversationMarkdown("support");+ const markdown = downloads[4];+ assert.ok(markdown.includes(`## System\n\n${SUPPORT_INSTRUCTIONS}`));+ assert.ok(markdown.indexOf("## System") < markdown.indexOf("## User"));++ const bulk = await exporters.buildThreadContent("support", "jsonl-raw");+ assert.equal(JSON.parse(bulk ?? "").messages[0].role, "system");+});++test("a chat with no system prompt exports only its own turns", async () => {+ const downloads: string[] = [];+ const exporters = loadExporters(["plain"], downloads);++ await exporters.exportConversationRawJsonl("plain");+ await exporters.exportConversationShareGPT("plain");+ await exporters.exportConversationCsv("plain");+ await exporters.exportConversationMarkdown("plain");++ assert.equal(JSON.parse(downloads[0]).messages[0].role, "user");+ assert.equal(JSON.parse(downloads[1]).conversations[0].from, "human");+ assert.ok(!downloads[2].includes('"system"'));+ assert.ok(!downloads[3].includes("## System"));+ assert.ok(!downloads.join("").includes("French"));+});++test("a chat whose snapshot omits the system prompt exports the default it runs with", async () => {+ const downloads: string[] = [];+ const exporters = loadExporters(["inherits"], downloads);++ await exporters.exportConversationRawJsonl("inherits");+ assert.deepEqual(JSON.parse(downloads[0]).messages[0], {+ role: "system",+ content: "Sign off as Ada.",+ });+});++test("an export waits for the chat's settings edit still being saved", async () => {+ const downloads: string[] = [];+ let saved = false;+ const exporters = loadExporters(["plain"], downloads, [], {+ settleThreadScopedSettingsForCopy: async () => {+ await new Promise((resolve) => setTimeout(resolve, 5));+ saved = true;+ return true;+ },+ getStoredChatThread: async (id: string) => ({+ ...THREADS.find((thread) => thread.id === id),+ settings: { systemPrompt: saved ? "Edited prompt." : "" },+ }),+ });++ await exporters.exportConversationRawJsonl("plain");+ assert.deepEqual(JSON.parse(downloads[0]).messages[0], {+ role: "system",+ content: "Edited prompt.",+ });+});++test("saving a chat to project sources leaves its system prompt out", async () => {+ const sources: string[] = [];+ const exporters = loadExporters(["support"], [], sources);++ await exporters.saveConversationAsProjectSource("support", "billing", "Support");++ assert.equal(sources.length, 1);+ assert.ok(!sources[0].includes("## System"));+ assert.ok(sources[0].includes("Where is my refund?"));+});diff --git a/studio/frontend/tests/finetune-export-branches.test.ts b/studio/frontend/tests/finetune-export-branches.test.tsindex 668fb01fe13..5309ae90272 100644--- a/studio/frontend/tests/finetune-export-branches.test.ts+++ b/studio/frontend/tests/finetune-export-branches.test.ts@@ -75,6 +75,8 @@ function loadBuilder(chats: Record<string, StoredMessage[]>) {listStoredChatThreads: async () =>Object.keys(chats).map((id) => ({ id })),listStoredChatMessages: async (id: string) => chats[id],+ getStoredChatThread: async () => undefined,+ settleThreadScopedSettingsForCopy: async () => {},...liveThreadHead,orderByParentChain,unwrapPastedTextContent,diff --git a/studio/frontend/tests/markdown-export-branches.test.ts b/studio/frontend/tests/markdown-export-branches.test.tsindex bcbc5d6bc16..1345325b020 100644--- a/studio/frontend/tests/markdown-export-branches.test.ts+++ b/studio/frontend/tests/markdown-export-branches.test.ts@@ -110,6 +110,9 @@ function loadExporters(listStoredChatMessages: async () => stored,getStoredChatThread: async (id: string) =>threads.find((thread) => thread.id === id),+ resolveChatInstructions: async () => "",+ threadScopedDefault: () => undefined,+ settleThreadScopedSettingsForCopy: async () => {},buildNamedConversationsMarkdown,CONVERSATION_MARKDOWN_MIME_TYPE,canMergeConversationExport,diff --git a/studio/frontend/tests/sharegpt-export-branches.test.ts b/studio/frontend/tests/sharegpt-export-branches.test.tsindex 1464ec4f238..2d85d3a7801 100644--- a/studio/frontend/tests/sharegpt-export-branches.test.ts+++ b/studio/frontend/tests/sharegpt-export-branches.test.ts@@ -83,6 +83,8 @@ function loadExporters(onRead();return stored;},+ getStoredChatThread: async () => undefined,+ settleThreadScopedSettingsForCopy: async () => {},...liveThreadHead,orderByParentChain,exportFormatIncludesSiblings,diff --git a/studio/frontend/tests/thread-scoped-default-export.test.ts b/studio/frontend/tests/thread-scoped-default-export.test.tsnew file mode 100644index 00000000000..db18689b900--- /dev/null+++ b/studio/frontend/tests/thread-scoped-default-export.test.ts@@ -0,0 +1,51 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// Chat exports read threadScopedDefault for a chat whose snapshot omits the system prompt. While+// the first chat's read is out, an edit to its prompt is held in the live store, and that edit+// must not become the default every other snapshot-less chat exports.++import assert from "node:assert/strict";+import { register } from "node:module";+import test from "node:test";++import { installLocalStorageFake } from "./helpers/kit.ts";++const { store: localStorageFake } = installLocalStorageFake();+localStorageFake.set("unsloth_chat_settings_imported_to_studio_db", "true");+register("./thread-sampling-resolver.mjs", import.meta.url);++const { settingsHttp } = await import("./helpers/store-stubs/settings-http.ts");++const STORE_URL = new URL(+ "../src/features/chat/stores/chat-runtime-store.ts",+ import.meta.url,+).href;++const INSTALLATION_PROMPT = "INSTALLATION PROMPT";+const EDITED_PROMPT = "CHAT A ONLY 9c1e";++test("a prompt edit held for a pairing chat is not the default other chats export", async () => {+ settingsHttp.settings = { inferenceParams: { systemPrompt: INSTALLATION_PROMPT } };+ settingsHttp.puts.length = 0;+ const { useChatRuntimeStore, beginThreadScopedPairing, threadScopedDefault } =+ (await import(`${STORE_URL}?scenario=default-export`)) as never as {+ useChatRuntimeStore: {+ getState: () => Record<string, (...args: never[]) => unknown> & {+ params: Record<string, unknown>;+ };+ };+ beginThreadScopedPairing: (threadId: string) => void;+ threadScopedDefault: (key: string) => unknown;+ };+ const state = () => useChatRuntimeStore.getState();+ await state().hydratePersistedSettings();+ assert.equal(threadScopedDefault("systemPrompt"), INSTALLATION_PROMPT);++ state().setActiveThreadId("chat-a" as never);+ beginThreadScopedPairing("chat-a");+ state().setParams({ ...state().params, systemPrompt: EDITED_PROMPT } as never);+ assert.equal(state().params.systemPrompt, EDITED_PROMPT);++ assert.equal(threadScopedDefault("systemPrompt"), INSTALLATION_PROMPT);+});