Dapr Agent SDK for Go
用于构建Dapr代理的Go SDK——持久、LLM驱动的自主应用程序,具有工作流支持的执行和MCP(模型上下文协议)支持。
特性
- LLM集成:通过Dapr Conversation API(OpenAI、Anthropic等)连接到任何LLM
- 持久执行:具有自动状态持久性的工作流支持执行
- 工具系统:内置Dapr集成的可扩展工具框架
- 多代理模式:链式、并行、路由器、监控器和协作模式
- MCP服务器:用于代理暴露的内置模型上下文协议服务器
- 演员基金会:基于Dapr虚拟Actor构建,实现可靠的状态管理
安装
go get github.com/gftdcojp/dapr-agents-go快速开始
package main
import (
"context"
"log"
"time"
agent "github.com/gftdcojp/dapr-agents-go"
)
func main() {
// Create agent configuration
config := &agent.AgentConfig{
Name: "MyAgent",
LLMComponent: "llm",
LLMModel: "gpt-4-turbo",
SystemPrompt: "You are a helpful assistant.",
}
// Create and configure the agent
myAgent := agent.NewBaseAgent(config)
// Register tools
myAgent.RegisterTool(agent.NewToolBuilder("greet").
Description("Greet a user by name").
AddStringParam("name", "The name to greet", true).
BuildFunc(func(ctx context.Context, params map[string]interface{}) (interface{}, error) {
name := params["name"].(string)
return map[string]string{"message": "Hello, " + name + "!"}, nil
}))
// Run the agent server
if err := agent.RunAgent(config, myAgent.GetTools()...); err != nil {
log.Fatal(err)
}
}代理配置
type AgentConfig struct {
Name string // Agent name (actor type)
LLMComponent string // Dapr conversation component
LLMModel string // LLM model name
MemoryStore string // State store for memory
WorkflowStore string // State store for workflow state
MaxSteps int // Max steps per run
StepTimeout time.Duration // Timeout per step
MemoryMaxTokens int // Max tokens in memory
SystemPrompt string // System prompt for LLM
}工具
内置工具类型
- FuncTool -将任何Go函数打包为工具
- HTTP工具 -调用HTTP端点
- DaprServiceTool -调用Dapr服务
- DaprActorTool -调用Dapr参与者
- DaprStateTool -读/写Dapr状态
- DaprPubSubTool -发布到Dapr发布/订阅
创建工具
// Using the builder pattern
tool := agent.NewToolBuilder("search").
Description("Search the web").
AddStringParam("query", "Search query", true).
AddNumberParam("limit", "Max results", false).
BuildFunc(searchHandler)
// Or implement the Tool interface directly
type MyTool struct{}
func (t *MyTool) Name() string { return "my_tool" }
func (t *MyTool) Description() string { return "My custom tool" }
func (t *MyTool) Schema() *agent.ToolSchema { return schema }
func (t *MyTool) Execute(ctx context.Context, params map[string]interface{}) (interface{}, error) {
// Implementation
}多代理模式
链模式
按顺序执行代理,将输出传递给下一个:
chain, _ := agent.NewChainPattern(
agent.AgentRef{Type: "Researcher", ID: "1"},
agent.AgentRef{Type: "Writer", ID: "1"},
agent.AgentRef{Type: "Editor", ID: "1"},
)
output, _ := chain.Execute(ctx, &agent.MultiAgentInput{
Prompt: "Write an article about AI agents",
})相似模式
同时执行代理并聚合结果:
parallel, _ := agent.NewParallelPattern(
[]agent.AgentRef{
{Type: "NewsAgent", ID: "1"},
{Type: "WeatherAgent", ID: "1"},
{Type: "StockAgent", ID: "1"},
},
aggregator, // Custom aggregation function
)路由器模式
根据标准将请求路由到不同的代理:
router, _ := agent.NewRouterPattern(
agents,
func(input *agent.MultiAgentInput) agent.AgentRef {
// Routing logic
if strings.Contains(input.Prompt, "weather") {
return agents["weather"]
}
return agents["general"]
},
)主管模式
主管代理人委托给工人代理人:
supervisor, _ := agent.NewSupervisorPattern(
agent.AgentRef{Type: "Supervisor", ID: "1"},
agent.AgentRef{Type: "Worker1", ID: "1"},
agent.AgentRef{Type: "Worker2", ID: "1"},
)MCP服务器
通过模型上下文协议公开您的代理:
mcpServer := agent.NewMCPServer(&agent.MCPServerConfig{
Port: 8081,
Name: "my-agent-mcp",
Version: "1.0.0",
Description: "My Agent MCP Server",
AuthType: "apikey",
APIKey: os.Getenv("MCP_API_KEY"),
})
mcpServer.RegisterAgent("MyAgent", agentFactory)
mcpServer.RegisterTool(myTool)
go mcpServer.Start()MCP端点
GET /mcp/v1/info-服务器信息GET /mcp/v1/tools-列出可用工具POST /mcp/v1/tools/{name}-执行工具GET /mcp/v1/prompts-列出可用提示GET /mcp/v1/resources-列出资源GET /mcp/v1/agents-列出代理POST /mcp/v1/agents/{name}-运行代理POST /mcp/v1/sse-服务器已发送流媒体事件
使用GFTD进行部署
创建 gftd.json 配置:
{
"$schema": "https://gftd.ai/schemas/gftd.json",
"name": "my-agent",
"type": "agent",
"dapr": {
"enabled": true,
"appId": "my-agent",
"appProtocol": "grpc",
"appPort": 50051,
"agents": {
"types": ["MyAgent"],
"llmComponent": "llm",
"llmModel": "gpt-4-turbo",
"mcp": {
"enabled": true,
"port": 8081
}
}
}
}使用以下方式部署:
gftd deploy --daprDapr组件
代理所需的Dapr组件:
LLM组件(对话API)
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: llm
spec:
type: conversation.openai
metadata:
- name: key
secretKeyRef:
name: openai-secret
key: api-key
- name: model
value: "gpt-4-turbo"州立存储器
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: conversation-statestore
spec:
type: state.redis
metadata:
- name: redisHost
value: redis:6379
- name: actorStateStore
value: "true"工作流状态存储
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: workflow-statestore
spec:
type: state.redis
metadata:
- name: redisHost
value: redis:6379
- name: actorStateStore
value: "true"api参考
看 API文档 供完整参考。
例子
许可证
Apache许可证2.0-请参阅 许可证 了解详情。
这个项目是一个Go实现,灵感来自 dapr/dapr代理.
贡献
欢迎投稿!请阅读 贡献.md 第一。
