Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

annotation_service.py 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 = (
  235. db.session.query(AppAnnotationHitHistory)
  236. .where(AppAnnotationHitHistory.annotation_id == annotation_id)
  237. .all()
  238. )
  239. if annotation_hit_histories:
  240. for annotation_hit_history in annotation_hit_histories:
  241. db.session.delete(annotation_hit_history)
  242. db.session.commit()
  243. # if annotation reply is enabled , delete annotation index
  244. app_annotation_setting = (
  245. db.session.query(AppAnnotationSetting).where(AppAnnotationSetting.app_id == app_id).first()
  246. )
  247. if app_annotation_setting:
  248. delete_annotation_index_task.delay(
  249. annotation.id, app_id, current_user.current_tenant_id, app_annotation_setting.collection_binding_id
  250. )
  251. @classmethod
  252. def delete_app_annotations_in_batch(cls, app_id: str, annotation_ids: list[str]):
  253. # get app info
  254. assert isinstance(current_user, Account)
  255. assert current_user.current_tenant_id is not None
  256. app = (
  257. db.session.query(App)
  258. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  259. .first()
  260. )
  261. if not app:
  262. raise NotFound("App not found")
  263. # Fetch annotations and their settings in a single query
  264. annotations_to_delete = (
  265. db.session.query(MessageAnnotation, AppAnnotationSetting)
  266. .outerjoin(AppAnnotationSetting, MessageAnnotation.app_id == AppAnnotationSetting.app_id)
  267. .where(MessageAnnotation.id.in_(annotation_ids))
  268. .all()
  269. )
  270. if not annotations_to_delete:
  271. return {"deleted_count": 0}
  272. # Step 1: Extract IDs for bulk operations
  273. annotation_ids_to_delete = [annotation.id for annotation, _ in annotations_to_delete]
  274. # Step 2: Bulk delete hit histories in a single query
  275. db.session.query(AppAnnotationHitHistory).where(
  276. AppAnnotationHitHistory.annotation_id.in_(annotation_ids_to_delete)
  277. ).delete(synchronize_session=False)
  278. # Step 3: Trigger async tasks for search index deletion
  279. for annotation, annotation_setting in annotations_to_delete:
  280. if annotation_setting:
  281. delete_annotation_index_task.delay(
  282. annotation.id, app_id, current_user.current_tenant_id, annotation_setting.collection_binding_id
  283. )
  284. # Step 4: Bulk delete annotations in a single query
  285. deleted_count = (
  286. db.session.query(MessageAnnotation)
  287. .where(MessageAnnotation.id.in_(annotation_ids_to_delete))
  288. .delete(synchronize_session=False)
  289. )
  290. db.session.commit()
  291. return {"deleted_count": deleted_count}
  292. @classmethod
  293. def batch_import_app_annotations(cls, app_id, file: FileStorage):
  294. # get app info
  295. assert isinstance(current_user, Account)
  296. assert current_user.current_tenant_id is not None
  297. app = (
  298. db.session.query(App)
  299. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  300. .first()
  301. )
  302. if not app:
  303. raise NotFound("App not found")
  304. try:
  305. # Skip the first row
  306. df = pd.read_csv(file, dtype=str)
  307. result = []
  308. for _, row in df.iterrows():
  309. content = {"question": row.iloc[0], "answer": row.iloc[1]}
  310. result.append(content)
  311. if len(result) == 0:
  312. raise ValueError("The CSV file is empty.")
  313. # check annotation limit
  314. features = FeatureService.get_features(current_user.current_tenant_id)
  315. if features.billing.enabled:
  316. annotation_quota_limit = features.annotation_quota_limit
  317. if annotation_quota_limit.limit < len(result) + annotation_quota_limit.size:
  318. raise ValueError("The number of annotations exceeds the limit of your subscription.")
  319. # async job
  320. job_id = str(uuid.uuid4())
  321. indexing_cache_key = f"app_annotation_batch_import_{str(job_id)}"
  322. # send batch add segments task
  323. redis_client.setnx(indexing_cache_key, "waiting")
  324. batch_import_annotations_task.delay(
  325. str(job_id), result, app_id, current_user.current_tenant_id, current_user.id
  326. )
  327. except Exception as e:
  328. return {"error_msg": str(e)}
  329. return {"job_id": job_id, "job_status": "waiting"}
  330. @classmethod
  331. def get_annotation_hit_histories(cls, app_id: str, annotation_id: str, page, limit):
  332. assert isinstance(current_user, Account)
  333. assert current_user.current_tenant_id is not None
  334. # get app info
  335. app = (
  336. db.session.query(App)
  337. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  338. .first()
  339. )
  340. if not app:
  341. raise NotFound("App not found")
  342. annotation = db.session.query(MessageAnnotation).where(MessageAnnotation.id == annotation_id).first()
  343. if not annotation:
  344. raise NotFound("Annotation not found")
  345. stmt = (
  346. select(AppAnnotationHitHistory)
  347. .where(
  348. AppAnnotationHitHistory.app_id == app_id,
  349. AppAnnotationHitHistory.annotation_id == annotation_id,
  350. )
  351. .order_by(AppAnnotationHitHistory.created_at.desc())
  352. )
  353. annotation_hit_histories = db.paginate(
  354. select=stmt, page=page, per_page=limit, max_per_page=100, error_out=False
  355. )
  356. return annotation_hit_histories.items, annotation_hit_histories.total
  357. @classmethod
  358. def get_annotation_by_id(cls, annotation_id: str) -> MessageAnnotation | None:
  359. annotation = db.session.query(MessageAnnotation).where(MessageAnnotation.id == annotation_id).first()
  360. if not annotation:
  361. return None
  362. return annotation
  363. @classmethod
  364. def add_annotation_history(
  365. cls,
  366. annotation_id: str,
  367. app_id: str,
  368. annotation_question: str,
  369. annotation_content: str,
  370. query: str,
  371. user_id: str,
  372. message_id: str,
  373. from_source: str,
  374. score: float,
  375. ):
  376. # add hit count to annotation
  377. db.session.query(MessageAnnotation).where(MessageAnnotation.id == annotation_id).update(
  378. {MessageAnnotation.hit_count: MessageAnnotation.hit_count + 1}, synchronize_session=False
  379. )
  380. annotation_hit_history = AppAnnotationHitHistory(
  381. annotation_id=annotation_id,
  382. app_id=app_id,
  383. account_id=user_id,
  384. question=query,
  385. source=from_source,
  386. score=score,
  387. message_id=message_id,
  388. annotation_question=annotation_question,
  389. annotation_content=annotation_content,
  390. )
  391. db.session.add(annotation_hit_history)
  392. db.session.commit()
  393. @classmethod
  394. def get_app_annotation_setting_by_app_id(cls, app_id: str):
  395. assert isinstance(current_user, Account)
  396. assert current_user.current_tenant_id is not None
  397. # get app info
  398. app = (
  399. db.session.query(App)
  400. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  401. .first()
  402. )
  403. if not app:
  404. raise NotFound("App not found")
  405. annotation_setting = db.session.query(AppAnnotationSetting).where(AppAnnotationSetting.app_id == app_id).first()
  406. if annotation_setting:
  407. collection_binding_detail = annotation_setting.collection_binding_detail
  408. return {
  409. "id": annotation_setting.id,
  410. "enabled": True,
  411. "score_threshold": annotation_setting.score_threshold,
  412. "embedding_model": {
  413. "embedding_provider_name": collection_binding_detail.provider_name,
  414. "embedding_model_name": collection_binding_detail.model_name,
  415. },
  416. }
  417. return {"enabled": False}
  418. @classmethod
  419. def update_app_annotation_setting(cls, app_id: str, annotation_setting_id: str, args: dict):
  420. assert isinstance(current_user, Account)
  421. assert current_user.current_tenant_id is not None
  422. # get app info
  423. app = (
  424. db.session.query(App)
  425. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  426. .first()
  427. )
  428. if not app:
  429. raise NotFound("App not found")
  430. annotation_setting = (
  431. db.session.query(AppAnnotationSetting)
  432. .where(
  433. AppAnnotationSetting.app_id == app_id,
  434. AppAnnotationSetting.id == annotation_setting_id,
  435. )
  436. .first()
  437. )
  438. if not annotation_setting:
  439. raise NotFound("App annotation not found")
  440. annotation_setting.score_threshold = args["score_threshold"]
  441. annotation_setting.updated_user_id = current_user.id
  442. annotation_setting.updated_at = naive_utc_now()
  443. db.session.add(annotation_setting)
  444. db.session.commit()
  445. collection_binding_detail = annotation_setting.collection_binding_detail
  446. return {
  447. "id": annotation_setting.id,
  448. "enabled": True,
  449. "score_threshold": annotation_setting.score_threshold,
  450. "embedding_model": {
  451. "embedding_provider_name": collection_binding_detail.provider_name,
  452. "embedding_model_name": collection_binding_detail.model_name,
  453. },
  454. }
  455. @classmethod
  456. def clear_all_annotations(cls, app_id: str):
  457. assert isinstance(current_user, Account)
  458. assert current_user.current_tenant_id is not None
  459. app = (
  460. db.session.query(App)
  461. .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal")
  462. .first()
  463. )
  464. if not app:
  465. raise NotFound("App not found")
  466. # if annotation reply is enabled, delete annotation index
  467. app_annotation_setting = (
  468. db.session.query(AppAnnotationSetting).where(AppAnnotationSetting.app_id == app_id).first()
  469. )
  470. annotations_query = db.session.query(MessageAnnotation).where(MessageAnnotation.app_id == app_id)
  471. for annotation in annotations_query.yield_per(100):
  472. annotation_hit_histories_query = db.session.query(AppAnnotationHitHistory).where(
  473. AppAnnotationHitHistory.annotation_id == annotation.id
  474. )
  475. for annotation_hit_history in annotation_hit_histories_query.yield_per(100):
  476. db.session.delete(annotation_hit_history)
  477. # if annotation reply is enabled, delete annotation index
  478. if app_annotation_setting:
  479. delete_annotation_index_task.delay(
  480. annotation.id, app_id, current_user.current_tenant_id, app_annotation_setting.collection_binding_id
  481. )
  482. db.session.delete(annotation)
  483. db.session.commit()
  484. return {"result": "success"}