Skip to content

fix: remove unnecessary curly braces in wf api doc #11658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 72 additions & 75 deletions web/app/components/develop/template/template_workflow.en.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -238,84 +238,81 @@ Workflow applications offers non-session support and is ideal for translation, a
</CodeGroup>
<CodeGroup title="File upload sample code">
```json {{ title: 'File upload sample code' }}
{
import requests
import json
import requests
import json

def upload_file(file_path, user):
upload_url = "https://api.dify.ai/v1/files/upload"
headers = {
"Authorization": "Bearer app-xxxxxxxx",
}

try:
print("Upload file...")
with open(file_path, 'rb') as file:
files = {
'file': (file_path, file, 'text/plain') # Make sure the file is uploaded with the appropriate MIME type
}
data = {
"user": user,
"type": "TXT" # Set the file type to TXT
}

response = requests.post(upload_url, headers=headers, files=files, data=data)
if response.status_code == 201: # 201 means creation is successful
print("File uploaded successfully")
return response.json().get("id") # Get the uploaded file ID
else:
print(f"File upload failed, status code: {response.status_code}")
return None
except Exception as e:
print(f"Error occurred: {str(e)}")
return None

def run_workflow(file_id, user, response_mode="blocking"):
workflow_url = "https://api.dify.ai/v1/workflows/run"
headers = {
"Authorization": "Bearer app-xxxxxxxxx",
"Content-Type": "application/json"
}

data = {
"inputs": {
"orig_mail": {
"transfer_method": "local_file",
"upload_file_id": file_id,
"type": "document"
}
},
"response_mode": response_mode,
"user": user
}
def upload_file(file_path, user):
upload_url = "https://api.dify.ai/v1/files/upload"
headers = {
"Authorization": "Bearer app-xxxxxxxx",
}

try:
print("Upload file...")
with open(file_path, 'rb') as file:
files = {
'file': (file_path, file, 'text/plain') # Make sure the file is uploaded with the appropriate MIME type
}
data = {
"user": user,
"type": "TXT" # Set the file type to TXT
}

response = requests.post(upload_url, headers=headers, files=files, data=data)
if response.status_code == 201: # 201 means creation is successful
print("File uploaded successfully")
return response.json().get("id") # Get the uploaded file ID
else:
print(f"File upload failed, status code: {response.status_code}")
return None
except Exception as e:
print(f"Error occurred: {str(e)}")
return None

def run_workflow(file_id, user, response_mode="blocking"):
workflow_url = "https://api.dify.ai/v1/workflows/run"
headers = {
"Authorization": "Bearer app-xxxxxxxxx",
"Content-Type": "application/json"
}

try:
print("Run Workflow...")
response = requests.post(workflow_url, headers=headers, json=data)
if response.status_code == 200:
print("Workflow execution successful")
return response.json()
else:
print(f"Workflow execution failed, status code: {response.status_code}")
return {"status": "error", "message": f"Failed to execute workflow, status code: {response.status_code}"}
except Exception as e:
print(f"Error occurred: {str(e)}")
return {"status": "error", "message": str(e)}

# Usage Examples
file_path = "{your_file_path}"
user = "difyuser"

# Upload files
file_id = upload_file(file_path, user)
if file_id:
# The file was uploaded successfully, and the workflow continues to run
result = run_workflow(file_id, user)
print(result)
else:
print("File upload failed and workflow cannot be executed")
data = {
"inputs": {
"orig_mail": {
"transfer_method": "local_file",
"upload_file_id": file_id,
"type": "document"
}
},
"response_mode": response_mode,
"user": user
}

}
try:
print("Run Workflow...")
response = requests.post(workflow_url, headers=headers, json=data)
if response.status_code == 200:
print("Workflow execution successful")
return response.json()
else:
print(f"Workflow execution failed, status code: {response.status_code}")
return {"status": "error", "message": f"Failed to execute workflow, status code: {response.status_code}"}
except Exception as e:
print(f"Error occurred: {str(e)}")
return {"status": "error", "message": str(e)}

# Usage Examples
file_path = "{your_file_path}"
user = "difyuser"

# Upload files
file_id = upload_file(file_path, user)
if file_id:
# The file was uploaded successfully, and the workflow continues to run
result = run_workflow(file_id, user)
print(result)
else:
print("File upload failed and workflow cannot be executed")
```
</CodeGroup>
</Col>
Expand Down
147 changes: 72 additions & 75 deletions web/app/components/develop/template/template_workflow.ja.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -238,84 +238,81 @@ import { Row, Col, Properties, Property, Heading, SubProperty, Paragraph } from
</CodeGroup>
<CodeGroup title="ファイルアップロードのサンプルコード">
```json {{ title: 'ファイルアップロードのサンプルコード' }}
{
import requests
import json
import requests
import json

def upload_file(file_path, user):
upload_url = "https://api.dify.ai/v1/files/upload"
headers = {
"Authorization": "Bearer app-xxxxxxxx",
}

try:
print("ファイルをアップロードしています...")
with open(file_path, 'rb') as file:
files = {
'file': (file_path, file, 'text/plain') # ファイルが適切な MIME タイプでアップロードされていることを確認してください
}
data = {
"user": user,
"type": "TXT" # ファイルタイプをTXTに設定します
}

response = requests.post(upload_url, headers=headers, files=files, data=data)
if response.status_code == 201: # 201 は作成が成功したことを意味します
print("ファイルが正常にアップロードされました")
return response.json().get("id") # アップロードされたファイルIDを取得する
else:
print(f"ファイルのアップロードに失敗しました。ステータス コード: {response.status_code}")
return None
except Exception as e:
print(f"エラーが発生しました: {str(e)}")
return None

def run_workflow(file_id, user, response_mode="blocking"):
workflow_url = "https://api.dify.ai/v1/workflows/run"
headers = {
"Authorization": "Bearer app-xxxxxxxxx",
"Content-Type": "application/json"
}

data = {
"inputs": {
"orig_mail": {
"transfer_method": "local_file",
"upload_file_id": file_id,
"type": "document"
}
},
"response_mode": response_mode,
"user": user
}
def upload_file(file_path, user):
upload_url = "https://api.dify.ai/v1/files/upload"
headers = {
"Authorization": "Bearer app-xxxxxxxx",
}

try:
print("ファイルをアップロードしています...")
with open(file_path, 'rb') as file:
files = {
'file': (file_path, file, 'text/plain') # ファイルが適切な MIME タイプでアップロードされていることを確認してください
}
data = {
"user": user,
"type": "TXT" # ファイルタイプをTXTに設定します
}

response = requests.post(upload_url, headers=headers, files=files, data=data)
if response.status_code == 201: # 201 は作成が成功したことを意味します
print("ファイルが正常にアップロードされました")
return response.json().get("id") # アップロードされたファイルIDを取得する
else:
print(f"ファイルのアップロードに失敗しました。ステータス コード: {response.status_code}")
return None
except Exception as e:
print(f"エラーが発生しました: {str(e)}")
return None

def run_workflow(file_id, user, response_mode="blocking"):
workflow_url = "https://api.dify.ai/v1/workflows/run"
headers = {
"Authorization": "Bearer app-xxxxxxxxx",
"Content-Type": "application/json"
}

try:
print("ワークフローを実行...")
response = requests.post(workflow_url, headers=headers, json=data)
if response.status_code == 200:
print("ワークフローが正常に実行されました")
return response.json()
else:
print(f"ワークフローの実行がステータス コードで失敗しました: {response.status_code}")
return {"status": "error", "message": f"Failed to execute workflow, status code: {response.status_code}"}
except Exception as e:
print(f"エラーが発生しました: {str(e)}")
return {"status": "error", "message": str(e)}

# 使用例
file_path = "{your_file_path}"
user = "difyuser"

# ファイルをアップロードする
file_id = upload_file(file_path, user)
if file_id:
# ファイルは正常にアップロードされました。ワークフローの実行を続行します
result = run_workflow(file_id, user)
print(result)
else:
print("ファイルのアップロードに失敗し、ワークフローを実行できません")
data = {
"inputs": {
"orig_mail": {
"transfer_method": "local_file",
"upload_file_id": file_id,
"type": "document"
}
},
"response_mode": response_mode,
"user": user
}

}
try:
print("ワークフローを実行...")
response = requests.post(workflow_url, headers=headers, json=data)
if response.status_code == 200:
print("ワークフローが正常に実行されました")
return response.json()
else:
print(f"ワークフローの実行がステータス コードで失敗しました: {response.status_code}")
return {"status": "error", "message": f"Failed to execute workflow, status code: {response.status_code}"}
except Exception as e:
print(f"エラーが発生しました: {str(e)}")
return {"status": "error", "message": str(e)}

# 使用例
file_path = "{your_file_path}"
user = "difyuser"

# ファイルをアップロードする
file_id = upload_file(file_path, user)
if file_id:
# ファイルは正常にアップロードされました。ワークフローの実行を続行します
result = run_workflow(file_id, user)
print(result)
else:
print("ファイルのアップロードに失敗し、ワークフローを実行できません")
```
</CodeGroup>
</Col>
Expand Down
Loading
Loading