From 15e434998ad90ec87fbd9e5ce41527026d307c96 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Mon, 19 Oct 2026 12:13:22 -0400 Subject: [PATCH] feat(ai): configure Compose model and label via env UNSLOTH_MODEL selects the model the Compose plugin calls, and the optional UNSLOTH_MODEL_NAME labels it in the admin (derived from the model id when omitted). The model-id select keeps its stable internal key, a Postgres enum, so switching models needs no schema change. The seeded per-prompt model value is cleared so the env var applies; an admin can still pin a model on a single prompt row. Migration 20261019_121500_use_env_ai_model drops the column default and clears the seeded value. --- .env.example | 4 +++ .../20261019_121500_use_env_ai_model.ts | 25 ++++++++++++++++++ src/migrations/index.ts | 6 +++++ src/payload-generated-schema.ts | 4 +-- src/payload.config.ts | 26 ++++++++++++++----- 5 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 src/migrations/20261019_121500_use_env_ai_model.ts diff --git a/.env.example b/.env.example index d781c268..8e52513d 100644 --- a/.env.example +++ b/.env.example @@ -30,6 +30,10 @@ GAME_TICK_NOTIFY_SECRET=YOUR_GAME_TICK_NOTIFY_SECRET UNSLOTH_URL=http://192.168.1.7:8888/v1 # sk-unsloth-... API key generated in Unsloth Studio (Settings -> API). UNSLOTH_API_KEY=YOUR_UNSLOTH_API_KEY +# Model id served by the Unsloth connection (the GGUF name its API reports). +UNSLOTH_MODEL=unsloth/Ornith-1.0-9B-GGUF +# Optional label for the model in the Compose UI; derived from UNSLOTH_MODEL when omitted. +# UNSLOTH_MODEL_NAME=Ornith 1.0 9B # --- Bin script file logging --- # Optional. Payload bin scripts (game-tick, market-tick, mission-tick, diff --git a/src/migrations/20261019_121500_use_env_ai_model.ts b/src/migrations/20261019_121500_use_env_ai_model.ts new file mode 100644 index 00000000..546ab84c --- /dev/null +++ b/src/migrations/20261019_121500_use_env_ai_model.ts @@ -0,0 +1,25 @@ +import { MigrateDownArgs, MigrateUpArgs, sql } from "@payloadcms/db-postgres"; + +/** + * The per-prompt model override no longer carries a hardcoded default. An empty + * value falls back to the UNSLOTH_MODEL environment variable at request time. + * Clear the previously seeded value so the env var takes effect; rows an admin + * deliberately set to a different model are left alone. + */ +export async function up({ db }: MigrateUpArgs): Promise { + await db.execute(sql` + ALTER TABLE "plugin_ai_instructions" ALTER COLUMN "ornith_9b_settings_model" DROP DEFAULT; + UPDATE "plugin_ai_instructions" + SET "ornith_9b_settings_model" = NULL + WHERE "ornith_9b_settings_model" = 'unsloth/Ornith-1.0-9B-GGUF'; + `); +} + +export async function down({ db }: MigrateDownArgs): Promise { + await db.execute(sql` + ALTER TABLE "plugin_ai_instructions" ALTER COLUMN "ornith_9b_settings_model" SET DEFAULT 'unsloth/Ornith-1.0-9B-GGUF'; + UPDATE "plugin_ai_instructions" + SET "ornith_9b_settings_model" = 'unsloth/Ornith-1.0-9B-GGUF' + WHERE "ornith_9b_settings_model" IS NULL; + `); +} diff --git a/src/migrations/index.ts b/src/migrations/index.ts index ea22d6fc..12c15ed1 100644 --- a/src/migrations/index.ts +++ b/src/migrations/index.ts @@ -141,6 +141,7 @@ import * as migration_20261014_192950_add_npc_dialogue_states from './20261014_1 import * as migration_20261014_203529_add_event_log_discord_fields from './20261014_203529_add_event_log_discord_fields'; import * as migration_20261015_120000_add_bomperator_minigame_stats from './20261015_120000_add_bomperator_minigame_stats'; import * as migration_20261019_120000_add_ai_prompt_entity_context from './20261019_120000_add_ai_prompt_entity_context'; +import * as migration_20261019_121500_use_env_ai_model from './20261019_121500_use_env_ai_model'; export const migrations = [ { @@ -858,4 +859,9 @@ export const migrations = [ down: migration_20261019_120000_add_ai_prompt_entity_context.down, name: '20261019_120000_add_ai_prompt_entity_context' }, + { + up: migration_20261019_121500_use_env_ai_model.up, + down: migration_20261019_121500_use_env_ai_model.down, + name: '20261019_121500_use_env_ai_model' + }, ]; diff --git a/src/payload-generated-schema.ts b/src/payload-generated-schema.ts index 9344fa85..d5488bc3 100644 --- a/src/payload-generated-schema.ts +++ b/src/payload-generated-schema.ts @@ -8128,9 +8128,7 @@ export const plugin_ai_instructions = pgTable( layout: varchar("layout").default( '[paragraph] - Write a concise introduction (2-3 sentences) that outlines the main topic.\n[horizontalrule] - Insert a horizontal rule to separate the introduction from the main content.\n[list] - Create a list with 3-5 items. Each list item should contain:\n a. [heading] - A brief, descriptive heading (up to 5 words)\n b. [paragraph] - A short explanation or elaboration (1-2 sentences)\n[horizontalrule] - Insert another horizontal rule to separate the main content from the conclusion.\n[paragraph] - Compose a brief conclusion (2-3 sentences) summarizing the key points.\n[quote] - Include a relevant quote from a famous person, directly related to the topic. Format: "Quote text." - Author Name', ), - "ornith-9b-settings_model": varchar("ornith_9b_settings_model").default( - "unsloth/Ornith-1.0-9B-GGUF", - ), + "ornith-9b-settings_model": varchar("ornith_9b_settings_model"), updatedAt: timestamp("updated_at", { mode: "string", withTimezone: true, precision: 3 }) .defaultNow() .notNull(), diff --git a/src/payload.config.ts b/src/payload.config.ts index 1a798657..3fc9fbe1 100644 --- a/src/payload.config.ts +++ b/src/payload.config.ts @@ -121,7 +121,19 @@ const unslothProvider = createOpenAI({ baseURL: process.env.UNSLOTH_URL || "http://192.168.1.7:8888/v1", }); -const DEFAULT_MODEL = "unsloth/Ornith-1.0-9B-GGUF"; +// The model-id select and its Postgres enum use a stable internal key. Changing +// that key requires a migration, so the served model is configured separately: +// point UNSLOTH_MODEL at the GGUF loaded in Unsloth Studio. +const MODEL_ID = "unsloth/Ornith-1.0-9B-GGUF"; +const DEFAULT_MODEL = process.env.UNSLOTH_MODEL || MODEL_ID; + +// Dropdown label for the model. UNSLOTH_MODEL_NAME wins; otherwise derive a +// readable name from UNSLOTH_MODEL, falling back to the bundled Ornith label. +const prettifyModel = (id: string) => + (id.split("/").pop() || id).replace(/[-_]+/g, " ").replace(/\b\w/g, (char) => char.toUpperCase()); +const MODEL_LABEL = + process.env.UNSLOTH_MODEL_NAME || + (process.env.UNSLOTH_MODEL ? prettifyModel(process.env.UNSLOTH_MODEL) : "Ornith 1.0 9B"); // Ornith is a reasoning model: it emits a long `reasoning_content` block before // the actual answer, and with a small token budget the reasoning consumes the @@ -773,8 +785,8 @@ export default buildConfig({ }, generationModels: [ { - id: "unsloth/Ornith-1.0-9B-GGUF", - name: "Ornith 1.0 9B", + id: MODEL_ID, + name: MODEL_LABEL, fields: ["text", "textarea", "richText"], handler: async ( prompt: string, @@ -851,18 +863,20 @@ export default buildConfig({ type: "group", admin: { condition(data) { - return data["model-id"] === "unsloth/Ornith-1.0-9B-GGUF"; + return data["model-id"] === MODEL_ID; }, }, fields: [ { name: "model", type: "text", - defaultValue: DEFAULT_MODEL, + admin: { + description: "Leave blank to use the UNSLOTH_MODEL environment variable.", + }, label: "Model", }, ], - label: "Ornith 9B Settings", + label: `${MODEL_LABEL} Settings`, }, }, ],