Skip to content

Commit 0d4a41b

Browse files
committed
优化了注释
1 parent 2fb5b95 commit 0d4a41b

File tree

2 files changed

+152
-19
lines changed

2 files changed

+152
-19
lines changed

pydify/common.py

Lines changed: 118 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -515,19 +515,63 @@ def get_app_info(self) -> Dict[str, Any]:
515515
"""
516516
return self.get("info")
517517

518-
def get_parameters(self) -> Dict[str, Any]:
518+
def get_parameters(self, **kwargs) -> Dict[str, Any]:
519519
"""
520-
获取应用参数,包括功能开关、输入参数名称、类型及默认值等。
521-
520+
获取应用参数,包括功能开关、输入参数配置、文件上传限制等。
521+
522+
此方法通常用于应用初始化阶段,获取应用的各种配置参数和功能开关状态。
523+
524+
Args:
525+
**kwargs: 额外的请求参数,如timeout、max_retries等
526+
522527
Returns:
523-
Dict[str, Any]: 应用参数配置
524-
525-
526-
528+
Dict[str, Any]: 包含应用参数的字典,可能包含以下字段:
529+
- opening_statement (str): 应用开场白文本
530+
- suggested_questions (List[str]): 开场推荐问题列表
531+
- suggested_questions_after_answer (Dict): 回答后推荐问题配置
532+
- enabled (bool): 是否启用此功能
533+
- speech_to_text (Dict): 语音转文本功能配置
534+
- enabled (bool): 是否启用此功能
535+
- retriever_resource (Dict): 引用和归属功能配置
536+
- enabled (bool): 是否启用此功能
537+
- annotation_reply (Dict): 标记回复功能配置
538+
- enabled (bool): 是否启用此功能
539+
- user_input_form (List[Dict]): 用户输入表单配置列表,每项可能包含:
540+
- text-input: 文本输入控件配置
541+
- paragraph: 段落文本输入控件配置
542+
- select: 下拉选择控件配置
543+
- file_upload (Dict): 文件上传相关配置
544+
- image (Dict): 图片上传配置
545+
- enabled (bool): 是否启用图片上传
546+
- number_limits (int): 图片数量限制
547+
- transfer_methods (List[str]): 支持的传输方式
548+
- system_parameters (Dict): 系统级参数
549+
- file_size_limit (int): 文档上传大小限制(MB)
550+
- image_file_size_limit (int): 图片上传大小限制(MB)
551+
- audio_file_size_limit (int): 音频上传大小限制(MB)
552+
- video_file_size_limit (int): 视频上传大小限制(MB)
553+
527554
Raises:
528555
DifyAPIError: 当API请求失败时
529-
"""
530-
return self.get("parameters")
556+
557+
Example:
558+
```python
559+
# 获取应用参数
560+
params = client.get_parameters()
561+
562+
# 检查是否启用了图片上传
563+
if params.get('file_upload', {}).get('image', {}).get('enabled', False):
564+
print("此应用支持图片上传")
565+
print(f"图片数量限制: {params['file_upload']['image']['number_limits']}")
566+
567+
# 获取用户输入表单配置
568+
forms = params.get('user_input_form', [])
569+
for form in forms:
570+
for form_type, config in form.items():
571+
print(f"表单类型: {form_type}, 字段名: {config.get('label')}")
572+
```
573+
"""
574+
return self.get("parameters", **kwargs)
531575

532576
def get_conversations(
533577
self,
@@ -703,3 +747,68 @@ def __str__(self) -> str:
703747
if self.status_code:
704748
return f"[{self.status_code}] {self.message}"
705749
return self.message
750+
751+
def analyze_app_capabilities(client):
752+
"""分析应用的功能和配置"""
753+
# 获取应用参数
754+
params = client.get_parameters()
755+
756+
# 基本信息
757+
print("=== 应用功能配置分析 ===")
758+
759+
# 检查基本功能
760+
features = []
761+
if "opening_statement" in params and params["opening_statement"]:
762+
features.append("开场白")
763+
if params.get("suggested_questions"):
764+
features.append("推荐问题")
765+
if params.get("suggested_questions_after_answer", {}).get("enabled"):
766+
features.append("回答后推荐问题")
767+
if params.get("speech_to_text", {}).get("enabled"):
768+
features.append("语音转文本")
769+
if params.get("retriever_resource", {}).get("enabled"):
770+
features.append("引用和归属")
771+
if params.get("annotation_reply", {}).get("enabled"):
772+
features.append("标记回复")
773+
774+
print(f"启用的功能: {', '.join(features) if features else '无特殊功能'}")
775+
776+
# 检查表单配置
777+
if "user_input_form" in params:
778+
form_types = []
779+
variables = []
780+
for item in params["user_input_form"]:
781+
for form_type in item:
782+
form_types.append(form_type)
783+
variables.append(item[form_type].get("variable"))
784+
785+
print(f"\n表单配置: 共{len(params['user_input_form'])}个控件")
786+
print(f"控件类型: {', '.join(form_types)}")
787+
print(f"变量名列表: {', '.join(variables)}")
788+
789+
# 检查文件上传能力
790+
if "file_upload" in params:
791+
upload_types = []
792+
for upload_type, config in params["file_upload"].items():
793+
if config.get("enabled"):
794+
upload_types.append(upload_type)
795+
796+
if upload_types:
797+
print(f"\n支持上传文件类型: {', '.join(upload_types)}")
798+
799+
# 详细的图片上传配置
800+
if "image" in params["file_upload"] and params["file_upload"]["image"].get("enabled"):
801+
img_config = params["file_upload"]["image"]
802+
print(f"图片上传限制: 最多{img_config.get('number_limits', 0)}张")
803+
print(f"支持的传输方式: {', '.join(img_config.get('transfer_methods', []))}")
804+
805+
# 检查系统参数
806+
if "system_parameters" in params:
807+
print("\n系统参数限制:")
808+
for param, value in params["system_parameters"].items():
809+
print(f"- {param}: {value}MB")
810+
811+
return params
812+
813+
# 使用示例
814+
app_params = analyze_app_capabilities(client)

pydify/workflow.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,25 @@ def run(
2727
) -> Union[Dict[str, Any], Generator[Dict[str, Any], None, None]]:
2828
"""
2929
执行工作流。
30-
31-
注意:此方法的参数格式可能需要根据您的Dify API版本进行调整。
32-
如果遇到"input is required in input form"或类似错误,您可能需要修改以下代码。
33-
常见的API参数格式有:
34-
1. {"inputs": {"prompt": "value"}, ...} - 复数形式嵌套
35-
2. {"input": {"prompt": "value"}, ...} - 单数形式嵌套
36-
3. {"prompt": "value", ...} - 扁平结构,无嵌套
37-
30+
3831
Args:
39-
inputs (Dict[str, Any]): 工作流输入变量
32+
inputs (Dict[str, Any]): 必需参数。包含工作流所需的输入变量键值对。
33+
每个键对应一个变量名称,值为该变量的具体内容。
34+
如果变量类型为文件,值需要是一个包含文件信息的字典,
35+
具体格式参考files参数说明。
4036
user (str): 用户标识
4137
response_mode (str, optional): 响应模式,'streaming'(流式)或'blocking'(阻塞)。默认为'streaming'。
42-
files (List[Dict[str, Any]], optional): 文件列表,每个文件为一个字典,包含类型、传递方式和URL/ID。
38+
files (List[Dict[str, Any]], optional): 文件列表,每个文件为一个字典,包含以下字段:
39+
- type (str): 文件类型,支持:
40+
- document: 支持'TXT', 'MD', 'MARKDOWN', 'PDF', 'HTML', 'XLSX', 'XLS',
41+
'DOCX', 'CSV', 'EML', 'MSG', 'PPTX', 'PPT', 'XML', 'EPUB'
42+
- image: 支持'JPG', 'JPEG', 'PNG', 'GIF', 'WEBP', 'SVG'
43+
- audio: 支持'MP3', 'M4A', 'WAV', 'WEBM', 'AMR'
44+
- video: 支持'MP4', 'MOV', 'MPEG', 'MPGA'
45+
- custom: 支持其他文件类型
46+
- transfer_method (str): 传递方式,'remote_url'(图片地址)或'local_file'(上传文件)
47+
- url (str): 图片地址(仅当transfer_method为'remote_url'时需要)
48+
- upload_file_id (str): 上传文件ID(仅当transfer_method为'local_file'时需要)
4349
**kwargs: 额外的请求参数,如timeout、max_retries等
4450
4551
Returns:
@@ -50,6 +56,24 @@ def run(
5056
Raises:
5157
ValueError: 当提供了无效的参数时
5258
DifyAPIError: 当API请求失败时
59+
60+
Example:
61+
```python
62+
# 基本文本输入示例
63+
inputs = {
64+
"prompt": "分析最近的经济数据",
65+
"data_source": "公开数据"
66+
}
67+
68+
# 包含文件的输入示例
69+
inputs = {
70+
"document": {
71+
"type": "document",
72+
"transfer_method": "remote_url",
73+
"url": "https://example.com/doc.pdf"
74+
}
75+
}
76+
```
5377
"""
5478
if response_mode not in ["streaming", "blocking"]:
5579
raise ValueError("response_mode must be 'streaming' or 'blocking'")

0 commit comments

Comments
 (0)