completion_service.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. import json
  2. import logging
  3. import threading
  4. import time
  5. import uuid
  6. from typing import Generator, Union, Any, Optional
  7. from flask import current_app, Flask
  8. from redis.client import PubSub
  9. from sqlalchemy import and_
  10. from core.completion import Completion
  11. from core.conversation_message_task import PubHandler, ConversationTaskStoppedException
  12. from core.model_providers.error import LLMBadRequestError, LLMAPIConnectionError, LLMAPIUnavailableError, \
  13. LLMRateLimitError, \
  14. LLMAuthorizationError, ProviderTokenNotInitError, QuotaExceededError, ModelCurrentlyNotSupportError
  15. from extensions.ext_database import db
  16. from extensions.ext_redis import redis_client
  17. from models.model import Conversation, AppModelConfig, App, Account, EndUser, Message
  18. from services.app_model_config_service import AppModelConfigService
  19. from services.errors.app import MoreLikeThisDisabledError
  20. from services.errors.app_model_config import AppModelConfigBrokenError
  21. from services.errors.completion import CompletionStoppedError
  22. from services.errors.conversation import ConversationNotExistsError, ConversationCompletedError
  23. from services.errors.message import MessageNotExistsError
  24. class CompletionService:
  25. @classmethod
  26. def completion(cls, app_model: App, user: Union[Account | EndUser], args: Any,
  27. from_source: str, streaming: bool = True,
  28. is_model_config_override: bool = False) -> Union[dict | Generator]:
  29. # is streaming mode
  30. inputs = args['inputs']
  31. query = args['query']
  32. if app_model.mode != 'completion' and not query:
  33. raise ValueError('query is required')
  34. query = query.replace('\x00', '')
  35. conversation_id = args['conversation_id'] if 'conversation_id' in args else None
  36. conversation = None
  37. if conversation_id:
  38. conversation_filter = [
  39. Conversation.id == args['conversation_id'],
  40. Conversation.app_id == app_model.id,
  41. Conversation.status == 'normal'
  42. ]
  43. if from_source == 'console':
  44. conversation_filter.append(Conversation.from_account_id == user.id)
  45. else:
  46. conversation_filter.append(Conversation.from_end_user_id == user.id if user else None)
  47. conversation = db.session.query(Conversation).filter(and_(*conversation_filter)).first()
  48. if not conversation:
  49. raise ConversationNotExistsError()
  50. if conversation.status != 'normal':
  51. raise ConversationCompletedError()
  52. if not conversation.override_model_configs:
  53. app_model_config = db.session.query(AppModelConfig).filter(
  54. AppModelConfig.id == conversation.app_model_config_id,
  55. AppModelConfig.app_id == app_model.id
  56. ).first()
  57. if not app_model_config:
  58. raise AppModelConfigBrokenError()
  59. else:
  60. conversation_override_model_configs = json.loads(conversation.override_model_configs)
  61. app_model_config = AppModelConfig(
  62. id=conversation.app_model_config_id,
  63. app_id=app_model.id,
  64. )
  65. app_model_config = app_model_config.from_model_config_dict(conversation_override_model_configs)
  66. if is_model_config_override:
  67. # build new app model config
  68. if 'model' not in args['model_config']:
  69. raise ValueError('model_config.model is required')
  70. if 'completion_params' not in args['model_config']['model']:
  71. raise ValueError('model_config.model.completion_params is required')
  72. completion_params = AppModelConfigService.validate_model_completion_params(
  73. cp=args['model_config']['model']['completion_params'],
  74. model_name=app_model_config.model_dict["name"]
  75. )
  76. app_model_config_model = app_model_config.model_dict
  77. app_model_config_model['completion_params'] = completion_params
  78. app_model_config.retriever_resource = json.dumps({'enabled': True})
  79. app_model_config = app_model_config.copy()
  80. app_model_config.model = json.dumps(app_model_config_model)
  81. else:
  82. if app_model.app_model_config_id is None:
  83. raise AppModelConfigBrokenError()
  84. app_model_config = app_model.app_model_config
  85. if not app_model_config:
  86. raise AppModelConfigBrokenError()
  87. if is_model_config_override:
  88. if not isinstance(user, Account):
  89. raise Exception("Only account can override model config")
  90. # validate config
  91. model_config = AppModelConfigService.validate_configuration(
  92. tenant_id=app_model.tenant_id,
  93. account=user,
  94. config=args['model_config'],
  95. mode=app_model.mode
  96. )
  97. app_model_config = AppModelConfig(
  98. id=app_model_config.id,
  99. app_id=app_model.id,
  100. )
  101. app_model_config = app_model_config.from_model_config_dict(model_config)
  102. # clean input by app_model_config form rules
  103. inputs = cls.get_cleaned_inputs(inputs, app_model_config)
  104. generate_task_id = str(uuid.uuid4())
  105. pubsub = redis_client.pubsub()
  106. pubsub.subscribe(PubHandler.generate_channel_name(user, generate_task_id))
  107. user = cls.get_real_user_instead_of_proxy_obj(user)
  108. generate_worker_thread = threading.Thread(target=cls.generate_worker, kwargs={
  109. 'flask_app': current_app._get_current_object(),
  110. 'generate_task_id': generate_task_id,
  111. 'detached_app_model': app_model,
  112. 'app_model_config': app_model_config,
  113. 'query': query,
  114. 'inputs': inputs,
  115. 'detached_user': user,
  116. 'detached_conversation': conversation,
  117. 'streaming': streaming,
  118. 'is_model_config_override': is_model_config_override,
  119. 'retriever_from': args['retriever_from'] if 'retriever_from' in args else 'dev'
  120. })
  121. generate_worker_thread.start()
  122. # wait for 10 minutes to close the thread
  123. cls.countdown_and_close(current_app._get_current_object(), generate_worker_thread, pubsub, user, generate_task_id)
  124. return cls.compact_response(pubsub, streaming)
  125. @classmethod
  126. def get_real_user_instead_of_proxy_obj(cls, user: Union[Account, EndUser]):
  127. if isinstance(user, Account):
  128. user = db.session.query(Account).filter(Account.id == user.id).first()
  129. elif isinstance(user, EndUser):
  130. user = db.session.query(EndUser).filter(EndUser.id == user.id).first()
  131. else:
  132. raise Exception("Unknown user type")
  133. return user
  134. @classmethod
  135. def generate_worker(cls, flask_app: Flask, generate_task_id: str, detached_app_model: App, app_model_config: AppModelConfig,
  136. query: str, inputs: dict, detached_user: Union[Account, EndUser],
  137. detached_conversation: Optional[Conversation], streaming: bool, is_model_config_override: bool,
  138. retriever_from: str = 'dev'):
  139. with flask_app.app_context():
  140. # fixed the state of the model object when it detached from the original session
  141. user = db.session.merge(detached_user)
  142. app_model = db.session.merge(detached_app_model)
  143. if detached_conversation:
  144. conversation = db.session.merge(detached_conversation)
  145. else:
  146. conversation = None
  147. try:
  148. # run
  149. Completion.generate(
  150. task_id=generate_task_id,
  151. app=app_model,
  152. app_model_config=app_model_config,
  153. query=query,
  154. inputs=inputs,
  155. user=user,
  156. conversation=conversation,
  157. streaming=streaming,
  158. is_override=is_model_config_override,
  159. retriever_from=retriever_from
  160. )
  161. except ConversationTaskStoppedException:
  162. pass
  163. except (LLMBadRequestError, LLMAPIConnectionError, LLMAPIUnavailableError,
  164. LLMRateLimitError, ProviderTokenNotInitError, QuotaExceededError,
  165. ModelCurrentlyNotSupportError) as e:
  166. PubHandler.pub_error(user, generate_task_id, e)
  167. except LLMAuthorizationError:
  168. PubHandler.pub_error(user, generate_task_id, LLMAuthorizationError('Incorrect API key provided'))
  169. except Exception as e:
  170. logging.exception("Unknown Error in completion")
  171. PubHandler.pub_error(user, generate_task_id, e)
  172. finally:
  173. db.session.commit()
  174. @classmethod
  175. def countdown_and_close(cls, flask_app: Flask, worker_thread, pubsub, detached_user, generate_task_id) -> threading.Thread:
  176. # wait for 10 minutes to close the thread
  177. timeout = 600
  178. def close_pubsub():
  179. with flask_app.app_context():
  180. user = db.session.merge(detached_user)
  181. sleep_iterations = 0
  182. while sleep_iterations < timeout and worker_thread.is_alive():
  183. if sleep_iterations > 0 and sleep_iterations % 10 == 0:
  184. PubHandler.ping(user, generate_task_id)
  185. time.sleep(1)
  186. sleep_iterations += 1
  187. if worker_thread.is_alive():
  188. PubHandler.stop(user, generate_task_id)
  189. try:
  190. pubsub.close()
  191. except:
  192. pass
  193. countdown_thread = threading.Thread(target=close_pubsub)
  194. countdown_thread.start()
  195. return countdown_thread
  196. @classmethod
  197. def generate_more_like_this(cls, app_model: App, user: Union[Account | EndUser],
  198. message_id: str, streaming: bool = True) -> Union[dict | Generator]:
  199. if not user:
  200. raise ValueError('user cannot be None')
  201. message = db.session.query(Message).filter(
  202. Message.id == message_id,
  203. Message.app_id == app_model.id,
  204. Message.from_source == ('api' if isinstance(user, EndUser) else 'console'),
  205. Message.from_end_user_id == (user.id if isinstance(user, EndUser) else None),
  206. Message.from_account_id == (user.id if isinstance(user, Account) else None),
  207. ).first()
  208. if not message:
  209. raise MessageNotExistsError()
  210. current_app_model_config = app_model.app_model_config
  211. more_like_this = current_app_model_config.more_like_this_dict
  212. if not current_app_model_config.more_like_this or more_like_this.get("enabled", False) is False:
  213. raise MoreLikeThisDisabledError()
  214. app_model_config = message.app_model_config
  215. if message.override_model_configs:
  216. override_model_configs = json.loads(message.override_model_configs)
  217. pre_prompt = override_model_configs.get("pre_prompt", '')
  218. elif app_model_config:
  219. pre_prompt = app_model_config.pre_prompt
  220. else:
  221. raise AppModelConfigBrokenError()
  222. generate_task_id = str(uuid.uuid4())
  223. pubsub = redis_client.pubsub()
  224. pubsub.subscribe(PubHandler.generate_channel_name(user, generate_task_id))
  225. user = cls.get_real_user_instead_of_proxy_obj(user)
  226. generate_worker_thread = threading.Thread(target=cls.generate_more_like_this_worker, kwargs={
  227. 'flask_app': current_app._get_current_object(),
  228. 'generate_task_id': generate_task_id,
  229. 'detached_app_model': app_model,
  230. 'app_model_config': app_model_config,
  231. 'detached_message': message,
  232. 'pre_prompt': pre_prompt,
  233. 'detached_user': user,
  234. 'streaming': streaming
  235. })
  236. generate_worker_thread.start()
  237. cls.countdown_and_close(current_app._get_current_object(), generate_worker_thread, pubsub, user, generate_task_id)
  238. return cls.compact_response(pubsub, streaming)
  239. @classmethod
  240. def generate_more_like_this_worker(cls, flask_app: Flask, generate_task_id: str, detached_app_model: App,
  241. app_model_config: AppModelConfig, detached_message: Message, pre_prompt: str,
  242. detached_user: Union[Account, EndUser], streaming: bool):
  243. with flask_app.app_context():
  244. # fixed the state of the model object when it detached from the original session
  245. user = db.session.merge(detached_user)
  246. app_model = db.session.merge(detached_app_model)
  247. message = db.session.merge(detached_message)
  248. try:
  249. # run
  250. Completion.generate_more_like_this(
  251. task_id=generate_task_id,
  252. app=app_model,
  253. user=user,
  254. message=message,
  255. pre_prompt=pre_prompt,
  256. app_model_config=app_model_config,
  257. streaming=streaming
  258. )
  259. except ConversationTaskStoppedException:
  260. pass
  261. except (LLMBadRequestError, LLMAPIConnectionError, LLMAPIUnavailableError,
  262. LLMRateLimitError, ProviderTokenNotInitError, QuotaExceededError,
  263. ModelCurrentlyNotSupportError) as e:
  264. PubHandler.pub_error(user, generate_task_id, e)
  265. except LLMAuthorizationError:
  266. PubHandler.pub_error(user, generate_task_id, LLMAuthorizationError('Incorrect API key provided'))
  267. except Exception as e:
  268. logging.exception("Unknown Error in completion")
  269. PubHandler.pub_error(user, generate_task_id, e)
  270. finally:
  271. db.session.commit()
  272. @classmethod
  273. def get_cleaned_inputs(cls, user_inputs: dict, app_model_config: AppModelConfig):
  274. if user_inputs is None:
  275. user_inputs = {}
  276. filtered_inputs = {}
  277. # Filter input variables from form configuration, handle required fields, default values, and option values
  278. input_form_config = app_model_config.user_input_form_list
  279. for config in input_form_config:
  280. input_config = list(config.values())[0]
  281. variable = input_config["variable"]
  282. input_type = list(config.keys())[0]
  283. if variable not in user_inputs or not user_inputs[variable]:
  284. if "required" in input_config and input_config["required"]:
  285. raise ValueError(f"{variable} is required in input form")
  286. else:
  287. filtered_inputs[variable] = input_config["default"] if "default" in input_config else ""
  288. continue
  289. value = user_inputs[variable]
  290. if input_type == "select":
  291. options = input_config["options"] if "options" in input_config else []
  292. if value not in options:
  293. raise ValueError(f"{variable} in input form must be one of the following: {options}")
  294. else:
  295. if 'max_length' in input_config:
  296. max_length = input_config['max_length']
  297. if len(value) > max_length:
  298. raise ValueError(f'{variable} in input form must be less than {max_length} characters')
  299. filtered_inputs[variable] = value.replace('\x00', '') if value else None
  300. return filtered_inputs
  301. @classmethod
  302. def compact_response(cls, pubsub: PubSub, streaming: bool = False) -> Union[dict | Generator]:
  303. generate_channel = list(pubsub.channels.keys())[0].decode('utf-8')
  304. if not streaming:
  305. try:
  306. message_result = {}
  307. for message in pubsub.listen():
  308. if message["type"] == "message":
  309. result = message["data"].decode('utf-8')
  310. result = json.loads(result)
  311. if result.get('error'):
  312. cls.handle_error(result)
  313. if result['event'] == 'message' and 'data' in result:
  314. message_result['message'] = result.get('data')
  315. if result['event'] == 'message_end' and 'data' in result:
  316. message_result['message_end'] = result.get('data')
  317. return cls.get_blocking_message_response_data(message_result)
  318. except ValueError as e:
  319. if e.args[0] != "I/O operation on closed file.": # ignore this error
  320. raise CompletionStoppedError()
  321. else:
  322. logging.exception(e)
  323. raise
  324. finally:
  325. db.session.commit()
  326. try:
  327. pubsub.unsubscribe(generate_channel)
  328. except ConnectionError:
  329. pass
  330. else:
  331. def generate() -> Generator:
  332. try:
  333. for message in pubsub.listen():
  334. if message["type"] == "message":
  335. result = message["data"].decode('utf-8')
  336. result = json.loads(result)
  337. if result.get('error'):
  338. cls.handle_error(result)
  339. event = result.get('event')
  340. if event == "end":
  341. logging.debug("{} finished".format(generate_channel))
  342. break
  343. if event == 'message':
  344. yield "data: " + json.dumps(cls.get_message_response_data(result.get('data'))) + "\n\n"
  345. elif event == 'chain':
  346. yield "data: " + json.dumps(cls.get_chain_response_data(result.get('data'))) + "\n\n"
  347. elif event == 'agent_thought':
  348. yield "data: " + json.dumps(
  349. cls.get_agent_thought_response_data(result.get('data'))) + "\n\n"
  350. elif event == 'message_end':
  351. yield "data: " + json.dumps(
  352. cls.get_message_end_data(result.get('data'))) + "\n\n"
  353. elif event == 'ping':
  354. yield "event: ping\n\n"
  355. else:
  356. yield "data: " + json.dumps(result) + "\n\n"
  357. except ValueError as e:
  358. if e.args[0] != "I/O operation on closed file.": # ignore this error
  359. logging.exception(e)
  360. raise
  361. finally:
  362. db.session.commit()
  363. try:
  364. pubsub.unsubscribe(generate_channel)
  365. except ConnectionError:
  366. pass
  367. return generate()
  368. @classmethod
  369. def get_message_response_data(cls, data: dict):
  370. response_data = {
  371. 'event': 'message',
  372. 'task_id': data.get('task_id'),
  373. 'id': data.get('message_id'),
  374. 'answer': data.get('text'),
  375. 'created_at': int(time.time())
  376. }
  377. if data.get('mode') == 'chat':
  378. response_data['conversation_id'] = data.get('conversation_id')
  379. return response_data
  380. @classmethod
  381. def get_blocking_message_response_data(cls, data: dict):
  382. message = data.get('message')
  383. response_data = {
  384. 'event': 'message',
  385. 'task_id': message.get('task_id'),
  386. 'id': message.get('message_id'),
  387. 'answer': message.get('text'),
  388. 'metadata': {},
  389. 'created_at': int(time.time())
  390. }
  391. if message.get('mode') == 'chat':
  392. response_data['conversation_id'] = message.get('conversation_id')
  393. if 'message_end' in data:
  394. message_end = data.get('message_end')
  395. if 'retriever_resources' in message_end:
  396. response_data['metadata']['retriever_resources'] = message_end.get('retriever_resources')
  397. return response_data
  398. @classmethod
  399. def get_message_end_data(cls, data: dict):
  400. response_data = {
  401. 'event': 'message_end',
  402. 'task_id': data.get('task_id'),
  403. 'id': data.get('message_id')
  404. }
  405. if 'retriever_resources' in data:
  406. response_data['retriever_resources'] = data.get('retriever_resources')
  407. if data.get('mode') == 'chat':
  408. response_data['conversation_id'] = data.get('conversation_id')
  409. return response_data
  410. @classmethod
  411. def get_chain_response_data(cls, data: dict):
  412. response_data = {
  413. 'event': 'chain',
  414. 'id': data.get('chain_id'),
  415. 'task_id': data.get('task_id'),
  416. 'message_id': data.get('message_id'),
  417. 'type': data.get('type'),
  418. 'input': data.get('input'),
  419. 'output': data.get('output'),
  420. 'created_at': int(time.time())
  421. }
  422. if data.get('mode') == 'chat':
  423. response_data['conversation_id'] = data.get('conversation_id')
  424. return response_data
  425. @classmethod
  426. def get_agent_thought_response_data(cls, data: dict):
  427. response_data = {
  428. 'event': 'agent_thought',
  429. 'id': data.get('id'),
  430. 'chain_id': data.get('chain_id'),
  431. 'task_id': data.get('task_id'),
  432. 'message_id': data.get('message_id'),
  433. 'position': data.get('position'),
  434. 'thought': data.get('thought'),
  435. 'tool': data.get('tool'),
  436. 'tool_input': data.get('tool_input'),
  437. 'created_at': int(time.time())
  438. }
  439. if data.get('mode') == 'chat':
  440. response_data['conversation_id'] = data.get('conversation_id')
  441. return response_data
  442. @classmethod
  443. def handle_error(cls, result: dict):
  444. logging.debug("error: %s", result)
  445. error = result.get('error')
  446. description = result.get('description')
  447. # handle errors
  448. llm_errors = {
  449. 'LLMBadRequestError': LLMBadRequestError,
  450. 'LLMAPIConnectionError': LLMAPIConnectionError,
  451. 'LLMAPIUnavailableError': LLMAPIUnavailableError,
  452. 'LLMRateLimitError': LLMRateLimitError,
  453. 'ProviderTokenNotInitError': ProviderTokenNotInitError,
  454. 'QuotaExceededError': QuotaExceededError,
  455. 'ModelCurrentlyNotSupportError': ModelCurrentlyNotSupportError
  456. }
  457. if error in llm_errors:
  458. raise llm_errors[error](description)
  459. elif error == 'LLMAuthorizationError':
  460. raise LLMAuthorizationError('Incorrect API key provided')
  461. else:
  462. raise Exception(description)