Skip to content
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
54 changes: 4 additions & 50 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,7 @@ test/
examples/
.pypirc
/poetry.lock
/logs/local_1714381518619/zhipuai.log
/logs/local_1714381521241/zhipuai.log
/logs/local_1714381523244/zhipuai.log
/logs/local_1714381534486/zhipuai.log
/logs/local_1714381536161/zhipuai.log
/logs/local_1714381537762/zhipuai.log
/logs/local_1714381540228/zhipuai.log
/logs/local_1714381541675/zhipuai.log
/logs/local_1714381544183/zhipuai.log
/logs/local_1714381554442/zhipuai.log
/logs/local_1714381557457/zhipuai.log
/logs/local_1714381560830/zhipuai.log
/logs/local_1714381561389/zhipuai.log
/logs/local_1714381570760/zhipuai.log
/logs/local_1714381572227/zhipuai.log
/logs/local_1714381574060/zhipuai.log
/logs/local_1714381574645/zhipuai.log
/logs/local_1714381586122/zhipuai.log
/logs/local_1714381596219/zhipuai.log
/logs/local_1714381598857/zhipuai.log
/logs/local_1714381599471/zhipuai.log
/logs/local_1714381609065/zhipuai.log
/logs/local_1714381621513/zhipuai.log
/logs/local_1714381622121/zhipuai.log
/logs/local_1714381833346/zhipuai.log
/logs/local_1714381918707/zhipuai.log
/logs/local_1714382871154/zhipuai.log
/logs/local_1714382884150/zhipuai.log
/logs/local_1714382885846/zhipuai.log
/logs/local_1714382887557/zhipuai.log
/logs/local_1714382889632/zhipuai.log
/logs/local_1714382891424/zhipuai.log
/logs/local_1714382893632/zhipuai.log
/logs/local_1714382904235/zhipuai.log
/logs/local_1714382907134/zhipuai.log
/logs/local_1714382908915/zhipuai.log
/logs/local_1714382912290/zhipuai.log
/logs/local_1714382915919/zhipuai.log
/logs/local_1714382935679/zhipuai.log
/logs/local_1714382945693/zhipuai.log
/logs/local_1714382951968/zhipuai.log
/logs/local_1714382953236/zhipuai.log
/logs/local_1714382954392/zhipuai.log
/logs/local_1714382957260/zhipuai.log
/logs/local_1714382958299/zhipuai.log
/logs/local_1714382959178/zhipuai.log
/logs/local_1714382963052/zhipuai.log
/logs/local_1714382964289/zhipuai.log
/logs/local_1714382975167/zhipuai.log
/logs/local_1714382979034/zhipuai.log
logs
/tests/integration_tests/batchoutput.jsonl
/tests/integration_tests/content_batchoutput.jsonl
/tests/integration_tests/write_to_file_batchoutput.jsonl
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Configuration for unit tests."""
import logging
from importlib import util
from pathlib import Path
from typing import Dict, List, Sequence

import pytest
Expand Down Expand Up @@ -97,14 +98,14 @@ def test_something():
@pytest.fixture
def logging_conf() -> dict:
return get_config_dict(
"DEBUG",
"info",
get_log_file(log_path="logs", sub_dir=f"local_{get_timestamp_ms()}"),
1024*1024,
1024*1024*1024,
)

@pytest.fixture
def test_file_path(request) -> str:
def test_file_path(request) -> Path:
from pathlib import Path
import os
# 当前执行目录
Expand Down
2 changes: 2 additions & 0 deletions tests/integration_tests/batchinput.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"custom_id": "request-1", "method": "POST", "url": "/v4/chat/completions", "body": {"model": "glm-4", "messages": [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Hello world!"}],"max_tokens": 1000}}
{"custom_id": "request-2", "method": "POST", "url": "/v4/chat/completions", "body": {"model": "glm-4", "messages": [{"role": "system", "content": "You are an unhelpful assistant."},{"role": "user", "content": "Hello world!"}],"max_tokens": 1000}}
120 changes: 120 additions & 0 deletions tests/integration_tests/test_batches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import os
import logging
import logging.config
import zhipuai
from zhipuai import ZhipuAI


def test_batch_input_file(test_file_path, logging_conf) -> None:
logging.config.dictConfig(logging_conf) # type: ignore
client = ZhipuAI() # 填写您自己的APIKey

try:
batch_input_file = client.files.create(
file=open(os.path.join(test_file_path, "batchinput.jsonl"), "rb"),
purpose="batch"
)

print(batch_input_file)

# 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')

except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)


def test_batch_create(logging_conf) -> None:
logging.config.dictConfig(logging_conf) # type: ignore
client = ZhipuAI() # 填写您自己的APIKey
try:
create = client.batches.create(
input_file_id="20240514_ea19d21b-d256-4586-b0df-e80a45e3c286",
endpoint="/v4/chat/completions",
completion_window="24h",
metadata={
"description": "job test"
}
)
print(create)
# 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))

except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)


def test_batch_retrieve(logging_conf) -> None:
logging.config.dictConfig(logging_conf) # type: ignore
client = ZhipuAI() # 填写您自己的APIKey
try:
retrieve = client.batches.retrieve("batch_1790291013237211136")
print(retrieve)

# 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))
except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)


def test_batch_cancel(logging_conf) -> None:
logging.config.dictConfig(logging_conf) # type: ignore
client = ZhipuAI() # 填写您自己的APIKey
try:
cancel = client.batches.cancel("batch_1790291013237211136")
print(cancel)
# 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))
except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)


def test_batch_list(logging_conf) -> None:
logging.config.dictConfig(logging_conf) # type: ignore
client = ZhipuAI() # 填写您自己的APIKey
try:
list = client.batches.list(limit=10)
print(list)
print("_________get_next_page___________")
batch = list.get_next_page()
print(batch)
print("_________iter_pages___________")
for batch in list.iter_pages():
print(batch)
except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)


def test_batch_result(test_file_path, logging_conf) -> None:
logging.config.dictConfig(logging_conf) # type: ignore
client = ZhipuAI() # 填写您自己的APIKey
try:
content = client.files.content("file-QDpVyDIhxj8mcFiduUydNqQN")
with open(os.path.join(test_file_path, "content_batchoutput.jsonl"), "wb") as f:
f.write(content.content)
content.write_to_file(os.path.join(test_file_path, "write_to_file_batchoutput.jsonl"))

assert content.content == open(os.path.join(test_file_path, "batchoutput.jsonl"), "rb").read()
assert content.content == open(os.path.join(test_file_path, "write_to_file_batchoutput.jsonl"), "rb").read()
except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)
23 changes: 23 additions & 0 deletions tests/unit_tests/maybe/test_maybe_transform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
import unittest
from typing import Type, cast, Iterable, TypedDict
import pytest
import httpx
import inspect

from zhipuai.core import maybe_transform
from zhipuai.types import batch_create_params


def test_response_joblist_model_cast() -> None:
params = maybe_transform(
{
"completion_window": "/v1/chat/completions",
"endpoint": None,
"metadata": {
"key": "value"
},
},
batch_create_params.BatchCreateParams,
)
assert isinstance(params, dict)
Loading