120 lines
4.2 KiB
Python
120 lines
4.2 KiB
Python
def format_response(prompt, language, request_id, timestamp):
|
||
"""
|
||
Format a standardized response structure based on language.
|
||
|
||
This utility creates a consistent response structure with the appropriate
|
||
keys based on the specified language.
|
||
|
||
:param prompt: The original user prompt
|
||
:type prompt: str
|
||
:param language: Language code ('en' or 'ar')
|
||
:type language: str
|
||
:param request_id: Unique identifier for the request
|
||
:type request_id: str
|
||
:param timestamp: ISO-formatted timestamp
|
||
:type timestamp: str
|
||
:return: Formatted response structure
|
||
:rtype: dict
|
||
"""
|
||
if language == "ar":
|
||
return {
|
||
"حالة": "نجاح",
|
||
"معرف_الطلب": request_id,
|
||
"الطابع_الزمني": timestamp,
|
||
"الاستعلام": prompt,
|
||
"التحليلات": [],
|
||
}
|
||
else:
|
||
return {
|
||
"status": "success",
|
||
"request_id": request_id,
|
||
"timestamp": timestamp,
|
||
"prompt": prompt,
|
||
"insights": [],
|
||
}
|
||
|
||
|
||
def format_error_response(message, status_code, language="en"):
|
||
"""
|
||
Format a standardized error response.
|
||
|
||
:param message: Error message
|
||
:type message: str
|
||
:param status_code: HTTP status code
|
||
:type status_code: int
|
||
:param language: Language code ('en' or 'ar')
|
||
:type language: str
|
||
:return: Formatted error response
|
||
:rtype: dict
|
||
"""
|
||
if language == "ar":
|
||
return {"حالة": "خطأ", "رسالة": message, "رمز_الحالة": status_code}
|
||
else:
|
||
return {"status": "error", "message": message, "status_code": status_code}
|
||
|
||
|
||
def format_insights_for_display(insights, language="en"):
|
||
"""
|
||
Format insights for human-readable display.
|
||
|
||
:param insights: Raw insights data
|
||
:type insights: dict
|
||
:param language: Language code ('en' or 'ar')
|
||
:type language: str
|
||
:return: Human-readable formatted insights
|
||
:rtype: str
|
||
"""
|
||
formatted_text = ""
|
||
|
||
# Determine keys based on language
|
||
insights_key = "التحليلات" if language == "ar" else "insights"
|
||
recs_key = "التوصيات" if language == "ar" else "recommendations"
|
||
|
||
# Format insights
|
||
if insights_key in insights and insights[insights_key]:
|
||
header = (
|
||
"## نتائج التحليل\n\n" if language == "ar" else "## Analysis Results\n\n"
|
||
)
|
||
formatted_text += header
|
||
|
||
for insight in insights[insights_key]:
|
||
if isinstance(insight, dict):
|
||
# Add insight type as header
|
||
if "type" in insight or "نوع" in insight:
|
||
type_key = "نوع" if language == "ar" else "type"
|
||
insight_type = insight.get(
|
||
type_key, insight.get("type", insight.get("نوع", ""))
|
||
)
|
||
formatted_text += f"### {insight_type}\n\n"
|
||
|
||
# Format results if present
|
||
results_key = "النتائج" if language == "ar" else "results"
|
||
if results_key in insight:
|
||
for result in insight[results_key]:
|
||
model_key = "النموذج" if language == "ar" else "model"
|
||
error_key = "خطأ" if language == "ar" else "error"
|
||
count_key = "العدد" if language == "ar" else "count"
|
||
|
||
model_name = result.get(model_key, result.get("model", ""))
|
||
|
||
if error_key in result:
|
||
formatted_text += (
|
||
f"- **{model_name}**: {result[error_key]}\n"
|
||
)
|
||
elif count_key in result:
|
||
formatted_text += (
|
||
f"- **{model_name}**: {result[count_key]}\n"
|
||
)
|
||
|
||
formatted_text += "\n"
|
||
|
||
# Format recommendations
|
||
if recs_key in insights and insights[recs_key]:
|
||
header = "## التوصيات\n\n" if language == "ar" else "## Recommendations\n\n"
|
||
formatted_text += header
|
||
|
||
for rec in insights[recs_key]:
|
||
formatted_text += f"- {rec}\n"
|
||
|
||
return formatted_text
|