| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import logging | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-06 11:58:49 +08:00
										 |  |  | from flask_restful import Resource, reqparse | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from werkzeug.exceptions import InternalServerError, NotFound | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import services | 
					
						
							|  |  |  | from controllers.service_api import api | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from controllers.service_api.app.error import ( | 
					
						
							|  |  |  |     AppUnavailableError, | 
					
						
							|  |  |  |     CompletionRequestError, | 
					
						
							|  |  |  |     ConversationCompletedError, | 
					
						
							|  |  |  |     NotChatAppError, | 
					
						
							|  |  |  |     ProviderModelCurrentlyNotSupportError, | 
					
						
							|  |  |  |     ProviderNotInitializeError, | 
					
						
							|  |  |  |     ProviderQuotaExceededError, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-02-28 16:09:56 +08:00
										 |  |  | from controllers.service_api.wraps import FetchUserArg, WhereisUserArg, validate_app_token | 
					
						
							| 
									
										
										
										
											2025-03-19 17:24:02 +08:00
										 |  |  | from controllers.web.error import InvokeRateLimitError as InvokeRateLimitHttpError | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from core.app.apps.base_app_queue_manager import AppQueueManager | 
					
						
							|  |  |  | from core.app.entities.app_invoke_entities import InvokeFrom | 
					
						
							| 
									
										
										
										
											2024-07-10 13:31:35 +00:00
										 |  |  | from core.errors.error import ( | 
					
						
							|  |  |  |     ModelCurrentlyNotSupportError, | 
					
						
							|  |  |  |     ProviderTokenNotInitError, | 
					
						
							|  |  |  |     QuotaExceededError, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | from core.model_runtime.errors.invoke import InvokeError | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from libs import helper | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from libs.helper import uuid_value | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from models.model import App, AppMode, EndUser | 
					
						
							|  |  |  | from services.app_generate_service import AppGenerateService | 
					
						
							| 
									
										
										
										
											2025-03-19 17:24:02 +08:00
										 |  |  | from services.errors.llm import InvokeRateLimitError | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-28 16:09:56 +08:00
										 |  |  | class CompletionApi(Resource): | 
					
						
							|  |  |  |     @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON, required=True)) | 
					
						
							|  |  |  |     def post(self, app_model: App, end_user: EndUser): | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if app_model.mode != "completion": | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             raise AppUnavailableError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("inputs", type=dict, required=True, location="json") | 
					
						
							|  |  |  |         parser.add_argument("query", type=str, location="json", default="") | 
					
						
							|  |  |  |         parser.add_argument("files", type=list, required=False, location="json") | 
					
						
							|  |  |  |         parser.add_argument("response_mode", type=str, choices=["blocking", "streaming"], location="json") | 
					
						
							|  |  |  |         parser.add_argument("retriever_from", type=str, required=False, default="dev", location="json") | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         streaming = args["response_mode"] == "streaming" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         args["auto_generate_name"] = False | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             response = AppGenerateService.generate( | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |                 app_model=app_model, | 
					
						
							|  |  |  |                 user=end_user, | 
					
						
							|  |  |  |                 args=args, | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |                 invoke_from=InvokeFrom.SERVICE_API, | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  |                 streaming=streaming, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             return helper.compact_generate_response(response) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         except services.errors.conversation.ConversationNotExistsError: | 
					
						
							|  |  |  |             raise NotFound("Conversation Not Exists.") | 
					
						
							|  |  |  |         except services.errors.conversation.ConversationCompletedError: | 
					
						
							|  |  |  |             raise ConversationCompletedError() | 
					
						
							|  |  |  |         except services.errors.app_model_config.AppModelConfigBrokenError: | 
					
						
							|  |  |  |             logging.exception("App model config broken.") | 
					
						
							|  |  |  |             raise AppUnavailableError() | 
					
						
							| 
									
										
										
										
											2023-07-17 00:14:19 +08:00
										 |  |  |         except ProviderTokenNotInitError as ex: | 
					
						
							|  |  |  |             raise ProviderNotInitializeError(ex.description) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         except QuotaExceededError: | 
					
						
							|  |  |  |             raise ProviderQuotaExceededError() | 
					
						
							|  |  |  |         except ModelCurrentlyNotSupportError: | 
					
						
							|  |  |  |             raise ProviderModelCurrentlyNotSupportError() | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         except InvokeError as e: | 
					
						
							| 
									
										
										
										
											2024-01-04 17:49:55 +08:00
										 |  |  |             raise CompletionRequestError(e.description) | 
					
						
							| 
									
										
										
										
											2025-01-02 14:25:12 +08:00
										 |  |  |         except ValueError as e: | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             raise e | 
					
						
							| 
									
										
										
										
											2025-03-19 17:24:02 +08:00
										 |  |  |         except Exception: | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             logging.exception("internal server error.") | 
					
						
							|  |  |  |             raise InternalServerError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-28 16:09:56 +08:00
										 |  |  | class CompletionStopApi(Resource): | 
					
						
							|  |  |  |     @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON, required=True)) | 
					
						
							|  |  |  |     def post(self, app_model: App, end_user: EndUser, task_id): | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if app_model.mode != "completion": | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             raise AppUnavailableError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         AppQueueManager.set_stop_flag(task_id, InvokeFrom.SERVICE_API, end_user.id) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return {"result": "success"}, 200 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-28 16:09:56 +08:00
										 |  |  | class ChatApi(Resource): | 
					
						
							|  |  |  |     @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON, required=True)) | 
					
						
							|  |  |  |     def post(self, app_model: App, end_user: EndUser): | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         app_mode = AppMode.value_of(app_model.mode) | 
					
						
							| 
									
										
										
										
											2024-09-13 22:42:08 +08:00
										 |  |  |         if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}: | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             raise NotChatAppError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("inputs", type=dict, required=True, location="json") | 
					
						
							|  |  |  |         parser.add_argument("query", type=str, required=True, location="json") | 
					
						
							|  |  |  |         parser.add_argument("files", type=list, required=False, location="json") | 
					
						
							|  |  |  |         parser.add_argument("response_mode", type=str, choices=["blocking", "streaming"], location="json") | 
					
						
							|  |  |  |         parser.add_argument("conversation_id", type=uuid_value, location="json") | 
					
						
							|  |  |  |         parser.add_argument("retriever_from", type=str, required=False, default="dev", location="json") | 
					
						
							|  |  |  |         parser.add_argument("auto_generate_name", type=bool, required=False, default=True, location="json") | 
					
						
							| 
									
										
										
										
											2023-09-10 15:17:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         streaming = args["response_mode"] == "streaming" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             response = AppGenerateService.generate( | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 app_model=app_model, user=end_user, args=args, invoke_from=InvokeFrom.SERVICE_API, streaming=streaming | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             return helper.compact_generate_response(response) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         except services.errors.conversation.ConversationNotExistsError: | 
					
						
							|  |  |  |             raise NotFound("Conversation Not Exists.") | 
					
						
							|  |  |  |         except services.errors.conversation.ConversationCompletedError: | 
					
						
							|  |  |  |             raise ConversationCompletedError() | 
					
						
							|  |  |  |         except services.errors.app_model_config.AppModelConfigBrokenError: | 
					
						
							|  |  |  |             logging.exception("App model config broken.") | 
					
						
							|  |  |  |             raise AppUnavailableError() | 
					
						
							| 
									
										
										
										
											2023-07-17 00:14:19 +08:00
										 |  |  |         except ProviderTokenNotInitError as ex: | 
					
						
							|  |  |  |             raise ProviderNotInitializeError(ex.description) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         except QuotaExceededError: | 
					
						
							|  |  |  |             raise ProviderQuotaExceededError() | 
					
						
							|  |  |  |         except ModelCurrentlyNotSupportError: | 
					
						
							|  |  |  |             raise ProviderModelCurrentlyNotSupportError() | 
					
						
							| 
									
										
										
										
											2025-03-19 17:24:02 +08:00
										 |  |  |         except InvokeRateLimitError as ex: | 
					
						
							|  |  |  |             raise InvokeRateLimitHttpError(ex.description) | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         except InvokeError as e: | 
					
						
							| 
									
										
										
										
											2024-01-04 17:49:55 +08:00
										 |  |  |             raise CompletionRequestError(e.description) | 
					
						
							| 
									
										
										
										
											2025-01-02 14:25:12 +08:00
										 |  |  |         except ValueError as e: | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             raise e | 
					
						
							| 
									
										
										
										
											2025-03-19 17:24:02 +08:00
										 |  |  |         except Exception: | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             logging.exception("internal server error.") | 
					
						
							|  |  |  |             raise InternalServerError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-28 16:09:56 +08:00
										 |  |  | class ChatStopApi(Resource): | 
					
						
							|  |  |  |     @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON, required=True)) | 
					
						
							|  |  |  |     def post(self, app_model: App, end_user: EndUser, task_id): | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         app_mode = AppMode.value_of(app_model.mode) | 
					
						
							| 
									
										
										
										
											2024-09-13 22:42:08 +08:00
										 |  |  |         if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}: | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             raise NotChatAppError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         AppQueueManager.set_stop_flag(task_id, InvokeFrom.SERVICE_API, end_user.id) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return {"result": "success"}, 200 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | api.add_resource(CompletionApi, "/completion-messages") | 
					
						
							|  |  |  | api.add_resource(CompletionStopApi, "/completion-messages/<string:task_id>/stop") | 
					
						
							|  |  |  | api.add_resource(ChatApi, "/chat-messages") | 
					
						
							|  |  |  | api.add_resource(ChatStopApi, "/chat-messages/<string:task_id>/stop") |