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
|
|
|
|
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-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: [],
|
|
|
|
updateModelList: () => {},
|
2023-11-18 11:53:35 +08:00
|
|
|
textGenerationDefaultModel: undefined,
|
|
|
|
mutateTextGenerationDefaultModel: () => {},
|
2023-08-22 14:55:20 +08:00
|
|
|
speech2textDefaultModel: undefined,
|
|
|
|
mutateSpeech2textDefaultModel: () => {},
|
|
|
|
embeddingsDefaultModel: undefined,
|
|
|
|
mutateEmbeddingsDefaultModel: () => {},
|
2023-11-18 11:53:35 +08:00
|
|
|
rerankDefaultModel: undefined,
|
|
|
|
isRerankDefaultModelVaild: false,
|
|
|
|
mutateRerankDefaultModel: () => {},
|
|
|
|
supportRetrievalMethods: [],
|
2023-12-03 22:10:16 +08:00
|
|
|
plan: {
|
|
|
|
type: Plan.sandbox,
|
|
|
|
usage: {
|
|
|
|
vectorSpace: 32,
|
|
|
|
buildApps: 12,
|
|
|
|
teamMembers: 1,
|
|
|
|
},
|
|
|
|
total: {
|
|
|
|
vectorSpace: 200,
|
|
|
|
buildApps: 50,
|
|
|
|
teamMembers: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isFetchedPlan: false,
|
|
|
|
enableBilling: 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-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)
|
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
const data = await fetchCurrentPlanInfo()
|
|
|
|
const enabled = data.enabled
|
|
|
|
setEnableBilling(enabled)
|
|
|
|
if (enabled) {
|
|
|
|
setPlan(parseCurrentPlan(data))
|
|
|
|
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,
|
|
|
|
mutateRerankDefaultModel,
|
|
|
|
supportRetrievalMethods: supportRetrievalMethods?.retrieval_method || [],
|
2023-12-03 22:10:16 +08:00
|
|
|
plan,
|
|
|
|
isFetchedPlan,
|
|
|
|
enableBilling,
|
2023-08-12 00:57:13 +08:00
|
|
|
}}>
|
2023-07-27 13:27:34 +08:00
|
|
|
{children}
|
|
|
|
</ProviderContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ProviderContext
|