annotation_service.py 17 KB

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