app_model_config_service.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import re
  2. import uuid
  3. from core.constant import llm_constant
  4. from models.account import Account
  5. from services.dataset_service import DatasetService
  6. class AppModelConfigService:
  7. @staticmethod
  8. def is_dataset_exists(account: Account, dataset_id: str) -> bool:
  9. # verify if the dataset ID exists
  10. dataset = DatasetService.get_dataset(dataset_id)
  11. if not dataset:
  12. return False
  13. if dataset.tenant_id != account.current_tenant_id:
  14. return False
  15. return True
  16. @staticmethod
  17. def validate_model_completion_params(cp: dict, model_name: str) -> dict:
  18. # 6. model.completion_params
  19. if not isinstance(cp, dict):
  20. raise ValueError("model.completion_params must be of object type")
  21. # max_tokens
  22. if 'max_tokens' not in cp:
  23. cp["max_tokens"] = 512
  24. if not isinstance(cp["max_tokens"], int) or cp["max_tokens"] <= 0 or cp["max_tokens"] > \
  25. llm_constant.max_context_token_length[model_name]:
  26. raise ValueError(
  27. "max_tokens must be an integer greater than 0 and not exceeding the maximum value of the corresponding model")
  28. # temperature
  29. if 'temperature' not in cp:
  30. cp["temperature"] = 1
  31. if not isinstance(cp["temperature"], (float, int)) or cp["temperature"] < 0 or cp["temperature"] > 2:
  32. raise ValueError("temperature must be a float between 0 and 2")
  33. # top_p
  34. if 'top_p' not in cp:
  35. cp["top_p"] = 1
  36. if not isinstance(cp["top_p"], (float, int)) or cp["top_p"] < 0 or cp["top_p"] > 2:
  37. raise ValueError("top_p must be a float between 0 and 2")
  38. # presence_penalty
  39. if 'presence_penalty' not in cp:
  40. cp["presence_penalty"] = 0
  41. if not isinstance(cp["presence_penalty"], (float, int)) or cp["presence_penalty"] < -2 or cp["presence_penalty"] > 2:
  42. raise ValueError("presence_penalty must be a float between -2 and 2")
  43. # presence_penalty
  44. if 'frequency_penalty' not in cp:
  45. cp["frequency_penalty"] = 0
  46. if not isinstance(cp["frequency_penalty"], (float, int)) or cp["frequency_penalty"] < -2 or cp["frequency_penalty"] > 2:
  47. raise ValueError("frequency_penalty must be a float between -2 and 2")
  48. # Filter out extra parameters
  49. filtered_cp = {
  50. "max_tokens": cp["max_tokens"],
  51. "temperature": cp["temperature"],
  52. "top_p": cp["top_p"],
  53. "presence_penalty": cp["presence_penalty"],
  54. "frequency_penalty": cp["frequency_penalty"]
  55. }
  56. return filtered_cp
  57. @staticmethod
  58. def validate_configuration(account: Account, config: dict, mode: str) -> dict:
  59. # opening_statement
  60. if 'opening_statement' not in config or not config["opening_statement"]:
  61. config["opening_statement"] = ""
  62. if not isinstance(config["opening_statement"], str):
  63. raise ValueError("opening_statement must be of string type")
  64. # suggested_questions
  65. if 'suggested_questions' not in config or not config["suggested_questions"]:
  66. config["suggested_questions"] = []
  67. if not isinstance(config["suggested_questions"], list):
  68. raise ValueError("suggested_questions must be of list type")
  69. for question in config["suggested_questions"]:
  70. if not isinstance(question, str):
  71. raise ValueError("Elements in suggested_questions list must be of string type")
  72. # suggested_questions_after_answer
  73. if 'suggested_questions_after_answer' not in config or not config["suggested_questions_after_answer"]:
  74. config["suggested_questions_after_answer"] = {
  75. "enabled": False
  76. }
  77. if not isinstance(config["suggested_questions_after_answer"], dict):
  78. raise ValueError("suggested_questions_after_answer must be of dict type")
  79. if "enabled" not in config["suggested_questions_after_answer"] or not config["suggested_questions_after_answer"]["enabled"]:
  80. config["suggested_questions_after_answer"]["enabled"] = False
  81. if not isinstance(config["suggested_questions_after_answer"]["enabled"], bool):
  82. raise ValueError("enabled in suggested_questions_after_answer must be of boolean type")
  83. # more_like_this
  84. if 'more_like_this' not in config or not config["more_like_this"]:
  85. config["more_like_this"] = {
  86. "enabled": False
  87. }
  88. if not isinstance(config["more_like_this"], dict):
  89. raise ValueError("more_like_this must be of dict type")
  90. if "enabled" not in config["more_like_this"] or not config["more_like_this"]["enabled"]:
  91. config["more_like_this"]["enabled"] = False
  92. if not isinstance(config["more_like_this"]["enabled"], bool):
  93. raise ValueError("enabled in more_like_this must be of boolean type")
  94. # model
  95. if 'model' not in config:
  96. raise ValueError("model is required")
  97. if not isinstance(config["model"], dict):
  98. raise ValueError("model must be of object type")
  99. # model.provider
  100. if 'provider' not in config["model"] or config["model"]["provider"] != "openai":
  101. raise ValueError("model.provider must be 'openai'")
  102. # model.name
  103. if 'name' not in config["model"]:
  104. raise ValueError("model.name is required")
  105. if config["model"]["name"] not in llm_constant.models_by_mode[mode]:
  106. raise ValueError("model.name must be in the specified model list")
  107. # model.completion_params
  108. if 'completion_params' not in config["model"]:
  109. raise ValueError("model.completion_params is required")
  110. config["model"]["completion_params"] = AppModelConfigService.validate_model_completion_params(
  111. config["model"]["completion_params"],
  112. config["model"]["name"]
  113. )
  114. # user_input_form
  115. if "user_input_form" not in config or not config["user_input_form"]:
  116. config["user_input_form"] = []
  117. if not isinstance(config["user_input_form"], list):
  118. raise ValueError("user_input_form must be a list of objects")
  119. variables = []
  120. for item in config["user_input_form"]:
  121. key = list(item.keys())[0]
  122. if key not in ["text-input", "select"]:
  123. raise ValueError("Keys in user_input_form list can only be 'text-input' or 'select'")
  124. form_item = item[key]
  125. if 'label' not in form_item:
  126. raise ValueError("label is required in user_input_form")
  127. if not isinstance(form_item["label"], str):
  128. raise ValueError("label in user_input_form must be of string type")
  129. if 'variable' not in form_item:
  130. raise ValueError("variable is required in user_input_form")
  131. if not isinstance(form_item["variable"], str):
  132. raise ValueError("variable in user_input_form must be of string type")
  133. pattern = re.compile(r"^(?!\d)[\u4e00-\u9fa5A-Za-z0-9_\U0001F300-\U0001F64F\U0001F680-\U0001F6FF]{1,100}$")
  134. if pattern.match(form_item["variable"]) is None:
  135. raise ValueError("variable in user_input_form must be a string, "
  136. "and cannot start with a number")
  137. variables.append(form_item["variable"])
  138. if 'required' not in form_item or not form_item["required"]:
  139. form_item["required"] = False
  140. if not isinstance(form_item["required"], bool):
  141. raise ValueError("required in user_input_form must be of boolean type")
  142. if key == "select":
  143. if 'options' not in form_item or not form_item["options"]:
  144. form_item["options"] = []
  145. if not isinstance(form_item["options"], list):
  146. raise ValueError("options in user_input_form must be a list of strings")
  147. if "default" in form_item and form_item['default'] \
  148. and form_item["default"] not in form_item["options"]:
  149. raise ValueError("default value in user_input_form must be in the options list")
  150. # pre_prompt
  151. if "pre_prompt" not in config or not config["pre_prompt"]:
  152. config["pre_prompt"] = ""
  153. if not isinstance(config["pre_prompt"], str):
  154. raise ValueError("pre_prompt must be of string type")
  155. template_vars = re.findall(r"\{\{(\w+)\}\}", config["pre_prompt"])
  156. for var in template_vars:
  157. if var not in variables:
  158. raise ValueError("Template variables in pre_prompt must be defined in user_input_form")
  159. # agent_mode
  160. if "agent_mode" not in config or not config["agent_mode"]:
  161. config["agent_mode"] = {
  162. "enabled": False,
  163. "tools": []
  164. }
  165. if not isinstance(config["agent_mode"], dict):
  166. raise ValueError("agent_mode must be of object type")
  167. if "enabled" not in config["agent_mode"] or not config["agent_mode"]["enabled"]:
  168. config["agent_mode"]["enabled"] = False
  169. if not isinstance(config["agent_mode"]["enabled"], bool):
  170. raise ValueError("enabled in agent_mode must be of boolean type")
  171. if "tools" not in config["agent_mode"] or not config["agent_mode"]["tools"]:
  172. config["agent_mode"]["tools"] = []
  173. if not isinstance(config["agent_mode"]["tools"], list):
  174. raise ValueError("tools in agent_mode must be a list of objects")
  175. for tool in config["agent_mode"]["tools"]:
  176. key = list(tool.keys())[0]
  177. if key not in ["sensitive-word-avoidance", "dataset"]:
  178. raise ValueError("Keys in agent_mode.tools list can only be 'sensitive-word-avoidance' or 'dataset'")
  179. tool_item = tool[key]
  180. if "enabled" not in tool_item or not tool_item["enabled"]:
  181. tool_item["enabled"] = False
  182. if not isinstance(tool_item["enabled"], bool):
  183. raise ValueError("enabled in agent_mode.tools must be of boolean type")
  184. if key == "sensitive-word-avoidance":
  185. if "words" not in tool_item or not tool_item["words"]:
  186. tool_item["words"] = ""
  187. if not isinstance(tool_item["words"], str):
  188. raise ValueError("words in sensitive-word-avoidance must be of string type")
  189. if "canned_response" not in tool_item or not tool_item["canned_response"]:
  190. tool_item["canned_response"] = ""
  191. if not isinstance(tool_item["canned_response"], str):
  192. raise ValueError("canned_response in sensitive-word-avoidance must be of string type")
  193. elif key == "dataset":
  194. if 'id' not in tool_item:
  195. raise ValueError("id is required in dataset")
  196. try:
  197. uuid.UUID(tool_item["id"])
  198. except ValueError:
  199. raise ValueError("id in dataset must be of UUID type")
  200. if not AppModelConfigService.is_dataset_exists(account, tool_item["id"]):
  201. raise ValueError("Dataset ID does not exist, please check your permission.")
  202. # Filter out extra parameters
  203. filtered_config = {
  204. "opening_statement": config["opening_statement"],
  205. "suggested_questions": config["suggested_questions"],
  206. "suggested_questions_after_answer": config["suggested_questions_after_answer"],
  207. "more_like_this": config["more_like_this"],
  208. "model": {
  209. "provider": config["model"]["provider"],
  210. "name": config["model"]["name"],
  211. "completion_params": config["model"]["completion_params"]
  212. },
  213. "user_input_form": config["user_input_form"],
  214. "pre_prompt": config["pre_prompt"],
  215. "agent_mode": config["agent_mode"]
  216. }
  217. return filtered_config