星阵围棋手机版
64.59MB · 2025-11-21
Agent Development Kit (ADK) 是一个灵活、模块化的框架,将软件开发原则应用于AI智能体创建。它旨在简化从简单任务到复杂系统的代理工作流程的构建、部署和编排。虽然针对Gemini进行了优化,但ADK是模型无关、部署无关的,并与其他框架兼容。
要将ADK Go添加到您的项目中,请运行:
go get google.golang.org/adk
package main
import (
"context"
"iter"
"google.golang.org/adk/agent"
"google.golang.org/adk/session"
)
// 自定义代理实现
type CustomAgent struct {
name string
description string
subAgents []agent.Agent
}
func (a *CustomAgent) Name() string { return a.name }
func (a *CustomAgent) Description() string { return a.description }
func (a *CustomAgent) SubAgents() []agent.Agent { return a.subAgents }
func (a *CustomAgent) Run(ctx agent.InvocationContext) iter.Seq2[*session.Event, error] {
return func(yield func(*session.Event, error) bool) {
// 代理执行逻辑
event := &session.Event{
Author: a.name,
// 设置其他事件属性
}
yield(event, nil)
}
}
func main() {
// 创建自定义代理
customAgent := &CustomAgent{
name: "MyAgent",
description: "我的自定义AI代理",
}
// 使用代理加载器
loader := agent.NewSingleLoader(customAgent)
// 运行代理逻辑...
}
package main
import (
"google.golang.org/adk/agent/llmagent"
"google.golang.org/adk/model"
"google.golang.org/genai"
)
func createLLMAgent() (agent.Agent, error) {
cfg := llmagent.Config{
AgentConfig: agent.Config{
Name: "聊天代理",
Description: "处理用户对话的LLM代理",
},
Model: &model.LLM{
// 配置LLM模型
},
Instruction: "你是一个有用的助手,用中文回答用户问题。",
}
return llmagent.New(cfg)
}
package main
import (
"google.golang.org/adk/agent/workflowagents/loopagent"
"google.golang.org/adk/agent/workflowagents/parallelagent"
"google.golang.org/adk/agent/workflowagents/sequentialagent"
)
func createWorkflowAgents() {
// 顺序代理 - 按固定顺序执行子代理
seqCfg := sequentialagent.Config{
AgentConfig: agent.Config{
Name: "顺序工作流",
SubAgents: []agent.Agent{/* 子代理列表 */},
},
}
sequentialAgent, _ := sequentialagent.New(seqCfg)
// 并行代理 - 同时运行子代理
parallelCfg := parallelagent.Config{
AgentConfig: agent.Config{
Name: "并行工作流",
SubAgents: []agent.Agent{/* 子代理列表 */},
},
}
parallelAgent, _ := parallelagent.New(parallelCfg)
// 循环代理 - 重复执行直到条件满足
loopCfg := loopagent.Config{
AgentConfig: agent.Config{
Name: "循环工作流",
SubAgents: []agent.Agent{/* 子代理列表 */},
},
MaxIterations: 5, // 最大迭代次数
}
loopAgent, _ := loopagent.New(loopCfg)
}
// Agent是所有代理必须实现的基础接口
type Agent interface {
Name() string
Description() string
Run(InvocationContext) iter.Seq2[*session.Event, error]
SubAgents() []Agent
internal() *agent
}
// 调用上下文提供代理执行环境
type InvocationContext interface {
context.Context
Agent() Agent
Artifacts() Artifacts
Memory() Memory
Session() session.Session
InvocationID() string
Branch() string
UserContent() *genai.Content
RunConfig() *RunConfig
EndInvocation()
Ended() bool
}
// 会话服务接口
type Service interface {
Create(context.Context, *CreateRequest) (*CreateResponse, error)
Get(context.Context, *GetRequest) (*GetResponse, error)
List(context.Context, *ListRequest) (*ListResponse, error)
Delete(context.Context, *DeleteRequest) error
AppendEvent(context.Context, Session, *Event) error
}
// 会话表示用户与代理的交互会话
type Session interface {
AppName() string
UserID() string
ID() string
State() State
Events() Events
LastUpdateTime() time.Time
}
// 工具接口定义代理可用的功能
type Tool interface {
Name() string
Description() string
Run(ctx Context, args any) (result map[string]any, err error)
}
// 工具上下文提供工具执行环境
type Context interface {
context.Context
Artifacts() Artifacts
FunctionCallID() string
Actions() *EventActions
AgentName() string
SearchMemory(ctx context.Context, query string) (*memory.SearchResponse, error)
}
// 运行器配置
type Config struct {
AppName string
Agent Agent // 启动执行的根代理
SessionService session.Service
ArtifactService artifact.Service // 可选
MemoryService memory.Service // 可选
}
// 运行器执行代理工作流
type Runner struct {
appName string
rootAgent Agent
sessionService session.Service
artifactService artifact.Service
memoryService memory.Service
parents parentmap.Map
}
func (r *Runner) Run(ctx context.Context, session session.Session, userContent *genai.Content, runConfig *agent.RunConfig) iter.Seq2[*session.Event, error] {
// 实现代理执行流水线
// 包括会话管理、记忆检索、工具执行等
}
这个框架提供了完整的AI代理开发基础设施,支持复杂的多代理工作流、工具集成和云原生部署,是构建企业级AI应用的强大工具。