Community程式設計與開發github.com

SystemTce/dify-skills

Dify 參考資料 - DSL 語法、API 文件、SDK 參考、CLI 指令

dify-skills 是什麼?

這個技能 (systemTce/dify-skills) 提供了 Dify 平台的完整技術參考資料,涵蓋工作流程 DSL 語法、API 端點、Python SDK 以及 CLI 指令。它專為開發者與 AI 代理設計,能快速查詢 Dify 的技術細節,例如如何撰寫工作流程 YAML、呼叫聊天或工作流程 API、使用 Python SDK 進行流式回應,以及透過 CLI 管理插件與環境變數。技能內容結構化,包含快速導覽、常見問題與相關資源連結,支援所有主要的 Dify 互動層面。主要平台為 AI 代理與開發人員工具。其運作方式是透過結構化的 SKILL.md 文件,讓 AI 代理能直接參考文件中的語法、範例與錯誤碼,從而正確回答使用者關於 Dify 開發的提問。

相容平台✓Claude Code✓Codex CLI~Cursor
npx skills add https://github.com/SystemTce/dify-skills/tree/main/06-reference

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

Dify 參考資料 SKILL

查詢語法、API、CLI 等參考資訊

快速導覽


DSL 語法參考

工作流程 DSL 結構

version: 0.0.1
graph:
  nodes:
    - id: node_id
      data:
        type: node_type
        # 節點配置
  edges:
    - source: source_node_id
      target: target_node_id

節點類型

類型說明必填欄位
start開始節點variables
llmLLM 節點model, prompt_template
agentAgent 節點agent_parameters
knowledge-retrieval知識檢索dataset_ids, query_variable_selector
http-requestHTTP 請求url, method, headers
code程式碼執行code_type, code
tool工具節點provider_type, tool_name
if-else條件分支conditions
iteration疊代iterator_variable_name
loop迴圈max_loop_count
answer回應節點answer

變數引用

# 系統變數
{{#sys.query#}}           # 使用者查詢
{{#sys.files#}}           # 上傳的檔案
{{#sys.conversation_id#}} # 對話 ID

# 節點輸出
{{#node_id.output_field#}}

# 陣列存取
{{#items[0]#}}           # 陣列第一個元素
{{#items[-1]#}}          # 陣列最後一個元素

# 物件屬性
{{#object.field#}}       # 物件屬性

完整範例

workflow:
  graph:
    nodes:
      - id: start
        data:
          type: start
          variables:
            - name: query
              type: text
              required: true

      - id: classify
        data:
          type: question-classifier
          instruction: "分類使用者問題"
          categories:
            - name: technical
              description: 技術問題
            - name: general
              description: 一般問題

      - id: llm
        data:
          type: llm
          model:
            provider: openai
            name: gpt-4
          prompt_template:
            - role: system
              text: "你是個助手"
            - role: user
              text: "{{#start.query#}}"
          context:
            enabled: false

      - id: answer
        data:
          type: answer
          answer: "{{#llm.text#}}"

    edges:
      - source: start
        target: classify
      - source: classify
        target: llm
        source_handle: general
      - source: llm
        target: answer

API 參考

認證

所有 API 請求需要在 Header 中攜帶 API Key:

Authorization: Bearer YOUR_API_KEY

端點概覽

應用 API

端點方法說明
/v1/chat-messagesPOST傳送聊天訊息
/v1/completionsPOST文字補全
/v1/parametersGET取得應用參數
/v1/file-uploadPOST檔案上傳

工作流程 API

端點方法說明
/v1/workflows/runPOST執行工作流程
/v1/workflows/runGET取得執行狀態
/v1/workflows/tasksGET取得任務列表

資料集 API

端點方法說明
/v1/datasetsGET列出資料集
/v1/datasetsPOST建立資料集
/v1/datasets/{id}/documentsGET列出文件
/v1/datasets/{id}/retrievePOST檢索知識

請求/回應格式

聊天訊息請求

{
  "query": "使用者查詢",
  "user": "user_123",
  "conversation_id": "可選的對話ID",
  "response_mode": "blocking | streaming",
  "files": [
    {
      "type": "image",
      "transfer_method": "remote_url",
      "url": "https://example.com/image.jpg"
    }
  ],
  "retrieval_model": {
    "query": "{{#sys.query#}}",
    "top_k": 3
  }
}

聊天訊息回應

{
  "message_id": "msg_xxx",
  "conversation_id": "conv_xxx",
  "answer": "AI 回覆內容",
  "metadata": {
    "usage": {
      "prompt_tokens": 100,
      "completion_tokens": 50,
      "total_tokens": 150
    },
    "retrieval_resources": [
      {
        "position": 1,
        "document_id": "doc_xxx",
        "content": "相關文件內容..."
      }
    ]
  }
}

錯誤回應

{
  "code": "invalid_api_key",
  "message": "Invalid API key",
  "status_code": 401
}

常見錯誤碼:

錯誤碼HTTP 狀態碼說明
invalid_api_key401API Key 無效
invalid_parameter400參數錯誤
quota_exceeded403配額超出
rate_limit_exceeded429速率限制
internal_server_error500伺服器錯誤

SDK 參考

Python SDK

安裝

pip install dify-client

基本用法

from dify_client import DifyClient

# 初始化客戶端
client = DifyClient(
    api_key="app-xxx",
    base_url="https://api.dify.ai/v1"
)

# 傳送聊天訊息
response = client.chat.messages.create(
    query="你好",
    user="user_123"
)
print(response.answer)

串流回應

from dify_client import DifyClient

client = DifyClient(api_key="app-xxx")

# 串流聊天
for event in client.chat.messages.stream(
    query="講個故事",
    user="user_123"
):
    if event.event == "message":
        print(event.data.answer, end="")

工作流程執行

# 執行工作流程
response = client.workflows.run(
    inputs={"query": "測試"},
    user="user_123",
    response_mode="blocking"
)
print(response.data.output)

SDK 類別參考

DifyClient

class DifyClient:
    def __init__(self, api_key: str, base_url: str = "https://api.dify.ai/v1"):
        """初始化客戶端"""
        pass

    @property
    def chat(self) -> ChatClient:
        """聊天客戶端"""
        pass

    @property
    def workflows(self) -> WorkflowClient:
        """工作流程客戶端"""
        pass

    @property
    def datasets(self) -> DatasetClient:
        """資料集客戶端"""
        pass

ChatClient

class ChatClient:
    def create(self, query: str, user: str, **kwargs) -> ChatMessage:
        """傳送聊天訊息(阻塞)"""
        pass

    def stream(self, query: str, user: str, **kwargs) -> Iterator[ChatEvent]:
        """傳送聊天訊息(串流)"""
        pass

CLI 參考

安裝

pip install dify-cli

指令概覽

外掛指令

# 初始化外掛
dify plugin init [options]

# 執行外掛
dify plugin run [options]

# 打包外掛
dify plugin package [options]

# 發佈外掛
dify plugin publish [options]

# 列出已安裝外掛
dify plugin list

工作流程指令

# 匯出工作流程
dify workflow export <workflow_id> [options]

# 匯入工作流程
dify workflow import <file> [options]

# 驗證工作流程
dify workflow validate <file>

# 執行工作流程
dify workflow run <workflow_id> --inputs '{}'

應用程式指令

# 列出應用程式
dify app list

# 取得應用程式資訊
dify app info <app_id>

# 部署應用程式
dify app deploy <app_id>

環境變數指令

# 列出環境變數
dify env list

# 設定環境變數
dify env set <key> <value>

# 取得環境變數
dify env get <key>

# 刪除環境變數
dify env delete <key>

選項說明

選項簡寫說明預設值
--api-key-kAPI Key環境變數 DIFY_API_KEY
--base-url-b基礎 URLhttps://api.dify.ai/v1
--output-o輸出格式json
--quiet-q靜默模式false
--debug-d除錯模式false

使用範例

# 設定 API Key
export DIFY_API_KEY="app-xxx"

# 初始化外掛專案
dify plugin init my-plugin --template tool

# 執行外掛
dify plugin run --port 5000

# 匯出工作流程
dify workflow export wf_xxx -o workflow.yaml

# 匯入工作流程
dify workflow import workflow.yaml

常見問題

API 相關

Q: 如何取得 API Key? A: 在 Dify 控制台 -> 設定 -> API 中建立。

Q: 請求頻率限制是多少? A: 預設 60 請求/分鐘,可申請提升。

Q: 如何處理串流回應? A: 使用 response_mode=streaming,解析 SSE 事件。

工作流程相關

Q: 如何引用變數? A: 使用 {{#node_id.field#}} 語法。

Q: 支援哪些節點類型? A: 詳見節點類型表格。

Q: 如何處理迴圈? A: 使用 iteration 或 loop 節點。

外掛相關

Q: 如何開發自訂外掛? A: 參考 02-plugin SKILL。

Q: 外掛如何取得憑證? A: 使用 self.runtime_credentials。


相關 SKILL


相關資源


版本資訊: v1.0.0 | 最後更新: 2026-03-05

Individual skills in this repo

This repo contains 5 individual skills — each has its own dedicated page.

相關技能