测试payload max_tokens参数
()
| 45 | |
| 46 | |
| 47 | def test_chat_usage_stream(): |
| 48 | """测试payload max_tokens参数""" |
| 49 | data = { |
| 50 | "messages": [ |
| 51 | {"role": "system", "content": "You are a helpful assistant."}, |
| 52 | {"role": "user", "content": "牛顿的三大运动定律是什么?"}, |
| 53 | ], |
| 54 | "max_tokens": 50, |
| 55 | "stream": True, |
| 56 | "stream_options": {"include_usage": True, "continuous_usage_stats": True}, |
| 57 | "metadata": {"min_tokens": 10}, |
| 58 | } |
| 59 | |
| 60 | payload = build_request_payload(TEMPLATE, data) |
| 61 | response = send_request(url=URL, payload=payload, stream=True) |
| 62 | chunks = get_stream_chunks(response) |
| 63 | # for idx, chunk in enumerate(chunks): |
| 64 | # print(f"\nchunk[{idx}]:\n{json.dumps(chunk, indent=2, ensure_ascii=False)}") |
| 65 | |
| 66 | usage = chunks[-1]["usage"] |
| 67 | total_tokens = usage["completion_tokens"] + usage["prompt_tokens"] |
| 68 | assert data["max_tokens"] >= usage["completion_tokens"], f"completion_tokens大于max_tokens, usage: {usage}" |
| 69 | assert ( |
| 70 | data["metadata"]["min_tokens"] <= usage["completion_tokens"] |
| 71 | ), f"completion_tokens小于min_tokens, usage: {usage}" |
| 72 | assert ( |
| 73 | usage["total_tokens"] == total_tokens |
| 74 | ), f"total_tokens不等于prompt_tokens + completion_tokens, usage: {usage}" |
| 75 | |
| 76 | |
| 77 | def test_chat_usage_non_stream(): |
nothing calls this directly
no test coverage detected