ndc8
commited on
Commit
·
09c9042
1
Parent(s):
8d962fd
Add API response endpoint and tests
Browse files- backend_service.py +16 -2
- test_health_endpoint.py +44 -0
backend_service.py
CHANGED
@@ -3,8 +3,6 @@ FastAPI Backend AI Service converted from Gradio app
|
|
3 |
Provides OpenAI-compatible chat completion endpoints
|
4 |
"""
|
5 |
|
6 |
-
import os
|
7 |
-
import sys
|
8 |
import asyncio
|
9 |
import logging
|
10 |
import time
|
@@ -573,6 +571,22 @@ async def create_completion(
|
|
573 |
logger.error(f"Error in completion: {e}")
|
574 |
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
575 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
576 |
@app.exception_handler(Exception)
|
577 |
async def global_exception_handler(request: Any, exc: Exception) -> JSONResponse:
|
578 |
"""Global exception handler"""
|
|
|
3 |
Provides OpenAI-compatible chat completion endpoints
|
4 |
"""
|
5 |
|
|
|
|
|
6 |
import asyncio
|
7 |
import logging
|
8 |
import time
|
|
|
571 |
logger.error(f"Error in completion: {e}")
|
572 |
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
573 |
|
574 |
+
@app.post("/api/response")
|
575 |
+
async def api_response(request: Request):
|
576 |
+
"""Endpoint to receive and send responses via API."""
|
577 |
+
try:
|
578 |
+
data = await request.json()
|
579 |
+
message = data.get("message", "No message provided")
|
580 |
+
response: dict[str, str] = {
|
581 |
+
"status": "success",
|
582 |
+
"received_message": message,
|
583 |
+
"response_message": f"You sent: {message}"
|
584 |
+
}
|
585 |
+
return JSONResponse(content=response)
|
586 |
+
except Exception as e:
|
587 |
+
logger.error(f"Error processing API response: {e}")
|
588 |
+
raise HTTPException(status_code=500, detail="Internal server error")
|
589 |
+
|
590 |
@app.exception_handler(Exception)
|
591 |
async def global_exception_handler(request: Any, exc: Exception) -> JSONResponse:
|
592 |
"""Global exception handler"""
|
test_health_endpoint.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
def test_health_endpoint():
|
4 |
+
"""Test the health endpoint of the API."""
|
5 |
+
base_url = "http://localhost:8000"
|
6 |
+
health_url = f"{base_url}/health"
|
7 |
+
|
8 |
+
try:
|
9 |
+
response = requests.get(health_url, timeout=10)
|
10 |
+
response.raise_for_status()
|
11 |
+
data = response.json()
|
12 |
+
|
13 |
+
assert response.status_code == 200, "Health endpoint did not return status 200"
|
14 |
+
assert data["status"] == "healthy", "Service is not healthy"
|
15 |
+
assert "model" in data, "Model information missing in health response"
|
16 |
+
assert "version" in data, "Version information missing in health response"
|
17 |
+
|
18 |
+
print("✅ Health endpoint test passed.")
|
19 |
+
except Exception as e:
|
20 |
+
print(f"❌ Health endpoint test failed: {e}")
|
21 |
+
|
22 |
+
def test_api_response():
|
23 |
+
"""Test the new API response endpoint."""
|
24 |
+
base_url = "http://localhost:8000"
|
25 |
+
response_url = f"{base_url}/api/response"
|
26 |
+
|
27 |
+
try:
|
28 |
+
payload = {"message": "Hello, API!"}
|
29 |
+
response = requests.post(response_url, json=payload, timeout=10)
|
30 |
+
response.raise_for_status()
|
31 |
+
data = response.json()
|
32 |
+
|
33 |
+
assert response.status_code == 200, "API response endpoint did not return status 200"
|
34 |
+
assert data["status"] == "success", "API response status is not success"
|
35 |
+
assert data["received_message"] == "Hello, API!", "Received message mismatch"
|
36 |
+
assert "response_message" in data, "Response message missing in API response"
|
37 |
+
|
38 |
+
print("✅ API response endpoint test passed.")
|
39 |
+
except Exception as e:
|
40 |
+
print(f"❌ API response endpoint test failed: {e}")
|
41 |
+
|
42 |
+
if __name__ == "__main__":
|
43 |
+
test_health_endpoint()
|
44 |
+
test_api_response()
|