11from zai import ZaiClient
22import time
33import json
4+ import traceback
45
56client = ZaiClient (
67 base_url = "" ,
7- api_key = "" ,
8- disable_token_cache = False
8+ api_key = ""
99)
1010
11- def file_parser_create_example (file_path ,tool_type ,file_type ):
11+
12+ def file_parser_create_example (file_path , tool_type , file_type ):
1213 """
1314 Example: Create a file parsing task
1415 """
@@ -26,6 +27,7 @@ def file_parser_create_example(file_path,tool_type,file_type):
2627 task_id = getattr (response , "task_id" , None )
2728 return task_id
2829
30+
2931def file_parser_content_example (task_id , format_type = "download_link" ):
3032 """
3133 Example: Get file parsing result
@@ -39,28 +41,31 @@ def file_parser_content_example(task_id, format_type="download_link"):
3941 )
4042 return response
4143 except Exception as err :
42- print ("Failed to get parsing result:" , err )
44+ print ("Failed to get parsing result:" , traceback . format_exc () )
4345 return None
4446
47+
4548def file_parser_complete_example ():
4649 """
4750 Full Example: Submit file for parsing, then poll until result is ready
4851 """
4952 # 1. 创建解析任务
5053 # 请修改本地文件路径
5154 file_path = 'your file path'
52- task_id = file_parser_create_example (file_path = file_path ,tool_type = "lite" ,file_type = "pdf" )
55+ task_id = file_parser_create_example (file_path = file_path , tool_type = "lite" , file_type = "pdf" )
5356 if not task_id :
5457 print ("Could not submit file for parsing." )
5558 return
59+
5660 ''
5761 # 2. 轮询获取结果
5862 max_wait = 60 # 最多等待1分钟
5963 wait_time = 0
6064 while wait_time < max_wait :
6165 print (f"Waiting { wait_time } /{ max_wait } seconds before querying result..." )
6266 # format_type = text / download_link
63- response = file_parser_content_example (task_id = task_id ,format_type = "download_link" )
67+ response = file_parser_content_example (task_id = task_id , format_type = "download_link" )
68+
6469 result = response .json ()
6570 if result .get ("status" ) == "processing" :
6671 print (result )
@@ -73,7 +78,6 @@ def file_parser_complete_example():
7378 print ("File parser demo completed." )
7479
7580
76-
7781if __name__ == "__main__" :
7882 print ("=== File Parsing Quick Demo ===\n " )
79- file_parser_complete_example ()
83+ file_parser_complete_example ()
0 commit comments