You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

annotation_service.py 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. import uuid
  2. import pandas as pd
  3. from sqlalchemy import or_, select
  4. from werkzeug.datastructures import FileStorage
  5. from werkzeug.exceptions import NotFound
  6. from extensions.ext_database import db
  7. from extensions.ext_redis import redis_client
  8. from libs.datetime_utils import naive_utc_now
  9. from libs.login import current_user
  10. from models.account import Account
  11. from models.model import App, AppAnnotationHitHistory, AppAnnotationSetting, Message, MessageAnnotation
  12. from services.feature_service import FeatureService
  13. from tasks.annotation.add_annotation_to_index_task import add_annotation_to_index_task
  14. from tasks.annotation.batch_import_annotations_task import batch_import_annotations_task
  15. from tasks.annotation.delete_annotation_index_task import delete_annotation_index_task
  16. from tasks.annotation.disable_annotation_reply_task import disable_annotation_reply_task
  17. from tasks.annotation.enable_annotation_reply_task import enable_annotation_reply_task
  18. from tasks.annotation.update_annotation_to_index_task import update_annotation_to_index_task
  19. class AppAnnotationService:
  20. @classmethod
  21. def up_insert_app_annotation_from_message(cls, args: dict, app_id: str) -> MessageAnnotation:
  22. # get app info
  23. assert isinstance(current_user, Account)
  24. app = (
  25. db.session.query(App)
  26. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  27. .first()
  28. )
  29. if not app:
  30. raise NotFound("App not found")
  31. if args.get("message_id"):
  32. message_id = str(args["message_id"])
  33. # get message info
  34. message = db.session.query(Message).where(Message.id == message_id, Message.app_id == app.id).first()
  35. if not message:
  36. raise NotFound("Message Not Exists.")
  37. annotation: MessageAnnotation | None = 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, content=args["answer"], question=args["question"], account_id=current_user.id
  54. )
  55. db.session.add(annotation)
  56. db.session.commit()
  57. # if annotation reply is enabled , add annotation to index
  58. annotation_setting = db.session.query(AppAnnotationSetting).where(AppAnnotationSetting.app_id == app_id).first()
  59. assert current_user.current_tenant_id is not None
  60. if annotation_setting:
  61. add_annotation_to_index_task.delay(
  62. annotation.id,
  63. args["question"],
  64. current_user.current_tenant_id,
  65. app_id,
  66. annotation_setting.collection_binding_id,
  67. )
  68. return annotation
  69. @classmethod
  70. def enable_app_annotation(cls, args: dict, app_id: str):
  71. enable_app_annotation_key = f"enable_app_annotation_{str(app_id)}"
  72. cache_result = redis_client.get(enable_app_annotation_key)
  73. if cache_result is not None:
  74. return {"job_id": cache_result, "job_status": "processing"}
  75. # async job
  76. job_id = str(uuid.uuid4())
  77. enable_app_annotation_job_key = f"enable_app_annotation_job_{str(job_id)}"
  78. # send batch add segments task
  79. redis_client.setnx(enable_app_annotation_job_key, "waiting")
  80. assert isinstance(current_user, Account)
  81. assert current_user.current_tenant_id is not None
  82. enable_annotation_reply_task.delay(
  83. str(job_id),
  84. app_id,
  85. current_user.id,
  86. current_user.current_tenant_id,
  87. args["score_threshold"],
  88. args["embedding_provider_name"],
  89. args["embedding_model_name"],
  90. )
  91. return {"job_id": job_id, "job_status": "waiting"}
  92. @classmethod
  93. def disable_app_annotation(cls, app_id: str):
  94. assert isinstance(current_user, Account)
  95. assert current_user.current_tenant_id is not None
  96. disable_app_annotation_key = f"disable_app_annotation_{str(app_id)}"
  97. cache_result = redis_client.get(disable_app_annotation_key)
  98. if cache_result is not None:
  99. return {"job_id": cache_result, "job_status": "processing"}
  100. # async job
  101. job_id = str(uuid.uuid4())
  102. disable_app_annotation_job_key = f"disable_app_annotation_job_{str(job_id)}"
  103. # send batch add segments task
  104. redis_client.setnx(disable_app_annotation_job_key, "waiting")
  105. disable_annotation_reply_task.delay(str(job_id), app_id, current_user.current_tenant_id)
  106. return {"job_id": job_id, "job_status": "waiting"}
  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. assert isinstance(current_user, Account)
  111. assert current_user.current_tenant_id is not None
  112. app = (
  113. db.session.query(App)
  114. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  115. .first()
  116. )
  117. if not app:
  118. raise NotFound("App not found")
  119. if keyword:
  120. stmt = (
  121. select(MessageAnnotation)
  122. .where(MessageAnnotation.app_id == app_id)
  123. .where(
  124. or_(
  125. MessageAnnotation.question.ilike(f"%{keyword}%"),
  126. MessageAnnotation.content.ilike(f"%{keyword}%"),
  127. )
  128. )
  129. .order_by(MessageAnnotation.created_at.desc(), MessageAnnotation.id.desc())
  130. )
  131. else:
  132. stmt = (
  133. select(MessageAnnotation)
  134. .where(MessageAnnotation.app_id == app_id)
  135. .order_by(MessageAnnotation.created_at.desc(), MessageAnnotation.id.desc())
  136. )
  137. annotations = db.paginate(select=stmt, page=page, per_page=limit, max_per_page=100, error_out=False)
  138. return annotations.items, annotations.total
  139. @classmethod
  140. def export_annotation_list_by_app_id(cls, app_id: str):
  141. # get app info
  142. assert isinstance(current_user, Account)
  143. assert current_user.current_tenant_id is not None
  144. app = (
  145. db.session.query(App)
  146. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  147. .first()
  148. )
  149. if not app:
  150. raise NotFound("App not found")
  151. annotations = (
  152. db.session.query(MessageAnnotation)
  153. .where(MessageAnnotation.app_id == app_id)
  154. .order_by(MessageAnnotation.created_at.desc())
  155. .all()
  156. )
  157. return annotations
  158. @classmethod
  159. def insert_app_annotation_directly(cls, args: dict, app_id: str) -> MessageAnnotation:
  160. # get app info
  161. assert isinstance(current_user, Account)
  162. assert current_user.current_tenant_id is not None
  163. app = (
  164. db.session.query(App)
  165. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  166. .first()
  167. )
  168. if not app:
  169. raise NotFound("App not found")
  170. annotation = MessageAnnotation(
  171. app_id=app.id, content=args["answer"], question=args["question"], account_id=current_user.id
  172. )
  173. db.session.add(annotation)
  174. db.session.commit()
  175. # if annotation reply is enabled , add annotation to index
  176. annotation_setting = db.session.query(AppAnnotationSetting).where(AppAnnotationSetting.app_id == app_id).first()
  177. if annotation_setting:
  178. add_annotation_to_index_task.delay(
  179. annotation.id,
  180. args["question"],
  181. current_user.current_tenant_id,
  182. app_id,
  183. annotation_setting.collection_binding_id,
  184. )
  185. return annotation
  186. @classmethod
  187. def update_app_annotation_directly(cls, args: dict, app_id: str, annotation_id: str):
  188. # get app info
  189. assert isinstance(current_user, Account)
  190. assert current_user.current_tenant_id is not None
  191. app = (
  192. db.session.query(App)
  193. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  194. .first()
  195. )
  196. if not app:
  197. raise NotFound("App not found")
  198. annotation = db.session.query(MessageAnnotation).where(MessageAnnotation.id == annotation_id).first()
  199. if not annotation:
  200. raise NotFound("Annotation not found")
  201. annotation.content = args["answer"]
  202. annotation.question = args["question"]
  203. db.session.commit()
  204. # if annotation reply is enabled , add annotation to index
  205. app_annotation_setting = (
  206. db.session.query(AppAnnotationSetting).where(AppAnnotationSetting.app_id == app_id).first()
  207. )
  208. if app_annotation_setting:
  209. update_annotation_to_index_task.delay(
  210. annotation.id,
  211. annotation.question,
  212. current_user.current_tenant_id,
  213. app_id,
  214. app_annotation_setting.collection_binding_id,
  215. )
  216. return annotation
  217. @classmethod
  218. def delete_app_annotation(cls, app_id: str, annotation_id: str):
  219. # get app info
  220. assert isinstance(current_user, Account)
  221. assert current_user.current_tenant_id is not None
  222. app = (
  223. db.session.query(App)
  224. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  225. .first()
  226. )
  227. if not app:
  228. raise NotFound("App not found")
  229. annotation = db.session.query(MessageAnnotation).where(MessageAnnotation.id == annotation_id).first()
  230. if not annotation:
  231. raise NotFound("Annotation not found")
  232. db.session.delete(annotation)
  233. annotation_hit_histories = db.session.scalars(
  234. select(AppAnnotationHitHistory).where(AppAnnotationHitHistory.annotation_id == annotation_id)
  235. ).all()
  236. if annotation_hit_histories:
  237. for annotation_hit_history in annotation_hit_histories:
  238. db.session.delete(annotation_hit_history)
  239. db.session.commit()
  240. # if annotation reply is enabled , delete annotation index
  241. app_annotation_setting = (
  242. db.session.query(AppAnnotationSetting).where(AppAnnotationSetting.app_id == app_id).first()
  243. )
  244. if app_annotation_setting:
  245. delete_annotation_index_task.delay(
  246. annotation.id, app_id, current_user.current_tenant_id, app_annotation_setting.collection_binding_id
  247. )
  248. @classmethod
  249. def delete_app_annotations_in_batch(cls, app_id: str, annotation_ids: list[str]):
  250. # get app info
  251. assert isinstance(current_user, Account)
  252. assert current_user.current_tenant_id is not None
  253. app = (
  254. db.session.query(App)
  255. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  256. .first()
  257. )
  258. if not app:
  259. raise NotFound("App not found")
  260. # Fetch annotations and their settings in a single query
  261. annotations_to_delete = (
  262. db.session.query(MessageAnnotation, AppAnnotationSetting)
  263. .outerjoin(AppAnnotationSetting, MessageAnnotation.app_id == AppAnnotationSetting.app_id)
  264. .where(MessageAnnotation.id.in_(annotation_ids))
  265. .all()
  266. )
  267. if not annotations_to_delete:
  268. return {"deleted_count": 0}
  269. # Step 1: Extract IDs for bulk operations
  270. annotation_ids_to_delete = [annotation.id for annotation, _ in annotations_to_delete]
  271. # Step 2: Bulk delete hit histories in a single query
  272. db.session.query(AppAnnotationHitHistory).where(
  273. AppAnnotationHitHistory.annotation_id.in_(annotation_ids_to_delete)
  274. ).delete(synchronize_session=False)
  275. # Step 3: Trigger async tasks for search index deletion
  276. for annotation, annotation_setting in annotations_to_delete:
  277. if annotation_setting:
  278. delete_annotation_index_task.delay(
  279. annotation.id, app_id, current_user.current_tenant_id, annotation_setting.collection_binding_id
  280. )
  281. # Step 4: Bulk delete annotations in a single query
  282. deleted_count = (
  283. db.session.query(MessageAnnotation)
  284. .where(MessageAnnotation.id.in_(annotation_ids_to_delete))
  285. .delete(synchronize_session=False)
  286. )
  287. db.session.commit()
  288. return {"deleted_count": deleted_count}
  289. @classmethod
  290. def batch_import_app_annotations(cls, app_id, file: FileStorage):
  291. # get app info
  292. assert isinstance(current_user, Account)
  293. assert current_user.current_tenant_id is not None
  294. app = (
  295. db.session.query(App)
  296. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  297. .first()
  298. )
  299. if not app:
  300. raise NotFound("App not found")
  301. try:
  302. # Skip the first row
  303. df = pd.read_csv(file.stream, dtype=str)
  304. result = []
  305. for _, row in df.iterrows():
  306. content = {"question": row.iloc[0], "answer": row.iloc[1]}
  307. result.append(content)
  308. if len(result) == 0:
  309. raise ValueError("The CSV file is empty.")
  310. # check annotation limit
  311. features = FeatureService.get_features(current_user.current_tenant_id)
  312. if features.billing.enabled:
  313. annotation_quota_limit = features.annotation_quota_limit
  314. if annotation_quota_limit.limit < len(result) + annotation_quota_limit.size:
  315. raise ValueError("The number of annotations exceeds the limit of your subscription.")
  316. # async job
  317. job_id = str(uuid.uuid4())
  318. indexing_cache_key = f"app_annotation_batch_import_{str(job_id)}"
  319. # send batch add segments task
  320. redis_client.setnx(indexing_cache_key, "waiting")
  321. batch_import_annotations_task.delay(
  322. str(job_id), result, app_id, current_user.current_tenant_id, current_user.id
  323. )
  324. except Exception as e:
  325. return {"error_msg": str(e)}
  326. return {"job_id": job_id, "job_status": "waiting"}
  327. @classmethod
  328. def get_annotation_hit_histories(cls, app_id: str, annotation_id: str, page, limit):
  329. assert isinstance(current_user, Account)
  330. assert current_user.current_tenant_id is not None
  331. # get app info
  332. app = (
  333. db.session.query(App)
  334. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  335. .first()
  336. )
  337. if not app:
  338. raise NotFound("App not found")
  339. annotation = db.session.query(MessageAnnotation).where(MessageAnnotation.id == annotation_id).first()
  340. if not annotation:
  341. raise NotFound("Annotation not found")
  342. stmt = (
  343. select(AppAnnotationHitHistory)
  344. .where(
  345. AppAnnotationHitHistory.app_id == app_id,
  346. AppAnnotationHitHistory.annotation_id == annotation_id,
  347. )
  348. .order_by(AppAnnotationHitHistory.created_at.desc())
  349. )
  350. annotation_hit_histories = db.paginate(
  351. select=stmt, page=page, per_page=limit, max_per_page=100, error_out=False
  352. )
  353. return annotation_hit_histories.items, annotation_hit_histories.total
  354. @classmethod
  355. def get_annotation_by_id(cls, annotation_id: str) -> MessageAnnotation | None:
  356. annotation = db.session.query(MessageAnnotation).where(MessageAnnotation.id == annotation_id).first()
  357. if not annotation:
  358. return None
  359. return annotation
  360. @classmethod
  361. def add_annotation_history(
  362. cls,
  363. annotation_id: str,
  364. app_id: str,
  365. annotation_question: str,
  366. annotation_content: str,
  367. query: str,
  368. user_id: str,
  369. message_id: str,
  370. from_source: str,
  371. score: float,
  372. ):
  373. # add hit count to annotation
  374. db.session.query(MessageAnnotation).where(MessageAnnotation.id == annotation_id).update(
  375. {MessageAnnotation.hit_count: MessageAnnotation.hit_count + 1}, synchronize_session=False
  376. )
  377. annotation_hit_history = AppAnnotationHitHistory(
  378. annotation_id=annotation_id,
  379. app_id=app_id,
  380. account_id=user_id,
  381. question=query,
  382. source=from_source,
  383. score=score,
  384. message_id=message_id,
  385. annotation_question=annotation_question,
  386. annotation_content=annotation_content,
  387. )
  388. db.session.add(annotation_hit_history)
  389. db.session.commit()
  390. @classmethod
  391. def get_app_annotation_setting_by_app_id(cls, app_id: str):
  392. assert isinstance(current_user, Account)
  393. assert current_user.current_tenant_id is not None
  394. # get app info
  395. app = (
  396. db.session.query(App)
  397. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  398. .first()
  399. )
  400. if not app:
  401. raise NotFound("App not found")
  402. annotation_setting = db.session.query(AppAnnotationSetting).where(AppAnnotationSetting.app_id == app_id).first()
  403. if annotation_setting:
  404. collection_binding_detail = annotation_setting.collection_binding_detail
  405. if collection_binding_detail:
  406. return {
  407. "id": annotation_setting.id,
  408. "enabled": True,
  409. "score_threshold": annotation_setting.score_threshold,
  410. "embedding_model": {
  411. "embedding_provider_name": collection_binding_detail.provider_name,
  412. "embedding_model_name": collection_binding_detail.model_name,
  413. },
  414. }
  415. else:
  416. return {
  417. "id": annotation_setting.id,
  418. "enabled": True,
  419. "score_threshold": annotation_setting.score_threshold,
  420. "embedding_model": {},
  421. }
  422. return {"enabled": False}
  423. @classmethod
  424. def update_app_annotation_setting(cls, app_id: str, annotation_setting_id: str, args: dict):
  425. assert isinstance(current_user, Account)
  426. assert current_user.current_tenant_id is not None
  427. # get app info
  428. app = (
  429. db.session.query(App)
  430. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  431. .first()
  432. )
  433. if not app:
  434. raise NotFound("App not found")
  435. annotation_setting = (
  436. db.session.query(AppAnnotationSetting)
  437. .where(
  438. AppAnnotationSetting.app_id == app_id,
  439. AppAnnotationSetting.id == annotation_setting_id,
  440. )
  441. .first()
  442. )
  443. if not annotation_setting:
  444. raise NotFound("App annotation not found")
  445. annotation_setting.score_threshold = args["score_threshold"]
  446. annotation_setting.updated_user_id = current_user.id
  447. annotation_setting.updated_at = naive_utc_now()
  448. db.session.add(annotation_setting)
  449. db.session.commit()
  450. collection_binding_detail = annotation_setting.collection_binding_detail
  451. if collection_binding_detail:
  452. return {
  453. "id": annotation_setting.id,
  454. "enabled": True,
  455. "score_threshold": annotation_setting.score_threshold,
  456. "embedding_model": {
  457. "embedding_provider_name": collection_binding_detail.provider_name,
  458. "embedding_model_name": collection_binding_detail.model_name,
  459. },
  460. }
  461. else:
  462. return {
  463. "id": annotation_setting.id,
  464. "enabled": True,
  465. "score_threshold": annotation_setting.score_threshold,
  466. "embedding_model": {},
  467. }
  468. @classmethod
  469. def clear_all_annotations(cls, app_id: str):
  470. assert isinstance(current_user, Account)
  471. assert current_user.current_tenant_id is not None
  472. app = (
  473. db.session.query(App)
  474. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  475. .first()
  476. )
  477. if not app:
  478. raise NotFound("App not found")
  479. # if annotation reply is enabled, delete annotation index
  480. app_annotation_setting = (
  481. db.session.query(AppAnnotationSetting).where(AppAnnotationSetting.app_id == app_id).first()
  482. )
  483. annotations_query = db.session.query(MessageAnnotation).where(MessageAnnotation.app_id == app_id)
  484. for annotation in annotations_query.yield_per(100):
  485. annotation_hit_histories_query = db.session.query(AppAnnotationHitHistory).where(
  486. AppAnnotationHitHistory.annotation_id == annotation.id
  487. )
  488. for annotation_hit_history in annotation_hit_histories_query.yield_per(100):
  489. db.session.delete(annotation_hit_history)
  490. # if annotation reply is enabled, delete annotation index
  491. if app_annotation_setting:
  492. delete_annotation_index_task.delay(
  493. annotation.id, app_id, current_user.current_tenant_id, app_annotation_setting.collection_binding_id
  494. )
  495. db.session.delete(annotation)
  496. db.session.commit()
  497. return {"result": "success"}