huggingface_hub.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. if (v.model_type === 'embeddings') {
  51. return [
  52. 'huggingfacehub_api_token',
  53. 'huggingface_namespace',
  54. 'model_name',
  55. 'huggingfacehub_endpoint_url',
  56. 'task_type',
  57. ]
  58. }
  59. return [
  60. 'huggingfacehub_api_token',
  61. 'model_name',
  62. 'huggingfacehub_endpoint_url',
  63. 'task_type',
  64. ]
  65. }
  66. return []
  67. },
  68. filterValue: (v?: FormValue) => {
  69. let filteredKeys: string[] = []
  70. if (v?.huggingfacehub_api_type === 'hosted_inference_api') {
  71. filteredKeys = [
  72. 'huggingfacehub_api_type',
  73. 'huggingfacehub_api_token',
  74. 'model_name',
  75. 'model_type',
  76. ]
  77. }
  78. if (v?.huggingfacehub_api_type === 'inference_endpoints') {
  79. if (v.model_type === 'embeddings') {
  80. filteredKeys = [
  81. 'huggingfacehub_api_type',
  82. 'huggingfacehub_api_token',
  83. 'huggingface_namespace',
  84. 'model_name',
  85. 'huggingfacehub_endpoint_url',
  86. 'task_type',
  87. 'model_type',
  88. ]
  89. }
  90. else {
  91. filteredKeys = [
  92. 'huggingfacehub_api_type',
  93. 'huggingfacehub_api_token',
  94. 'model_name',
  95. 'huggingfacehub_endpoint_url',
  96. 'task_type',
  97. 'model_type',
  98. ]
  99. }
  100. }
  101. return filteredKeys.reduce((prev: FormValue, next: string) => {
  102. prev[next] = v?.[next] || ''
  103. return prev
  104. }, {})
  105. },
  106. fields: [
  107. {
  108. type: 'radio',
  109. key: 'model_type',
  110. required: true,
  111. label: {
  112. 'en': 'Model Type',
  113. 'zh-Hans': '模型类型',
  114. },
  115. options: [
  116. {
  117. key: 'text-generation',
  118. label: {
  119. 'en': 'Text Generation',
  120. 'zh-Hans': '文本生成',
  121. },
  122. },
  123. {
  124. key: 'embeddings',
  125. label: {
  126. 'en': 'Embeddings',
  127. 'zh-Hans': 'Embeddings',
  128. },
  129. },
  130. ],
  131. },
  132. {
  133. type: 'radio',
  134. key: 'huggingfacehub_api_type',
  135. required: true,
  136. label: {
  137. 'en': 'Endpoint Type',
  138. 'zh-Hans': '端点类型',
  139. },
  140. options: [
  141. {
  142. key: 'hosted_inference_api',
  143. label: {
  144. 'en': 'Hosted Inference API',
  145. 'zh-Hans': 'Hosted Inference API',
  146. },
  147. },
  148. {
  149. key: 'inference_endpoints',
  150. label: {
  151. 'en': 'Inference Endpoints',
  152. 'zh-Hans': 'Inference Endpoints',
  153. },
  154. },
  155. ],
  156. },
  157. {
  158. type: 'text',
  159. key: 'huggingfacehub_api_token',
  160. required: true,
  161. label: {
  162. 'en': 'API Token',
  163. 'zh-Hans': 'API Token',
  164. },
  165. placeholder: {
  166. 'en': 'Enter your Hugging Face Hub API Token here',
  167. 'zh-Hans': '在此输入您的 Hugging Face Hub API Token',
  168. },
  169. },
  170. {
  171. hidden: (value?: FormValue) => !(value?.huggingfacehub_api_type === 'inference_endpoints' && value?.model_type === 'embeddings'),
  172. type: 'text',
  173. key: 'huggingface_namespace',
  174. required: true,
  175. label: {
  176. 'en': 'User Name / Organization Name',
  177. 'zh-Hans': '用户名 / 组织名称',
  178. },
  179. placeholder: {
  180. 'en': 'Enter your User Name / Organization Name here',
  181. 'zh-Hans': '在此输入您的用户名 / 组织名称',
  182. },
  183. },
  184. {
  185. type: 'text',
  186. key: 'model_name',
  187. required: true,
  188. label: {
  189. 'en': 'Model Name',
  190. 'zh-Hans': '模型名称',
  191. },
  192. placeholder: {
  193. 'en': 'Enter your Model Name here',
  194. 'zh-Hans': '在此输入您的模型名称',
  195. },
  196. },
  197. {
  198. hidden: (value?: FormValue) => value?.huggingfacehub_api_type === 'hosted_inference_api',
  199. type: 'text',
  200. key: 'huggingfacehub_endpoint_url',
  201. label: {
  202. 'en': 'Endpoint URL',
  203. 'zh-Hans': '端点 URL',
  204. },
  205. placeholder: {
  206. 'en': 'Enter your Endpoint URL here',
  207. 'zh-Hans': '在此输入您的端点 URL',
  208. },
  209. },
  210. {
  211. hidden: (value?: FormValue) => value?.huggingfacehub_api_type === 'hosted_inference_api' || value?.model_type === 'embeddings',
  212. type: 'radio',
  213. key: 'task_type',
  214. required: true,
  215. label: {
  216. 'en': 'Task',
  217. 'zh-Hans': 'Task',
  218. },
  219. options: [
  220. {
  221. key: 'text2text-generation',
  222. label: {
  223. 'en': 'Text-to-Text Generation',
  224. 'zh-Hans': 'Text-to-Text Generation',
  225. },
  226. },
  227. {
  228. key: 'text-generation',
  229. label: {
  230. 'en': 'Text Generation',
  231. 'zh-Hans': 'Text Generation',
  232. },
  233. },
  234. ],
  235. },
  236. {
  237. hidden: (value?: FormValue) => !(value?.huggingfacehub_api_type === 'inference_endpoints' && value?.model_type === 'embeddings'),
  238. type: 'radio',
  239. key: 'task_type',
  240. required: true,
  241. label: {
  242. 'en': 'Task',
  243. 'zh-Hans': 'Task',
  244. },
  245. options: [
  246. {
  247. key: 'feature-extraction',
  248. label: {
  249. 'en': 'Feature Extraction',
  250. 'zh-Hans': 'Feature Extraction',
  251. },
  252. },
  253. ],
  254. },
  255. ],
  256. },
  257. }
  258. export default config