|
1 | | -# import os |
2 | | -# import logging |
3 | | -# import logging.config |
4 | | -# import zhipuai |
5 | | -# from zhipuai import ZhipuAI |
6 | | -# |
7 | | -# |
8 | | -# def test_batch_input_file(test_file_path, logging_conf) -> None: |
9 | | -# logging.config.dictConfig(logging_conf) # type: ignore |
10 | | -# client = ZhipuAI() # 填写您自己的APIKey |
11 | | -# |
12 | | -# try: |
13 | | -# batch_input_file = client.files.create( |
14 | | -# file=open(os.path.join(test_file_path, "batchinput.jsonl"), "rb"), |
15 | | -# purpose="batch" |
16 | | -# ) |
17 | | -# |
18 | | -# print(batch_input_file) |
19 | | -# |
20 | | -# # FileObject(id='20240514_ea19d21b-d256-4586-b0df-e80a45e3c286', bytes=490, created_at=1715673494, filename=None, object='file', purpose='batch', status=None, status_details=None, fileName='batchinput.jsonl') |
21 | | -# |
22 | | -# except zhipuai.core._errors.APIRequestFailedError as err: |
23 | | -# print(err) |
24 | | -# except zhipuai.core._errors.APIInternalError as err: |
25 | | -# print(err) |
26 | | -# except zhipuai.core._errors.APIStatusError as err: |
27 | | -# print(err) |
28 | | -# |
29 | | -# |
30 | | -# def test_batch_create(logging_conf) -> None: |
31 | | -# logging.config.dictConfig(logging_conf) # type: ignore |
32 | | -# client = ZhipuAI() # 填写您自己的APIKey |
33 | | -# try: |
34 | | -# create = client.batches.create( |
35 | | -# input_file_id="20240514_ea19d21b-d256-4586-b0df-e80a45e3c286", |
36 | | -# endpoint="/v4/chat/completions", |
37 | | -# completion_window="24h", |
38 | | -# metadata={ |
39 | | -# "description": "job test" |
40 | | -# }, |
41 | | -# auto_delete_input_file=True |
42 | | -# ) |
43 | | -# print(create) |
44 | | -# # Batch(id='batch_1790292763050508288', completion_window='24h', created_at=1715674031399, endpoint='/v4/chat/completions', input_file_id='20240514_ea19d21b-d256-4586-b0df-e80a45e3c286', object='batch', status='validating', cancelled_at=None, cancelling_at=None, completed_at=None, error_file_id=None, errors=None, expired_at=None, expires_at=None, failed_at=None, finalizing_at=None, in_progress_at=None, metadata={'description': 'job test'}, output_file_id=None, request_counts=BatchRequestCounts(completed=None, failed=None, total=None)) |
45 | | -# |
46 | | -# except zhipuai.core._errors.APIRequestFailedError as err: |
47 | | -# print(err) |
48 | | -# except zhipuai.core._errors.APIInternalError as err: |
49 | | -# print(err) |
50 | | -# except zhipuai.core._errors.APIStatusError as err: |
51 | | -# print(err) |
52 | | -# |
53 | | -# |
54 | | -# def test_batch_retrieve(logging_conf) -> None: |
55 | | -# logging.config.dictConfig(logging_conf) # type: ignore |
56 | | -# client = ZhipuAI() # 填写您自己的APIKey |
57 | | -# try: |
58 | | -# retrieve = client.batches.retrieve("batch_1790291013237211136") |
59 | | -# print(retrieve) |
60 | | -# |
61 | | -# # Batch(id='batch_1790291013237211136', completion_window='24h', created_at=1715673614000, endpoint='/v4/chat/completions', input_file_id='20240514_ea19d21b-d256-4586-b0df-e80a45e3c286', object='batch', status='validating', cancelled_at=None, cancelling_at=None, completed_at=None, error_file_id='', errors=None, expired_at=None, expires_at=None, failed_at=None, finalizing_at=None, in_progress_at=None, metadata={'description': 'job test'}, output_file_id='', request_counts=BatchRequestCounts(completed=None, failed=None, total=None)) |
62 | | -# except zhipuai.core._errors.APIRequestFailedError as err: |
63 | | -# print(err) |
64 | | -# except zhipuai.core._errors.APIInternalError as err: |
65 | | -# print(err) |
66 | | -# except zhipuai.core._errors.APIStatusError as err: |
67 | | -# print(err) |
68 | | -# |
69 | | -# |
70 | | -# def test_batch_cancel(logging_conf) -> None: |
71 | | -# logging.config.dictConfig(logging_conf) # type: ignore |
72 | | -# client = ZhipuAI() # 填写您自己的APIKey |
73 | | -# try: |
74 | | -# cancel = client.batches.cancel("batch_1790291013237211136") |
75 | | -# print(cancel) |
76 | | -# # Batch(id='batch_1790291013237211136', completion_window='24h', created_at=1715673614000, endpoint='/v4/chat/completions', input_file_id='20240514_ea19d21b-d256-4586-b0df-e80a45e3c286', object='batch', status='cancelling', cancelled_at=None, cancelling_at=1715673698775, completed_at=None, error_file_id='', errors=None, expired_at=None, expires_at=None, failed_at=None, finalizing_at=None, in_progress_at=None, metadata={'description': 'job test'}, output_file_id='', request_counts=BatchRequestCounts(completed=None, failed=None, total=None)) |
77 | | -# except zhipuai.core._errors.APIRequestFailedError as err: |
78 | | -# print(err) |
79 | | -# except zhipuai.core._errors.APIInternalError as err: |
80 | | -# print(err) |
81 | | -# except zhipuai.core._errors.APIStatusError as err: |
82 | | -# print(err) |
83 | | -# |
84 | | -# |
85 | | -# def test_batch_list(logging_conf) -> None: |
86 | | -# logging.config.dictConfig(logging_conf) # type: ignore |
87 | | -# client = ZhipuAI() # 填写您自己的APIKey |
88 | | -# try: |
89 | | -# list = client.batches.list(limit=10) |
90 | | -# print(list) |
91 | | -# if list.has_more: |
92 | | -# print("_________get_next_page___________") |
93 | | -# batch = list.get_next_page() |
94 | | -# print(batch) |
95 | | -# print("_________iter_pages___________") |
96 | | -# for batch in list.iter_pages(): |
97 | | -# print(batch) |
98 | | -# except zhipuai.core._errors.APIRequestFailedError as err: |
99 | | -# print(err) |
100 | | -# except zhipuai.core._errors.APIInternalError as err: |
101 | | -# print(err) |
102 | | -# except zhipuai.core._errors.APIStatusError as err: |
103 | | -# print(err) |
104 | | -# |
105 | | -# |
106 | | -# def test_batch_result(test_file_path, logging_conf) -> None: |
107 | | -# logging.config.dictConfig(logging_conf) # type: ignore |
108 | | -# client = ZhipuAI() # 填写您自己的APIKey |
109 | | -# try: |
110 | | -# content = client.files.content("file-QDpVyDIhxj8mcFiduUydNqQN") |
111 | | -# with open(os.path.join(test_file_path, "content_batchoutput.jsonl"), "wb") as f: |
112 | | -# f.write(content.content) |
113 | | -# content.write_to_file(os.path.join(test_file_path, "write_to_file_batchoutput.jsonl")) |
114 | | -# |
115 | | -# assert content.content == open(os.path.join(test_file_path, "batchoutput.jsonl"), "rb").read() |
116 | | -# assert content.content == open(os.path.join(test_file_path, "write_to_file_batchoutput.jsonl"), "rb").read() |
117 | | -# except zhipuai.core._errors.APIRequestFailedError as err: |
118 | | -# print(err) |
119 | | -# except zhipuai.core._errors.APIInternalError as err: |
120 | | -# print(err) |
121 | | -# except zhipuai.core._errors.APIStatusError as err: |
122 | | -# print(err) |
| 1 | +import os |
| 2 | +import logging |
| 3 | +import logging.config |
| 4 | +import zhipuai |
| 5 | +from zhipuai import ZhipuAI |
| 6 | + |
| 7 | + |
| 8 | +def test_batch_input_file(test_file_path, logging_conf) -> None: |
| 9 | + logging.config.dictConfig(logging_conf) # type: ignore |
| 10 | + client = ZhipuAI() # 填写您自己的APIKey |
| 11 | + |
| 12 | + try: |
| 13 | + batch_input_file = client.files.create( |
| 14 | + file=open(os.path.join(test_file_path, "batchinput.jsonl"), "rb"), |
| 15 | + purpose="batch" |
| 16 | + ) |
| 17 | + |
| 18 | + print(batch_input_file) |
| 19 | + |
| 20 | + # FileObject(id='20240514_ea19d21b-d256-4586-b0df-e80a45e3c286', bytes=490, created_at=1715673494, filename=None, object='file', purpose='batch', status=None, status_details=None, fileName='batchinput.jsonl') |
| 21 | + |
| 22 | + except zhipuai.core._errors.APIRequestFailedError as err: |
| 23 | + print(err) |
| 24 | + except zhipuai.core._errors.APIInternalError as err: |
| 25 | + print(err) |
| 26 | + except zhipuai.core._errors.APIStatusError as err: |
| 27 | + print(err) |
| 28 | + |
| 29 | + |
| 30 | +def test_batch_create(logging_conf) -> None: |
| 31 | + logging.config.dictConfig(logging_conf) # type: ignore |
| 32 | + client = ZhipuAI() # 填写您自己的APIKey |
| 33 | + try: |
| 34 | + create = client.batches.create( |
| 35 | + input_file_id="20240514_ea19d21b-d256-4586-b0df-e80a45e3c286", |
| 36 | + endpoint="/v4/chat/completions", |
| 37 | + completion_window="24h", |
| 38 | + metadata={ |
| 39 | + "description": "job test" |
| 40 | + }, |
| 41 | + auto_delete_input_file=True |
| 42 | + ) |
| 43 | + print(create) |
| 44 | + # Batch(id='batch_1790292763050508288', completion_window='24h', created_at=1715674031399, endpoint='/v4/chat/completions', input_file_id='20240514_ea19d21b-d256-4586-b0df-e80a45e3c286', object='batch', status='validating', cancelled_at=None, cancelling_at=None, completed_at=None, error_file_id=None, errors=None, expired_at=None, expires_at=None, failed_at=None, finalizing_at=None, in_progress_at=None, metadata={'description': 'job test'}, output_file_id=None, request_counts=BatchRequestCounts(completed=None, failed=None, total=None)) |
| 45 | + |
| 46 | + except zhipuai.core._errors.APIRequestFailedError as err: |
| 47 | + print(err) |
| 48 | + except zhipuai.core._errors.APIInternalError as err: |
| 49 | + print(err) |
| 50 | + except zhipuai.core._errors.APIStatusError as err: |
| 51 | + print(err) |
| 52 | + |
| 53 | + |
| 54 | +def test_batch_retrieve(logging_conf) -> None: |
| 55 | + logging.config.dictConfig(logging_conf) # type: ignore |
| 56 | + client = ZhipuAI() # 填写您自己的APIKey |
| 57 | + try: |
| 58 | + retrieve = client.batches.retrieve("batch_1790291013237211136") |
| 59 | + print(retrieve) |
| 60 | + |
| 61 | + # Batch(id='batch_1790291013237211136', completion_window='24h', created_at=1715673614000, endpoint='/v4/chat/completions', input_file_id='20240514_ea19d21b-d256-4586-b0df-e80a45e3c286', object='batch', status='validating', cancelled_at=None, cancelling_at=None, completed_at=None, error_file_id='', errors=None, expired_at=None, expires_at=None, failed_at=None, finalizing_at=None, in_progress_at=None, metadata={'description': 'job test'}, output_file_id='', request_counts=BatchRequestCounts(completed=None, failed=None, total=None)) |
| 62 | + except zhipuai.core._errors.APIRequestFailedError as err: |
| 63 | + print(err) |
| 64 | + except zhipuai.core._errors.APIInternalError as err: |
| 65 | + print(err) |
| 66 | + except zhipuai.core._errors.APIStatusError as err: |
| 67 | + print(err) |
| 68 | + |
| 69 | + |
| 70 | +def test_batch_cancel(logging_conf) -> None: |
| 71 | + logging.config.dictConfig(logging_conf) # type: ignore |
| 72 | + client = ZhipuAI() # 填写您自己的APIKey |
| 73 | + try: |
| 74 | + cancel = client.batches.cancel("batch_1790291013237211136") |
| 75 | + print(cancel) |
| 76 | + # Batch(id='batch_1790291013237211136', completion_window='24h', created_at=1715673614000, endpoint='/v4/chat/completions', input_file_id='20240514_ea19d21b-d256-4586-b0df-e80a45e3c286', object='batch', status='cancelling', cancelled_at=None, cancelling_at=1715673698775, completed_at=None, error_file_id='', errors=None, expired_at=None, expires_at=None, failed_at=None, finalizing_at=None, in_progress_at=None, metadata={'description': 'job test'}, output_file_id='', request_counts=BatchRequestCounts(completed=None, failed=None, total=None)) |
| 77 | + except zhipuai.core._errors.APIRequestFailedError as err: |
| 78 | + print(err) |
| 79 | + except zhipuai.core._errors.APIInternalError as err: |
| 80 | + print(err) |
| 81 | + except zhipuai.core._errors.APIStatusError as err: |
| 82 | + print(err) |
| 83 | + |
| 84 | + |
| 85 | +def test_batch_list(logging_conf) -> None: |
| 86 | + logging.config.dictConfig(logging_conf) # type: ignore |
| 87 | + client = ZhipuAI() # 填写您自己的APIKey |
| 88 | + try: |
| 89 | + list = client.batches.list(limit=10) |
| 90 | + print(list) |
| 91 | + if list.has_more: |
| 92 | + print("_________get_next_page___________") |
| 93 | + batch = list.get_next_page() |
| 94 | + print(batch) |
| 95 | + print("_________iter_pages___________") |
| 96 | + for batch in list.iter_pages(): |
| 97 | + print(batch) |
| 98 | + except zhipuai.core._errors.APIRequestFailedError as err: |
| 99 | + print(err) |
| 100 | + except zhipuai.core._errors.APIInternalError as err: |
| 101 | + print(err) |
| 102 | + except zhipuai.core._errors.APIStatusError as err: |
| 103 | + print(err) |
| 104 | + |
| 105 | + |
| 106 | +def test_batch_result(test_file_path, logging_conf) -> None: |
| 107 | + logging.config.dictConfig(logging_conf) # type: ignore |
| 108 | + client = ZhipuAI() # 填写您自己的APIKey |
| 109 | + try: |
| 110 | + content = client.files.content("file-QDpVyDIhxj8mcFiduUydNqQN") |
| 111 | + with open(os.path.join(test_file_path, "content_batchoutput.jsonl"), "wb") as f: |
| 112 | + f.write(content.content) |
| 113 | + content.write_to_file(os.path.join(test_file_path, "write_to_file_batchoutput.jsonl")) |
| 114 | + |
| 115 | + assert content.content == open(os.path.join(test_file_path, "batchoutput.jsonl"), "rb").read() |
| 116 | + assert content.content == open(os.path.join(test_file_path, "write_to_file_batchoutput.jsonl"), "rb").read() |
| 117 | + except zhipuai.core._errors.APIRequestFailedError as err: |
| 118 | + print(err) |
| 119 | + except zhipuai.core._errors.APIInternalError as err: |
| 120 | + print(err) |
| 121 | + except zhipuai.core._errors.APIStatusError as err: |
| 122 | + print(err) |
0 commit comments