Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

use-models.ts 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import {
  2. del,
  3. get,
  4. post,
  5. put,
  6. } from './base'
  7. import type {
  8. ModelCredential,
  9. ModelItem,
  10. ModelLoadBalancingConfig,
  11. ModelTypeEnum,
  12. ProviderCredential,
  13. } from '@/app/components/header/account-setting/model-provider-page/declarations'
  14. import {
  15. useMutation,
  16. useQuery,
  17. // useQueryClient,
  18. } from '@tanstack/react-query'
  19. const NAME_SPACE = 'models'
  20. export const useModelProviderModelList = (provider: string) => {
  21. return useQuery({
  22. queryKey: [NAME_SPACE, 'model-list', provider],
  23. queryFn: () => get<{ data: ModelItem[] }>(`/workspaces/current/model-providers/${provider}/models`),
  24. })
  25. }
  26. export const useGetProviderCredential = (enabled: boolean, provider: string, credentialId?: string) => {
  27. return useQuery({
  28. enabled,
  29. queryKey: [NAME_SPACE, 'model-list', provider, credentialId],
  30. queryFn: () => get<ProviderCredential>(`/workspaces/current/model-providers/${provider}/credentials${credentialId ? `?credential_id=${credentialId}` : ''}`),
  31. })
  32. }
  33. export const useAddProviderCredential = (provider: string) => {
  34. return useMutation({
  35. mutationFn: (data: ProviderCredential) => post<{ result: string }>(`/workspaces/current/model-providers/${provider}/credentials`, {
  36. body: data,
  37. }),
  38. })
  39. }
  40. export const useEditProviderCredential = (provider: string) => {
  41. return useMutation({
  42. mutationFn: (data: ProviderCredential) => put<{ result: string }>(`/workspaces/current/model-providers/${provider}/credentials`, {
  43. body: data,
  44. }),
  45. })
  46. }
  47. export const useDeleteProviderCredential = (provider: string) => {
  48. return useMutation({
  49. mutationFn: (data: {
  50. credential_id: string
  51. }) => del<{ result: string }>(`/workspaces/current/model-providers/${provider}/credentials`, {
  52. body: data,
  53. }),
  54. })
  55. }
  56. export const useActiveProviderCredential = (provider: string) => {
  57. return useMutation({
  58. mutationFn: (data: {
  59. credential_id: string
  60. model?: string
  61. model_type?: ModelTypeEnum
  62. }) => post<{ result: string }>(`/workspaces/current/model-providers/${provider}/credentials/switch`, {
  63. body: data,
  64. }),
  65. })
  66. }
  67. export const useGetModelCredential = (
  68. enabled: boolean,
  69. provider: string,
  70. credentialId?: string,
  71. model?: string,
  72. modelType?: string,
  73. configFrom?: string,
  74. ) => {
  75. return useQuery({
  76. enabled,
  77. queryKey: [NAME_SPACE, 'model-list', provider, model, modelType, credentialId],
  78. queryFn: () => get<ModelCredential>(`/workspaces/current/model-providers/${provider}/models/credentials?model=${model}&model_type=${modelType}&config_from=${configFrom}${credentialId ? `&credential_id=${credentialId}` : ''}`),
  79. staleTime: 0,
  80. gcTime: 0,
  81. })
  82. }
  83. export const useAddModelCredential = (provider: string) => {
  84. return useMutation({
  85. mutationFn: (data: ModelCredential) => post<{ result: string }>(`/workspaces/current/model-providers/${provider}/models/credentials`, {
  86. body: data,
  87. }),
  88. })
  89. }
  90. export const useEditModelCredential = (provider: string) => {
  91. return useMutation({
  92. mutationFn: (data: ModelCredential) => put<{ result: string }>(`/workspaces/current/model-providers/${provider}/models/credentials`, {
  93. body: data,
  94. }),
  95. })
  96. }
  97. export const useDeleteModelCredential = (provider: string) => {
  98. return useMutation({
  99. mutationFn: (data: {
  100. credential_id: string
  101. model?: string
  102. model_type?: ModelTypeEnum
  103. }) => del<{ result: string }>(`/workspaces/current/model-providers/${provider}/models/credentials`, {
  104. body: data,
  105. }),
  106. })
  107. }
  108. export const useDeleteModel = (provider: string) => {
  109. return useMutation({
  110. mutationFn: (data: {
  111. model: string
  112. model_type: ModelTypeEnum
  113. }) => del<{ result: string }>(`/workspaces/current/model-providers/${provider}/models/credentials`, {
  114. body: data,
  115. }),
  116. })
  117. }
  118. export const useActiveModelCredential = (provider: string) => {
  119. return useMutation({
  120. mutationFn: (data: {
  121. credential_id: string
  122. model?: string
  123. model_type?: ModelTypeEnum
  124. }) => post<{ result: string }>(`/workspaces/current/model-providers/${provider}/models/credentials/switch`, {
  125. body: data,
  126. }),
  127. })
  128. }
  129. export const useUpdateModelLoadBalancingConfig = (provider: string) => {
  130. return useMutation({
  131. mutationFn: (data: {
  132. config_from: string
  133. model: string
  134. model_type: ModelTypeEnum
  135. load_balancing: ModelLoadBalancingConfig
  136. credential_id?: string
  137. }) => post<{ result: string }>(`/workspaces/current/model-providers/${provider}/models`, {
  138. body: data,
  139. }),
  140. })
  141. }