feat:重试和失败均通知前端
This commit is contained in:
@@ -16,6 +16,8 @@ class Settings(BaseSettings):
|
|||||||
env_file_encoding='utf-8',
|
env_file_encoding='utf-8',
|
||||||
extra='ignore' # 忽略环境变量中多余的键
|
extra='ignore' # 忽略环境变量中多余的键
|
||||||
)
|
)
|
||||||
|
# 启动端口
|
||||||
|
SERVE_PROD: int = Field(default=8000, description='')
|
||||||
# 调试配饰
|
# 调试配饰
|
||||||
LOCAL: int = Field(default=0, description="是否在本地运行,1表示本地运行,0表示生产环境运行")
|
LOCAL: int = Field(default=0, description="是否在本地运行,1表示本地运行,0表示生产环境运行")
|
||||||
|
|
||||||
|
|||||||
@@ -27,4 +27,4 @@ if __name__ == "__main__":
|
|||||||
agent_api = LCAgent(enable_async=True, api_path='/api/v1/agent')
|
agent_api = LCAgent(enable_async=True, api_path='/api/v1/agent')
|
||||||
reface_api = ReFace(api_path='/api/v1/reface')
|
reface_api = ReFace(api_path='/api/v1/reface')
|
||||||
server = ls.LitServer([chat_boot_api, agent_api, reface_api])
|
server = ls.LitServer([chat_boot_api, agent_api, reface_api])
|
||||||
server.run(port=8000)
|
server.run(port=settings.SERVE_PROD)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
import uuid
|
import uuid
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
@@ -15,6 +16,7 @@ from app.server.ChatbotAgent.core.redis_manager import RedisManager
|
|||||||
from app.server.ChatbotAgent.core.stylist_agent_server import AsyncStylistAgent
|
from app.server.ChatbotAgent.core.stylist_agent_server import AsyncStylistAgent
|
||||||
from app.server.ChatbotAgent.core.prompt import SUMMARY_PROMPT
|
from app.server.ChatbotAgent.core.prompt import SUMMARY_PROMPT
|
||||||
from app.server.ChatbotAgent.core.vector_database import VectorDatabase
|
from app.server.ChatbotAgent.core.vector_database import VectorDatabase
|
||||||
|
from app.server.utils.request_post import post_request
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -262,7 +264,14 @@ class LCAgent(ls.LitAPI):
|
|||||||
logger.error(f"Outfit {outfit_id} failed with error: {result}. Current retries: {current_retries}.")
|
logger.error(f"Outfit {outfit_id} failed with error: {result}. Current retries: {current_retries}.")
|
||||||
|
|
||||||
if current_retries < retry_limit:
|
if current_retries < retry_limit:
|
||||||
# 尚未达到重试上限,准备重试
|
# 尚未达到重试上限,准备重试 并通知前端
|
||||||
|
object_data = {
|
||||||
|
'outfit_id': outfit_id,
|
||||||
|
"status": "retrying",
|
||||||
|
"path": "",
|
||||||
|
}
|
||||||
|
post_request(url=f'{callback_url}/api/style/callback', data=json.dumps(object_data))
|
||||||
|
|
||||||
task_info["retries"] += 1
|
task_info["retries"] += 1
|
||||||
logger.info(f"--- Retrying outfit {outfit_id} (Attempt {current_retries + 1}/{retry_limit}). ---")
|
logger.info(f"--- Retrying outfit {outfit_id} (Attempt {current_retries + 1}/{retry_limit}). ---")
|
||||||
|
|
||||||
@@ -286,7 +295,13 @@ class LCAgent(ls.LitAPI):
|
|||||||
# 清理旧任务(可选,但推荐,以防内存泄漏或混淆)
|
# 清理旧任务(可选,但推荐,以防内存泄漏或混淆)
|
||||||
del task_map[task]
|
del task_map[task]
|
||||||
else:
|
else:
|
||||||
# 达到重试上限,最终失败
|
# 达到重试上限,最终失败 , 并通知前端
|
||||||
|
object_data = {
|
||||||
|
'outfit_id': outfit_id,
|
||||||
|
"status": "failed",
|
||||||
|
"path": "",
|
||||||
|
}
|
||||||
|
post_request(url=f'{callback_url}/api/style/callback', data=json.dumps(object_data))
|
||||||
failed_outfits.append(f"Outfit {outfit_id} ultimately failed after {retry_limit} retries: {result}")
|
failed_outfits.append(f"Outfit {outfit_id} ultimately failed after {retry_limit} retries: {result}")
|
||||||
del task_map[task]
|
del task_map[task]
|
||||||
|
|
||||||
|
|||||||
@@ -38,13 +38,6 @@ class AsyncStylistAgent:
|
|||||||
self.local_db = local_db
|
self.local_db = local_db
|
||||||
self.gemini_model_name = gemini_model_name
|
self.gemini_model_name = gemini_model_name
|
||||||
self.stop_reason = ""
|
self.stop_reason = ""
|
||||||
self.headers = {
|
|
||||||
'Accept': "*/*",
|
|
||||||
'Accept-Encoding': "gzip, deflate, br",
|
|
||||||
'User-Agent': "PostmanRuntime-ApipostRuntime/1.1.0",
|
|
||||||
'Connection': "keep-alive",
|
|
||||||
'Content-Type': "application/json"
|
|
||||||
}
|
|
||||||
|
|
||||||
# 存储桶配置
|
# 存储桶配置
|
||||||
try:
|
try:
|
||||||
@@ -262,7 +255,7 @@ class AsyncStylistAgent:
|
|||||||
"request_summary": request_summary,
|
"request_summary": request_summary,
|
||||||
"occasions": occasions
|
"occasions": occasions
|
||||||
}
|
}
|
||||||
response = post_request(url=callback_url, data=json.dumps(response_data), headers=self.headers)
|
response = post_request(url=callback_url, data=json.dumps(response_data))
|
||||||
logger.info(f"request data :{json.dumps(response_data, ensure_ascii=False, indent=2)} | JAVA callback info -> status:{response.status_code} | message:{response.text}")
|
logger.info(f"request data :{json.dumps(response_data, ensure_ascii=False, indent=2)} | JAVA callback info -> status:{response.status_code} | message:{response.text}")
|
||||||
return response_data
|
return response_data
|
||||||
else:
|
else:
|
||||||
@@ -520,7 +513,13 @@ class AsyncStylistAgent:
|
|||||||
user_id,
|
user_id,
|
||||||
url
|
url
|
||||||
)
|
)
|
||||||
|
# 推荐即将完成 回调通知前端
|
||||||
|
self.post_operation(
|
||||||
|
status="almost_done",
|
||||||
|
message="Recommendation has been completed and the outfit is being assembled",
|
||||||
|
callback_url=url,
|
||||||
|
img_path="",
|
||||||
|
)
|
||||||
final_image_path, _ = await self._merge_images(self.outfit_id, user_id, self.stylist_name)
|
final_image_path, _ = await self._merge_images(self.outfit_id, user_id, self.stylist_name)
|
||||||
response_data = self.post_operation(
|
response_data = self.post_operation(
|
||||||
status="stop",
|
status="stop",
|
||||||
|
|||||||
@@ -4,18 +4,24 @@ import time
|
|||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
|
||||||
def post_request(url, data=None, json_data=None, headers=None, auth=None, timeout=5):
|
def post_request(url, data=None, json_data=None, auth=None, timeout=5):
|
||||||
"""
|
"""
|
||||||
发送POST请求的封装函数
|
发送POST请求的封装函数
|
||||||
|
|
||||||
:param url: 接口的URL地址
|
:param url: 接口的URL地址
|
||||||
:param data: 要发送的数据(字典形式,用于表单数据等,会自动编码)
|
:param data: 要发送的数据(字典形式,用于表单数据等,会自动编码)
|
||||||
:param json_data: 要发送的JSON数据(字典形式,会自动转换为JSON字符串)
|
:param json_data: 要发送的JSON数据(字典形式,会自动转换为JSON字符串)
|
||||||
:param headers: 请求头字典
|
|
||||||
:param auth: 认证信息(如 ('username', 'password') 形式用于基本认证)
|
:param auth: 认证信息(如 ('username', 'password') 形式用于基本认证)
|
||||||
:param timeout: 超时时间,单位为秒
|
:param timeout: 超时时间,单位为秒
|
||||||
:return: 返回接口的响应对象
|
:return: 返回接口的响应对象
|
||||||
"""
|
"""
|
||||||
|
headers = {
|
||||||
|
'Accept': "*/*",
|
||||||
|
'Accept-Encoding': "gzip, deflate, br",
|
||||||
|
'User-Agent': "PostmanRuntime-ApipostRuntime/1.1.0",
|
||||||
|
'Connection': "keep-alive",
|
||||||
|
'Content-Type': "application/json"
|
||||||
|
}
|
||||||
try:
|
try:
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
url,
|
url,
|
||||||
@@ -52,6 +58,6 @@ if __name__ == '__main__':
|
|||||||
'Content-Type': "application/json"
|
'Content-Type': "application/json"
|
||||||
}
|
}
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
X = post_request(url=url, data=json.dumps(object_data), headers=headers)
|
X = post_request(url=url, data=json.dumps(object_data))
|
||||||
print(time.time() - start_time)
|
print(time.time() - start_time)
|
||||||
print(X)
|
print(X)
|
||||||
|
|||||||
Reference in New Issue
Block a user