mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-12 04:29:07 +08:00
fix: remove unnecessary curly braces in wf api doc (#11658)
This commit is contained in:
parent
22258fb0bf
commit
8ecb9aaa91
@ -238,84 +238,81 @@ Workflow applications offers non-session support and is ideal for translation, a
|
|||||||
</CodeGroup>
|
</CodeGroup>
|
||||||
<CodeGroup title="File upload sample code">
|
<CodeGroup title="File upload sample code">
|
||||||
```json {{ title: 'File upload sample code' }}
|
```json {{ title: 'File upload sample code' }}
|
||||||
{
|
import requests
|
||||||
import requests
|
import json
|
||||||
import json
|
|
||||||
|
|
||||||
def upload_file(file_path, user):
|
def upload_file(file_path, user):
|
||||||
upload_url = "https://api.dify.ai/v1/files/upload"
|
upload_url = "https://api.dify.ai/v1/files/upload"
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": "Bearer app-xxxxxxxx",
|
"Authorization": "Bearer app-xxxxxxxx",
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print("Upload file...")
|
print("Upload file...")
|
||||||
with open(file_path, 'rb') as file:
|
with open(file_path, 'rb') as file:
|
||||||
files = {
|
files = {
|
||||||
'file': (file_path, file, 'text/plain') # Make sure the file is uploaded with the appropriate MIME type
|
'file': (file_path, file, 'text/plain') # Make sure the file is uploaded with the appropriate MIME type
|
||||||
}
|
}
|
||||||
data = {
|
data = {
|
||||||
"user": user,
|
"user": user,
|
||||||
"type": "TXT" # Set the file type to TXT
|
"type": "TXT" # Set the file type to TXT
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.post(upload_url, headers=headers, files=files, data=data)
|
response = requests.post(upload_url, headers=headers, files=files, data=data)
|
||||||
if response.status_code == 201: # 201 means creation is successful
|
if response.status_code == 201: # 201 means creation is successful
|
||||||
print("File uploaded successfully")
|
print("File uploaded successfully")
|
||||||
return response.json().get("id") # Get the uploaded file ID
|
return response.json().get("id") # Get the uploaded file ID
|
||||||
else:
|
else:
|
||||||
print(f"File upload failed, status code: {response.status_code}")
|
print(f"File upload failed, status code: {response.status_code}")
|
||||||
return None
|
return None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error occurred: {str(e)}")
|
print(f"Error occurred: {str(e)}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def run_workflow(file_id, user, response_mode="blocking"):
|
def run_workflow(file_id, user, response_mode="blocking"):
|
||||||
workflow_url = "https://api.dify.ai/v1/workflows/run"
|
workflow_url = "https://api.dify.ai/v1/workflows/run"
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": "Bearer app-xxxxxxxxx",
|
"Authorization": "Bearer app-xxxxxxxxx",
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"orig_mail": {
|
"orig_mail": {
|
||||||
"transfer_method": "local_file",
|
"transfer_method": "local_file",
|
||||||
"upload_file_id": file_id,
|
"upload_file_id": file_id,
|
||||||
"type": "document"
|
"type": "document"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"response_mode": response_mode,
|
"response_mode": response_mode,
|
||||||
"user": user
|
"user": user
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print("Run Workflow...")
|
print("Run Workflow...")
|
||||||
response = requests.post(workflow_url, headers=headers, json=data)
|
response = requests.post(workflow_url, headers=headers, json=data)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
print("Workflow execution successful")
|
print("Workflow execution successful")
|
||||||
return response.json()
|
return response.json()
|
||||||
else:
|
else:
|
||||||
print(f"Workflow execution failed, status code: {response.status_code}")
|
print(f"Workflow execution failed, status code: {response.status_code}")
|
||||||
return {"status": "error", "message": f"Failed to execute workflow, status code: {response.status_code}"}
|
return {"status": "error", "message": f"Failed to execute workflow, status code: {response.status_code}"}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error occurred: {str(e)}")
|
print(f"Error occurred: {str(e)}")
|
||||||
return {"status": "error", "message": str(e)}
|
return {"status": "error", "message": str(e)}
|
||||||
|
|
||||||
# Usage Examples
|
# Usage Examples
|
||||||
file_path = "{your_file_path}"
|
file_path = "{your_file_path}"
|
||||||
user = "difyuser"
|
user = "difyuser"
|
||||||
|
|
||||||
# Upload files
|
# Upload files
|
||||||
file_id = upload_file(file_path, user)
|
file_id = upload_file(file_path, user)
|
||||||
if file_id:
|
if file_id:
|
||||||
# The file was uploaded successfully, and the workflow continues to run
|
# The file was uploaded successfully, and the workflow continues to run
|
||||||
result = run_workflow(file_id, user)
|
result = run_workflow(file_id, user)
|
||||||
print(result)
|
print(result)
|
||||||
else:
|
else:
|
||||||
print("File upload failed and workflow cannot be executed")
|
print("File upload failed and workflow cannot be executed")
|
||||||
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
</CodeGroup>
|
</CodeGroup>
|
||||||
</Col>
|
</Col>
|
||||||
|
@ -238,84 +238,81 @@ import { Row, Col, Properties, Property, Heading, SubProperty, Paragraph } from
|
|||||||
</CodeGroup>
|
</CodeGroup>
|
||||||
<CodeGroup title="ファイルアップロードのサンプルコード">
|
<CodeGroup title="ファイルアップロードのサンプルコード">
|
||||||
```json {{ title: 'ファイルアップロードのサンプルコード' }}
|
```json {{ title: 'ファイルアップロードのサンプルコード' }}
|
||||||
{
|
import requests
|
||||||
import requests
|
import json
|
||||||
import json
|
|
||||||
|
|
||||||
def upload_file(file_path, user):
|
def upload_file(file_path, user):
|
||||||
upload_url = "https://api.dify.ai/v1/files/upload"
|
upload_url = "https://api.dify.ai/v1/files/upload"
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": "Bearer app-xxxxxxxx",
|
"Authorization": "Bearer app-xxxxxxxx",
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print("ファイルをアップロードしています...")
|
print("ファイルをアップロードしています...")
|
||||||
with open(file_path, 'rb') as file:
|
with open(file_path, 'rb') as file:
|
||||||
files = {
|
files = {
|
||||||
'file': (file_path, file, 'text/plain') # ファイルが適切な MIME タイプでアップロードされていることを確認してください
|
'file': (file_path, file, 'text/plain') # ファイルが適切な MIME タイプでアップロードされていることを確認してください
|
||||||
}
|
}
|
||||||
data = {
|
data = {
|
||||||
"user": user,
|
"user": user,
|
||||||
"type": "TXT" # ファイルタイプをTXTに設定します
|
"type": "TXT" # ファイルタイプをTXTに設定します
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.post(upload_url, headers=headers, files=files, data=data)
|
response = requests.post(upload_url, headers=headers, files=files, data=data)
|
||||||
if response.status_code == 201: # 201 は作成が成功したことを意味します
|
if response.status_code == 201: # 201 は作成が成功したことを意味します
|
||||||
print("ファイルが正常にアップロードされました")
|
print("ファイルが正常にアップロードされました")
|
||||||
return response.json().get("id") # アップロードされたファイルIDを取得する
|
return response.json().get("id") # アップロードされたファイルIDを取得する
|
||||||
else:
|
else:
|
||||||
print(f"ファイルのアップロードに失敗しました。ステータス コード: {response.status_code}")
|
print(f"ファイルのアップロードに失敗しました。ステータス コード: {response.status_code}")
|
||||||
return None
|
return None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"エラーが発生しました: {str(e)}")
|
print(f"エラーが発生しました: {str(e)}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def run_workflow(file_id, user, response_mode="blocking"):
|
def run_workflow(file_id, user, response_mode="blocking"):
|
||||||
workflow_url = "https://api.dify.ai/v1/workflows/run"
|
workflow_url = "https://api.dify.ai/v1/workflows/run"
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": "Bearer app-xxxxxxxxx",
|
"Authorization": "Bearer app-xxxxxxxxx",
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"orig_mail": {
|
"orig_mail": {
|
||||||
"transfer_method": "local_file",
|
"transfer_method": "local_file",
|
||||||
"upload_file_id": file_id,
|
"upload_file_id": file_id,
|
||||||
"type": "document"
|
"type": "document"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"response_mode": response_mode,
|
"response_mode": response_mode,
|
||||||
"user": user
|
"user": user
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print("ワークフローを実行...")
|
print("ワークフローを実行...")
|
||||||
response = requests.post(workflow_url, headers=headers, json=data)
|
response = requests.post(workflow_url, headers=headers, json=data)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
print("ワークフローが正常に実行されました")
|
print("ワークフローが正常に実行されました")
|
||||||
return response.json()
|
return response.json()
|
||||||
else:
|
else:
|
||||||
print(f"ワークフローの実行がステータス コードで失敗しました: {response.status_code}")
|
print(f"ワークフローの実行がステータス コードで失敗しました: {response.status_code}")
|
||||||
return {"status": "error", "message": f"Failed to execute workflow, status code: {response.status_code}"}
|
return {"status": "error", "message": f"Failed to execute workflow, status code: {response.status_code}"}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"エラーが発生しました: {str(e)}")
|
print(f"エラーが発生しました: {str(e)}")
|
||||||
return {"status": "error", "message": str(e)}
|
return {"status": "error", "message": str(e)}
|
||||||
|
|
||||||
# 使用例
|
# 使用例
|
||||||
file_path = "{your_file_path}"
|
file_path = "{your_file_path}"
|
||||||
user = "difyuser"
|
user = "difyuser"
|
||||||
|
|
||||||
# ファイルをアップロードする
|
# ファイルをアップロードする
|
||||||
file_id = upload_file(file_path, user)
|
file_id = upload_file(file_path, user)
|
||||||
if file_id:
|
if file_id:
|
||||||
# ファイルは正常にアップロードされました。ワークフローの実行を続行します
|
# ファイルは正常にアップロードされました。ワークフローの実行を続行します
|
||||||
result = run_workflow(file_id, user)
|
result = run_workflow(file_id, user)
|
||||||
print(result)
|
print(result)
|
||||||
else:
|
else:
|
||||||
print("ファイルのアップロードに失敗し、ワークフローを実行できません")
|
print("ファイルのアップロードに失敗し、ワークフローを実行できません")
|
||||||
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
</CodeGroup>
|
</CodeGroup>
|
||||||
</Col>
|
</Col>
|
||||||
|
@ -232,84 +232,81 @@ Workflow 应用无会话支持,适合用于翻译/文章写作/总结 AI 等
|
|||||||
</CodeGroup>
|
</CodeGroup>
|
||||||
<CodeGroup title="File upload sample code">
|
<CodeGroup title="File upload sample code">
|
||||||
```json {{ title: 'File upload sample code' }}
|
```json {{ title: 'File upload sample code' }}
|
||||||
{
|
import requests
|
||||||
import requests
|
import json
|
||||||
import json
|
|
||||||
|
|
||||||
def upload_file(file_path, user):
|
def upload_file(file_path, user):
|
||||||
upload_url = "https://api.dify.ai/v1/files/upload"
|
upload_url = "https://api.dify.ai/v1/files/upload"
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": "Bearer app-xxxxxxxx",
|
"Authorization": "Bearer app-xxxxxxxx",
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print("上传文件中...")
|
print("上传文件中...")
|
||||||
with open(file_path, 'rb') as file:
|
with open(file_path, 'rb') as file:
|
||||||
files = {
|
files = {
|
||||||
'file': (file_path, file, 'text/plain') # 确保文件以适当的MIME类型上传
|
'file': (file_path, file, 'text/plain') # 确保文件以适当的MIME类型上传
|
||||||
}
|
}
|
||||||
data = {
|
data = {
|
||||||
"user": user,
|
"user": user,
|
||||||
"type": "TXT" # 设置文件类型为TXT
|
"type": "TXT" # 设置文件类型为TXT
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.post(upload_url, headers=headers, files=files, data=data)
|
response = requests.post(upload_url, headers=headers, files=files, data=data)
|
||||||
if response.status_code == 201: # 201 表示创建成功
|
if response.status_code == 201: # 201 表示创建成功
|
||||||
print("文件上传成功")
|
print("文件上传成功")
|
||||||
return response.json().get("id") # 获取上传的文件 ID
|
return response.json().get("id") # 获取上传的文件 ID
|
||||||
else:
|
else:
|
||||||
print(f"文件上传失败,状态码: {response.status_code}")
|
print(f"文件上传失败,状态码: {response.status_code}")
|
||||||
return None
|
return None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"发生错误: {str(e)}")
|
print(f"发生错误: {str(e)}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def run_workflow(file_id, user, response_mode="blocking"):
|
def run_workflow(file_id, user, response_mode="blocking"):
|
||||||
workflow_url = "https://api.dify.ai/v1/workflows/run"
|
workflow_url = "https://api.dify.ai/v1/workflows/run"
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": "Bearer app-xxxxxxxxx",
|
"Authorization": "Bearer app-xxxxxxxxx",
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"orig_mail": {
|
"orig_mail": {
|
||||||
"transfer_method": "local_file",
|
"transfer_method": "local_file",
|
||||||
"upload_file_id": file_id,
|
"upload_file_id": file_id,
|
||||||
"type": "document"
|
"type": "document"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"response_mode": response_mode,
|
"response_mode": response_mode,
|
||||||
"user": user
|
"user": user
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print("运行工作流...")
|
print("运行工作流...")
|
||||||
response = requests.post(workflow_url, headers=headers, json=data)
|
response = requests.post(workflow_url, headers=headers, json=data)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
print("工作流执行成功")
|
print("工作流执行成功")
|
||||||
return response.json()
|
return response.json()
|
||||||
else:
|
else:
|
||||||
print(f"工作流执行失败,状态码: {response.status_code}")
|
print(f"工作流执行失败,状态码: {response.status_code}")
|
||||||
return {"status": "error", "message": f"Failed to execute workflow, status code: {response.status_code}"}
|
return {"status": "error", "message": f"Failed to execute workflow, status code: {response.status_code}"}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"发生错误: {str(e)}")
|
print(f"发生错误: {str(e)}")
|
||||||
return {"status": "error", "message": str(e)}
|
return {"status": "error", "message": str(e)}
|
||||||
|
|
||||||
# 使用示例
|
# 使用示例
|
||||||
file_path = "{your_file_path}"
|
file_path = "{your_file_path}"
|
||||||
user = "difyuser"
|
user = "difyuser"
|
||||||
|
|
||||||
# 上传文件
|
# 上传文件
|
||||||
file_id = upload_file(file_path, user)
|
file_id = upload_file(file_path, user)
|
||||||
if file_id:
|
if file_id:
|
||||||
# 文件上传成功,继续运行工作流
|
# 文件上传成功,继续运行工作流
|
||||||
result = run_workflow(file_id, user)
|
result = run_workflow(file_id, user)
|
||||||
print(result)
|
print(result)
|
||||||
else:
|
else:
|
||||||
print("文件上传失败,无法执行工作流")
|
print("文件上传失败,无法执行工作流")
|
||||||
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
</CodeGroup>
|
</CodeGroup>
|
||||||
</Col>
|
</Col>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user