feat: 完善流式测试覆盖并精简用例
- 提取共享定义(tool_weather, image_url, json_schema_math)到功能块前 - 流式用例精简为代表子集:核心 6-8 个 + 扩展各 1-2 个 + 高级参数代表 - OpenAI: 15 个流式用例(核心 8 + vision/tools/logprobs/json_schema + 高级参数) - Anthropic: 11 个流式用例(核心 6 + vision/tools/thinking + 高级参数) - 更新 README:新增流式测试覆盖原则、parse_sse_events 函数说明
This commit is contained in:
@@ -661,13 +661,42 @@ def main():
|
||||
}
|
||||
))
|
||||
|
||||
# ---- 共享定义(供流式和非流式用例共同使用)----
|
||||
image_url = (
|
||||
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/"
|
||||
"Gfp-wisconsin-madison-the-nature-boardwalk.jpg/"
|
||||
"2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
|
||||
)
|
||||
tool_weather = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_weather",
|
||||
"description": "获取指定城市的天气",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"location": {"type": "string", "description": "城市名称"}
|
||||
},
|
||||
"required": ["location"]
|
||||
}
|
||||
}
|
||||
}
|
||||
json_schema_math = {
|
||||
"name": "math_answer",
|
||||
"strict": True,
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"answer": {"type": "number"},
|
||||
"explanation": {"type": "string"}
|
||||
},
|
||||
"required": ["answer", "explanation"],
|
||||
"additionalProperties": False
|
||||
}
|
||||
}
|
||||
|
||||
# ---- --vision ----
|
||||
if args.vision:
|
||||
image_url = (
|
||||
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/"
|
||||
"Gfp-wisconsin-madison-the-nature-boardwalk.jpg/"
|
||||
"2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
|
||||
)
|
||||
cases.append(TestCase(
|
||||
desc="图片 URL 输入 + detail 参数 (--vision)",
|
||||
method="POST",
|
||||
@@ -691,67 +720,159 @@ def main():
|
||||
|
||||
# ---- --stream ----
|
||||
if args.stream:
|
||||
# 核心用例
|
||||
cases.append(TestCase(
|
||||
desc="基本流式 (--stream)",
|
||||
desc="流式基本对话",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={
|
||||
"model": model,
|
||||
"messages": [{"role": "user", "content": "Hi"}],
|
||||
"max_tokens": 5,
|
||||
"stream": True
|
||||
},
|
||||
body={"model": model, "messages": [{"role": "user", "content": "Hi"}], "max_tokens": 5, "stream": True},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
cases.append(TestCase(
|
||||
desc="流式 + include_usage (--stream)",
|
||||
desc="流式 + include_usage",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={
|
||||
"model": model,
|
||||
"messages": [{"role": "user", "content": "Hi"}],
|
||||
"max_tokens": 5,
|
||||
"stream": True,
|
||||
"stream_options": {"include_usage": True}
|
||||
},
|
||||
body={"model": model, "messages": [{"role": "user", "content": "Hi"}], "max_tokens": 5, "stream": True, "stream_options": {"include_usage": True}},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
cases.append(TestCase(
|
||||
desc="流式 + stop sequences (--stream)",
|
||||
desc="流式 + system prompt",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={
|
||||
"model": model,
|
||||
"messages": [{"role": "user", "content": "数数: 1,2,3,"}],
|
||||
"max_tokens": 20,
|
||||
"stream": True,
|
||||
"stop": ["5"]
|
||||
},
|
||||
body={"model": model, "messages": [{"role": "system", "content": "有帮助的助手"}, {"role": "user", "content": "Hi"}], "max_tokens": 5, "stream": True},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
cases.append(TestCase(
|
||||
desc="流式多轮对话",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={"model": model, "messages": [{"role": "user", "content": "1+1?"}, {"role": "assistant", "content": "2"}, {"role": "user", "content": "2+2?"}], "max_tokens": 5, "stream": True},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
cases.append(TestCase(
|
||||
desc="流式 temperature + top_p",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={"model": model, "messages": [{"role": "user", "content": "Hi"}], "temperature": 0.5, "top_p": 0.9, "max_tokens": 5, "stream": True},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
cases.append(TestCase(
|
||||
desc="流式 max_tokens",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={"model": model, "messages": [{"role": "user", "content": "Hi"}], "max_tokens": 3, "stream": True},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
cases.append(TestCase(
|
||||
desc="流式 stop_sequences",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={"model": model, "messages": [{"role": "user", "content": "数数: 1,2,3,"}], "max_tokens": 10, "stop": ["5"], "stream": True},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
cases.append(TestCase(
|
||||
desc="流式 JSON mode",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={"model": model, "messages": [{"role": "system", "content": "以JSON回复"}, {"role": "user", "content": "颜色"}], "max_tokens": 20, "response_format": {"type": "json_object"}, "stream": True},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
|
||||
# 流式 + vision
|
||||
if args.vision:
|
||||
cases.append(TestCase(
|
||||
desc="流式图片输入",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={"model": model, "messages": [{"role": "user", "content": [{"type": "text", "text": "描述图"}, {"type": "image_url", "image_url": {"url": image_url}}]}], "max_tokens": 10, "stream": True},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
|
||||
# 流式 + tools
|
||||
if args.tools:
|
||||
cases.append(TestCase(
|
||||
desc="流式工具调用 auto",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={"model": model, "messages": [{"role": "user", "content": "北京天气?"}], "max_tokens": 50, "stream": True, "tools": [tool_weather], "tool_choice": "auto"},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
cases.append(TestCase(
|
||||
desc="流式多轮工具调用",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={"model": model, "messages": [{"role": "user", "content": "北京天气?"}, {"role": "assistant", "content": None, "tool_calls": [{"id": "call_001", "type": "function", "function": {"name": "get_weather", "arguments": '{"location": "Beijing"}'}}]}, {"role": "tool", "tool_call_id": "call_001", "content": '{"temp": 22}'}], "max_tokens": 10, "stream": True, "tools": [tool_weather]},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
|
||||
# 流式 + logprobs
|
||||
if args.logprobs:
|
||||
cases.append(TestCase(
|
||||
desc="流式 logprobs",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={"model": model, "messages": [{"role": "user", "content": "Hi"}], "max_tokens": 5, "logprobs": True, "top_logprobs": 2, "stream": True},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
|
||||
# 流式 + json_schema
|
||||
if args.json_schema:
|
||||
cases.append(TestCase(
|
||||
desc="流式 json_schema",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={"model": model, "messages": [{"role": "user", "content": "1+1=?"}], "max_tokens": 20, "response_format": {"type": "json_schema", "json_schema": json_schema_math}, "stream": True},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
|
||||
# 流式高级参数(选代表)
|
||||
cases.append(TestCase(
|
||||
desc="流式 reasoning_effort: medium",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={"model": model, "messages": [{"role": "user", "content": "Hi"}], "max_tokens": 5, "reasoning_effort": "medium", "stream": True},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
cases.append(TestCase(
|
||||
desc="流式 service_tier: auto",
|
||||
method="POST",
|
||||
url=chat_url,
|
||||
headers=headers,
|
||||
body={"model": model, "messages": [{"role": "user", "content": "Hi"}], "max_tokens": 5, "service_tier": "auto", "stream": True},
|
||||
stream=True,
|
||||
validator=validate_openai_streaming_response
|
||||
))
|
||||
|
||||
# ---- --tools ----
|
||||
if args.tools:
|
||||
tool_weather = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_weather",
|
||||
"description": "获取指定城市的天气",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"location": {"type": "string", "description": "城市名称"}
|
||||
},
|
||||
"required": ["location"]
|
||||
}
|
||||
}
|
||||
}
|
||||
cases.append(TestCase(
|
||||
desc="工具调用 tool_choice: auto (--tools)",
|
||||
method="POST",
|
||||
@@ -867,19 +988,7 @@ def main():
|
||||
"max_tokens": 20,
|
||||
"response_format": {
|
||||
"type": "json_schema",
|
||||
"json_schema": {
|
||||
"name": "math_answer",
|
||||
"strict": True,
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"answer": {"type": "number"},
|
||||
"explanation": {"type": "string"}
|
||||
},
|
||||
"required": ["answer", "explanation"],
|
||||
"additionalProperties": False
|
||||
}
|
||||
}
|
||||
"json_schema": json_schema_math
|
||||
}
|
||||
},
|
||||
validator=validate_openai_chat_completion_response
|
||||
|
||||
Reference in New Issue
Block a user