|
| 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 | + ) |
| 42 | + print(create) |
| 43 | + # 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)) |
| 44 | + |
| 45 | + except zhipuai.core._errors.APIRequestFailedError as err: |
| 46 | + print(err) |
| 47 | + except zhipuai.core._errors.APIInternalError as err: |
| 48 | + print(err) |
| 49 | + except zhipuai.core._errors.APIStatusError as err: |
| 50 | + print(err) |
| 51 | + |
| 52 | + |
| 53 | +def test_batch_retrieve(logging_conf) -> None: |
| 54 | + logging.config.dictConfig(logging_conf) # type: ignore |
| 55 | + client = ZhipuAI() # 填写您自己的APIKey |
| 56 | + try: |
| 57 | + retrieve = client.batches.retrieve("batch_1790291013237211136") |
| 58 | + print(retrieve) |
| 59 | + |
| 60 | + # 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)) |
| 61 | + except zhipuai.core._errors.APIRequestFailedError as err: |
| 62 | + print(err) |
| 63 | + except zhipuai.core._errors.APIInternalError as err: |
| 64 | + print(err) |
| 65 | + except zhipuai.core._errors.APIStatusError as err: |
| 66 | + print(err) |
| 67 | + |
| 68 | + |
| 69 | +def test_batch_cancel(logging_conf) -> None: |
| 70 | + logging.config.dictConfig(logging_conf) # type: ignore |
| 71 | + client = ZhipuAI() # 填写您自己的APIKey |
| 72 | + try: |
| 73 | + cancel = client.batches.cancel("batch_1790291013237211136") |
| 74 | + print(cancel) |
| 75 | + # 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)) |
| 76 | + except zhipuai.core._errors.APIRequestFailedError as err: |
| 77 | + print(err) |
| 78 | + except zhipuai.core._errors.APIInternalError as err: |
| 79 | + print(err) |
| 80 | + except zhipuai.core._errors.APIStatusError as err: |
| 81 | + print(err) |
| 82 | + |
| 83 | + |
| 84 | +def test_batch_list(logging_conf) -> None: |
| 85 | + logging.config.dictConfig(logging_conf) # type: ignore |
| 86 | + client = ZhipuAI() # 填写您自己的APIKey |
| 87 | + try: |
| 88 | + list = client.batches.list(limit=10) |
| 89 | + print(list) |
| 90 | + print("_________get_next_page___________") |
| 91 | + batch = list.get_next_page() |
| 92 | + print(batch) |
| 93 | + print("_________iter_pages___________") |
| 94 | + for batch in list.iter_pages(): |
| 95 | + print(batch) |
| 96 | + except zhipuai.core._errors.APIRequestFailedError as err: |
| 97 | + print(err) |
| 98 | + except zhipuai.core._errors.APIInternalError as err: |
| 99 | + print(err) |
| 100 | + except zhipuai.core._errors.APIStatusError as err: |
| 101 | + print(err) |
| 102 | + |
| 103 | + |
| 104 | +def test_batch_result(test_file_path, logging_conf) -> None: |
| 105 | + logging.config.dictConfig(logging_conf) # type: ignore |
| 106 | + client = ZhipuAI() # 填写您自己的APIKey |
| 107 | + try: |
| 108 | + content = client.files.content("file-QDpVyDIhxj8mcFiduUydNqQN") |
| 109 | + with open(os.path.join(test_file_path, "content_batchoutput.jsonl"), "wb") as f: |
| 110 | + f.write(content.content) |
| 111 | + content.write_to_file(os.path.join(test_file_path, "write_to_file_batchoutput.jsonl")) |
| 112 | + |
| 113 | + assert content.content == open(os.path.join(test_file_path, "batchoutput.jsonl"), "rb").read() |
| 114 | + assert content.content == open(os.path.join(test_file_path, "write_to_file_batchoutput.jsonl"), "rb").read() |
| 115 | + except zhipuai.core._errors.APIRequestFailedError as err: |
| 116 | + print(err) |
| 117 | + except zhipuai.core._errors.APIInternalError as err: |
| 118 | + print(err) |
| 119 | + except zhipuai.core._errors.APIStatusError as err: |
| 120 | + print(err) |
0 commit comments