annotation_service.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. import datetime
  2. import uuid
  3. import pandas as pd
  4. from flask_login import current_user
  5. from sqlalchemy import or_
  6. from werkzeug.datastructures import FileStorage
  7. from werkzeug.exceptions import NotFound
  8. from extensions.ext_database import db
  9. from extensions.ext_redis import redis_client
  10. from models.model import App, AppAnnotationHitHistory, AppAnnotationSetting, Message, MessageAnnotation
  11. from services.feature_service import FeatureService
  12. from tasks.annotation.add_annotation_to_index_task import add_annotation_to_index_task
  13. from tasks.annotation.batch_import_annotations_task import batch_import_annotations_task
  14. from tasks.annotation.delete_annotation_index_task import delete_annotation_index_task
  15. from tasks.annotation.disable_annotation_reply_task import disable_annotation_reply_task
  16. from tasks.annotation.enable_annotation_reply_task import enable_annotation_reply_task
  17. from tasks.annotation.update_annotation_to_index_task import update_annotation_to_index_task
  18. class AppAnnotationService:
  19. @classmethod
  20. def up_insert_app_annotation_from_message(cls, args: dict, app_id: str) -> MessageAnnotation:
  21. # get app info
  22. app = db.session.query(App).filter(
  23. App.id == app_id,
  24. App.tenant_id == current_user.current_tenant_id,
  25. App.status == 'normal'
  26. ).first()
  27. if not app:
  28. raise NotFound("App not found")
  29. if args.get('message_id'):
  30. message_id = str(args['message_id'])
  31. # get message info
  32. message = db.session.query(Message).filter(
  33. Message.id == message_id,
  34. Message.app_id == app.id
  35. ).first()
  36. if not message:
  37. raise NotFound("Message Not Exists.")
  38. annotation = message.annotation
  39. # save the message annotation
  40. if annotation:
  41. annotation.content = args['answer']
  42. annotation.question = args['question']
  43. else:
  44. annotation = MessageAnnotation(
  45. app_id=app.id,
  46. conversation_id=message.conversation_id,
  47. message_id=message.id,
  48. content=args['answer'],
  49. question=args['question'],
  50. account_id=current_user.id
  51. )
  52. else:
  53. annotation = MessageAnnotation(
  54. app_id=app.id,
  55. content=args['answer'],
  56. question=args['question'],
  57. account_id=current_user.id
  58. )
  59. db.session.add(annotation)
  60. db.session.commit()
  61. # if annotation reply is enabled , add annotation to index
  62. annotation_setting = db.session.query(AppAnnotationSetting).filter(
  63. AppAnnotationSetting.app_id == app_id).first()
  64. if annotation_setting:
  65. add_annotation_to_index_task.delay(annotation.id, args['question'], current_user.current_tenant_id,
  66. app_id, annotation_setting.collection_binding_id)
  67. return annotation
  68. @classmethod
  69. def enable_app_annotation(cls, args: dict, app_id: str) -> dict:
  70. enable_app_annotation_key = 'enable_app_annotation_{}'.format(str(app_id))
  71. cache_result = redis_client.get(enable_app_annotation_key)
  72. if cache_result is not None:
  73. return {
  74. 'job_id': cache_result,
  75. 'job_status': 'processing'
  76. }
  77. # async job
  78. job_id = str(uuid.uuid4())
  79. enable_app_annotation_job_key = 'enable_app_annotation_job_{}'.format(str(job_id))
  80. # send batch add segments task
  81. redis_client.setnx(enable_app_annotation_job_key, 'waiting')
  82. enable_annotation_reply_task.delay(str(job_id), app_id, current_user.id, current_user.current_tenant_id,
  83. args['score_threshold'],
  84. args['embedding_provider_name'], args['embedding_model_name'])
  85. return {
  86. 'job_id': job_id,
  87. 'job_status': 'waiting'
  88. }
  89. @classmethod
  90. def disable_app_annotation(cls, app_id: str) -> dict:
  91. disable_app_annotation_key = 'disable_app_annotation_{}'.format(str(app_id))
  92. cache_result = redis_client.get(disable_app_annotation_key)
  93. if cache_result is not None:
  94. return {
  95. 'job_id': cache_result,
  96. 'job_status': 'processing'
  97. }
  98. # async job
  99. job_id = str(uuid.uuid4())
  100. disable_app_annotation_job_key = 'disable_app_annotation_job_{}'.format(str(job_id))
  101. # send batch add segments task
  102. redis_client.setnx(disable_app_annotation_job_key, 'waiting')
  103. disable_annotation_reply_task.delay(str(job_id), app_id, current_user.current_tenant_id)
  104. return {
  105. 'job_id': job_id,
  106. 'job_status': 'waiting'
  107. }
  108. @classmethod
  109. def get_annotation_list_by_app_id(cls, app_id: str, page: int, limit: int, keyword: str):
  110. # get app info
  111. app = db.session.query(App).filter(
  112. App.id == app_id,
  113. App.tenant_id == current_user.current_tenant_id,
  114. App.status == 'normal'
  115. ).first()
  116. if not app:
  117. raise NotFound("App not found")
  118. if keyword:
  119. annotations = (db.session.query(MessageAnnotation)
  120. .filter(MessageAnnotation.app_id == app_id)
  121. .filter(
  122. or_(
  123. MessageAnnotation.question.ilike('%{}%'.format(keyword)),
  124. MessageAnnotation.content.ilike('%{}%'.format(keyword))
  125. )
  126. )
  127. .order_by(MessageAnnotation.created_at.desc())
  128. .paginate(page=page, per_page=limit, max_per_page=100, error_out=False))
  129. else:
  130. annotations = (db.session.query(MessageAnnotation)
  131. .filter(MessageAnnotation.app_id == app_id)
  132. .order_by(MessageAnnotation.created_at.desc())
  133. .paginate(page=page, per_page=limit, max_per_page=100, error_out=False))
  134. return annotations.items, annotations.total
  135. @classmethod
  136. def export_annotation_list_by_app_id(cls, app_id: str):
  137. # get app info
  138. app = db.session.query(App).filter(
  139. App.id == app_id,
  140. App.tenant_id == current_user.current_tenant_id,
  141. App.status == 'normal'
  142. ).first()
  143. if not app:
  144. raise NotFound("App not found")
  145. annotations = (db.session.query(MessageAnnotation)
  146. .filter(MessageAnnotation.app_id == app_id)
  147. .order_by(MessageAnnotation.created_at.desc()).all())
  148. return annotations
  149. @classmethod
  150. def insert_app_annotation_directly(cls, args: dict, app_id: str) -> MessageAnnotation:
  151. # get app info
  152. app = db.session.query(App).filter(
  153. App.id == app_id,
  154. App.tenant_id == current_user.current_tenant_id,
  155. App.status == 'normal'
  156. ).first()
  157. if not app:
  158. raise NotFound("App not found")
  159. annotation = MessageAnnotation(
  160. app_id=app.id,
  161. content=args['answer'],
  162. question=args['question'],
  163. account_id=current_user.id
  164. )
  165. db.session.add(annotation)
  166. db.session.commit()
  167. # if annotation reply is enabled , add annotation to index
  168. annotation_setting = db.session.query(AppAnnotationSetting).filter(
  169. AppAnnotationSetting.app_id == app_id).first()
  170. if annotation_setting:
  171. add_annotation_to_index_task.delay(annotation.id, args['question'], current_user.current_tenant_id,
  172. app_id, annotation_setting.collection_binding_id)
  173. return annotation
  174. @classmethod
  175. def update_app_annotation_directly(cls, args: dict, app_id: str, annotation_id: str):
  176. # get app info
  177. app = db.session.query(App).filter(
  178. App.id == app_id,
  179. App.tenant_id == current_user.current_tenant_id,
  180. App.status == 'normal'
  181. ).first()
  182. if not app:
  183. raise NotFound("App not found")
  184. annotation = db.session.query(MessageAnnotation).filter(MessageAnnotation.id == annotation_id).first()
  185. if not annotation:
  186. raise NotFound("Annotation not found")
  187. annotation.content = args['answer']
  188. annotation.question = args['question']
  189. db.session.commit()
  190. # if annotation reply is enabled , add annotation to index
  191. app_annotation_setting = db.session.query(AppAnnotationSetting).filter(
  192. AppAnnotationSetting.app_id == app_id
  193. ).first()
  194. if app_annotation_setting:
  195. update_annotation_to_index_task.delay(annotation.id, annotation.question,
  196. current_user.current_tenant_id,
  197. app_id, app_annotation_setting.collection_binding_id)
  198. return annotation
  199. @classmethod
  200. def delete_app_annotation(cls, app_id: str, annotation_id: str):
  201. # get app info
  202. app = db.session.query(App).filter(
  203. App.id == app_id,
  204. App.tenant_id == current_user.current_tenant_id,
  205. App.status == 'normal'
  206. ).first()
  207. if not app:
  208. raise NotFound("App not found")
  209. annotation = db.session.query(MessageAnnotation).filter(MessageAnnotation.id == annotation_id).first()
  210. if not annotation:
  211. raise NotFound("Annotation not found")
  212. db.session.delete(annotation)
  213. annotation_hit_histories = (db.session.query(AppAnnotationHitHistory)
  214. .filter(AppAnnotationHitHistory.annotation_id == annotation_id)
  215. .all()
  216. )
  217. if annotation_hit_histories:
  218. for annotation_hit_history in annotation_hit_histories:
  219. db.session.delete(annotation_hit_history)
  220. db.session.commit()
  221. # if annotation reply is enabled , delete annotation index
  222. app_annotation_setting = db.session.query(AppAnnotationSetting).filter(
  223. AppAnnotationSetting.app_id == app_id
  224. ).first()
  225. if app_annotation_setting:
  226. delete_annotation_index_task.delay(annotation.id, app_id,
  227. current_user.current_tenant_id,
  228. app_annotation_setting.collection_binding_id)
  229. @classmethod
  230. def batch_import_app_annotations(cls, app_id, file: FileStorage) -> dict:
  231. # get app info
  232. app = db.session.query(App).filter(
  233. App.id == app_id,
  234. App.tenant_id == current_user.current_tenant_id,
  235. App.status == 'normal'
  236. ).first()
  237. if not app:
  238. raise NotFound("App not found")
  239. try:
  240. # Skip the first row
  241. df = pd.read_csv(file)
  242. result = []
  243. for index, row in df.iterrows():
  244. content = {
  245. 'question': row[0],
  246. 'answer': row[1]
  247. }
  248. result.append(content)
  249. if len(result) == 0:
  250. raise ValueError("The CSV file is empty.")
  251. # check annotation limit
  252. features = FeatureService.get_features(current_user.current_tenant_id)
  253. if features.billing.enabled:
  254. annotation_quota_limit = features.annotation_quota_limit
  255. if annotation_quota_limit.limit < len(result) + annotation_quota_limit.size:
  256. raise ValueError("The number of annotations exceeds the limit of your subscription.")
  257. # async job
  258. job_id = str(uuid.uuid4())
  259. indexing_cache_key = 'app_annotation_batch_import_{}'.format(str(job_id))
  260. # send batch add segments task
  261. redis_client.setnx(indexing_cache_key, 'waiting')
  262. batch_import_annotations_task.delay(str(job_id), result, app_id,
  263. current_user.current_tenant_id, current_user.id)
  264. except Exception as e:
  265. return {
  266. 'error_msg': str(e)
  267. }
  268. return {
  269. 'job_id': job_id,
  270. 'job_status': 'waiting'
  271. }
  272. @classmethod
  273. def get_annotation_hit_histories(cls, app_id: str, annotation_id: str, page, limit):
  274. # get app info
  275. app = db.session.query(App).filter(
  276. App.id == app_id,
  277. App.tenant_id == current_user.current_tenant_id,
  278. App.status == 'normal'
  279. ).first()
  280. if not app:
  281. raise NotFound("App not found")
  282. annotation = db.session.query(MessageAnnotation).filter(MessageAnnotation.id == annotation_id).first()
  283. if not annotation:
  284. raise NotFound("Annotation not found")
  285. annotation_hit_histories = (db.session.query(AppAnnotationHitHistory)
  286. .filter(AppAnnotationHitHistory.app_id == app_id,
  287. AppAnnotationHitHistory.annotation_id == annotation_id,
  288. )
  289. .order_by(AppAnnotationHitHistory.created_at.desc())
  290. .paginate(page=page, per_page=limit, max_per_page=100, error_out=False))
  291. return annotation_hit_histories.items, annotation_hit_histories.total
  292. @classmethod
  293. def get_annotation_by_id(cls, annotation_id: str) -> MessageAnnotation | None:
  294. annotation = db.session.query(MessageAnnotation).filter(MessageAnnotation.id == annotation_id).first()
  295. if not annotation:
  296. return None
  297. return annotation
  298. @classmethod
  299. def add_annotation_history(cls, annotation_id: str, app_id: str, annotation_question: str,
  300. annotation_content: str, query: str, user_id: str,
  301. message_id: str, from_source: str, score: float):
  302. # add hit count to annotation
  303. db.session.query(MessageAnnotation).filter(
  304. MessageAnnotation.id == annotation_id
  305. ).update(
  306. {MessageAnnotation.hit_count: MessageAnnotation.hit_count + 1},
  307. synchronize_session=False
  308. )
  309. annotation_hit_history = AppAnnotationHitHistory(
  310. annotation_id=annotation_id,
  311. app_id=app_id,
  312. account_id=user_id,
  313. question=query,
  314. source=from_source,
  315. score=score,
  316. message_id=message_id,
  317. annotation_question=annotation_question,
  318. annotation_content=annotation_content
  319. )
  320. db.session.add(annotation_hit_history)
  321. db.session.commit()
  322. @classmethod
  323. def get_app_annotation_setting_by_app_id(cls, app_id: str):
  324. # get app info
  325. app = db.session.query(App).filter(
  326. App.id == app_id,
  327. App.tenant_id == current_user.current_tenant_id,
  328. App.status == 'normal'
  329. ).first()
  330. if not app:
  331. raise NotFound("App not found")
  332. annotation_setting = db.session.query(AppAnnotationSetting).filter(
  333. AppAnnotationSetting.app_id == app_id).first()
  334. if annotation_setting:
  335. collection_binding_detail = annotation_setting.collection_binding_detail
  336. return {
  337. "id": annotation_setting.id,
  338. "enabled": True,
  339. "score_threshold": annotation_setting.score_threshold,
  340. "embedding_model": {
  341. "embedding_provider_name": collection_binding_detail.provider_name,
  342. "embedding_model_name": collection_binding_detail.model_name
  343. }
  344. }
  345. return {
  346. "enabled": False
  347. }
  348. @classmethod
  349. def update_app_annotation_setting(cls, app_id: str, annotation_setting_id: str, args: dict):
  350. # get app info
  351. app = db.session.query(App).filter(
  352. App.id == app_id,
  353. App.tenant_id == current_user.current_tenant_id,
  354. App.status == 'normal'
  355. ).first()
  356. if not app:
  357. raise NotFound("App not found")
  358. annotation_setting = db.session.query(AppAnnotationSetting).filter(
  359. AppAnnotationSetting.app_id == app_id,
  360. AppAnnotationSetting.id == annotation_setting_id,
  361. ).first()
  362. if not annotation_setting:
  363. raise NotFound("App annotation not found")
  364. annotation_setting.score_threshold = args['score_threshold']
  365. annotation_setting.updated_user_id = current_user.id
  366. annotation_setting.updated_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
  367. db.session.add(annotation_setting)
  368. db.session.commit()
  369. collection_binding_detail = annotation_setting.collection_binding_detail
  370. return {
  371. "id": annotation_setting.id,
  372. "enabled": True,
  373. "score_threshold": annotation_setting.score_threshold,
  374. "embedding_model": {
  375. "embedding_provider_name": collection_binding_detail.provider_name,
  376. "embedding_model_name": collection_binding_detail.model_name
  377. }
  378. }