Skip to main content

Module builder

Module builder 

Source
Expand description

AgentBuilder — Agent 链式构建器。

提供推荐的入口 API,一步构建 Graph<AgentState>

§两层 API

use lellm_agent::{AgentBuilder, ToolUseLoop};
use lellm_core::Prompt;

// DSL 层 — build() 返回 Graph<AgentState>,可嵌入更大编排
let graph = AgentBuilder::new(model)
    .system("你是一个有帮助的助手。")
    .tool(search_tool)
    .tool(weather_tool)
    .max_iterations(20)
    .build();

// 分层构建 — 最大化前缀缓存
let graph = AgentBuilder::new(model)
    .system(
        Prompt::new()
            .stable("核心身份…")
            .stable("工具指南…")
            .dynamic("会话上下文…")
            .build(),
    )
    .build();

// Facade 层 — compile() 返回 ToolUseLoop,提供 invoke() 便捷 API
let result = AgentBuilder::new(model)
    .tools([search_tool, weather_tool])
    .compile()
    .invoke(messages)
    .await?;

// 手动组合 — 等价于 compile()
let graph = AgentBuilder::new(model).tools([...]).build();
let agent = ToolUseLoop::new(graph, config);

Structs§

AgentBuilder
Agent 链式构建器 — 推荐的 Agent 创建方式。