ling-baseling-base

Agent

Agent 接口与 LLM/Graph/Chain/Parallel 实现

在线 Playground

在浏览器中直接体验本页相关 API,无需本地安装 Go 环境。

Agent

agentkit/agent 定义所有 Agent 必须实现的 Agent 接口;常用实现包括 llmagentgraphagentchainagentparallelagentteam 等。

Agent 接口

type Agent interface {
    Run(ctx context.Context, invocation *Invocation) (<-chan *event.Event, error)
    Tools() []tool.Tool
    Info() Info
    SubAgents() []Agent
    FindSubAgent(name string) (Agent, bool)
}

Invocation 携带用户消息、会话状态、工具上下文等;Run 返回事件流(文本增量、工具调用、转移等)。

LLM Agent(最常用)

import (
    "github.com/LingByte/ling-base/agentkit/agent/llmagent"
    "github.com/LingByte/ling-base/agentkit/relaymodel"
    compat "github.com/LingByte/ling-base/relay/compat"
)

model := relaymodel.New("gpt-4o",
    relaymodel.WithAPIKey("sk-xxx"),
    relaymodel.WithChannel(relaymodel.ChannelOpenAI),
)

ag, err := llmagent.New("assistant",
    llmagent.WithModel(model),
    llmagent.WithInstruction("你是专业 Go 开发助手。"),
    llmagent.WithDescription("帮助用户编写和审查 Go 代码"),
    llmagent.WithGenerationConfig(compat.GenerationConfig{
        Temperature: 0.7,
        MaxTokens:   4096,
    }),
    llmagent.WithMaxToolIterations(10),
)

常用 Option

Option说明
WithModel主 LLM(compat.Model
WithModels / WithModelSelector多模型切换
WithInstruction / WithGlobalInstruction系统提示词
WithTools / WithToolSets注册工具
WithSubAgents子 Agent 委托
WithPlanner注入规划器(ReAct 等)
WithCodeExecutor代码执行后端
WithOutputSchema结构化 JSON 输出
WithSkillsSKILL.md 技能仓库

Graph Agent

graph.StateGraph 包装为 Agent,适合多步骤工作流:

import (
    "github.com/LingByte/ling-base/agentkit/agent/graphagent"
    "github.com/LingByte/ling-base/agentkit/graph"
)

sg := graph.NewStateGraph[MyState]()
// sg.AddNode(...).AddEdge(...)
ga, _ := graphagent.New("workflow", graphagent.WithGraph(sg))

Chain / Parallel / Cycle

模式
agent/chainagent顺序执行多个 Agent
agent/parallelagent并行执行后合并
agent/cycleagent循环直到条件满足

Team 多 Agent

import "github.com/LingByte/ling-base/agentkit/team"

t := team.NewCoordinatorTeam("support",
    team.WithMembers(agentA, agentB),
    team.WithCoordinatorModel(model),
)

支持 coordinator(协调者分配任务)与 swarmtransfer_to_agent 自由转移)两种模式。

外部平台集成

平台
agent/a2aagentAgent-to-Agent 协议
agent/difyDify 工作流
agent/n8nn8n 工作流
agent/claudecodeClaude Code
agent/codexCodex 风格

停止与错误

// Agent 主动停止
return agent.NewStopError("task completed")

// 判断停止错误
if stopErr, ok := agent.AsStopError(err); ok {
    fmt.Println(stopErr.Message)
}

On this page