utils.ts 5.5 KB

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