huggingface_hub.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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: 'chat',
  125. // label: {
  126. // 'en': 'Chat',
  127. // 'zh-Hans': '聊天',
  128. // },
  129. // },
  130. {
  131. key: 'embeddings',
  132. label: {
  133. 'en': 'Embeddings',
  134. 'zh-Hans': 'Embeddings',
  135. },
  136. },
  137. ],
  138. },
  139. {
  140. type: 'radio',
  141. key: 'huggingfacehub_api_type',
  142. required: true,
  143. label: {
  144. 'en': 'Endpoint Type',
  145. 'zh-Hans': '端点类型',
  146. },
  147. options: [
  148. {
  149. key: 'hosted_inference_api',
  150. label: {
  151. 'en': 'Hosted Inference API',
  152. 'zh-Hans': 'Hosted Inference API',
  153. },
  154. },
  155. {
  156. key: 'inference_endpoints',
  157. label: {
  158. 'en': 'Inference Endpoints',
  159. 'zh-Hans': 'Inference Endpoints',
  160. },
  161. },
  162. ],
  163. },
  164. {
  165. type: 'text',
  166. key: 'huggingfacehub_api_token',
  167. required: true,
  168. label: {
  169. 'en': 'API Token',
  170. 'zh-Hans': 'API Token',
  171. },
  172. placeholder: {
  173. 'en': 'Enter your Hugging Face Hub API Token here',
  174. 'zh-Hans': '在此输入您的 Hugging Face Hub API Token',
  175. },
  176. },
  177. {
  178. hidden: (value?: FormValue) => !(value?.huggingfacehub_api_type === 'inference_endpoints' && value?.model_type === 'embeddings'),
  179. type: 'text',
  180. key: 'huggingface_namespace',
  181. required: true,
  182. label: {
  183. 'en': 'User Name / Organization Name',
  184. 'zh-Hans': '用户名 / 组织名称',
  185. },
  186. placeholder: {
  187. 'en': 'Enter your User Name / Organization Name here',
  188. 'zh-Hans': '在此输入您的用户名 / 组织名称',
  189. },
  190. },
  191. {
  192. type: 'text',
  193. key: 'model_name',
  194. required: true,
  195. label: {
  196. 'en': 'Model Name',
  197. 'zh-Hans': '模型名称',
  198. },
  199. placeholder: {
  200. 'en': 'Enter your Model Name here',
  201. 'zh-Hans': '在此输入您的模型名称',
  202. },
  203. },
  204. {
  205. hidden: (value?: FormValue) => value?.huggingfacehub_api_type === 'hosted_inference_api',
  206. type: 'text',
  207. key: 'huggingfacehub_endpoint_url',
  208. label: {
  209. 'en': 'Endpoint URL',
  210. 'zh-Hans': '端点 URL',
  211. },
  212. placeholder: {
  213. 'en': 'Enter your Endpoint URL here',
  214. 'zh-Hans': '在此输入您的端点 URL',
  215. },
  216. },
  217. {
  218. hidden: (value?: FormValue) => value?.huggingfacehub_api_type === 'hosted_inference_api' || value?.model_type === 'embeddings',
  219. type: 'radio',
  220. key: 'task_type',
  221. required: true,
  222. label: {
  223. 'en': 'Task',
  224. 'zh-Hans': 'Task',
  225. },
  226. options: (value?: FormValue) => {
  227. if (value?.model_type === 'chat') {
  228. return [{
  229. key: 'question-answer',
  230. label: {
  231. 'en': '问答',
  232. 'zh-Hans': 'Question Answer',
  233. },
  234. }]
  235. }
  236. return [
  237. {
  238. key: 'text2text-generation',
  239. label: {
  240. 'en': 'Text-to-Text Generation',
  241. 'zh-Hans': 'Text-to-Text Generation',
  242. },
  243. },
  244. {
  245. key: 'text-generation',
  246. label: {
  247. 'en': 'Text Generation',
  248. 'zh-Hans': 'Text Generation',
  249. },
  250. },
  251. ]
  252. },
  253. },
  254. {
  255. hidden: (value?: FormValue) => !(value?.huggingfacehub_api_type === 'inference_endpoints' && value?.model_type === 'embeddings'),
  256. type: 'radio',
  257. key: 'task_type',
  258. required: true,
  259. label: {
  260. 'en': 'Task',
  261. 'zh-Hans': 'Task',
  262. },
  263. options: [
  264. {
  265. key: 'feature-extraction',
  266. label: {
  267. 'en': 'Feature Extraction',
  268. 'zh-Hans': 'Feature Extraction',
  269. },
  270. },
  271. ],
  272. },
  273. ],
  274. },
  275. }
  276. export default config