z-ai-sdk-python/tests/unit_tests/test_client.py
wellenzheng 82e09d6ee9
feat: modeldump and usage support (#25)
Co-authored-by: zhengweijun <weijun.zheng@aminer.cn>
2025-08-07 18:21:57 +08:00

23 lines
No EOL
975 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

def test_completions_image_url_data_image(monkeypatch):
from zai import ZaiClient
client = ZaiClient()
# 构造 messages包含 image_url 且 url 以 data:image/ 开头
base64_str = 'abc123=='
messages = [
{
'role': 'user',
'content': [
{'type': 'text', 'text': 'What is in this image?'},
{'type': 'image_url', 'image_url': {'url': f'data:image/png;base64,{base64_str}'}},
]
}
]
# mock client.post 方法只返回 body 以便断言
def fake_post(*args, **kwargs):
return kwargs.get('body', {})
monkeypatch.setattr(client, 'post', fake_post)
result = client.chat.completions.create(model='glm-4', messages=messages)
# 验证 image_url 的 url 字段已去除前缀
content = result['messages'][0]['content']
image_url = [x for x in content if x.get('type') == 'image_url'][0]
assert image_url['image_url']['url'] == base64_str