add configurable max_tokens per AI activity
Allows users to configure the max_tokens sent to the AI endpoint for each activity (tagging, description, extraction, translation) individually, with per-library overrides following the same pattern as model overrides. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -58,6 +58,10 @@ export interface AiConfig {
|
||||
promptTagger: string
|
||||
promptExtract: string
|
||||
promptTranslate: string
|
||||
maxTokensTag: number
|
||||
maxTokensDescribe: number
|
||||
maxTokensExtract: number
|
||||
maxTokensTranslate: number
|
||||
}
|
||||
|
||||
export function getAiConfig(): AiConfig {
|
||||
@@ -76,9 +80,14 @@ export function getAiConfig(): AiConfig {
|
||||
const promptExtract = promptExtractRaw !== null ? promptExtractRaw : DEFAULT_PROMPT_EXTRACT
|
||||
const promptTranslateRaw = getSetting('ai_prompt_translate')
|
||||
const promptTranslate = promptTranslateRaw !== null ? promptTranslateRaw : DEFAULT_PROMPT_TRANSLATE
|
||||
const maxTokensTag = parseInt(getSetting('ai_max_tokens_tag') ?? '8192', 10) || 8192
|
||||
const maxTokensDescribe = parseInt(getSetting('ai_max_tokens_describe') ?? '8192', 10) || 8192
|
||||
const maxTokensExtract = parseInt(getSetting('ai_max_tokens_extract') ?? '8192', 10) || 8192
|
||||
const maxTokensTranslate = parseInt(getSetting('ai_max_tokens_translate') ?? '8192', 10) || 8192
|
||||
return {
|
||||
endpoint, model, modelTagging, modelDescribe, modelExtract, modelTranslate, enabled,
|
||||
promptDescribe, promptTagger, promptExtract, promptTranslate,
|
||||
maxTokensTag, maxTokensDescribe, maxTokensExtract, maxTokensTranslate,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,6 +103,10 @@ export function updateAiConfig(
|
||||
promptTagger?: string,
|
||||
promptExtract?: string,
|
||||
promptTranslate?: string,
|
||||
maxTokensTag?: number,
|
||||
maxTokensDescribe?: number,
|
||||
maxTokensExtract?: number,
|
||||
maxTokensTranslate?: number,
|
||||
): void {
|
||||
setSetting('ai_endpoint', endpoint)
|
||||
setSetting('ai_model', model)
|
||||
@@ -106,6 +119,10 @@ export function updateAiConfig(
|
||||
if (promptTagger !== undefined) setSetting('ai_prompt_tagger', promptTagger)
|
||||
if (promptExtract !== undefined) setSetting('ai_prompt_extract', promptExtract)
|
||||
if (promptTranslate !== undefined) setSetting('ai_prompt_translate', promptTranslate)
|
||||
if (maxTokensTag !== undefined) setSetting('ai_max_tokens_tag', String(Math.max(1, Math.floor(maxTokensTag))))
|
||||
if (maxTokensDescribe !== undefined) setSetting('ai_max_tokens_describe', String(Math.max(1, Math.floor(maxTokensDescribe))))
|
||||
if (maxTokensExtract !== undefined) setSetting('ai_max_tokens_extract', String(Math.max(1, Math.floor(maxTokensExtract))))
|
||||
if (maxTokensTranslate !== undefined) setSetting('ai_max_tokens_translate', String(Math.max(1, Math.floor(maxTokensTranslate))))
|
||||
}
|
||||
|
||||
export function getPreferredLanguage(): string {
|
||||
@@ -127,6 +144,10 @@ export interface LibraryAiOverrides {
|
||||
promptTagger: string
|
||||
promptExtract: string
|
||||
promptTranslate: string
|
||||
maxTokensTag: number | null
|
||||
maxTokensDescribe: number | null
|
||||
maxTokensExtract: number | null
|
||||
maxTokensTranslate: number | null
|
||||
}
|
||||
|
||||
interface LibraryAiSettingsRow {
|
||||
@@ -138,6 +159,10 @@ interface LibraryAiSettingsRow {
|
||||
prompt_tagger: string | null
|
||||
prompt_extract: string | null
|
||||
prompt_translate: string | null
|
||||
max_tokens_tag: number | null
|
||||
max_tokens_describe: number | null
|
||||
max_tokens_extract: number | null
|
||||
max_tokens_translate: number | null
|
||||
}
|
||||
|
||||
export function getLibraryAiOverrides(libraryId: string): LibraryAiOverrides {
|
||||
@@ -154,6 +179,10 @@ export function getLibraryAiOverrides(libraryId: string): LibraryAiOverrides {
|
||||
promptTagger: row?.prompt_tagger ?? '',
|
||||
promptExtract: row?.prompt_extract ?? '',
|
||||
promptTranslate: row?.prompt_translate ?? '',
|
||||
maxTokensTag: row?.max_tokens_tag ?? null,
|
||||
maxTokensDescribe: row?.max_tokens_describe ?? null,
|
||||
maxTokensExtract: row?.max_tokens_extract ?? null,
|
||||
maxTokensTranslate: row?.max_tokens_translate ?? null,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,7 +193,7 @@ export function setLibraryAiOverrides(libraryId: string, overrides: Partial<Libr
|
||||
'INSERT OR IGNORE INTO library_ai_settings (library_id) VALUES (?)'
|
||||
).run(libraryId)
|
||||
|
||||
const fields: Record<string, string | undefined> = {
|
||||
const stringFields: Record<string, string | undefined> = {
|
||||
model_tagging: overrides.modelTagging,
|
||||
model_describe: overrides.modelDescribe,
|
||||
model_extract: overrides.modelExtract,
|
||||
@@ -175,7 +204,7 @@ export function setLibraryAiOverrides(libraryId: string, overrides: Partial<Libr
|
||||
prompt_translate: overrides.promptTranslate,
|
||||
}
|
||||
|
||||
for (const [col, val] of Object.entries(fields)) {
|
||||
for (const [col, val] of Object.entries(stringFields)) {
|
||||
if (val !== undefined) {
|
||||
db.prepare(`UPDATE library_ai_settings SET ${col} = ? WHERE library_id = ?`).run(
|
||||
val === '' ? null : val,
|
||||
@@ -183,6 +212,22 @@ export function setLibraryAiOverrides(libraryId: string, overrides: Partial<Libr
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const numberFields: Record<string, number | null | undefined> = {
|
||||
max_tokens_tag: overrides.maxTokensTag,
|
||||
max_tokens_describe: overrides.maxTokensDescribe,
|
||||
max_tokens_extract: overrides.maxTokensExtract,
|
||||
max_tokens_translate: overrides.maxTokensTranslate,
|
||||
}
|
||||
|
||||
for (const [col, val] of Object.entries(numberFields)) {
|
||||
if (val !== undefined) {
|
||||
db.prepare(`UPDATE library_ai_settings SET ${col} = ? WHERE library_id = ?`).run(
|
||||
val === null ? null : Math.max(1, Math.floor(val)),
|
||||
libraryId,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getEffectiveAiConfig(libraryId: string): AiConfig {
|
||||
@@ -200,6 +245,10 @@ export function getEffectiveAiConfig(libraryId: string): AiConfig {
|
||||
promptTagger: overrides.promptTagger || global.promptTagger,
|
||||
promptExtract: overrides.promptExtract || global.promptExtract,
|
||||
promptTranslate: overrides.promptTranslate || global.promptTranslate,
|
||||
maxTokensTag: overrides.maxTokensTag ?? global.maxTokensTag,
|
||||
maxTokensDescribe: overrides.maxTokensDescribe ?? global.maxTokensDescribe,
|
||||
maxTokensExtract: overrides.maxTokensExtract ?? global.maxTokensExtract,
|
||||
maxTokensTranslate: overrides.maxTokensTranslate ?? global.maxTokensTranslate,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user