一次绑定,单次多模型与跨次切换
构造时提供稳定模型配置;请求用配置 ID 选择,不反复注入 Provider。
单模型可在构造时传 model 配置,或稍后 bindModel 一次。多模型则在 models 中登记配置,model 字符串选择默认项;多个模型时不能猜默认值。未知模型 ID 在调用前拒绝。
switchModel 改后续运行的默认选择;run(state,{modelId}) 只覆盖本次,节点 model.invoke({modelId}) 只覆盖这次模型调用。单次 Workflow 可先快模型草拟、再另一个模型复核。多模型共享 maxModelCalls 请求预算,每个模型仍有各自的局部限制;切换不刷新祖先额度。
模型配置与选择绑定在 Agent 上,Workflow 通过节点中的模型端口使用它们。
由简到繁的同业务示例
将模型提前绑到 Agent
入门 · Node.js
ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
type State = { input: string; answer?: string };
const workflow = defineWorkflow<State, string>({ id: 'chat', nodes: [{
id: 'reply', description: '用绑定模型回答', startsRound: true,
async run(state, { model }) {
const { value } = await model.invoke({ input: state.input });
return { answer: value };
}, routes: [route.deliver('answer')],
}] });
const provider = {
async *stream(messages: readonly { content: string }[]) {
yield '收到:' + messages.at(-1)!.content;
},
};
const agent = new AgentCoreEngine({ workflow, model: { provider } });
console.log(await agent.run({ input: '你好' }));js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const workflow = defineWorkflow({ id: 'chat', nodes: [{
id: 'reply', description: '用绑定模型回答', startsRound: true,
async run(state, { model }) {
const { value } = await model.invoke({ input: state.input });
return { answer: value };
}, routes: [route.deliver('answer')],
}] });
const provider = {
async *stream(messages) {
yield '收到:' + messages.at(-1).content;
},
};
const agent = new AgentCoreEngine({ workflow, model: { provider } });
console.log(await agent.run({ input: '你好' }));增加模型目录,调用时只给 ID
配置 · Node.js
ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
type State = { input: string; answer?: string };
const workflow = defineWorkflow<State, string>({ id: 'chat', nodes: [{
id: 'reply', description: '用绑定模型回答', startsRound: true,
async run(state, { model }) {
const { value } = await model.invoke({ input: state.input });
return { answer: value };
}, routes: [route.deliver('answer')],
}] });
const provider = {
async *stream(messages: readonly { content: string }[]) {
yield '收到:' + messages.at(-1)!.content;
},
};
const agent = new AgentCoreEngine({ workflow, model: 'quick', maxModelCalls: 4,
models: { quick: { provider, maxCalls: 2 }, careful: { provider, maxCalls: 2 } },
});
console.log(await agent.run({ input: '简答' }));
console.log(await agent.run({ input: '仔细回答' }, { modelId: 'careful' }));
agent.switchModel('careful');
console.log(await agent.run({ input: '后续默认使用 careful' }));js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const workflow = defineWorkflow({ id: 'chat', nodes: [{
id: 'reply', description: '用绑定模型回答', startsRound: true,
async run(state, { model }) {
const { value } = await model.invoke({ input: state.input });
return { answer: value };
}, routes: [route.deliver('answer')],
}] });
const provider = {
async *stream(messages) {
yield '收到:' + messages.at(-1).content;
},
};
const agent = new AgentCoreEngine({ workflow, model: 'quick', maxModelCalls: 4,
models: { quick: { provider, maxCalls: 2 }, careful: { provider, maxCalls: 2 } },
});
console.log(await agent.run({ input: '简答' }));
console.log(await agent.run({ input: '仔细回答' }, { modelId: 'careful' }));
agent.switchModel('careful');
console.log(await agent.run({ input: '后续默认使用 careful' }));同一任务中调用两个已绑定模型
扩展 · Node.js
ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
type State = { input: string; answer?: string };
const provider = {
async *stream(messages: readonly { content: string }[]) {
yield '收到:' + messages.at(-1)!.content;
},
};
const workflow = defineWorkflow<State, string>({ id: 'draft-review', nodes: [{
id: 'reply', description: '草拟后复核', startsRound: true,
async run(state, { model }) {
const draft = await model.invoke({ input: state.input, modelId: 'quick' });
const reviewed = await model.invoke({ input: '复核这份草稿:' + draft.value, modelId: 'careful' });
return { answer: reviewed.value };
}, routes: [route.deliver('answer')],
}] });
const agent = new AgentCoreEngine({ workflow, model: 'quick', maxModelCalls: 2,
models: { quick: { provider }, careful: { provider } },
});
console.log(await agent.run({ input: '解释服务状态' }));js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const provider = {
async *stream(messages) {
yield '收到:' + messages.at(-1).content;
},
};
const workflow = defineWorkflow({ id: 'draft-review', nodes: [{
id: 'reply', description: '草拟后复核', startsRound: true,
async run(state, { model }) {
const draft = await model.invoke({ input: state.input, modelId: 'quick' });
const reviewed = await model.invoke({ input: '复核这份草稿:' + draft.value, modelId: 'careful' });
return { answer: reviewed.value };
}, routes: [route.deliver('answer')],
}] });
const agent = new AgentCoreEngine({ workflow, model: 'quick', maxModelCalls: 2,
models: { quick: { provider }, careful: { provider } },
});
console.log(await agent.run({ input: '解释服务状态' }));