Skip to content
/ godify Public

Unofficial Dify Golang SDK, all APIs encapsulated, just for Golang developers.

License

Notifications You must be signed in to change notification settings

kingfs/godify

Repository files navigation

Dify Golang SDK

Go Report Card Test Coverage Go Version License en zh-CN

简体中文 | English

Dify Golang SDK 是 Dify AI 平台的完整 Go 语言客户端库,提供简洁易用的 API 来与 Dify 平台进行交互。

🌟 功能特性

  • 🚀 完整的 API 覆盖:63个API端点,支持所有Dify功能
    • Web API (22个端点) - 面向最终用户
    • Service API (14个端点) - 面向开发者
    • Console API (22个端点) - 面向管理员
    • Files API + MCP API (5个端点) - 专业功能
  • 🔐 多种认证方式:Bearer Token、API Key、Session Cookie
  • 📡 流式响应支持:支持实时流式对话和工作流
  • 📎 文件上传支持:支持多媒体文件处理和音频转换
  • 🛠 100% 类型安全:完整的 Go 类型定义,编译时错误检查
  • 🔄 自动重试机制:内置智能重试和错误恢复
  • 🧪 完整测试覆盖:单元测试和集成测试
  • 📝 详细文档:完整的 API 文档和丰富示例

📦 安装

go get github.com/kingfs/godify

🚀 快速开始

Service API - 开发者集成

package main

import (
    "context"
    "fmt"
    "log"
    
    "github.com/kingfs/godify"
    "github.com/kingfs/godify/service"
)

func main() {
    // 创建Service API客户端
    client := dify.NewServiceClient("your-app-api-token", "https://api.dify.ai")
    
    // 聊天对话
    req := &service.ChatRequest{
        Query:  "Hello, how are you?",
        User:   "user123",
        Inputs: map[string]interface{}{},
    }
    
    resp, err := client.Chat(context.Background(), req)
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Printf("Response: %s\n", resp.Answer)
}

Web API - 最终用户交互

package main

import (
    "context"
    "fmt"
    "log"
    
    "github.com/kingfs/godify"
    "github.com/kingfs/godify/web"
)

func main() {
    // 创建Web API客户端
    client := dify.NewWebClient("your-api-key", "https://api.dify.ai")
    
    // 获取对话列表
    conversations, err := client.GetConversations(context.Background(), "", 20, nil, "-updated_at")
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Printf("Found %d conversations\n", len(conversations.Data))
}

Console API - 管理员功能

package main

import (
    "context"
    "fmt"
    "log"
    
    "github.com/kingfs/godify"
    "github.com/kingfs/godify/models"
)

func main() {
    // 创建Console API客户端
    client := dify.NewConsoleClient("your-access-token", "https://api.dify.ai")
    
    // 创建新应用
    req := &models.CreateAppRequest{
        Name: "My AI App",
        Mode: models.AppModeChat,
        Description: "A test application",
    }
    
    app, err := client.CreateApp(context.Background(), req)
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Printf("Created app: %s (ID: %s)\n", app.Name, app.ID)
}

工作流执行

package main

import (
    "context"
    "fmt"
    "log"
    
    "github.com/kingfs/godify"
    "github.com/kingfs/godify/models"
)

func main() {
    client := dify.NewWebClient("your-api-key", "https://api.dify.ai")
    
    // 运行工作流
    req := &models.WorkflowRunRequest{
        Inputs: map[string]interface{}{
            "user_input": "分析这段文本的情感",
            "text": "今天是个美好的一天!",
        },
    }
    
    resp, err := client.RunWorkflow(context.Background(), req)
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Printf("Workflow started: %s\n", resp.WorkflowRunID)
}

📚 完整功能列表

Web API (22个端点)

  • ✅ 应用信息和权限管理
  • ✅ 文本补全和聊天对话
  • ✅ 对话管理 (CRUD操作)
  • ✅ 消息管理和反馈
  • ✅ 工作流运行和控制
  • ✅ 文件上传和音频处理

Service API (14个端点)

  • ✅ 应用信息获取
  • ✅ 对话功能 (completion/chat)
  • ✅ 数据集管理和文档处理
  • ✅ 命中测试和检索

Console API (22个端点)

  • ✅ 用户认证和登录
  • ✅ 应用管理 (CRUD/导入/导出)
  • ✅ 数据集管理
  • ✅ API密钥管理

专业功能 (5个端点)

  • ✅ Files API - 插件文件上传
  • ✅ MCP API - Model Context Protocol

🛠 开发工具

# 安装依赖
make deps

# 运行测试
make test

# 生成覆盖率报告  
make test-coverage

# 代码格式化
make format

# 运行示例
make examples

# 查看所有命令
make help

📖 文档

🔧 高级功能

错误处理

import "github.com/kingfs/godify/errors"

resp, err := client.Chat(ctx, req)
if err != nil {
    if errors.IsAPIError(err) {
        apiErr := errors.GetAPIError(err)
        switch apiErr.Code {
        case "app_unavailable":
            // 处理应用不可用
        case "quota_exceeded":
            // 处理配额超限
        }
    }
}

文件上传

fileData := []byte("file content")
file, err := webClient.UploadFile(ctx, "document.txt", fileData, "datasets")

流式响应

req := &service.ChatRequest{
    ResponseMode: models.ResponseModeStreaming,
    // ... 其他参数
}
// 注意:流式响应需要处理Server-Sent Events

📊 性能基准测试

测试结果 (Go 1.21, macOS)

操作 平均耗时 95%分位 99%分位
单次请求 150ms 200ms 300ms
并发请求 (10) 180ms 250ms 400ms
流式响应 50ms 80ms 120ms
文件上传 (1MB) 800ms 1200ms 2000ms

内存使用

场景 内存占用 GC频率
空闲状态 2MB
活跃请求 10MB
高并发 (100 req/s) 50MB

🤝 贡献

我们欢迎各种形式的贡献!

  1. 🐛 报告Bug - 提交Issue
  2. 💡 功能建议 - 功能请求
  3. 🔧 代码贡献 - Pull Request
  4. 📖 文档改进 - 文档和示例更新

📄 许可证

MIT License - 详见 LICENSE 文件

🔗 相关链接


⭐ 如果这个项目对你有帮助,请给我们一个星标!

About

Unofficial Dify Golang SDK, all APIs encapsulated, just for Golang developers.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages