| 
									
										
										
										
											2024-07-26 11:20:23 +09:00
										 |  |  | import os | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-06 11:58:49 +08:00
										 |  |  | from flask_login import current_user | 
					
						
							|  |  |  | from flask_restful import Resource, reqparse | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-31 22:03:15 +08:00
										 |  |  | from controllers.console import api | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from controllers.console.app.error import ( | 
					
						
							|  |  |  |     CompletionRequestError, | 
					
						
							|  |  |  |     ProviderModelCurrentlyNotSupportError, | 
					
						
							|  |  |  |     ProviderNotInitializeError, | 
					
						
							|  |  |  |     ProviderQuotaExceededError, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-11-01 15:51:22 +08:00
										 |  |  | from controllers.console.wraps import account_initialization_required, setup_required | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from core.llm_generator.llm_generator import LLMGenerator | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from core.model_runtime.errors.invoke import InvokeError | 
					
						
							|  |  |  | from libs.login import login_required | 
					
						
							| 
									
										
										
										
											2023-05-31 22:03:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RuleGenerateApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     def post(self): | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("instruction", type=str, required=True, nullable=False, location="json") | 
					
						
							|  |  |  |         parser.add_argument("model_config", type=dict, required=True, nullable=False, location="json") | 
					
						
							|  |  |  |         parser.add_argument("no_variable", type=bool, required=True, default=False, location="json") | 
					
						
							| 
									
										
										
										
											2023-05-31 22:03:15 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         account = current_user | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         PROMPT_GENERATION_MAX_TOKENS = int(os.getenv("PROMPT_GENERATION_MAX_TOKENS", "512")) | 
					
						
							| 
									
										
										
										
											2023-05-31 22:03:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             rules = LLMGenerator.generate_rule_config( | 
					
						
							| 
									
										
										
										
											2024-07-23 19:52:14 +08:00
										 |  |  |                 tenant_id=account.current_tenant_id, | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 instruction=args["instruction"], | 
					
						
							|  |  |  |                 model_config=args["model_config"], | 
					
						
							|  |  |  |                 no_variable=args["no_variable"], | 
					
						
							|  |  |  |                 rule_config_max_tokens=PROMPT_GENERATION_MAX_TOKENS, | 
					
						
							| 
									
										
										
										
											2023-05-31 22:03:15 +08:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2023-07-17 00:14:19 +08:00
										 |  |  |         except ProviderTokenNotInitError as ex: | 
					
						
							|  |  |  |             raise ProviderNotInitializeError(ex.description) | 
					
						
							| 
									
										
										
										
											2023-05-31 22:03:15 +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) | 
					
						
							| 
									
										
										
										
											2023-05-31 22:03:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return rules | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 22:57:54 +09:00
										 |  |  | class RuleCodeGenerateApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     def post(self): | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							|  |  |  |         parser.add_argument("instruction", type=str, required=True, nullable=False, location="json") | 
					
						
							|  |  |  |         parser.add_argument("model_config", type=dict, required=True, nullable=False, location="json") | 
					
						
							|  |  |  |         parser.add_argument("no_variable", type=bool, required=True, default=False, location="json") | 
					
						
							|  |  |  |         parser.add_argument("code_language", type=str, required=False, default="javascript", location="json") | 
					
						
							|  |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         account = current_user | 
					
						
							|  |  |  |         CODE_GENERATION_MAX_TOKENS = int(os.getenv("CODE_GENERATION_MAX_TOKENS", "1024")) | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             code_result = LLMGenerator.generate_code( | 
					
						
							|  |  |  |                 tenant_id=account.current_tenant_id, | 
					
						
							|  |  |  |                 instruction=args["instruction"], | 
					
						
							|  |  |  |                 model_config=args["model_config"], | 
					
						
							|  |  |  |                 code_language=args["code_language"], | 
					
						
							|  |  |  |                 max_tokens=CODE_GENERATION_MAX_TOKENS, | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         except ProviderTokenNotInitError as ex: | 
					
						
							|  |  |  |             raise ProviderNotInitializeError(ex.description) | 
					
						
							|  |  |  |         except QuotaExceededError: | 
					
						
							|  |  |  |             raise ProviderQuotaExceededError() | 
					
						
							|  |  |  |         except ModelCurrentlyNotSupportError: | 
					
						
							|  |  |  |             raise ProviderModelCurrentlyNotSupportError() | 
					
						
							|  |  |  |         except InvokeError as e: | 
					
						
							|  |  |  |             raise CompletionRequestError(e.description) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return code_result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-18 16:33:53 +08:00
										 |  |  | class RuleStructuredOutputGenerateApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     def post(self): | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							|  |  |  |         parser.add_argument("instruction", type=str, required=True, nullable=False, location="json") | 
					
						
							|  |  |  |         parser.add_argument("model_config", type=dict, required=True, nullable=False, location="json") | 
					
						
							|  |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         account = current_user | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             structured_output = LLMGenerator.generate_structured_output( | 
					
						
							|  |  |  |                 tenant_id=account.current_tenant_id, | 
					
						
							|  |  |  |                 instruction=args["instruction"], | 
					
						
							|  |  |  |                 model_config=args["model_config"], | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         except ProviderTokenNotInitError as ex: | 
					
						
							|  |  |  |             raise ProviderNotInitializeError(ex.description) | 
					
						
							|  |  |  |         except QuotaExceededError: | 
					
						
							|  |  |  |             raise ProviderQuotaExceededError() | 
					
						
							|  |  |  |         except ModelCurrentlyNotSupportError: | 
					
						
							|  |  |  |             raise ProviderModelCurrentlyNotSupportError() | 
					
						
							|  |  |  |         except InvokeError as e: | 
					
						
							|  |  |  |             raise CompletionRequestError(e.description) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return structured_output | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | api.add_resource(RuleGenerateApi, "/rule-generate") | 
					
						
							| 
									
										
										
										
											2024-10-22 22:57:54 +09:00
										 |  |  | api.add_resource(RuleCodeGenerateApi, "/rule-code-generate") | 
					
						
							| 
									
										
										
										
											2025-04-18 16:33:53 +08:00
										 |  |  | api.add_resource(RuleStructuredOutputGenerateApi, "/rule-structured-output-generate") |