use-annotation-config.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import React, { useState } from 'react'
  2. import produce from 'immer'
  3. import type { AnnotationReplyConfig } from '@/models/debug'
  4. import { queryAnnotationJobStatus, updateAnnotationStatus } from '@/service/annotation'
  5. import type { EmbeddingModelConfig } from '@/app/components/app/annotation/type'
  6. import { AnnotationEnableStatus, JobStatus } from '@/app/components/app/annotation/type'
  7. import { sleep } from '@/utils'
  8. import { ANNOTATION_DEFAULT } from '@/config'
  9. import { useProviderContext } from '@/context/provider-context'
  10. type Params = {
  11. appId: string
  12. annotationConfig: AnnotationReplyConfig
  13. setAnnotationConfig: (annotationConfig: AnnotationReplyConfig) => void
  14. }
  15. const useAnnotationConfig = ({
  16. appId,
  17. annotationConfig,
  18. setAnnotationConfig,
  19. }: Params) => {
  20. const { plan, enableBilling } = useProviderContext()
  21. const isAnnotationFull = (enableBilling && plan.usage.annotatedResponse >= plan.total.annotatedResponse)
  22. const [isShowAnnotationFullModal, setIsShowAnnotationFullModal] = useState(false)
  23. const [isShowAnnotationConfigInit, doSetIsShowAnnotationConfigInit] = React.useState(false)
  24. const setIsShowAnnotationConfigInit = (isShow: boolean) => {
  25. if (isShow) {
  26. if (isAnnotationFull) {
  27. setIsShowAnnotationFullModal(true)
  28. return
  29. }
  30. }
  31. doSetIsShowAnnotationConfigInit(isShow)
  32. }
  33. const ensureJobCompleted = async (jobId: string, status: AnnotationEnableStatus) => {
  34. let isCompleted = false
  35. while (!isCompleted) {
  36. const res: any = await queryAnnotationJobStatus(appId, status, jobId)
  37. isCompleted = res.job_status === JobStatus.completed
  38. if (isCompleted)
  39. break
  40. await sleep(2000)
  41. }
  42. }
  43. const handleEnableAnnotation = async (embeddingModel: EmbeddingModelConfig, score?: number) => {
  44. if (isAnnotationFull)
  45. return
  46. const { job_id: jobId }: any = await updateAnnotationStatus(appId, AnnotationEnableStatus.enable, embeddingModel, score)
  47. await ensureJobCompleted(jobId, AnnotationEnableStatus.enable)
  48. setAnnotationConfig(produce(annotationConfig, (draft: AnnotationReplyConfig) => {
  49. draft.enabled = true
  50. draft.embedding_model = embeddingModel
  51. if (!draft.score_threshold)
  52. draft.score_threshold = ANNOTATION_DEFAULT.score_threshold
  53. }))
  54. }
  55. const setScore = (score: number, embeddingModel?: EmbeddingModelConfig) => {
  56. setAnnotationConfig(produce(annotationConfig, (draft: AnnotationReplyConfig) => {
  57. draft.score_threshold = score
  58. if (embeddingModel)
  59. draft.embedding_model = embeddingModel
  60. }))
  61. }
  62. const handleDisableAnnotation = async (embeddingModel: EmbeddingModelConfig) => {
  63. if (!annotationConfig.enabled)
  64. return
  65. await updateAnnotationStatus(appId, AnnotationEnableStatus.disable, embeddingModel)
  66. setAnnotationConfig(produce(annotationConfig, (draft: AnnotationReplyConfig) => {
  67. draft.enabled = false
  68. }))
  69. }
  70. return {
  71. handleEnableAnnotation,
  72. handleDisableAnnotation,
  73. isShowAnnotationConfigInit,
  74. setIsShowAnnotationConfigInit,
  75. isShowAnnotationFullModal,
  76. setIsShowAnnotationFullModal,
  77. setScore,
  78. }
  79. }
  80. export default useAnnotationConfig