Featured image of post Uploading Files to Dify in Python and Calling Dify API Workflows

Uploading Files to Dify in Python and Calling Dify API Workflows

📌 Introduction

這邊記錄我在 Python 呼叫 Dify API ,並使用 Dify workflows 的上傳檔案功能。

同時也算是踩坑的紀錄,因為官方的 API 文件有寫要怎麼使用,但是沒有實際的範例可以參考。

底下是使用的 Note 是使用 Python 的參考做法,要使用 curl 的可以直接在官方的文件中查看。

This document records my experience calling the Dify API with Python and using the file upload feature in Dify workflows.

It also serves as a record of challenges encountered, as the official API documentation explains how to use it, but there are no practical examples for reference.

The notes below provide a Python-based example; if you prefer to use curl, you can refer to the official documentation.

🗒️ Notes

1. 用 API 將本機的檔案上傳到 Dify 平台

底下第一行的 api_key 要修改成自己的,然後每一個「專案」的 API Key 都不一樣。

第二行的 file_path 要改成電腦上檔案的相對或絕對路徑。

In the first line below, replace api_key with your own; each project has a unique API Key.

The second line, file_path, should be changed to the relative or absolute path of the file on your computer.

api_key = "..."
file_path = "/home/user/test.txt"

def upload_file(file_path):
    with open(file_path, "rb") as f:
        file_name = f.name.split("/")[-1]
        data = {"user": "abc-123"}

        response = requests.post(
            "https://api.dify.ai/v1/files/upload",
            headers = {
                "Authorization": f"Bearer {api_key}"
            },
            files = {
                'file': (file_name, f, 'document/txt'),
            },
            data = data
        )
        return response.json()
    
document_id = upload_file(file_path)
document_id = document_id.get("id")
print(document_id)

2. 使用上傳的檔案作為 LLM 的輸入

下圖是在 Dify 上面拉的流程圖並使用 LLM 的範例。

The following is an example of a workflow diagram created in Dify using an LLM.

workflow

下面的第十行 InputFile 名字要跟在 Dify workflow 「開始」節點裡面自己設定的相同。

最後一個節點「結束」裡面設定的「變量名」會影響 API 回傳的 JSON 內容的名稱,就是底下最後一行的 ouputs 裡面的內容。

In line 10, the name InputFile should match what you set in the “Start” node of the Dify workflow.

The “variable name” configured in the last “End” node affects the name of the API response’s JSON content, as shown in the last line under outputs.

document_id is the UUID (?) obtained from the upload step.

document_id 是前面步驟上傳後得到的 UUID(?)。

def dify_workflow_api(document_id):
    response = requests.post(
        "https://api.dify.ai/v1/workflows/run",
        headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json"
        },
        data=json.dumps({
            "inputs": {
                "InputFile": {
                    "type": "document",
                    "transfer_method": "local_file",
                    "upload_file_id": f"{document_id}"
                }
            },
            "response_mode": "blocking",
            "user": "abc-123"
        })
    )

    return response.json()

result = dify_workflow_api(document_id)
print(result.get("data").get("outputs"))

📖 Ref.

以下資訊有參考,但不多。

https://docs.dify.ai/guides/application-publishing/developing-with-apis

 

Licensed under CC BY-NC-SA 4.0
Last updated on Nov 02, 2024 11:10 CST