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

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