An open API service indexing awesome lists of open source software.

https://github.com/kingfs/godify

Unofficial Dify Golang SDK, all APIs encapsulated, just for Golang developers.
https://github.com/kingfs/godify

dify golang sdk-go

Last synced: 9 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# Dify Golang SDK

[![Go Report Card](https://goreportcard.com/badge/github.com/kingfs/godify)](https://goreportcard.com/report/github.com/kingfs/godify)
[![Test Coverage](https://img.shields.io/badge/test%20coverage-25%25-red)](https://github.com/kingfs/godify)
[![Go Version](https://img.shields.io/badge/go-1.21+-blue.svg)](https://golang.org)
[![License](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)
[![en](https://img.shields.io/badge/lang-en-blue.svg)](README.md)
[![zh-CN](https://img.shields.io/badge/lang-zh--CN-red.svg)](README.zh-CN.md)

[简体中文](README.md) | [English](README_EN.md)

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 文档和丰富示例

## 📦 安装

```bash
go get github.com/kingfs/godify
```

## 🚀 快速开始

### Service API - 开发者集成

```go
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 - 最终用户交互

```go
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 - 管理员功能

```go
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)
}
```

### 工作流执行

```go
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

## 🛠 开发工具

```bash
# 安装依赖
make deps

# 运行测试
make test

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

# 代码格式化
make format

# 运行示例
make examples

# 查看所有命令
make help
```

## 📖 文档

- [完整API文档](./docs/API.md) - 详细的API参考
- [使用示例](./examples/) - 丰富的代码示例
- [FAQ常见问题](./docs/FAQ.md) - 常见问题解答
- [英文文档](./docs/README_EN.md) - English documentation
- [更新日志](./CHANGELOG.md) - 版本变更记录

## 🔧 高级功能

### 错误处理

```go
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":
// 处理配额超限
}
}
}
```

### 文件上传

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

### 流式响应

```go
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](https://github.com/kingfs/godify/issues)
2. 💡 **功能建议** - [功能请求](https://github.com/kingfs/godify/issues)
3. 🔧 **代码贡献** - [Pull Request](https://github.com/kingfs/godify/pulls)
4. 📖 **文档改进** - 文档和示例更新

## 📄 许可证

MIT License - 详见 [LICENSE](LICENSE) 文件

## 🔗 相关链接

- [Dify 官网](https://dify.ai)
- [Dify GitHub](https://github.com/langgenius/dify)
- [API 文档](https://docs.dify.ai/api)

---

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