utils.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { ValidatedStatus } from '../key-validator/declarations'
  2. import type {
  3. CredentialFormSchemaRadio,
  4. CredentialFormSchemaTextInput,
  5. FormValue,
  6. ModelLoadBalancingConfig,
  7. } from './declarations'
  8. import {
  9. ConfigurationMethodEnum,
  10. FormTypeEnum,
  11. MODEL_TYPE_TEXT,
  12. ModelTypeEnum,
  13. } from './declarations'
  14. import {
  15. deleteModelProvider,
  16. setModelProvider,
  17. validateModelLoadBalancingCredentials,
  18. validateModelProvider,
  19. } from '@/service/common'
  20. export const MODEL_PROVIDER_QUOTA_GET_PAID = ['anthropic', 'openai', 'azure_openai']
  21. export const DEFAULT_BACKGROUND_COLOR = '#F3F4F6'
  22. export const isNullOrUndefined = (value: any) => {
  23. return value === undefined || value === null
  24. }
  25. export const validateCredentials = async (predefined: boolean, provider: string, v: FormValue) => {
  26. let body, url
  27. if (predefined) {
  28. body = {
  29. credentials: v,
  30. }
  31. url = `/workspaces/current/model-providers/${provider}/credentials/validate`
  32. }
  33. else {
  34. const { __model_name, __model_type, ...credentials } = v
  35. body = {
  36. model: __model_name,
  37. model_type: __model_type,
  38. credentials,
  39. }
  40. url = `/workspaces/current/model-providers/${provider}/models/credentials/validate`
  41. }
  42. try {
  43. const res = await validateModelProvider({ url, body })
  44. if (res.result === 'success')
  45. return Promise.resolve({ status: ValidatedStatus.Success })
  46. else
  47. return Promise.resolve({ status: ValidatedStatus.Error, message: res.error || 'error' })
  48. }
  49. catch (e: any) {
  50. return Promise.resolve({ status: ValidatedStatus.Error, message: e.message })
  51. }
  52. }
  53. export const validateLoadBalancingCredentials = async (predefined: boolean, provider: string, v: FormValue, id?: string): Promise<{
  54. status: ValidatedStatus
  55. message?: string
  56. }> => {
  57. const { __model_name, __model_type, ...credentials } = v
  58. try {
  59. const res = await validateModelLoadBalancingCredentials({
  60. url: `/workspaces/current/model-providers/${provider}/models/load-balancing-configs/${id ? `${id}/` : ''}credentials-validate`,
  61. body: {
  62. model: __model_name,
  63. model_type: __model_type,
  64. credentials,
  65. },
  66. })
  67. if (res.result === 'success')
  68. return Promise.resolve({ status: ValidatedStatus.Success })
  69. else
  70. return Promise.resolve({ status: ValidatedStatus.Error, message: res.error || 'error' })
  71. }
  72. catch (e: any) {
  73. return Promise.resolve({ status: ValidatedStatus.Error, message: e.message })
  74. }
  75. }
  76. export const saveCredentials = async (predefined: boolean, provider: string, v: FormValue, loadBalancing?: ModelLoadBalancingConfig) => {
  77. let body, url
  78. if (predefined) {
  79. body = {
  80. config_from: ConfigurationMethodEnum.predefinedModel,
  81. credentials: v,
  82. load_balancing: loadBalancing,
  83. }
  84. url = `/workspaces/current/model-providers/${provider}`
  85. }
  86. else {
  87. const { __model_name, __model_type, ...credentials } = v
  88. body = {
  89. model: __model_name,
  90. model_type: __model_type,
  91. credentials,
  92. load_balancing: loadBalancing,
  93. }
  94. url = `/workspaces/current/model-providers/${provider}/models`
  95. }
  96. return setModelProvider({ url, body })
  97. }
  98. export const savePredefinedLoadBalancingConfig = async (provider: string, v: FormValue, loadBalancing?: ModelLoadBalancingConfig) => {
  99. const { __model_name, __model_type, ...credentials } = v
  100. const body = {
  101. config_from: ConfigurationMethodEnum.predefinedModel,
  102. model: __model_name,
  103. model_type: __model_type,
  104. credentials,
  105. load_balancing: loadBalancing,
  106. }
  107. const url = `/workspaces/current/model-providers/${provider}/models`
  108. return setModelProvider({ url, body })
  109. }
  110. export const removeCredentials = async (predefined: boolean, provider: string, v: FormValue) => {
  111. let url = ''
  112. let body
  113. if (predefined) {
  114. url = `/workspaces/current/model-providers/${provider}`
  115. }
  116. else {
  117. if (v) {
  118. const { __model_name, __model_type } = v
  119. body = {
  120. model: __model_name,
  121. model_type: __model_type,
  122. }
  123. url = `/workspaces/current/model-providers/${provider}/models`
  124. }
  125. }
  126. return deleteModelProvider({ url, body })
  127. }
  128. export const sizeFormat = (size: number) => {
  129. const remainder = Math.floor(size / 1000)
  130. if (remainder < 1)
  131. return `${size}`
  132. else
  133. return `${remainder}K`
  134. }
  135. export const modelTypeFormat = (modelType: ModelTypeEnum) => {
  136. if (modelType === ModelTypeEnum.textEmbedding)
  137. return 'TEXT EMBEDDING'
  138. return modelType.toLocaleUpperCase()
  139. }
  140. export const genModelTypeFormSchema = (modelTypes: ModelTypeEnum[]) => {
  141. return {
  142. type: FormTypeEnum.radio,
  143. label: {
  144. zh_Hans: '模型类型',
  145. en_US: 'Model Type',
  146. },
  147. variable: '__model_type',
  148. default: modelTypes[0],
  149. required: true,
  150. show_on: [],
  151. options: modelTypes.map((modelType: ModelTypeEnum) => {
  152. return {
  153. value: modelType,
  154. label: {
  155. zh_Hans: MODEL_TYPE_TEXT[modelType],
  156. en_US: MODEL_TYPE_TEXT[modelType],
  157. },
  158. show_on: [],
  159. }
  160. }),
  161. } as CredentialFormSchemaRadio
  162. }
  163. export const genModelNameFormSchema = (model?: Pick<CredentialFormSchemaTextInput, 'label' | 'placeholder'>) => {
  164. return {
  165. type: FormTypeEnum.textInput,
  166. label: model?.label || {
  167. zh_Hans: '模型名称',
  168. en_US: 'Model Name',
  169. },
  170. variable: '__model_name',
  171. required: true,
  172. show_on: [],
  173. placeholder: model?.placeholder || {
  174. zh_Hans: '请输入模型名称',
  175. en_US: 'Please enter model name',
  176. },
  177. } as CredentialFormSchemaTextInput
  178. }