2023-07-27 13:27:34 +08:00
|
|
|
'use client'
|
|
|
|
|
|
|
|
import { createContext, useContext } from 'use-context-selector'
|
|
|
|
import useSWR from 'swr'
|
2023-12-03 22:10:16 +08:00
|
|
|
import { useEffect, useState } from 'react'
|
2023-11-18 11:53:35 +08:00
|
|
|
import { fetchDefaultModal, fetchModelList, fetchSupportRetrievalMethods } from '@/service/common'
|
2023-08-12 00:57:13 +08:00
|
|
|
import { ModelFeature, ModelType } from '@/app/components/header/account-setting/model-page/declarations'
|
|
|
|
import type { BackendModel } from '@/app/components/header/account-setting/model-page/declarations'
|
2023-11-18 11:53:35 +08:00
|
|
|
import type { RETRIEVE_METHOD } from '@/types/app'
|
2023-12-03 22:10:16 +08:00
|
|
|
import { Plan, type UsagePlanInfo } from '@/app/components/billing/type'
|
|
|
|
import { fetchCurrentPlanInfo } from '@/service/billing'
|
|
|
|
import { parseCurrentPlan } from '@/app/components/billing/utils'
|
|
|
|
import { defaultPlan } from '@/app/components/billing/config'
|
|
|
|
|
2023-08-12 00:57:13 +08:00
|
|
|
const ProviderContext = createContext<{
|
|
|
|
textGenerationModelList: BackendModel[]
|
|
|
|
embeddingsModelList: BackendModel[]
|
|
|
|
speech2textModelList: BackendModel[]
|
2023-11-18 11:53:35 +08:00
|
|
|
rerankModelList: BackendModel[]
|
2023-08-12 00:57:13 +08:00
|
|
|
agentThoughtModelList: BackendModel[]
|
|
|
|
updateModelList: (type: ModelType) => void
|
2023-11-18 11:53:35 +08:00
|
|
|
textGenerationDefaultModel?: BackendModel
|
|
|
|
mutateTextGenerationDefaultModel: () => void
|
2023-08-22 14:55:20 +08:00
|
|
|
embeddingsDefaultModel?: BackendModel
|
2023-12-18 15:41:24 +08:00
|
|
|
isEmbeddingsDefaultModelValid: boolean
|
2023-08-22 14:55:20 +08:00
|
|
|
mutateEmbeddingsDefaultModel: () => void
|
|
|
|
speech2textDefaultModel?: BackendModel
|
|
|
|
mutateSpeech2textDefaultModel: () => void
|
2023-11-18 11:53:35 +08:00
|
|
|
rerankDefaultModel?: BackendModel
|
|
|
|
isRerankDefaultModelVaild: boolean
|
|
|
|
mutateRerankDefaultModel: () => void
|
|
|
|
supportRetrievalMethods: RETRIEVE_METHOD[]
|
2023-12-03 22:10:16 +08:00
|
|
|
plan: {
|
|
|
|
type: Plan
|
|
|
|
usage: UsagePlanInfo
|
|
|
|
total: UsagePlanInfo
|
|
|
|
}
|
|
|
|
isFetchedPlan: boolean
|
|
|
|
enableBilling: boolean
|
2023-12-20 15:37:57 +08:00
|
|
|
enableReplaceWebAppLogo: boolean
|
2023-08-12 00:57:13 +08:00
|
|
|
}>({
|
|
|
|
textGenerationModelList: [],
|
|
|
|
embeddingsModelList: [],
|
|
|
|
speech2textModelList: [],
|
2023-11-18 11:53:35 +08:00
|
|
|
rerankModelList: [],
|
2023-08-12 00:57:13 +08:00
|
|
|
agentThoughtModelList: [],
|
2023-12-18 15:41:24 +08:00
|
|
|
updateModelList: () => { },
|
2023-11-18 11:53:35 +08:00
|
|
|
textGenerationDefaultModel: undefined,
|
2023-12-18 15:41:24 +08:00
|
|
|
mutateTextGenerationDefaultModel: () => { },
|
2023-08-22 14:55:20 +08:00
|
|
|
speech2textDefaultModel: undefined,
|
2023-12-18 15:41:24 +08:00
|
|
|
mutateSpeech2textDefaultModel: () => { },
|
2023-08-22 14:55:20 +08:00
|
|
|
embeddingsDefaultModel: undefined,
|
2023-12-18 15:41:24 +08:00
|
|
|
isEmbeddingsDefaultModelValid: false,
|
|
|
|
mutateEmbeddingsDefaultModel: () => { },
|
2023-11-18 11:53:35 +08:00
|
|
|
rerankDefaultModel: undefined,
|
|
|
|
isRerankDefaultModelVaild: false,
|
2023-12-18 15:41:24 +08:00
|
|
|
mutateRerankDefaultModel: () => { },
|
2023-11-18 11:53:35 +08:00
|
|
|
supportRetrievalMethods: [],
|
2023-12-03 22:10:16 +08:00
|
|
|
plan: {
|
|
|
|
type: Plan.sandbox,
|
|
|
|
usage: {
|
|
|
|
vectorSpace: 32,
|
|
|
|
buildApps: 12,
|
|
|
|
teamMembers: 1,
|
2023-12-18 15:41:24 +08:00
|
|
|
annotatedResponse: 1,
|
2023-12-03 22:10:16 +08:00
|
|
|
},
|
|
|
|
total: {
|
|
|
|
vectorSpace: 200,
|
|
|
|
buildApps: 50,
|
|
|
|
teamMembers: 1,
|
2023-12-18 15:41:24 +08:00
|
|
|
annotatedResponse: 10,
|
2023-12-03 22:10:16 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
isFetchedPlan: false,
|
|
|
|
enableBilling: false,
|
2023-12-20 15:37:57 +08:00
|
|
|
enableReplaceWebAppLogo: false,
|
2023-08-12 00:57:13 +08:00
|
|
|
})
|
2023-07-27 13:27:34 +08:00
|
|
|
|
|
|
|
export const useProviderContext = () => useContext(ProviderContext)
|
|
|
|
|
|
|
|
type ProviderContextProviderProps = {
|
|
|
|
children: React.ReactNode
|
|
|
|
}
|
|
|
|
export const ProviderContextProvider = ({
|
|
|
|
children,
|
|
|
|
}: ProviderContextProviderProps) => {
|
2023-11-18 11:53:35 +08:00
|
|
|
const { data: textGenerationDefaultModel, mutate: mutateTextGenerationDefaultModel } = useSWR('/workspaces/current/default-model?model_type=text-generation', fetchDefaultModal)
|
2023-08-22 14:55:20 +08:00
|
|
|
const { data: embeddingsDefaultModel, mutate: mutateEmbeddingsDefaultModel } = useSWR('/workspaces/current/default-model?model_type=embeddings', fetchDefaultModal)
|
|
|
|
const { data: speech2textDefaultModel, mutate: mutateSpeech2textDefaultModel } = useSWR('/workspaces/current/default-model?model_type=speech2text', fetchDefaultModal)
|
2023-11-18 11:53:35 +08:00
|
|
|
const { data: rerankDefaultModel, mutate: mutateRerankDefaultModel } = useSWR('/workspaces/current/default-model?model_type=reranking', fetchDefaultModal)
|
2023-08-12 00:57:13 +08:00
|
|
|
const fetchModelListUrlPrefix = '/workspaces/current/models/model-type/'
|
|
|
|
const { data: textGenerationModelList, mutate: mutateTextGenerationModelList } = useSWR(`${fetchModelListUrlPrefix}${ModelType.textGeneration}`, fetchModelList)
|
|
|
|
const { data: embeddingsModelList, mutate: mutateEmbeddingsModelList } = useSWR(`${fetchModelListUrlPrefix}${ModelType.embeddings}`, fetchModelList)
|
2023-11-18 11:53:35 +08:00
|
|
|
const { data: speech2textModelList, mutate: mutateSpeech2textModelList } = useSWR(`${fetchModelListUrlPrefix}${ModelType.speech2text}`, fetchModelList)
|
|
|
|
const { data: rerankModelList, mutate: mutateRerankModelList } = useSWR(`${fetchModelListUrlPrefix}${ModelType.reranking}`, fetchModelList)
|
|
|
|
const { data: supportRetrievalMethods } = useSWR('/datasets/retrieval-setting', fetchSupportRetrievalMethods)
|
|
|
|
|
2023-08-12 00:57:13 +08:00
|
|
|
const agentThoughtModelList = textGenerationModelList?.filter((item) => {
|
|
|
|
return item.features?.includes(ModelFeature.agentThought)
|
|
|
|
})
|
|
|
|
|
2023-11-18 11:53:35 +08:00
|
|
|
const isRerankDefaultModelVaild = !!rerankModelList?.find(
|
|
|
|
item => item.model_name === rerankDefaultModel?.model_name && item.model_provider.provider_name === rerankDefaultModel?.model_provider.provider_name,
|
|
|
|
)
|
|
|
|
|
2023-12-18 15:41:24 +08:00
|
|
|
const isEmbeddingsDefaultModelValid = !!embeddingsModelList?.find(
|
|
|
|
item => item.model_name === embeddingsDefaultModel?.model_name && item.model_provider.provider_name === embeddingsDefaultModel?.model_provider.provider_name,
|
|
|
|
)
|
|
|
|
|
2023-08-12 00:57:13 +08:00
|
|
|
const updateModelList = (type: ModelType) => {
|
|
|
|
if (type === ModelType.textGeneration)
|
|
|
|
mutateTextGenerationModelList()
|
|
|
|
if (type === ModelType.embeddings)
|
|
|
|
mutateEmbeddingsModelList()
|
2023-11-18 11:53:35 +08:00
|
|
|
if (type === ModelType.speech2text)
|
|
|
|
mutateSpeech2textModelList()
|
|
|
|
if (type === ModelType.reranking)
|
|
|
|
mutateRerankModelList()
|
2023-08-12 00:57:13 +08:00
|
|
|
}
|
2023-07-27 13:27:34 +08:00
|
|
|
|
2023-12-03 22:10:16 +08:00
|
|
|
const [plan, setPlan] = useState(defaultPlan)
|
|
|
|
const [isFetchedPlan, setIsFetchedPlan] = useState(false)
|
|
|
|
const [enableBilling, setEnableBilling] = useState(true)
|
2023-12-20 15:37:57 +08:00
|
|
|
const [enableReplaceWebAppLogo, setEnableReplaceWebAppLogo] = useState(false)
|
2023-12-03 22:10:16 +08:00
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
const data = await fetchCurrentPlanInfo()
|
2023-12-20 15:37:57 +08:00
|
|
|
const enabled = data.billing.enabled
|
2023-12-03 22:10:16 +08:00
|
|
|
setEnableBilling(enabled)
|
2023-12-20 15:37:57 +08:00
|
|
|
setEnableReplaceWebAppLogo(data.can_replace_logo)
|
2023-12-03 22:10:16 +08:00
|
|
|
if (enabled) {
|
|
|
|
setPlan(parseCurrentPlan(data))
|
2023-12-18 15:41:24 +08:00
|
|
|
// setPlan(parseCurrentPlan({
|
|
|
|
// ...data,
|
|
|
|
// annotation_quota_limit: {
|
|
|
|
// ...data.annotation_quota_limit,
|
|
|
|
// limit: 10,
|
|
|
|
// },
|
|
|
|
// }))
|
2023-12-03 22:10:16 +08:00
|
|
|
setIsFetchedPlan(true)
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
}, [])
|
|
|
|
|
2023-07-27 13:27:34 +08:00
|
|
|
return (
|
2023-08-12 00:57:13 +08:00
|
|
|
<ProviderContext.Provider value={{
|
|
|
|
textGenerationModelList: textGenerationModelList || [],
|
|
|
|
embeddingsModelList: embeddingsModelList || [],
|
|
|
|
speech2textModelList: speech2textModelList || [],
|
2023-11-18 11:53:35 +08:00
|
|
|
rerankModelList: rerankModelList || [],
|
2023-08-12 00:57:13 +08:00
|
|
|
agentThoughtModelList: agentThoughtModelList || [],
|
|
|
|
updateModelList,
|
2023-11-18 11:53:35 +08:00
|
|
|
textGenerationDefaultModel,
|
|
|
|
mutateTextGenerationDefaultModel,
|
2023-08-22 14:55:20 +08:00
|
|
|
embeddingsDefaultModel,
|
|
|
|
mutateEmbeddingsDefaultModel,
|
|
|
|
speech2textDefaultModel,
|
|
|
|
mutateSpeech2textDefaultModel,
|
2023-11-18 11:53:35 +08:00
|
|
|
rerankDefaultModel,
|
|
|
|
isRerankDefaultModelVaild,
|
2023-12-18 15:41:24 +08:00
|
|
|
isEmbeddingsDefaultModelValid,
|
2023-11-18 11:53:35 +08:00
|
|
|
mutateRerankDefaultModel,
|
|
|
|
supportRetrievalMethods: supportRetrievalMethods?.retrieval_method || [],
|
2023-12-03 22:10:16 +08:00
|
|
|
plan,
|
|
|
|
isFetchedPlan,
|
|
|
|
enableBilling,
|
2023-12-20 15:37:57 +08:00
|
|
|
enableReplaceWebAppLogo,
|
2023-08-12 00:57:13 +08:00
|
|
|
}}>
|
2023-07-27 13:27:34 +08:00
|
|
|
{children}
|
|
|
|
</ProviderContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ProviderContext
|