Add tool ranker fallback policy tests
This commit is contained in:
+1
-1
@@ -75,7 +75,7 @@
|
|||||||
- [x] Сохранить status UX: `🧩 Выбираю подходящие инструменты...`.
|
- [x] Сохранить status UX: `🧩 Выбираю подходящие инструменты...`.
|
||||||
- [x] Гарантировать `clearStatus()` после ranker success/failure.
|
- [x] Гарантировать `clearStatus()` после ranker success/failure.
|
||||||
- [ ] Добавить fallback через `PipelineFallbackExecutor`: main model, all tools, no tools.
|
- [ ] Добавить fallback через `PipelineFallbackExecutor`: main model, all tools, no tools.
|
||||||
- [ ] Добавить tests на fallback ranker policy.
|
- [x] Добавить tests на fallback ranker policy.
|
||||||
|
|
||||||
## 6. Сделать model_call и tool_loop физически отдельными stages
|
## 6. Сделать model_call и tool_loop физически отдельными stages
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import {Mistral} from "@mistralai/mistralai";
|
import {Mistral} from "@mistralai/mistralai";
|
||||||
import {Ollama} from "ollama";
|
import {Ollama} from "ollama";
|
||||||
import {OpenAI} from "openai";
|
import {OpenAI} from "openai";
|
||||||
import {Environment} from "../common/environment";
|
import {Environment} from "../common/environment.js";
|
||||||
import {AiModelCapabilities} from "../model/ai-model-capabilities";
|
import {AiModelCapabilities} from "../model/ai-model-capabilities.js";
|
||||||
import {AiProvider} from "../model/ai-provider";
|
import {AiProvider} from "../model/ai-provider.js";
|
||||||
|
|
||||||
export type AiCapabilityName = keyof AiModelCapabilities;
|
export type AiCapabilityName = keyof AiModelCapabilities;
|
||||||
export type AiRuntimePurpose = AiCapabilityName | "chat";
|
export type AiRuntimePurpose = AiCapabilityName | "chat";
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import {ToolRankerFallbackPolicy} from "../common/policies.js";
|
||||||
|
|
||||||
|
export type ToolRankerFallbackSelection = {
|
||||||
|
toolNames: string[];
|
||||||
|
usedRanker: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function resolveToolRankerFallbackSelection(params: {
|
||||||
|
fallbackPolicy: ToolRankerFallbackPolicy;
|
||||||
|
availableToolNames: readonly string[];
|
||||||
|
}): ToolRankerFallbackSelection {
|
||||||
|
if (params.fallbackPolicy === ToolRankerFallbackPolicy.NO_TOOLS) {
|
||||||
|
return {
|
||||||
|
toolNames: [],
|
||||||
|
usedRanker: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
toolNames: [...params.availableToolNames],
|
||||||
|
usedRanker: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
import type {BoundaryValue} from "../common/boundary-types";
|
import type {BoundaryValue} from "../common/boundary-types.js";
|
||||||
import type {AiRuntimeTarget} from "./ai-runtime-target";
|
import type {AiRuntimeTarget} from "./ai-runtime-target.js";
|
||||||
import {AiProvider} from "../model/ai-provider";
|
import {AiProvider} from "../model/ai-provider.js";
|
||||||
import {RuntimeConfigSnapshot, toolSchemaNames} from "./unified-ai-runner.shared";
|
import {RuntimeConfigSnapshot, toolSchemaNames} from "./unified-ai-runner.shared.js";
|
||||||
import {
|
import {
|
||||||
buildToolRankerSystemPrompt,
|
buildToolRankerSystemPrompt,
|
||||||
getToolRankerAvailableToolInfos,
|
getToolRankerAvailableToolInfos,
|
||||||
type ToolRankerToolInfo,
|
type ToolRankerToolInfo,
|
||||||
} from "./tool-ranker-metadata";
|
} from "./tool-ranker-metadata.js";
|
||||||
|
|
||||||
export type ToolRankerMessage = {
|
export type ToolRankerMessage = {
|
||||||
role?: string;
|
role?: string;
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ import {spawn} from "node:child_process";
|
|||||||
import {copyFile, lstat, mkdir, readdir, rm, writeFile} from "node:fs/promises";
|
import {copyFile, lstat, mkdir, readdir, rm, writeFile} from "node:fs/promises";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import {AiTool} from "../tool-types";
|
import {AiTool} from "../tool-types.js";
|
||||||
import {Environment} from "../../common/environment";
|
import {Environment} from "../../common/environment.js";
|
||||||
import {toolsLogger} from "./tool-logger";
|
import {toolsLogger} from "./tool-logger.js";
|
||||||
import {randomUUID} from "node:crypto";
|
import {randomUUID} from "node:crypto";
|
||||||
import {AiJsonObject} from "../tool-types";
|
import {AiJsonObject} from "../tool-types.js";
|
||||||
|
|
||||||
const logger = toolsLogger.child("python-interpreter");
|
const logger = toolsLogger.child("python-interpreter");
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
import {appLogger} from "../../logging/logger";
|
import {appLogger} from "../../logging/logger.js";
|
||||||
|
|
||||||
export const toolsLogger = appLogger.child("ai-tools");
|
export const toolsLogger = appLogger.child("ai-tools");
|
||||||
|
|||||||
@@ -1,20 +1,21 @@
|
|||||||
import {ChatCompletionMessageParam} from "openai/resources/chat/completions";
|
import {ChatCompletionMessageParam} from "openai/resources/chat/completions";
|
||||||
import {ChatRequest} from "ollama";
|
import {ChatRequest} from "ollama";
|
||||||
import {BoundaryValue} from "../common/boundary-types";
|
import {BoundaryValue} from "../common/boundary-types.js";
|
||||||
import {ToolRankerFallbackPolicy} from "../common/policies";
|
import {ToolRankerFallbackPolicy} from "../common/policies.js";
|
||||||
import {AiProvider} from "../model/ai-provider";
|
import {AiProvider} from "../model/ai-provider.js";
|
||||||
import {createMistralClient, createOllamaClient, createOpenAiClient, sameRuntimeEndpoint} from "./ai-runtime-target";
|
import {createMistralClient, createOllamaClient, createOpenAiClient, sameRuntimeEndpoint} from "./ai-runtime-target.js";
|
||||||
import {aiLog, aiLogDuration, aiLogProviderTarget} from "../logging/ai-logger";
|
import {aiLog, aiLogDuration, aiLogProviderTarget} from "../logging/ai-logger.js";
|
||||||
import {providerChatTarget, RuntimeConfigSnapshot} from "./unified-ai-runner.shared";
|
import {providerChatTarget, RuntimeConfigSnapshot} from "./unified-ai-runner.shared.js";
|
||||||
import {
|
import {
|
||||||
buildRankerContext,
|
buildRankerContext,
|
||||||
buildRankerTarget,
|
buildRankerTarget,
|
||||||
buildToolRankerPrompt,
|
buildToolRankerPrompt,
|
||||||
filterRankedTools,
|
filterRankedTools,
|
||||||
ToolRankerSelection,
|
ToolRankerSelection,
|
||||||
} from "./tool-ranker-pipeline";
|
} from "./tool-ranker-pipeline.js";
|
||||||
import {allToolSchemaNames} from "./unified-ai-runner.shared";
|
import {allToolSchemaNames} from "./unified-ai-runner.shared.js";
|
||||||
import {sanitizeToolRankerResult} from "./tool-ranker-metadata";
|
import {sanitizeToolRankerResult} from "./tool-ranker-metadata.js";
|
||||||
|
import {resolveToolRankerFallbackSelection} from "./tool-ranker-fallback.js";
|
||||||
|
|
||||||
export class ToolRanker {
|
export class ToolRanker {
|
||||||
constructor(private readonly config: RuntimeConfigSnapshot) {
|
constructor(private readonly config: RuntimeConfigSnapshot) {
|
||||||
@@ -27,8 +28,15 @@ export class ToolRanker {
|
|||||||
round: number;
|
round: number;
|
||||||
signal: AbortSignal;
|
signal: AbortSignal;
|
||||||
messages?: readonly { role?: string; content?: string | readonly { text?: string }[] }[];
|
messages?: readonly { role?: string; content?: string | readonly { text?: string }[] }[];
|
||||||
|
runRanker?: (
|
||||||
|
provider: AiProvider,
|
||||||
|
target: NonNullable<ReturnType<typeof buildRankerTarget>>,
|
||||||
|
prompt: string,
|
||||||
|
userQuery: string,
|
||||||
|
) => Promise<string>;
|
||||||
}): Promise<ToolRankerSelection> {
|
}): Promise<ToolRankerSelection> {
|
||||||
const {availableTools, provider, round, signal, userQuery} = args;
|
const {availableTools, provider, round, signal, userQuery} = args;
|
||||||
|
const runRanker = args.runRanker ?? this.runRanker.bind(this);
|
||||||
const availableNames = allToolSchemaNames(availableTools);
|
const availableNames = allToolSchemaNames(availableTools);
|
||||||
const fallbackPolicy = this.config.toolRankerFallbackPolicy;
|
const fallbackPolicy = this.config.toolRankerFallbackPolicy;
|
||||||
const configuredTarget = buildRankerTarget(this.config, provider);
|
const configuredTarget = buildRankerTarget(this.config, provider);
|
||||||
@@ -41,11 +49,10 @@ export class ToolRanker {
|
|||||||
const target = configuredTarget ?? (fallbackPolicy === ToolRankerFallbackPolicy.MAIN_MODEL ? mainModelTarget : undefined);
|
const target = configuredTarget ?? (fallbackPolicy === ToolRankerFallbackPolicy.MAIN_MODEL ? mainModelTarget : undefined);
|
||||||
|
|
||||||
if (!target) {
|
if (!target) {
|
||||||
if (fallbackPolicy === ToolRankerFallbackPolicy.NO_TOOLS) {
|
return resolveToolRankerFallbackSelection({
|
||||||
return {toolNames: [], usedRanker: false};
|
fallbackPolicy,
|
||||||
}
|
availableToolNames: availableNames,
|
||||||
|
});
|
||||||
return {toolNames: availableNames, usedRanker: false};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const startedAt = Date.now();
|
const startedAt = Date.now();
|
||||||
@@ -63,7 +70,7 @@ export class ToolRanker {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (signal.aborted) throw new Error("Aborted");
|
if (signal.aborted) throw new Error("Aborted");
|
||||||
const raw = await this.runRanker(provider, target, ranker.prompt, userQuery);
|
const raw = await runRanker(provider, target, ranker.prompt, userQuery);
|
||||||
if (signal.aborted) throw new Error("Aborted");
|
if (signal.aborted) throw new Error("Aborted");
|
||||||
const selectedNames = sanitizeToolRankerResult({
|
const selectedNames = sanitizeToolRankerResult({
|
||||||
raw,
|
raw,
|
||||||
@@ -106,7 +113,7 @@ export class ToolRanker {
|
|||||||
const fallbackRanker = buildToolRankerPrompt(
|
const fallbackRanker = buildToolRankerPrompt(
|
||||||
buildRankerContext(this.config, provider, mainModelTarget, round, userQuery, availableTools),
|
buildRankerContext(this.config, provider, mainModelTarget, round, userQuery, availableTools),
|
||||||
);
|
);
|
||||||
const raw = await this.runRanker(provider, mainModelTarget, fallbackRanker.prompt, userQuery);
|
const raw = await runRanker(provider, mainModelTarget, fallbackRanker.prompt, userQuery);
|
||||||
const selectedNames = sanitizeToolRankerResult({
|
const selectedNames = sanitizeToolRankerResult({
|
||||||
raw,
|
raw,
|
||||||
availableToolNames: availableNames,
|
availableToolNames: availableNames,
|
||||||
@@ -151,14 +158,10 @@ export class ToolRanker {
|
|||||||
error: failureMessage,
|
error: failureMessage,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (fallbackPolicy === ToolRankerFallbackPolicy.NO_TOOLS) {
|
return resolveToolRankerFallbackSelection({
|
||||||
return {toolNames: [], usedRanker: false};
|
fallbackPolicy,
|
||||||
}
|
availableToolNames: availableNames,
|
||||||
|
});
|
||||||
return {
|
|
||||||
toolNames: availableNames,
|
|
||||||
usedRanker: false,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,9 +6,7 @@ import {z} from "zod";
|
|||||||
import {appLogger} from "../logging/logger.js";
|
import {appLogger} from "../logging/logger.js";
|
||||||
import type {BoundaryValue, ErrorLike} from "./boundary-types";
|
import type {BoundaryValue, ErrorLike} from "./boundary-types";
|
||||||
|
|
||||||
import {saveData} from "../db/database.js";
|
|
||||||
import {Answers} from "../model/answers.js";
|
import {Answers} from "../model/answers.js";
|
||||||
import {ifTrue} from "../util/utils.js";
|
|
||||||
import {AiProvider} from "../model/ai-provider.js";
|
import {AiProvider} from "../model/ai-provider.js";
|
||||||
import {ImageHandleFallbackPolicy, ImageHandlePolicy, RateLimitFallbackPolicy} from "./policies.js";
|
import {ImageHandleFallbackPolicy, ImageHandlePolicy, RateLimitFallbackPolicy} from "./policies.js";
|
||||||
import {ToolRankerFallbackPolicy} from "./policies.js";
|
import {ToolRankerFallbackPolicy} from "./policies.js";
|
||||||
@@ -16,6 +14,11 @@ import type {ToolCallData} from "../ai/unified-ai-runner.js";
|
|||||||
import {PYTHON_INTERPRETER_TOOL_NAME} from "../ai/tools/python-interpretator.js";
|
import {PYTHON_INTERPRETER_TOOL_NAME} from "../ai/tools/python-interpretator.js";
|
||||||
import {Localization, type LocalizationParams} from "./localization.js";
|
import {Localization, type LocalizationParams} from "./localization.js";
|
||||||
|
|
||||||
|
function parseBooleanLike(value: string): boolean {
|
||||||
|
const normalized = value.trim().toLowerCase();
|
||||||
|
return ["true", "t", "y", "1"].includes(normalized);
|
||||||
|
}
|
||||||
|
|
||||||
type EnvRecord = Record<string, string>;
|
type EnvRecord = Record<string, string>;
|
||||||
type StringEnumLike = Record<string, string>;
|
type StringEnumLike = Record<string, string>;
|
||||||
type StringEnumValue<T extends StringEnumLike> = T[keyof T];
|
type StringEnumValue<T extends StringEnumLike> = T[keyof T];
|
||||||
@@ -53,7 +56,7 @@ function booleanWithDefaultSchema(defaultValue: boolean) {
|
|||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ifTrue(normalized);
|
return parseBooleanLike(normalized);
|
||||||
}, z.boolean())
|
}, z.boolean())
|
||||||
.default(defaultValue)
|
.default(defaultValue)
|
||||||
.catch(defaultValue);
|
.catch(defaultValue);
|
||||||
@@ -62,7 +65,7 @@ function booleanWithDefaultSchema(defaultValue: boolean) {
|
|||||||
const optionalBooleanSchema = z
|
const optionalBooleanSchema = z
|
||||||
.preprocess(value => {
|
.preprocess(value => {
|
||||||
const normalized = normalizeString(value as BoundaryValue);
|
const normalized = normalizeString(value as BoundaryValue);
|
||||||
return normalized === undefined ? undefined : ifTrue(normalized);
|
return normalized === undefined ? undefined : parseBooleanLike(normalized);
|
||||||
}, z.boolean().optional())
|
}, z.boolean().optional())
|
||||||
.optional()
|
.optional()
|
||||||
.catch(undefined);
|
.catch(undefined);
|
||||||
@@ -1939,6 +1942,7 @@ export class Environment {
|
|||||||
|
|
||||||
if (!has) {
|
if (!has) {
|
||||||
this.ADMIN_IDS.add(id);
|
this.ADMIN_IDS.add(id);
|
||||||
|
const {saveData} = await import("../db/database.js");
|
||||||
await saveData();
|
await saveData();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1950,6 +1954,7 @@ export class Environment {
|
|||||||
|
|
||||||
if (has) {
|
if (has) {
|
||||||
this.ADMIN_IDS.delete(id);
|
this.ADMIN_IDS.delete(id);
|
||||||
|
const {saveData} = await import("../db/database.js");
|
||||||
await saveData();
|
await saveData();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1966,6 +1971,7 @@ export class Environment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.MUTED_IDS.add(id);
|
this.MUTED_IDS.add(id);
|
||||||
|
const {saveData} = await import("../db/database.js");
|
||||||
await saveData();
|
await saveData();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1976,6 +1982,7 @@ export class Environment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.MUTED_IDS.delete(id);
|
this.MUTED_IDS.delete(id);
|
||||||
|
const {saveData} = await import("../db/database.js");
|
||||||
await saveData();
|
await saveData();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import {AsyncLocalStorage} from "node:async_hooks";
|
import {AsyncLocalStorage} from "node:async_hooks";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import {appLogger} from "../logging/logger";
|
import {appLogger} from "../logging/logger.js";
|
||||||
|
|
||||||
const logger = appLogger.child("localization");
|
const logger = appLogger.child("localization");
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import {Message} from "typescript-telegram-bot-api";
|
import {Message} from "typescript-telegram-bot-api";
|
||||||
import {createLogger, formatDuration, LogDetails, LogLevel} from "./logger";
|
import {createLogger, formatDuration, LogDetails, LogLevel} from "./logger.js";
|
||||||
|
|
||||||
export type AiRunnerLogLevel = LogLevel;
|
export type AiRunnerLogLevel = LogLevel;
|
||||||
export type AiRunnerLogDetails = LogDetails;
|
export type AiRunnerLogDetails = LogDetails;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {AiProvider} from "./ai-provider";
|
import {AiProvider} from "./ai-provider.js";
|
||||||
|
|
||||||
export type AiEndpointInfo = {
|
export type AiEndpointInfo = {
|
||||||
provider?: AiProvider;
|
provider?: AiProvider;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {AiCapabilityInfo} from "./ai-capability-info";
|
import {AiCapabilityInfo} from "./ai-capability-info.js";
|
||||||
|
|
||||||
export class AiModelCapabilities {
|
export class AiModelCapabilities {
|
||||||
chat: AiCapabilityInfo | undefined;
|
chat: AiCapabilityInfo | undefined;
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
import test from "node:test";
|
||||||
|
import assert from "node:assert/strict";
|
||||||
|
|
||||||
|
const {ToolRankerFallbackPolicy} = await import("../dist/common/policies.js");
|
||||||
|
const {resolveToolRankerFallbackSelection} = await import("../dist/ai/tool-ranker-fallback.js");
|
||||||
|
|
||||||
|
const availableToolNames = ["read_file", "search_files"];
|
||||||
|
|
||||||
|
test("tool ranker fallback returns no tools when policy is NO_TOOLS", () => {
|
||||||
|
assert.deepEqual(
|
||||||
|
resolveToolRankerFallbackSelection({
|
||||||
|
fallbackPolicy: ToolRankerFallbackPolicy.NO_TOOLS,
|
||||||
|
availableToolNames,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
toolNames: [],
|
||||||
|
usedRanker: false,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("tool ranker fallback returns all tools when policy is ALL_TOOLS", () => {
|
||||||
|
assert.deepEqual(
|
||||||
|
resolveToolRankerFallbackSelection({
|
||||||
|
fallbackPolicy: ToolRankerFallbackPolicy.ALL_TOOLS,
|
||||||
|
availableToolNames,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
toolNames: ["read_file", "search_files"],
|
||||||
|
usedRanker: false,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("tool ranker fallback keeps all tools when policy is MAIN_MODEL", () => {
|
||||||
|
assert.deepEqual(
|
||||||
|
resolveToolRankerFallbackSelection({
|
||||||
|
fallbackPolicy: ToolRankerFallbackPolicy.MAIN_MODEL,
|
||||||
|
availableToolNames,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
toolNames: ["read_file", "search_files"],
|
||||||
|
usedRanker: false,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user