Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion slash-runner
Submodule slash-runner updated 106 files
2 changes: 1 addition & 1 deletion src/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ export const buttons: Button[] = [
!isFunctionCallingSupported()
) {
toastr.info(
`当前配置指定的LLM不支持函数调用,请调整额外模型解析设置`,
`当前 TavernHelper 版本或配置指定的 LLM 不支持函数调用,请调整额外模型解析设置`,
'[MVU]重试额外模型解析',
{
timeOut: 3000,
Expand Down
55 changes: 34 additions & 21 deletions src/function/function_call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@ import { parseString } from '@util/common';
export const MVU_FUNCTION_NAME = `mvu_VariableUpdate_${getScriptId()}`;
const mvu_update_call_function_name = 'mvu_updateRound';

const mvu_update_schema = Object.freeze({
$schema: 'http://json-schema.org/draft-04/schema#',
type: 'object',
additionalProperties: false,
properties: {
analysis: {
type: 'string',
minLength: 1,
description:
'Write in ENGLISH. A compact reasoning summary that includes: (1) calculate time passed; (2) decide whether dramatic updates are allowed (special case or sufficiently long time); (3) list every variable name BEFORE actual variable analysis, without revealing their contents; (4) for each variable, judge whether it satisfies its change conditions and output only Y/N without reasons; (5) only evaluate stories inside <past_observe> block.',
},
delta: {
type: 'string',
minLength: 0,
description: 'variable update block',
},
},
required: ['delta'],
});

export const MVU_TOOL_DEFINITION = Object.freeze({
type: 'function',
function: {
name: MVU_FUNCTION_NAME,
description: 'use this tool to UpdateVariable.',
parameters: mvu_update_schema,
},
}) satisfies ToolDefinition;

/*
e.g.: [
[
Expand Down Expand Up @@ -43,7 +72,7 @@ interface FunctionCallBody {

/** 单个工具调用(function-calling 形态) */
interface ToolFunctionCall {
index: number; // 这条 tool_call 在“本批次”中的顺序
index?: number; // 这条 tool_call 在“本批次”中的顺序
id: string; // 流式/合并用的临时 ID
type: 'function'; // 本题场景锁定 function;留扩展点以兼容其它类型
function: FunctionCallBody;
Expand Down Expand Up @@ -135,26 +164,6 @@ export function registerFunction() {
return () => {};
}

const mvu_update_schema = Object.freeze({
$schema: 'http://json-schema.org/draft-04/schema#',
type: 'object',
additionalProperties: false,
properties: {
analysis: {
type: 'string',
minLength: 1,
description:
'Write in ENGLISH. A compact reasoning summary that includes: (1) calculate time passed; (2) decide whether dramatic updates are allowed (special case or sufficiently long time); (3) list every variable name BEFORE actual variable analysis, without revealing their contents; (4) for each variable, judge whether it satisfies its change conditions and output only Y/N without reasons; (5) only evaluate stories inside <past_observe> block.',
},
delta: {
type: 'string',
minLength: 0,
description: 'variable update block',
},
},
required: ['delta'],
});

registerFunctionTool({
name: MVU_FUNCTION_NAME,
displayName: 'MVU update',
Expand Down Expand Up @@ -329,3 +338,7 @@ export function extractFromToolCall(tool_calls: ToolCallBatches | undefined): st
}
return null;
}

export function extractFromGenerateToolCallResult(result: GenerateToolCallResult): string | null {
return extractFromToolCall([result.tool_calls]);
}
21 changes: 19 additions & 2 deletions src/function/is_function_calling_supported.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import { useDataStore } from '@/store';
import { compare } from 'compare-versions';

export const MIN_FUNCTION_CALLING_TAVERN_HELPER_VERSION = '4.8.4';

export function getFunctionCallingApiVersionUnsupportedMessage(): string | null {
const version = useDataStore().versions.tavernhelper;
if (version === '' || compare(version, MIN_FUNCTION_CALLING_TAVERN_HELPER_VERSION, '>=')) {
return null;
}
return `当前酒馆助手版本为 ${version},函数调用需要酒馆助手 ${MIN_FUNCTION_CALLING_TAVERN_HELPER_VERSION} 或更高版本`;
}

export function isFunctionCallingApiVersionSupported() {
return getFunctionCallingApiVersionUnsupportedMessage() === null;
}

export function isFunctionCallingSupported() {
if (!SillyTavern.ToolManager.isToolCallingSupported()) {
if (!isFunctionCallingApiVersionSupported()) {
return false;
}
if (SillyTavern.chatCompletionSettings.function_calling === false) {
if (!SillyTavern.ToolManager.isToolCallingSupported()) {
return false;
}
return true;
Expand Down
44 changes: 44 additions & 0 deletions src/function/request/extra_model_request_override.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { ExtraModelRequestOverrides } from '@/function/update/extra_model_preset';

let current_request_overrides: ExtraModelRequestOverrides | null = null;

export function setExtraModelRequestOverrides(
overrides: ExtraModelRequestOverrides | null | undefined
) {
current_request_overrides =
overrides && Object.keys(overrides).length > 0 ? { ...overrides } : null;
}

export function clearExtraModelRequestOverrides() {
current_request_overrides = null;
}

export function applyExtraModelRequestOverrides(generate_data: Record<string, any>) {
const overrides = current_request_overrides;
if (!overrides) {
return;
}
Comment thread
MagicalAstrogy marked this conversation as resolved.

if (overrides.max_tokens !== undefined) {
generate_data.max_tokens = overrides.max_tokens;
generate_data.max_completion_tokens =
overrides.max_completion_tokens ?? overrides.max_tokens;
}

const override_keys = [
'temperature',
'frequency_penalty',
'presence_penalty',
'repetition_penalty',
'top_p',
'min_p',
'top_k',
'top_a',
] as const;

for (const key of override_keys) {
if (overrides[key] !== undefined) {
generate_data[key] = overrides[key];
}
}
}
2 changes: 1 addition & 1 deletion src/function/request/filter_entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function filterEntries(lores: {
}
if (store.settings.额外模型解析配置.使用函数调用 && !isFunctionCallingSupported()) {
toastr.warning(
'当前预设/API 不支持函数调用,已退化回 `随AI输出`',
'当前 TavernHelper 版本或 预设/API 不支持函数调用,已退化回 `随AI输出`',
'[MVU]无法使用函数调用',
{
timeOut: 2000,
Expand Down
7 changes: 7 additions & 0 deletions src/function/request/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { applyExtraModelRequestOverrides } from '@/function/request/extra_model_request_override';
import { overrideToolRequest, registerFunction } from '@/function/function_call';
import { filterEntries } from '@/function/request/filter_entries';
import { filterPrompts } from '@/function/request/filter_prompts';
Expand All @@ -8,6 +9,12 @@ export function initRequest() {
stop_list.push(registerFunction());

stop_list.push(controlledStoppableEventOn('worldinfo_entries_loaded', filterEntries));
stop_list.push(
controlledStoppableEventOn(
tavern_events.CHAT_COMPLETION_SETTINGS_READY,
applyExtraModelRequestOverrides
)
);
stop_list.push(
controlledStoppableEventOn(
tavern_events.CHAT_COMPLETION_SETTINGS_READY,
Expand Down
Loading
Loading