UNPKG

langchain

Version:
62 lines • 2.42 kB
import { AgentMiddleware } from "./types.js"; import { AgentLanguageModelLike } from "../model.js"; import { z } from "zod/v4"; import { BaseDecisionModel, ChoiceAnswer, Content, JsonValue } from "@langchain/core/decisions"; /** A model available to the router and the criterion for selecting it. */ export interface ModelChoice { /** A chat model, or a model string accepted by `initChatModel`. */ model: string | AgentLanguageModelLike; /** Description of the tasks suited to the model. */ criteria: JsonValue; } export interface ModelRouterMiddlewareConfig { /** * Named model choices, each containing a model and the criterion for * selecting it. */ choices: Record<string, ModelChoice>; /** Instructions the classifier should follow when selecting a route. */ instructions: Content; /** Decision model used to select a route. */ classifier: BaseDecisionModel; } /** * Select an agent's model with a decision model's `Choice` answer (beta). * * The middleware classifies the latest human message once before an agent * run, stores the complete `ChoiceAnswer` in agent state as `modelRoute`, and * uses its selected label for every model call in the run. Keeping the * complete answer makes probabilities and confidence available in state and * traces. Classifier failures propagate and terminate the run rather than * silently selecting a different model. * * @example * ```ts * import { createAgent, modelRouterMiddleware } from "langchain"; * * const router = modelRouterMiddleware({ * choices: { * fast: { * model: "openai:gpt-5-mini", * criteria: "Simple, well-scoped tasks.", * }, * powerful: { * model: powerfulModel, * criteria: "Complex tasks requiring deeper reasoning.", * }, * }, * instructions: "Choose the least costly model suited to the task.", * classifier, * }); * const agent = createAgent({ * model: "openai:gpt-5-mini", * middleware: [router], * }); * ``` * * @throws Error if `choices` is empty. */ export declare function modelRouterMiddleware(config: ModelRouterMiddlewareConfig): AgentMiddleware<z.ZodObject<{ modelRoute: z.ZodOptional<z.ZodCustom<ChoiceAnswer, ChoiceAnswer>>; }, z.core.$strip>, undefined, unknown, readonly (import("@langchain/core/tools").ServerTool | import("@langchain/core/tools").ClientTool)[], readonly []>; //# sourceMappingURL=modelRouter.d.ts.map