huggingface_hub.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { ProviderEnum } from '../declarations'
  2. import type { FormValue, ProviderConfig } from '../declarations'
  3. import { Huggingface, HuggingfaceText } from '@/app/components/base/icons/src/public/llm'
  4. const config: ProviderConfig = {
  5. selector: {
  6. name: {
  7. 'en': 'Hugging Face',
  8. 'zh-Hans': 'Hugging Face',
  9. },
  10. icon: <Huggingface className='w-full h-full' />,
  11. },
  12. item: {
  13. key: ProviderEnum.huggingface_hub,
  14. titleIcon: {
  15. 'en': <HuggingfaceText className='h-6' />,
  16. 'zh-Hans': <HuggingfaceText className='h-6' />,
  17. },
  18. hit: {
  19. 'en': '🐑 Llama 2 Supported',
  20. 'zh-Hans': '🐑 Llama 2 已支持',
  21. },
  22. },
  23. modal: {
  24. key: ProviderEnum.huggingface_hub,
  25. title: {
  26. 'en': 'Hugging Face Model',
  27. 'zh-Hans': 'Hugging Face Model',
  28. },
  29. icon: <Huggingface className='h-6' />,
  30. link: {
  31. href: 'https://huggingface.co/settings/tokens',
  32. label: {
  33. 'en': 'Get your API key from Hugging Face Hub',
  34. 'zh-Hans': '从 Hugging Face Hub 获取 API Key',
  35. },
  36. },
  37. defaultValue: {
  38. model_type: 'text-generation',
  39. huggingfacehub_api_type: 'hosted_inference_api',
  40. task_type: 'text-generation',
  41. },
  42. validateKeys: (v?: FormValue) => {
  43. if (v?.huggingfacehub_api_type === 'hosted_inference_api') {
  44. return [
  45. 'huggingfacehub_api_token',
  46. 'model_name',
  47. ]
  48. }
  49. if (v?.huggingfacehub_api_type === 'inference_endpoints') {
  50. return [
  51. 'huggingfacehub_api_token',
  52. 'model_name',
  53. 'huggingfacehub_endpoint_url',
  54. 'task_type',
  55. ]
  56. }
  57. return []
  58. },
  59. filterValue: (v?: FormValue) => {
  60. let filteredKeys: string[] = []
  61. if (v?.huggingfacehub_api_type === 'hosted_inference_api') {
  62. filteredKeys = [
  63. 'huggingfacehub_api_type',
  64. 'huggingfacehub_api_token',
  65. 'model_name',
  66. 'model_type',
  67. ]
  68. }
  69. if (v?.huggingfacehub_api_type === 'inference_endpoints') {
  70. filteredKeys = [
  71. 'huggingfacehub_api_type',
  72. 'huggingfacehub_api_token',
  73. 'model_name',
  74. 'huggingfacehub_endpoint_url',
  75. 'task_type',
  76. 'model_type',
  77. ]
  78. }
  79. return filteredKeys.reduce((prev: FormValue, next: string) => {
  80. prev[next] = v?.[next] || ''
  81. return prev
  82. }, {})
  83. },
  84. fields: [
  85. {
  86. type: 'radio',
  87. key: 'huggingfacehub_api_type',
  88. required: true,
  89. label: {
  90. 'en': 'Endpoint Type',
  91. 'zh-Hans': '端点类型',
  92. },
  93. options: [
  94. {
  95. key: 'hosted_inference_api',
  96. label: {
  97. 'en': 'Hosted Inference API',
  98. 'zh-Hans': 'Hosted Inference API',
  99. },
  100. },
  101. {
  102. key: 'inference_endpoints',
  103. label: {
  104. 'en': 'Inference Endpoints',
  105. 'zh-Hans': 'Inference Endpoints',
  106. },
  107. },
  108. ],
  109. },
  110. {
  111. type: 'text',
  112. key: 'huggingfacehub_api_token',
  113. required: true,
  114. label: {
  115. 'en': 'API Token',
  116. 'zh-Hans': 'API Token',
  117. },
  118. placeholder: {
  119. 'en': 'Enter your Hugging Face Hub API Token here',
  120. 'zh-Hans': '在此输入您的 Hugging Face Hub API Token',
  121. },
  122. },
  123. {
  124. type: 'text',
  125. key: 'model_name',
  126. required: true,
  127. label: {
  128. 'en': 'Model Name',
  129. 'zh-Hans': '模型名称',
  130. },
  131. placeholder: {
  132. 'en': 'Enter your Model Name here',
  133. 'zh-Hans': '在此输入您的模型名称',
  134. },
  135. },
  136. {
  137. hidden: (value?: FormValue) => value?.huggingfacehub_api_type === 'hosted_inference_api',
  138. type: 'text',
  139. key: 'huggingfacehub_endpoint_url',
  140. label: {
  141. 'en': 'Endpoint URL',
  142. 'zh-Hans': '端点 URL',
  143. },
  144. placeholder: {
  145. 'en': 'Enter your Endpoint URL here',
  146. 'zh-Hans': '在此输入您的端点 URL',
  147. },
  148. },
  149. {
  150. hidden: (value?: FormValue) => value?.huggingfacehub_api_type === 'hosted_inference_api',
  151. type: 'radio',
  152. key: 'task_type',
  153. required: true,
  154. label: {
  155. 'en': 'Task',
  156. 'zh-Hans': 'Task',
  157. },
  158. options: [
  159. {
  160. key: 'text2text-generation',
  161. label: {
  162. 'en': 'Text-to-Text Generation',
  163. 'zh-Hans': 'Text-to-Text Generation',
  164. },
  165. },
  166. {
  167. key: 'text-generation',
  168. label: {
  169. 'en': 'Text Generation',
  170. 'zh-Hans': 'Text Generation',
  171. },
  172. },
  173. ],
  174. },
  175. ],
  176. },
  177. }
  178. export default config