优化交互式测试验证脚本,针对场景4修改提示词以及代码
This commit is contained in:
60
tools/test_validate/modules/api_client.py
Normal file
60
tools/test_validate/modules/api_client.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import requests
|
||||
import time
|
||||
import json
|
||||
|
||||
class APIClient:
|
||||
def __init__(self, base_url="http://127.0.0.1:8000"):
|
||||
self.base_url = base_url
|
||||
self.endpoint = "/generate_plan"
|
||||
|
||||
def send_request(self, prompt, timeout=60):
|
||||
"""
|
||||
Sends a request to the API and returns a structured result.
|
||||
Returns:
|
||||
dict: {
|
||||
"success": bool,
|
||||
"data": dict or None,
|
||||
"latency": float (seconds),
|
||||
"error": str or None,
|
||||
"http_status": int or None
|
||||
}
|
||||
"""
|
||||
url = f"{self.base_url}{self.endpoint}"
|
||||
payload = {"user_prompt": prompt}
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
start_time = time.time()
|
||||
try:
|
||||
response = requests.post(url, json=payload, headers=headers, timeout=timeout)
|
||||
latency = time.time() - start_time
|
||||
|
||||
response.raise_for_status()
|
||||
|
||||
try:
|
||||
data = response.json()
|
||||
return {
|
||||
"success": True,
|
||||
"data": data,
|
||||
"latency": latency,
|
||||
"error": None,
|
||||
"http_status": response.status_code
|
||||
}
|
||||
except json.JSONDecodeError:
|
||||
return {
|
||||
"success": False,
|
||||
"data": None,
|
||||
"latency": latency,
|
||||
"error": f"Invalid JSON response: {response.text[:200]}",
|
||||
"http_status": response.status_code
|
||||
}
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
latency = time.time() - start_time
|
||||
return {
|
||||
"success": False,
|
||||
"data": None,
|
||||
"latency": latency,
|
||||
"error": str(e),
|
||||
"http_status": getattr(e.response, 'status_code', None)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user