loader.py 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. import re
  2. import json
  3. from dataclasses import dataclass
  4. from typing import Any, Dict, List, Union, Optional, Tuple
  5. import requests
  6. from modelscope import AutoTokenizer, get_logger, snapshot_download, AutoConfig
  7. from . import TemplateType
  8. from .base import Template, get_template
  9. logger = get_logger()
  10. @dataclass
  11. class TemplateInfo:
  12. template: str = None
  13. template_regex: str = None
  14. modelfile_prefix: str = None
  15. allow_general_name: bool = True
  16. def cases(*names):
  17. ret = []
  18. for name in names:
  19. regex = ''
  20. for letter in name:
  21. if letter.upper() != letter.lower():
  22. regex += f'[{letter.upper()}{letter.lower()}]'
  23. else:
  24. regex += letter
  25. ret.append(regex)
  26. if '-' in regex:
  27. ret.append(regex.replace('-', ' '))
  28. if len(ret) > 1:
  29. ret = '|'.join(ret)
  30. ret = '(' + ret + ')'
  31. else:
  32. ret = ret[0]
  33. return ret
  34. chat_suffix = cases('instruct', 'chat', '-rl', '-it')
  35. def no(*names):
  36. return f'(?!.*{cases(*names)})'
  37. def no_multi_modal():
  38. return no('audio', 'video', 'vl', 'vision')
  39. # Order matters
  40. template_info = [
  41. # llama
  42. TemplateInfo(
  43. template_regex=
  44. f'.*{cases("llama4", "llama-4")}.*',
  45. modelfile_prefix=
  46. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llama4',
  47. ),
  48. ## "llama3"
  49. TemplateInfo(
  50. template_regex=
  51. f'.*{cases("llama3.3", "llama-3.3")}.*',
  52. modelfile_prefix=
  53. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llama3.3',
  54. ),
  55. TemplateInfo(
  56. template_regex=
  57. f'.*{cases("llama3.2", "llama-3.2")}.*{cases("vision")}.*',
  58. modelfile_prefix=
  59. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llama3.2-vision',
  60. ),
  61. TemplateInfo(
  62. template=TemplateType.llama3,
  63. template_regex=
  64. f'.*{cases("llama3.2", "llama-3.2")}{no_multi_modal()}.*{chat_suffix}.*',
  65. modelfile_prefix=
  66. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llama3.2',
  67. ),
  68. TemplateInfo(
  69. template=TemplateType.llama3,
  70. template_regex=
  71. f'.*{cases("llama3.1", "llama-3.1")}{no_multi_modal()}.*{chat_suffix}.*',
  72. modelfile_prefix=
  73. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llama3.1',
  74. ),
  75. TemplateInfo(
  76. template_regex=
  77. f'.*{cases("llama3", "llama-3")}.*{no_multi_modal()}.*{chat_suffix}.*{cases("gradient")}.*',
  78. modelfile_prefix=
  79. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llama3-gradient',
  80. ),
  81. TemplateInfo(
  82. template_regex=
  83. f'.*{cases("llama3", "llama-3")}.*{no_multi_modal()}.*{cases("groq")}.*{cases("tool-use", "tool_use")}.*',
  84. modelfile_prefix=
  85. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llama3-groq-tool-use',
  86. ),
  87. TemplateInfo(
  88. template_regex=
  89. f'.*{cases("llama3", "llama-3")}.*{no_multi_modal()}.*{cases("chatqa")}.*',
  90. modelfile_prefix=
  91. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llama3-chatqa',
  92. ),
  93. TemplateInfo(
  94. template_regex=f'.*{cases("llava-llama-3")}.*',
  95. modelfile_prefix='https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llava-llama3'),
  96. TemplateInfo(
  97. template_regex=f'.*{cases("dolphin")}.*{cases("llama3")}.*',
  98. modelfile_prefix='https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/dolphin-llama3'),
  99. TemplateInfo(
  100. template=TemplateType.llama3,
  101. template_regex=
  102. f'.*{cases("llama3", "llama-3")}{no_multi_modal()}.*{chat_suffix}.*',
  103. modelfile_prefix=
  104. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llama3',
  105. ),
  106. ## "llama"
  107. TemplateInfo(
  108. template_regex=
  109. f'.*{cases("llama2", "llama-2")}{no_multi_modal()}.*{cases("chinese")}.*{chat_suffix}.*',
  110. modelfile_prefix=
  111. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llama2-chinese',
  112. ),
  113. TemplateInfo(
  114. template_regex=
  115. f'.*{cases("codellama")}.*',
  116. modelfile_prefix=
  117. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/codellama',
  118. ),
  119. TemplateInfo(
  120. template_regex=
  121. f'.*{cases("tinyllama")}.*',
  122. modelfile_prefix=
  123. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/tinyllama',
  124. ),
  125. TemplateInfo(
  126. template_regex=
  127. f'.*{cases("llama-pro", "llama_pro")}.*',
  128. modelfile_prefix=
  129. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llama-pro',
  130. ),
  131. TemplateInfo(
  132. template_regex=
  133. f'.*{cases("llama")}.*{cases("guard")}.*',
  134. modelfile_prefix=
  135. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llama-guard3',
  136. ),
  137. TemplateInfo(
  138. template=TemplateType.llama,
  139. template_regex=
  140. f'.*{cases("llama")}{no_multi_modal()}.*{chat_suffix}.*',
  141. modelfile_prefix=
  142. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llama2',
  143. ),
  144. # "deepseek"
  145. TemplateInfo(
  146. template=TemplateType.deepseek2_5,
  147. template_regex=
  148. f'.*{cases("deepseek")}.*{cases("v2.5")}{no_multi_modal()}.*',
  149. modelfile_prefix=
  150. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/deepseek-v2.5',
  151. ),
  152. TemplateInfo(
  153. template=TemplateType.deepseek_coder,
  154. template_regex=
  155. f'.*{cases("deepseek")}.*{cases("coder")}.*{cases("v2")}.*',
  156. modelfile_prefix=
  157. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/deepseek-coder-v2',
  158. ),
  159. TemplateInfo(
  160. template=TemplateType.deepseek_coder,
  161. template_regex=
  162. f'.*{cases("deepseek")}{no("v2", "v2.5")}.*{cases("coder")}.*',
  163. modelfile_prefix=
  164. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/deepseek-coder',
  165. ),
  166. TemplateInfo(
  167. template=TemplateType.deepseek2,
  168. template_regex=
  169. f'.*{cases("deepseek")}.*{cases("v2")}{no("v2.5")}{no_multi_modal()}.*{chat_suffix}.*',
  170. modelfile_prefix=
  171. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/deepseek-v2',
  172. ),
  173. TemplateInfo(
  174. template=TemplateType.deepseek,
  175. template_regex=
  176. f'.*{cases("deepseek")}{no("v2", "v2.5", "coder")}{no_multi_modal()}.*{chat_suffix}.*',
  177. modelfile_prefix=
  178. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/deepseek-llm',
  179. ),
  180. TemplateInfo(
  181. template_regex=f'.*{cases("DeepSeek-V3.1")}.*',
  182. modelfile_prefix=
  183. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/deepseek-v3.1'),
  184. TemplateInfo(
  185. template_regex=
  186. f'.*{cases("deepseek")}.*{cases("v3")}.*',
  187. modelfile_prefix=
  188. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/deepseek-v3',
  189. ),
  190. TemplateInfo(
  191. template_regex=
  192. f'.*{cases("deepseek")}.*{cases("r1")}.*',
  193. modelfile_prefix=
  194. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/deepseek-r1',
  195. ),
  196. # qwen
  197. TemplateInfo(
  198. template_regex=f'.*{cases("qwen3")}.*{cases("coder")}.*',
  199. modelfile_prefix=
  200. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/qwen3-coder',
  201. allow_general_name=False,
  202. ),
  203. TemplateInfo(
  204. template_regex=f'.*{cases("qwen3")}{no_multi_modal()}.*',
  205. modelfile_prefix=
  206. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/qwen3',
  207. ),
  208. TemplateInfo(
  209. template_regex=f'.*{cases("qwen2.5")}.*{cases("vl")}.*',
  210. modelfile_prefix=
  211. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/qwen2.5vl',
  212. ),
  213. TemplateInfo(
  214. template=TemplateType.qwen,
  215. template_regex=f'.*{cases("qwen2.5")}.*{cases("coder")}.*{chat_suffix}.*',
  216. modelfile_prefix=
  217. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/qwen2.5-coder',
  218. ),
  219. TemplateInfo(
  220. template=TemplateType.qwen,
  221. template_regex=f'.*{cases("qwen2.5")}{no_multi_modal()}.*{chat_suffix}.*',
  222. modelfile_prefix=
  223. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/qwen2.5',
  224. ),
  225. TemplateInfo(
  226. template_regex=f'.*{cases("qwen2-math")}.*',
  227. modelfile_prefix=
  228. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/qwen2-math',
  229. ),
  230. TemplateInfo(
  231. template_regex=
  232. f'.*{cases("codeqwen1.5", "codeqwen-1.5")}.*{chat_suffix}.*',
  233. modelfile_prefix=
  234. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/codeqwen',
  235. ),
  236. TemplateInfo(
  237. template=TemplateType.qwen,
  238. template_regex=f'.*{cases("qwen2", "qwen1.5")}{no_multi_modal()}.*',
  239. modelfile_prefix=
  240. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/qwen2',
  241. ),
  242. TemplateInfo(
  243. template=TemplateType.qwen,
  244. template_regex=f'.*{cases("qwen")}{no_multi_modal()}.*{chat_suffix}.*',
  245. modelfile_prefix=
  246. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/qwen',
  247. ),
  248. # gemma
  249. TemplateInfo(
  250. template=TemplateType.gemma,
  251. template_regex=
  252. f'{no("pali")}.*{cases("gemma")}.*{cases("3n")}.*',
  253. modelfile_prefix=
  254. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/gemma3n',
  255. ),
  256. TemplateInfo(
  257. template_regex=
  258. f'.*{cases("codegemma")}.*',
  259. modelfile_prefix=
  260. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/codegemma',
  261. ),
  262. TemplateInfo(
  263. template=TemplateType.gemma,
  264. template_regex=
  265. f'{no("pali")}.*{cases("gemma3", "gemma-3")}.*',
  266. modelfile_prefix=
  267. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/gemma3',
  268. ),
  269. TemplateInfo(
  270. template=TemplateType.gemma,
  271. template_regex=
  272. f'{no("pali")}.*{cases("gemma2", "gemma-2")}\\b.*{chat_suffix}.*',
  273. modelfile_prefix=
  274. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/gemma2',
  275. ),
  276. TemplateInfo(
  277. template_regex=
  278. f'.*{cases("shieldgemma")}.*',
  279. modelfile_prefix=
  280. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/shieldgemma',
  281. ),
  282. TemplateInfo(
  283. template=TemplateType.gemma,
  284. template_regex=
  285. f'{no("pali")}.*{cases("gemma")}.*',
  286. modelfile_prefix=
  287. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/gemma',
  288. ),
  289. # "dolphin"
  290. TemplateInfo(
  291. template_regex=
  292. f'.*{cases("dolphin")}.*{cases("-mixtral")}.*',
  293. modelfile_prefix=
  294. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/dolphin-mixtral',
  295. ),
  296. TemplateInfo(
  297. template_regex=
  298. f'.*{cases("dolphin")}.*{cases("mistral")}.*',
  299. modelfile_prefix=
  300. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/dolphin-mistral',
  301. ),
  302. TemplateInfo(
  303. template_regex=f'.*{cases("dolphin3", "dolphin-3")}.*',
  304. modelfile_prefix=
  305. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/dolphin3'),
  306. # "phi"
  307. TemplateInfo(
  308. template_regex=
  309. f'.*{cases("phi-4-reasoning")}.*',
  310. modelfile_prefix=
  311. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/phi4-reasoning',
  312. ),
  313. TemplateInfo(
  314. template_regex=
  315. f'.*{cases("phi-4-mini-reasoning")}.*',
  316. modelfile_prefix=
  317. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/phi4-mini-reasoning',
  318. ),
  319. TemplateInfo(
  320. template_regex=
  321. f'.*{cases("llava-phi3", "llava-phi-3")}.*',
  322. modelfile_prefix=
  323. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llava-phi3',
  324. ),
  325. TemplateInfo(
  326. template_regex=
  327. f'.*{cases("phi3.5", "phi-3.5")}{no_multi_modal()}.*{chat_suffix}.*',
  328. modelfile_prefix=
  329. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/phi3.5',
  330. ),
  331. TemplateInfo(
  332. template=TemplateType.phi3,
  333. template_regex=
  334. f'.*{cases("phi3", "phi-3")}{no_multi_modal()}.*{chat_suffix}.*',
  335. modelfile_prefix=
  336. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/phi3',
  337. ),
  338. TemplateInfo(
  339. template_regex=
  340. f'.*{cases("phi4-mini", "phi-4-mini")}.*',
  341. modelfile_prefix=
  342. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/phi4-mini',
  343. ),
  344. TemplateInfo(
  345. template_regex=
  346. f'.*{cases("phi4", "phi-4")}{no_multi_modal()}.*',
  347. modelfile_prefix=
  348. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/phi4',
  349. ),
  350. TemplateInfo(
  351. template_regex=
  352. f'.*{cases("phi")}{no_multi_modal()}.*',
  353. modelfile_prefix=
  354. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/phi',
  355. ),
  356. # "mistral"
  357. TemplateInfo(
  358. template_regex=
  359. f'.*{cases("yarn")}.*{cases("mistral")}.*',
  360. modelfile_prefix=
  361. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/yarn-mistral',
  362. ),
  363. TemplateInfo(
  364. template_regex=
  365. f'.*{cases("mistral")}.*{cases("small3.2", "small-3.2")}.*',
  366. modelfile_prefix=
  367. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/mistral-small3.2',
  368. ),
  369. TemplateInfo(
  370. template_regex=
  371. f'.*{cases("mistral")}.*{cases("small3.1", "small-3.1")}.*',
  372. modelfile_prefix=
  373. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/mistral-small3.1',
  374. ),
  375. TemplateInfo(
  376. template_regex=
  377. f'.*{cases("mistral")}.*{cases("large")}.*',
  378. modelfile_prefix=
  379. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/mistral-large',
  380. ),
  381. TemplateInfo(
  382. template_regex=
  383. f'.*{cases("mistral")}.*{cases("small")}.*',
  384. modelfile_prefix=
  385. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/mistral-small',
  386. ),
  387. TemplateInfo(
  388. template=TemplateType.mistral_nemo,
  389. template_regex=f'.*{cases("Mistral-Nemo")}{no_multi_modal()}.*',
  390. modelfile_prefix=
  391. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/mistral-nemo',
  392. ),
  393. TemplateInfo(
  394. template_regex=
  395. f'.*{cases("mistral")}.*{cases("openorca")}.*',
  396. modelfile_prefix=
  397. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/mistral-openorca',
  398. ),
  399. TemplateInfo(
  400. template_regex=
  401. f'.*{cases("mistrallite")}.*',
  402. modelfile_prefix=
  403. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/mistrallite',
  404. ),
  405. ## other mistral: set Type.llama
  406. TemplateInfo(
  407. template=TemplateType.llama,
  408. template_regex=
  409. f'.*{cases("mistral", "ministral")}{no_multi_modal()}.*{chat_suffix}.*',
  410. modelfile_prefix=
  411. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/mistral',
  412. ),
  413. # "mixtral"
  414. TemplateInfo(
  415. template_regex=
  416. f'.*{cases("nous-hermes2", "nous-hermes-2")}.*{cases("mixtral")}.*',
  417. modelfile_prefix=
  418. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/nous-hermes2-mixtral',
  419. ),
  420. TemplateInfo(
  421. template=TemplateType.llama,
  422. template_regex=
  423. f'.*{cases("mixtral")}{no_multi_modal()}.*',
  424. modelfile_prefix=
  425. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/mixtral',
  426. ),
  427. # codestral
  428. TemplateInfo(
  429. template=TemplateType.llama,
  430. template_regex=
  431. f'.*{cases("codestral")}{no_multi_modal()}.*',
  432. modelfile_prefix=
  433. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/codestral',
  434. ),
  435. # nous-hermes2
  436. TemplateInfo(
  437. template_regex=
  438. f'.*{cases("nous-hermes2", "nous-hermes-2")}.*',
  439. modelfile_prefix=
  440. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/nous-hermes2',
  441. ),
  442. TemplateInfo(
  443. template_regex=f'.*{cases("nous-hermes")}.*',
  444. modelfile_prefix=
  445. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/nous-hermes'),
  446. # "yi"
  447. TemplateInfo(
  448. template=TemplateType.yi_coder,
  449. template_regex=f'.*{cases("yi")}.*{cases("coder")}.*{chat_suffix}.*',
  450. modelfile_prefix=
  451. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/yi-coder',
  452. ),
  453. TemplateInfo(
  454. template=TemplateType.chatml,
  455. template_regex=
  456. f'.*{cases("yi")}{no_multi_modal()}{no("coder")}.*',
  457. modelfile_prefix=
  458. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/yi',
  459. ),
  460. # "llava"
  461. TemplateInfo(
  462. template_regex=
  463. f'.*{cases("bakllava")}.*',
  464. modelfile_prefix=
  465. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/bakllava',
  466. ),
  467. TemplateInfo(
  468. template_regex=
  469. f'.*{cases("llava")}.*',
  470. modelfile_prefix=
  471. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/llava',
  472. ),
  473. # "nemotron"
  474. TemplateInfo(
  475. template_regex=
  476. f'.*{cases("nemotron-mini")}.*',
  477. modelfile_prefix=
  478. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/nemotron-mini',
  479. ),
  480. TemplateInfo(
  481. template_regex=
  482. f'.*{cases("nemotron")}.*',
  483. modelfile_prefix=
  484. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/nemotron',
  485. ),
  486. # "minicpm"
  487. TemplateInfo(
  488. template_regex=f'.*{cases("minicpm-v")}.*',
  489. modelfile_prefix=
  490. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/minicpm-v'
  491. ),
  492. TemplateInfo(
  493. template=TemplateType.chatml,
  494. template_regex=f'.*{cases("minicpm")}{no("-v")}.*',
  495. modelfile_prefix=
  496. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/yi'
  497. ),
  498. # chatglm
  499. TemplateInfo(
  500. template=TemplateType.chatglm2,
  501. template_regex=f'.*{cases("chatglm2")}{no_multi_modal()}.*'),
  502. TemplateInfo(
  503. template=TemplateType.chatglm3,
  504. template_regex=f'.*{cases("chatglm3")}{no_multi_modal()}.*'),
  505. TemplateInfo(
  506. template=TemplateType.chatglm4,
  507. template_regex=f'.*{cases("glm4", "glm-4")}{no_multi_modal()}.*{chat_suffix}.*',
  508. modelfile_prefix=
  509. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/glm4',
  510. ),
  511. # baichuan
  512. TemplateInfo(
  513. template=TemplateType.baichuan,
  514. template_regex=
  515. f'.*{cases("baichuan")}{no_multi_modal()}.*{chat_suffix}.*'),
  516. # "command-r"
  517. TemplateInfo(
  518. template_regex=
  519. f'.*{cases("command-r-plus")}.*',
  520. modelfile_prefix=
  521. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/command-r-plus',
  522. ),
  523. TemplateInfo(
  524. template_regex=
  525. f'.*{cases("command-r7b-arabic")}.*',
  526. modelfile_prefix=
  527. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/command-r7b-arabic',
  528. ),
  529. TemplateInfo(
  530. template_regex=
  531. f'.*{cases("command-r7b")}.*',
  532. modelfile_prefix=
  533. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/command-r7b',
  534. ),
  535. TemplateInfo(
  536. template_regex=
  537. f'.*{cases("command-r")}.*',
  538. modelfile_prefix=
  539. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/command-r',
  540. ),
  541. TemplateInfo(
  542. template_regex=
  543. f'.*{cases("command-a")}.*',
  544. modelfile_prefix=
  545. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/command-a',
  546. ),
  547. # codegeex
  548. TemplateInfo(
  549. template=TemplateType.codegeex4,
  550. template_regex=f'.*{cases("codegeex4")}{no_multi_modal()}.*',
  551. modelfile_prefix=
  552. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/codegeex4',
  553. ),
  554. # wizard
  555. TemplateInfo(
  556. template_regex=
  557. f'.*{cases("wizard-vicuna")}.*{cases("uncensored")}.*',
  558. modelfile_prefix=
  559. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/wizard-vicuna-uncensored',
  560. ),
  561. TemplateInfo(
  562. template_regex=
  563. f'.*{cases("wizardlm2", "wizardlm-2")}.*',
  564. modelfile_prefix=
  565. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/wizardlm2',
  566. ),
  567. TemplateInfo(
  568. template_regex=
  569. f'.*{cases("wizardcoder")}.*',
  570. modelfile_prefix=
  571. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/wizardcoder',
  572. ),
  573. TemplateInfo(
  574. template_regex=
  575. f'.*{cases("wizard-math", "wizardmath")}.*',
  576. modelfile_prefix=
  577. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/wizard-math',
  578. ),
  579. TemplateInfo(
  580. template_regex=
  581. f'.*{cases("wizardlm")}.*',
  582. modelfile_prefix=
  583. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/wizardlm',
  584. ),
  585. # magistral
  586. TemplateInfo(
  587. template_regex=
  588. f'.*{cases("magistral")}.*',
  589. modelfile_prefix=
  590. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/magistral',
  591. ),
  592. # devstral
  593. TemplateInfo(
  594. template_regex=
  595. f'.*{cases("devstral")}.*',
  596. modelfile_prefix=
  597. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/devstral',
  598. ),
  599. # vicuna
  600. TemplateInfo(
  601. template_regex=
  602. f'.*{cases("vicuna")}.*',
  603. modelfile_prefix=
  604. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/vicuna',
  605. ),
  606. # "stable"
  607. TemplateInfo(
  608. template_regex=
  609. f'.*{cases("stable-code")}.*',
  610. modelfile_prefix=
  611. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/stable-code',
  612. ),
  613. TemplateInfo(
  614. template_regex=
  615. f'.*{cases("stablelm")}.*',
  616. modelfile_prefix=
  617. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/stablelm2',
  618. ),
  619. # idefics3
  620. TemplateInfo(
  621. template=TemplateType.idefics3,
  622. template_regex=f'.*{cases("idefics3")}{no_multi_modal()}.*'),
  623. # internlm
  624. TemplateInfo(
  625. template=TemplateType.internlm,
  626. template_regex=
  627. f'.*{cases("internlm")}{no("internlm2", "internlm3")}{no_multi_modal()}.*{chat_suffix}.*'
  628. ),
  629. # internlm2
  630. TemplateInfo(
  631. template=TemplateType.internlm2,
  632. template_regex=
  633. f'.*{cases("internlm2")}{no_multi_modal()}.*{chat_suffix}.*',
  634. modelfile_prefix=
  635. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/internlm2',
  636. ),
  637. # yuan
  638. TemplateInfo(
  639. template=TemplateType.yuan,
  640. template_regex=f'.*{cases("Yuan")}{no_multi_modal()}.*'),
  641. # xverse
  642. TemplateInfo(
  643. template=TemplateType.xverse,
  644. template_regex=f'.*{cases("xverse")}{no_multi_modal()}.*{chat_suffix}.*'
  645. ),
  646. # skywork
  647. TemplateInfo(
  648. template=TemplateType.skywork,
  649. template_regex=
  650. f'.*{cases("skywork")}{no_multi_modal()}.*{chat_suffix}.*'),
  651. # bluelm
  652. TemplateInfo(
  653. template=TemplateType.bluelm,
  654. template_regex=f'.*{cases("bluelm")}{no_multi_modal()}.*{chat_suffix}.*'
  655. ),
  656. # zephyr
  657. TemplateInfo(
  658. template=TemplateType.zephyr,
  659. template_regex=f'.*{cases("zephyr")}{no_multi_modal()}.*'),
  660. # orion
  661. TemplateInfo(
  662. template=TemplateType.orion,
  663. template_regex=f'.*{cases("orion")}{no_multi_modal()}.*{chat_suffix}.*'
  664. ),
  665. # telechat
  666. TemplateInfo(
  667. template=TemplateType.telechat,
  668. template_regex=f'.*{cases("TeleChat")}{no("v2")}.*'),
  669. # telechat_v2
  670. TemplateInfo(
  671. template=TemplateType.telechat_v2,
  672. template_regex=f'.*{cases("TeleChat")}.*{cases("v2")}.*'),
  673. # tulu3
  674. TemplateInfo(
  675. template_regex=f'.*{cases("tulu3", "tulu-3")}.*',
  676. modelfile_prefix=
  677. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/tulu3'),
  678. # athene-v2
  679. TemplateInfo(
  680. template_regex=f'.*{cases("athene-v2")}.*',
  681. modelfile_prefix=
  682. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/athene-v2'),
  683. # granite
  684. TemplateInfo(
  685. template_regex=f'.*{cases("granite-guardian-3")}.*',
  686. modelfile_prefix=
  687. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/granite3-guardian'),
  688. TemplateInfo(
  689. template_regex=f'.*{cases("granite")}.*{cases("code")}.*',
  690. modelfile_prefix=
  691. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/granite-code'),
  692. TemplateInfo(
  693. template_regex=f'.*{cases("granite")}.*{cases("3.3")}.*',
  694. modelfile_prefix=
  695. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/granite3.3'),
  696. TemplateInfo(
  697. template_regex=f'.*{cases("granite")}.*{cases("vision")}.*{cases("3.2")}.*',
  698. modelfile_prefix=
  699. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/granite3.2-vision'),
  700. TemplateInfo(
  701. template_regex=f'.*{cases("granite")}.*{cases("3.2")}.*',
  702. modelfile_prefix=
  703. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/granite3.2'),
  704. TemplateInfo(
  705. template_regex=f'.*{cases("granite-3.1")}.*{cases("2b", "8b")}.*',
  706. modelfile_prefix=
  707. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/granite3.1-dense'),
  708. TemplateInfo(
  709. template_regex=f'.*{cases("granite-3.1")}.*{cases("1b", "3b")}.*',
  710. modelfile_prefix=
  711. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/granite3.1-moe'),
  712. TemplateInfo(
  713. template_regex=f'.*{cases("granite-embedding")}.*',
  714. modelfile_prefix=
  715. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/granite-embedding'),
  716. TemplateInfo(
  717. template_regex=f'.*{cases("granite-3")}.*{cases("2b", "8b")}.*',
  718. modelfile_prefix=
  719. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/granite3-dense'),
  720. TemplateInfo(
  721. template_regex=f'.*{cases("granite-3")}.*{cases("1b", "3b")}.*',
  722. modelfile_prefix=
  723. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/granite3-moe'),
  724. # opencoder
  725. TemplateInfo(
  726. template_regex=f'.*{cases("opencoder")}.*',
  727. modelfile_prefix=
  728. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/opencoder'),
  729. # smollm
  730. TemplateInfo(
  731. template_regex=f'.*{cases("smollm2")}.*',
  732. modelfile_prefix=
  733. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/smollm2'),
  734. TemplateInfo(
  735. template_regex=f'.*{cases("smollm")}.*',
  736. modelfile_prefix=
  737. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/smollm'),
  738. # 'aya'
  739. TemplateInfo(
  740. template_regex=f'.*{cases("aya-expanse")}.*',
  741. modelfile_prefix=
  742. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/aya-expanse'),
  743. TemplateInfo(
  744. template_regex=f'.*{cases("aya")}.*',
  745. modelfile_prefix=
  746. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/aya'),
  747. # falcon
  748. TemplateInfo(
  749. template_regex=f'.*{cases("falcon3")}.*',
  750. modelfile_prefix=
  751. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/falcon3'),
  752. TemplateInfo(
  753. template_regex=f'.*{cases("falcon")}.*{cases("-2")}.*',
  754. modelfile_prefix=
  755. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/falcon2'),
  756. TemplateInfo(
  757. template_regex=f'.*{cases("falcon")}.*',
  758. modelfile_prefix=
  759. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/falcon'),
  760. # smallthinker
  761. TemplateInfo(
  762. template_regex=f'.*{cases("smallthinker")}.*',
  763. modelfile_prefix=
  764. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/smallthinker'),
  765. TemplateInfo(
  766. template_regex=f'.*{cases("openthinker")}.*',
  767. modelfile_prefix=
  768. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/openthinker',
  769. allow_general_name=False),
  770. TemplateInfo(
  771. template_regex=
  772. f'.*{cases("olmo2", "olmo-2")}.*',
  773. modelfile_prefix=
  774. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/olmo2',
  775. ),
  776. TemplateInfo(
  777. template_regex=f'.*{cases("nomic-embed-text")}.*',
  778. modelfile_prefix=
  779. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/nomic-embed-text'),
  780. TemplateInfo(
  781. template_regex=f'.*{cases("mxbai-embed-large")}.*',
  782. modelfile_prefix=
  783. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/mxbai-embed-large'),
  784. TemplateInfo(
  785. template_regex=f'.*{cases("starcoder2")}.*',
  786. modelfile_prefix=
  787. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/starcoder2'),
  788. TemplateInfo(
  789. template_regex=f'.*{cases("orca-mini", "orca_mini")}.*',
  790. modelfile_prefix=
  791. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/orca-mini'),
  792. TemplateInfo(
  793. template_regex=f'.*{cases("zephyr")}.*',
  794. modelfile_prefix=
  795. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/zephyr'),
  796. TemplateInfo(
  797. template_regex=f'.*{cases("snowflake-arctic-embed")}.*',
  798. modelfile_prefix=
  799. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/snowflake-arctic-embed'),
  800. TemplateInfo(
  801. template_regex=f'.*{cases("starcoder")}.*',
  802. modelfile_prefix=
  803. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/starcoder'),
  804. TemplateInfo(
  805. template_regex=f'.*{cases("all-minilm")}.*',
  806. modelfile_prefix=
  807. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/all-minilm'),
  808. TemplateInfo(
  809. template_regex=f'.*{cases("openchat")}.*',
  810. modelfile_prefix=
  811. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/openchat'),
  812. TemplateInfo(
  813. template_regex=f'.*{cases("openhermes")}.*',
  814. modelfile_prefix=
  815. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/openhermes'),
  816. TemplateInfo(
  817. template_regex=f'.*{cases("reflection")}.*',
  818. modelfile_prefix=
  819. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/reflection'),
  820. TemplateInfo(
  821. template_regex=f'.*{cases("neural-chat")}.*',
  822. modelfile_prefix=
  823. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/neural-chat'),
  824. TemplateInfo(
  825. template_regex=f'.*{cases("moondream")}.*',
  826. modelfile_prefix=
  827. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/moondream'),
  828. TemplateInfo(
  829. template_regex=f'.*{cases("xwin")}.*{cases("lm")}.*',
  830. modelfile_prefix=
  831. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/xwinlm'),
  832. TemplateInfo(
  833. template_regex=f'.*{cases("sqlcoder")}.*',
  834. modelfile_prefix=
  835. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/sqlcoder'),
  836. TemplateInfo(
  837. template_regex=f'.*{cases("starling-lm")}.*',
  838. modelfile_prefix=
  839. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/starling-lm'),
  840. TemplateInfo(
  841. template_regex=f'.*{cases("solar-pro")}.*',
  842. modelfile_prefix=
  843. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/solar-pro'),
  844. TemplateInfo(
  845. template_regex=f'.*{cases("solar")}.*',
  846. modelfile_prefix=
  847. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/solar'),
  848. TemplateInfo(
  849. template_regex=f'.*{cases("orca2", "orca-2", "orca_2")}.*',
  850. modelfile_prefix=
  851. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/orca2'),
  852. TemplateInfo(
  853. template_regex=f'.*{cases("hermes3", "hermes-3", "hermes_3")}.*',
  854. modelfile_prefix=
  855. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/hermes3'),
  856. TemplateInfo(
  857. template_regex=f'.*{cases("meditron")}.*',
  858. modelfile_prefix=
  859. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/meditron'),
  860. TemplateInfo(
  861. template_regex=f'.*{cases("nexusraven")}.*',
  862. modelfile_prefix=
  863. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/nexusraven'),
  864. TemplateInfo(
  865. template_regex=f'.*{cases("magicoder")}.*',
  866. modelfile_prefix=
  867. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/magicoder'),
  868. TemplateInfo(
  869. template_regex=
  870. f'.*{cases("deepcoder")}.*',
  871. modelfile_prefix=
  872. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/deepcoder'),
  873. TemplateInfo(
  874. template_regex=f'.*{cases("cogito")}.*',
  875. modelfile_prefix=
  876. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/cogito'),
  877. TemplateInfo(
  878. template_regex=f'.*{cases("bge-m3")}.*',
  879. modelfile_prefix=
  880. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/bge-m3'),
  881. TemplateInfo(
  882. template_regex=f'.*{cases("notux")}.*',
  883. modelfile_prefix=
  884. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/notux'),
  885. TemplateInfo(
  886. template_regex=f'.*{cases("open")}.*{cases("orca")}.*{cases("platypus2")}.*',
  887. modelfile_prefix=
  888. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/open-orca-platypus2'),
  889. TemplateInfo(
  890. template_regex=f'.*{cases("notus")}.*',
  891. modelfile_prefix=
  892. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/notus'),
  893. TemplateInfo(
  894. template_regex=f'.*{cases("mathstral")}.*',
  895. modelfile_prefix=
  896. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/mathstral'),
  897. TemplateInfo(
  898. template_regex=f'.*{cases("dbrx")}.*',
  899. modelfile_prefix=
  900. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/dbrx'),
  901. TemplateInfo(
  902. template_regex=f'.*{cases("nuextract")}.*',
  903. modelfile_prefix=
  904. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/nuextract'),
  905. TemplateInfo(
  906. template_regex=f'.*{cases("reader-lm")}.*',
  907. modelfile_prefix=
  908. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/reader-lm'),
  909. TemplateInfo(
  910. template_regex=f'.*{cases("alfred")}.*',
  911. modelfile_prefix=
  912. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/alfred'),
  913. TemplateInfo(
  914. template_regex=f'.*{cases("bge-large")}.*',
  915. modelfile_prefix=
  916. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/bge-large'),
  917. TemplateInfo(
  918. template_regex=f'.*{cases("paraphrase-multilingual")}.*',
  919. modelfile_prefix=
  920. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/paraphrase-multilingual'),
  921. TemplateInfo(
  922. template_regex=f'.*{cases("marco")}.*{cases("o1")}.*',
  923. modelfile_prefix=
  924. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/marco-o1'),
  925. TemplateInfo(
  926. template_regex=f'.*{cases("qwq")}.*',
  927. modelfile_prefix=
  928. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/qwq'),
  929. TemplateInfo(
  930. template_regex=f'.*{cases("exaone")}.*',
  931. modelfile_prefix=
  932. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/exaone3.5'),
  933. TemplateInfo(
  934. template_regex=f'.*{cases("r1-1776")}.*',
  935. modelfile_prefix=
  936. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/r1-1776'),
  937. TemplateInfo(
  938. template_regex=f'.*{cases("deepscaler")}.*',
  939. modelfile_prefix=
  940. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/deepscaler'),
  941. TemplateInfo(
  942. template_regex=f'.*{cases("granite-4.0")}.*',
  943. modelfile_prefix=
  944. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/granite4'),
  945. TemplateInfo(
  946. template_regex=f'.*{cases("gpt-oss")}.*',
  947. modelfile_prefix=
  948. 'https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/gpt-oss'),
  949. ]
  950. class TemplateLoader:
  951. @staticmethod
  952. def load_by_model_id(model_id: str, **kwargs) -> Template:
  953. """Load a template by model-id
  954. Args:
  955. model_id: The model-id used to load the proper template
  956. kwargs:
  957. revision: the revision of the model, default is `master`
  958. Returns:
  959. The template instance
  960. """
  961. ignore_file_pattern = [r'.+\.bin$', r'.+\.safetensors$', r'.+\.gguf$']
  962. tokenizer = kwargs.get('tokenizer')
  963. config = kwargs.get('config')
  964. trust_remote_code = kwargs.pop('trust_remote_code', None)
  965. for _info in template_info:
  966. if re.fullmatch(_info.template_regex, model_id):
  967. if _info.template:
  968. if tokenizer is None:
  969. try:
  970. model_dir = snapshot_download(
  971. model_id,
  972. revision=kwargs.pop('revision', 'master'),
  973. ignore_file_pattern=ignore_file_pattern)
  974. logger.warning(f'Use trust_remote_code=True. Will invoke codes from {model_dir}.'
  975. ' Please make sure that you can trust the external codes.'
  976. )
  977. tokenizer = AutoTokenizer.from_pretrained(
  978. model_dir, trust_remote_code=trust_remote_code)
  979. config = AutoConfig.from_pretrained(model_dir, trust_remote_code=trust_remote_code)
  980. except Exception:
  981. pass
  982. return TemplateLoader.load_by_template_name(
  983. _info.template, tokenizer=tokenizer, config=config, **kwargs)
  984. @staticmethod
  985. def load_by_template_name(template_name: str, **kwargs) -> Template:
  986. """Load a template by model-id
  987. Args:
  988. template_name: The template name used to load the proper template
  989. kwargs:
  990. tokenizer: The tokenizer of the model
  991. default_system: The extra default system info
  992. max_length: The max_length for the sequence
  993. truncation_strategy: 'delete' or 'truncation_left' the sequence of the length exceeds the limit
  994. Returns:
  995. The template instance
  996. """
  997. template = get_template(template_name, tokenizer=kwargs.pop('tokenizer', None), **kwargs)
  998. template.config = kwargs.get('config')
  999. return template
  1000. @staticmethod
  1001. def replace_and_concat(template: Template, template_list: List,
  1002. placeholder: str, keyword: str):
  1003. final_str = ''
  1004. for t in template_list:
  1005. if isinstance(t, str):
  1006. final_str += t.replace(placeholder, keyword)
  1007. elif isinstance(t, (tuple, list)):
  1008. if isinstance(t[0], int):
  1009. final_str += template.tokenizer.decode(t)
  1010. else:
  1011. for attr in t:
  1012. if attr == 'bos_token_id':
  1013. final_str += template.tokenizer.bos_token
  1014. elif attr == 'eos_token_id':
  1015. final_str += template.tokenizer.eos_token
  1016. else:
  1017. raise ValueError(f'Unknown token: {attr}')
  1018. return final_str
  1019. @staticmethod
  1020. def _format_return(template_lines: str, params: Dict, split: bool, license: Optional[str] = None) -> Union[
  1021. str, Dict]:
  1022. if split:
  1023. if params:
  1024. params = json.dumps(params)
  1025. return {'params': params, 'template': template_lines, 'license': license}
  1026. content = ''
  1027. content += 'FROM {gguf_file}\n\n'
  1028. if params:
  1029. for key, values in params.items():
  1030. if isinstance(values, list):
  1031. for value in values:
  1032. content += f'PARAMETER {key} {json.dumps(value)}\n'
  1033. else:
  1034. content += f'PARAMETER {key} {json.dumps(values)}\n'
  1035. content += '\n'
  1036. if template_lines:
  1037. content += ('TEMPLATE """' + template_lines + '"""\n')
  1038. return content
  1039. @staticmethod
  1040. def to_ollama(model_id: str = None,
  1041. template_name: str = None,
  1042. gguf_file: str = None,
  1043. gguf_meta: Dict[str, Any] = None,
  1044. split: bool = False,
  1045. debug: bool = False,
  1046. **kwargs) -> Union[str, Dict, Tuple[Dict, TemplateInfo], Tuple[str, TemplateInfo], None]:
  1047. """Export to ollama ModelFile
  1048. Args:
  1049. model_id: The model-id to use
  1050. template_name: An extra template name to use
  1051. gguf_file: An extra gguf_file path to use in the `FROM` field
  1052. gguf_meta: An gguf extra meta info
  1053. split: bool. Return str modelfile content, or dict of params and template
  1054. debug: bool. Whether or not to return the matched TemplateInfo
  1055. Returns:
  1056. The ModelFile content, or dictionary of params and template, returns `None` if no template found
  1057. """
  1058. if not model_id and not template_name and not gguf_meta:
  1059. raise ValueError(
  1060. f'Please make sure you model_id: {model_id} '
  1061. f'and template_name: {template_name} is supported.')
  1062. logger.info('Exporting to ollama:')
  1063. names = {}
  1064. match_infos = {}
  1065. if gguf_meta:
  1066. gguf_header_name = gguf_meta.get("general.name", None)
  1067. if gguf_header_name:
  1068. names['gguf_header_name'] = gguf_header_name
  1069. if model_id:
  1070. names['model_id'] = model_id
  1071. for name_type, name in names.items():
  1072. for _info in template_info:
  1073. if re.fullmatch(_info.template_regex, name):
  1074. if _info.modelfile_prefix and not kwargs.get('ignore_oss_model_file', False):
  1075. match_infos[name_type] = name, _info
  1076. break
  1077. _name = None
  1078. _info = None
  1079. if len(match_infos) == 1:
  1080. _, (_name, _info) = match_infos.popitem()
  1081. elif len(match_infos) > 1:
  1082. if not match_infos['model_id'][1].allow_general_name:
  1083. _name, _info = match_infos['model_id']
  1084. else:
  1085. _name, _info = match_infos['gguf_header_name']
  1086. if _info:
  1087. template_str = TemplateLoader._read_content_from_url(
  1088. _info.modelfile_prefix + '.template')
  1089. if not template_str:
  1090. logger.info(f'{_name} has no template file.')
  1091. params = TemplateLoader._read_content_from_url(_info.modelfile_prefix + '.params')
  1092. if params:
  1093. params = json.loads(params)
  1094. else:
  1095. logger.info(f'{_name} has no params file.')
  1096. license = TemplateLoader._read_content_from_url(
  1097. _info.modelfile_prefix + '.license')
  1098. if not template_str:
  1099. logger.info(f'{_name} has no license file.')
  1100. format_out = TemplateLoader._format_return(template_str, params, split, license)
  1101. if debug:
  1102. return format_out, _info
  1103. return format_out
  1104. if template_name:
  1105. template = TemplateLoader.load_by_template_name(
  1106. template_name, **kwargs)
  1107. else:
  1108. template = TemplateLoader.load_by_model_id(
  1109. model_id, **kwargs)
  1110. if not template:
  1111. return None
  1112. # template
  1113. template_lines = ''
  1114. _prefix = TemplateLoader.replace_and_concat(template, template.prefix, "", "")
  1115. if _prefix:
  1116. template_lines += (
  1117. f'{{{{ if .System }}}}'
  1118. f'{TemplateLoader.replace_and_concat(template, template.system_prefix or [], "{{SYSTEM}}", "{{ .System }}")}'
  1119. f'{{{{ else }}}}{_prefix}'
  1120. f'{{{{ end }}}}')
  1121. else:
  1122. template_lines += (
  1123. f'{{{{ if .System }}}}'
  1124. f'{TemplateLoader.replace_and_concat(template, template.system_prefix or [], "{{SYSTEM}}", "{{ .System }}")}'
  1125. f'{{{{ end }}}}')
  1126. template_lines += (
  1127. f'{{{{ if .Prompt }}}}'
  1128. f'{TemplateLoader.replace_and_concat(template, template.prompt, "{{QUERY}}", "{{ .Prompt }}")}'
  1129. f'{{{{ end }}}}')
  1130. template_lines += '{{ .Response }}'
  1131. template_lines += TemplateLoader.replace_and_concat(template, template.suffix,
  1132. '', '')
  1133. # stop tokens
  1134. all_eos_tokens = {TemplateLoader.replace_and_concat(template, template.suffix, "", "")}
  1135. if getattr(template, 'tokenizer', None):
  1136. eos_token = TemplateLoader.replace_and_concat(template, [["eos_token_id"]], "", "")
  1137. all_eos_tokens.add(eos_token)
  1138. if getattr(template, 'config', None) and getattr(template.config, 'eos_token_id'):
  1139. eos_token_id = template.config.eos_token_id
  1140. eos_token = TemplateLoader.replace_and_concat(template, [[eos_token_id]], "", "")
  1141. all_eos_tokens.add(eos_token)
  1142. stop_tokens = list()
  1143. for eos_token in all_eos_tokens:
  1144. stop_tokens.append(eos_token)
  1145. params = {'stop': stop_tokens}
  1146. return TemplateLoader._format_return(template_lines, params, split)
  1147. @staticmethod
  1148. def _read_content_from_url(url):
  1149. try:
  1150. response = requests.get(url)
  1151. response.raise_for_status()
  1152. except requests.exceptions.HTTPError as e:
  1153. return None
  1154. content = response.content
  1155. return content.decode('utf-8')