@langgraph-js/pro
Version:
The Pro SDK for LangGraph - seamlessly integrate your AI agents with frontend interfaces and build complex AI workflows
26 lines (25 loc) • 858 B
JavaScript
import { ChatOpenAICompletions } from "../openai/completion.js";
import { createMiddleware } from "langchain";
/**
* 根据 state.model_name 决定模型名称
* @example
* ```typescript
* const middleware = createDynamicModelMiddleware(stateSchema, "model_name");
* ```
*/
export const createDynamicModelMiddleware = (stateSchema, keyInState = "model_name", options) => {
return createMiddleware({
stateSchema,
name: "dynamic_model_middleware",
wrapModelCall: async (request, handler) => {
const model = new ChatOpenAICompletions({
...options,
/** @ts-ignore */
modelName: request.state[keyInState],
streaming: true,
streamUsage: true,
});
return await handler({ ...request, model });
},
});
};