routes_create_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. package server
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "path/filepath"
  12. "slices"
  13. "testing"
  14. "github.com/gin-gonic/gin"
  15. "github.com/ollama/ollama/api"
  16. "github.com/ollama/ollama/envconfig"
  17. "github.com/ollama/ollama/llm"
  18. )
  19. var stream bool = false
  20. func createBinFile(t *testing.T, kv map[string]any, ti []llm.Tensor) string {
  21. t.Helper()
  22. f, err := os.CreateTemp(t.TempDir(), "")
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. defer f.Close()
  27. if err := llm.NewGGUFV3(binary.LittleEndian).Encode(f, kv, ti); err != nil {
  28. t.Fatal(err)
  29. }
  30. return f.Name()
  31. }
  32. type responseRecorder struct {
  33. *httptest.ResponseRecorder
  34. http.CloseNotifier
  35. }
  36. func NewRecorder() *responseRecorder {
  37. return &responseRecorder{
  38. ResponseRecorder: httptest.NewRecorder(),
  39. }
  40. }
  41. func (t *responseRecorder) CloseNotify() <-chan bool {
  42. return make(chan bool)
  43. }
  44. func createRequest(t *testing.T, fn func(*gin.Context), body any) *httptest.ResponseRecorder {
  45. t.Helper()
  46. w := NewRecorder()
  47. c, _ := gin.CreateTestContext(w)
  48. var b bytes.Buffer
  49. if err := json.NewEncoder(&b).Encode(body); err != nil {
  50. t.Fatal(err)
  51. }
  52. c.Request = &http.Request{
  53. Body: io.NopCloser(&b),
  54. }
  55. fn(c)
  56. return w.ResponseRecorder
  57. }
  58. func checkFileExists(t *testing.T, p string, expect []string) {
  59. t.Helper()
  60. actual, err := filepath.Glob(p)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. if !slices.Equal(actual, expect) {
  65. t.Fatalf("expected slices to be equal %v", actual)
  66. }
  67. }
  68. func TestCreateFromBin(t *testing.T) {
  69. gin.SetMode(gin.TestMode)
  70. p := t.TempDir()
  71. t.Setenv("OLLAMA_MODELS", p)
  72. envconfig.LoadConfig()
  73. var s Server
  74. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  75. Name: "test",
  76. Modelfile: fmt.Sprintf("FROM %s", createBinFile(t, nil, nil)),
  77. Stream: &stream,
  78. })
  79. if w.Code != http.StatusOK {
  80. t.Fatalf("expected status code 200, actual %d", w.Code)
  81. }
  82. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  83. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  84. })
  85. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  86. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  87. filepath.Join(p, "blobs", "sha256-ca239d7bd8ea90e4a5d2e6bf88f8d74a47b14336e73eb4e18bed4dd325018116"),
  88. })
  89. }
  90. func TestCreateFromModel(t *testing.T) {
  91. gin.SetMode(gin.TestMode)
  92. p := t.TempDir()
  93. t.Setenv("OLLAMA_MODELS", p)
  94. envconfig.LoadConfig()
  95. var s Server
  96. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  97. Name: "test",
  98. Modelfile: fmt.Sprintf("FROM %s", createBinFile(t, nil, nil)),
  99. Stream: &stream,
  100. })
  101. if w.Code != http.StatusOK {
  102. t.Fatalf("expected status code 200, actual %d", w.Code)
  103. }
  104. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  105. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  106. })
  107. w = createRequest(t, s.CreateModelHandler, api.CreateRequest{
  108. Name: "test2",
  109. Modelfile: "FROM test",
  110. Stream: &stream,
  111. })
  112. if w.Code != http.StatusOK {
  113. t.Fatalf("expected status code 200, actual %d", w.Code)
  114. }
  115. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  116. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  117. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test2", "latest"),
  118. })
  119. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  120. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  121. filepath.Join(p, "blobs", "sha256-ca239d7bd8ea90e4a5d2e6bf88f8d74a47b14336e73eb4e18bed4dd325018116"),
  122. })
  123. }
  124. func TestCreateRemovesLayers(t *testing.T) {
  125. gin.SetMode(gin.TestMode)
  126. p := t.TempDir()
  127. t.Setenv("OLLAMA_MODELS", p)
  128. envconfig.LoadConfig()
  129. var s Server
  130. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  131. Name: "test",
  132. Modelfile: fmt.Sprintf("FROM %s\nTEMPLATE {{ .Prompt }}", createBinFile(t, nil, nil)),
  133. Stream: &stream,
  134. })
  135. if w.Code != http.StatusOK {
  136. t.Fatalf("expected status code 200, actual %d", w.Code)
  137. }
  138. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  139. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  140. })
  141. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  142. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  143. filepath.Join(p, "blobs", "sha256-b507b9c2f6ca642bffcd06665ea7c91f235fd32daeefdf875a0f938db05fb315"),
  144. filepath.Join(p, "blobs", "sha256-bc80b03733773e0728011b2f4adf34c458b400e1aad48cb28d61170f3a2ad2d6"),
  145. })
  146. w = createRequest(t, s.CreateModelHandler, api.CreateRequest{
  147. Name: "test",
  148. Modelfile: fmt.Sprintf("FROM %s\nTEMPLATE {{ .System }} {{ .Prompt }}", createBinFile(t, nil, nil)),
  149. Stream: &stream,
  150. })
  151. if w.Code != http.StatusOK {
  152. t.Fatalf("expected status code 200, actual %d", w.Code)
  153. }
  154. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  155. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  156. })
  157. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  158. filepath.Join(p, "blobs", "sha256-8f2c2167d789c6b2302dff965160fa5029f6a24096d262c1cbb469f21a045382"),
  159. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  160. filepath.Join(p, "blobs", "sha256-fe7ac77b725cda2ccad03f88a880ecdfd7a33192d6cae08fce2c0ee1455991ed"),
  161. })
  162. }
  163. func TestCreateUnsetsSystem(t *testing.T) {
  164. gin.SetMode(gin.TestMode)
  165. p := t.TempDir()
  166. t.Setenv("OLLAMA_MODELS", p)
  167. envconfig.LoadConfig()
  168. var s Server
  169. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  170. Name: "test",
  171. Modelfile: fmt.Sprintf("FROM %s\nSYSTEM Say hi!", createBinFile(t, nil, nil)),
  172. Stream: &stream,
  173. })
  174. if w.Code != http.StatusOK {
  175. t.Fatalf("expected status code 200, actual %d", w.Code)
  176. }
  177. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  178. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  179. })
  180. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  181. filepath.Join(p, "blobs", "sha256-8585df945d1069bc78b79bd10bb73ba07fbc29b0f5479a31a601c0d12731416e"),
  182. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  183. filepath.Join(p, "blobs", "sha256-f29e82a8284dbdf5910b1555580ff60b04238b8da9d5e51159ada67a4d0d5851"),
  184. })
  185. w = createRequest(t, s.CreateModelHandler, api.CreateRequest{
  186. Name: "test",
  187. Modelfile: fmt.Sprintf("FROM %s\nSYSTEM \"\"", createBinFile(t, nil, nil)),
  188. Stream: &stream,
  189. })
  190. if w.Code != http.StatusOK {
  191. t.Fatalf("expected status code 200, actual %d", w.Code)
  192. }
  193. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  194. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  195. })
  196. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  197. filepath.Join(p, "blobs", "sha256-67d4b8d106af2a5b100a46e9bdc038c71eef2a35c9abac784092654212f97cf5"),
  198. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  199. filepath.Join(p, "blobs", "sha256-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
  200. })
  201. bts, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"))
  202. if err != nil {
  203. t.Fatal(err)
  204. }
  205. if string(bts) != "" {
  206. t.Fatalf("expected empty string, actual %s", string(bts))
  207. }
  208. }
  209. func TestCreateMergeParameters(t *testing.T) {
  210. gin.SetMode(gin.TestMode)
  211. p := t.TempDir()
  212. t.Setenv("OLLAMA_MODELS", p)
  213. envconfig.LoadConfig()
  214. var s Server
  215. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  216. Name: "test",
  217. Modelfile: fmt.Sprintf("FROM %s\nPARAMETER temperature 1\nPARAMETER top_k 10\nPARAMETER stop USER:\nPARAMETER stop ASSISTANT:", createBinFile(t, nil, nil)),
  218. Stream: &stream,
  219. })
  220. if w.Code != http.StatusOK {
  221. t.Fatalf("expected status code 200, actual %d", w.Code)
  222. }
  223. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  224. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  225. })
  226. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  227. filepath.Join(p, "blobs", "sha256-1d0ad71299d48c2fb7ae2b98e683643e771f8a5b72be34942af90d97a91c1e37"),
  228. filepath.Join(p, "blobs", "sha256-4a384beaf47a9cbe452dfa5ab70eea691790f3b35a832d12933a1996685bf2b6"),
  229. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  230. })
  231. // in order to merge parameters, the second model must be created FROM the first
  232. w = createRequest(t, s.CreateModelHandler, api.CreateRequest{
  233. Name: "test2",
  234. Modelfile: "FROM test\nPARAMETER temperature 0.6\nPARAMETER top_p 0.7",
  235. Stream: &stream,
  236. })
  237. if w.Code != http.StatusOK {
  238. t.Fatalf("expected status code 200, actual %d", w.Code)
  239. }
  240. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  241. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  242. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test2", "latest"),
  243. })
  244. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  245. filepath.Join(p, "blobs", "sha256-1d0ad71299d48c2fb7ae2b98e683643e771f8a5b72be34942af90d97a91c1e37"),
  246. filepath.Join(p, "blobs", "sha256-4a384beaf47a9cbe452dfa5ab70eea691790f3b35a832d12933a1996685bf2b6"),
  247. filepath.Join(p, "blobs", "sha256-4cd9d4ba6b734d9b4cbd1e5caa60374c00722e993fce5e1e2d15a33698f71187"),
  248. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  249. filepath.Join(p, "blobs", "sha256-e29a7b3c47287a2489c895d21fe413c20f859a85d20e749492f52a838e36e1ba"),
  250. })
  251. actual, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-e29a7b3c47287a2489c895d21fe413c20f859a85d20e749492f52a838e36e1ba"))
  252. if err != nil {
  253. t.Fatal(err)
  254. }
  255. expect, err := json.Marshal(map[string]any{"temperature": 0.6, "top_k": 10, "top_p": 0.7, "stop": []string{"USER:", "ASSISTANT:"}})
  256. if err != nil {
  257. t.Fatal(err)
  258. }
  259. if !bytes.Equal(bytes.TrimSpace(expect), bytes.TrimSpace(actual)) {
  260. t.Errorf("expected %s, actual %s", string(expect), string(actual))
  261. }
  262. // slices are replaced
  263. w = createRequest(t, s.CreateModelHandler, api.CreateRequest{
  264. Name: "test2",
  265. Modelfile: "FROM test\nPARAMETER temperature 0.6\nPARAMETER top_p 0.7\nPARAMETER stop <|endoftext|>",
  266. Stream: &stream,
  267. })
  268. if w.Code != http.StatusOK {
  269. t.Fatalf("expected status code 200, actual %d", w.Code)
  270. }
  271. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  272. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  273. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test2", "latest"),
  274. })
  275. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  276. filepath.Join(p, "blobs", "sha256-12f58bb75cb3042d69a7e013ab87fb3c3c7088f50ddc62f0c77bd332f0d44d35"),
  277. filepath.Join(p, "blobs", "sha256-1d0ad71299d48c2fb7ae2b98e683643e771f8a5b72be34942af90d97a91c1e37"),
  278. filepath.Join(p, "blobs", "sha256-257aa726584f24970a4f240765e75a7169bfbe7f4966c1f04513d6b6c860583a"),
  279. filepath.Join(p, "blobs", "sha256-4a384beaf47a9cbe452dfa5ab70eea691790f3b35a832d12933a1996685bf2b6"),
  280. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  281. })
  282. actual, err = os.ReadFile(filepath.Join(p, "blobs", "sha256-12f58bb75cb3042d69a7e013ab87fb3c3c7088f50ddc62f0c77bd332f0d44d35"))
  283. if err != nil {
  284. t.Fatal(err)
  285. }
  286. expect, err = json.Marshal(map[string]any{"temperature": 0.6, "top_k": 10, "top_p": 0.7, "stop": []string{"<|endoftext|>"}})
  287. if err != nil {
  288. t.Fatal(err)
  289. }
  290. if !bytes.Equal(bytes.TrimSpace(expect), bytes.TrimSpace(actual)) {
  291. t.Errorf("expected %s, actual %s", string(expect), string(actual))
  292. }
  293. }
  294. func TestCreateReplacesMessages(t *testing.T) {
  295. gin.SetMode(gin.TestMode)
  296. p := t.TempDir()
  297. t.Setenv("OLLAMA_MODELS", p)
  298. envconfig.LoadConfig()
  299. var s Server
  300. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  301. Name: "test",
  302. Modelfile: fmt.Sprintf("FROM %s\nMESSAGE assistant \"What is my purpose?\"\nMESSAGE user \"You run tests.\"\nMESSAGE assistant \"Oh, my god.\"", createBinFile(t, nil, nil)),
  303. Stream: &stream,
  304. })
  305. if w.Code != http.StatusOK {
  306. t.Fatalf("expected status code 200, actual %d", w.Code)
  307. }
  308. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  309. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  310. })
  311. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  312. filepath.Join(p, "blobs", "sha256-298baeaf6928a60cf666d88d64a1ba606feb43a2865687c39e40652e407bffc4"),
  313. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  314. filepath.Join(p, "blobs", "sha256-e0e27d47045063ccb167ae852c51d49a98eab33fabaee4633fdddf97213e40b5"),
  315. })
  316. w = createRequest(t, s.CreateModelHandler, api.CreateRequest{
  317. Name: "test2",
  318. Modelfile: "FROM test\nMESSAGE assistant \"You're a test, Harry.\"\nMESSAGE user \"I-I'm a what?\"\nMESSAGE assistant \"A test. And a thumping good one at that, I'd wager.\"",
  319. Stream: &stream,
  320. })
  321. if w.Code != http.StatusOK {
  322. t.Fatalf("expected status code 200, actual %d", w.Code)
  323. }
  324. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  325. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  326. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test2", "latest"),
  327. })
  328. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  329. filepath.Join(p, "blobs", "sha256-298baeaf6928a60cf666d88d64a1ba606feb43a2865687c39e40652e407bffc4"),
  330. filepath.Join(p, "blobs", "sha256-4f48b25fe9969564c82f58eb1cedbdff6484cc0baf474bc6c2a9b37c8da3362a"),
  331. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  332. filepath.Join(p, "blobs", "sha256-a60ecc9da299ec7ede453f99236e5577fd125e143689b646d9f0ddc9971bf4db"),
  333. filepath.Join(p, "blobs", "sha256-e0e27d47045063ccb167ae852c51d49a98eab33fabaee4633fdddf97213e40b5"),
  334. })
  335. type message struct {
  336. Role string `json:"role"`
  337. Content string `json:"content"`
  338. }
  339. f, err := os.Open(filepath.Join(p, "blobs", "sha256-a60ecc9da299ec7ede453f99236e5577fd125e143689b646d9f0ddc9971bf4db"))
  340. if err != nil {
  341. t.Fatal(err)
  342. }
  343. defer f.Close()
  344. var actual []message
  345. if err := json.NewDecoder(f).Decode(&actual); err != nil {
  346. t.Fatal(err)
  347. }
  348. expect := []message{
  349. {Role: "assistant", Content: "You're a test, Harry."},
  350. {Role: "user", Content: "I-I'm a what?"},
  351. {Role: "assistant", Content: "A test. And a thumping good one at that, I'd wager."},
  352. }
  353. if !slices.Equal(actual, expect) {
  354. t.Errorf("expected %s, actual %s", expect, actual)
  355. }
  356. }
  357. func TestCreateTemplateSystem(t *testing.T) {
  358. gin.SetMode(gin.TestMode)
  359. p := t.TempDir()
  360. t.Setenv("OLLAMA_MODELS", p)
  361. envconfig.LoadConfig()
  362. var s Server
  363. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  364. Name: "test",
  365. Modelfile: fmt.Sprintf("FROM %s\nTEMPLATE {{ .Prompt }}\nSYSTEM Say hello!\nTEMPLATE {{ .System }} {{ .Prompt }}\nSYSTEM Say bye!", createBinFile(t, nil, nil)),
  366. Stream: &stream,
  367. })
  368. if w.Code != http.StatusOK {
  369. t.Fatalf("expected status code 200, actual %d", w.Code)
  370. }
  371. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  372. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  373. })
  374. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  375. filepath.Join(p, "blobs", "sha256-2b5e330885117c82f3fd75169ea323e141070a2947c11ddb9f79ee0b01c589c1"),
  376. filepath.Join(p, "blobs", "sha256-4c5f51faac758fecaff8db42f0b7382891a4d0c0bb885f7b86be88c814a7cc86"),
  377. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  378. filepath.Join(p, "blobs", "sha256-fe7ac77b725cda2ccad03f88a880ecdfd7a33192d6cae08fce2c0ee1455991ed"),
  379. })
  380. template, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-fe7ac77b725cda2ccad03f88a880ecdfd7a33192d6cae08fce2c0ee1455991ed"))
  381. if err != nil {
  382. t.Fatal(err)
  383. }
  384. if string(template) != "{{ .System }} {{ .Prompt }}" {
  385. t.Errorf("expected \"{{ .System }} {{ .Prompt }}\", actual %s", template)
  386. }
  387. system, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-4c5f51faac758fecaff8db42f0b7382891a4d0c0bb885f7b86be88c814a7cc86"))
  388. if err != nil {
  389. t.Fatal(err)
  390. }
  391. if string(system) != "Say bye!" {
  392. t.Errorf("expected \"Say bye!\", actual %s", system)
  393. }
  394. t.Run("incomplete template", func(t *testing.T) {
  395. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  396. Name: "test",
  397. Modelfile: fmt.Sprintf("FROM %s\nTEMPLATE {{ .Prompt", createBinFile(t, nil, nil)),
  398. Stream: &stream,
  399. })
  400. if w.Code != http.StatusBadRequest {
  401. t.Fatalf("expected status code 400, actual %d", w.Code)
  402. }
  403. })
  404. t.Run("template with unclosed if", func(t *testing.T) {
  405. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  406. Name: "test",
  407. Modelfile: fmt.Sprintf("FROM %s\nTEMPLATE {{ if .Prompt }}", createBinFile(t, nil, nil)),
  408. Stream: &stream,
  409. })
  410. if w.Code != http.StatusBadRequest {
  411. t.Fatalf("expected status code 400, actual %d", w.Code)
  412. }
  413. })
  414. t.Run("template with undefined function", func(t *testing.T) {
  415. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  416. Name: "test",
  417. Modelfile: fmt.Sprintf("FROM %s\nTEMPLATE {{ Prompt }}", createBinFile(t, nil, nil)),
  418. Stream: &stream,
  419. })
  420. if w.Code != http.StatusBadRequest {
  421. t.Fatalf("expected status code 400, actual %d", w.Code)
  422. }
  423. })
  424. }
  425. func TestCreateLicenses(t *testing.T) {
  426. gin.SetMode(gin.TestMode)
  427. p := t.TempDir()
  428. t.Setenv("OLLAMA_MODELS", p)
  429. envconfig.LoadConfig()
  430. var s Server
  431. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  432. Name: "test",
  433. Modelfile: fmt.Sprintf("FROM %s\nLICENSE MIT\nLICENSE Apache-2.0", createBinFile(t, nil, nil)),
  434. Stream: &stream,
  435. })
  436. if w.Code != http.StatusOK {
  437. t.Fatalf("expected status code 200, actual %d", w.Code)
  438. }
  439. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  440. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  441. })
  442. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  443. filepath.Join(p, "blobs", "sha256-2af71558e438db0b73a20beab92dc278a94e1bbe974c00c1a33e3ab62d53a608"),
  444. filepath.Join(p, "blobs", "sha256-79a39c37536ddee29cbadd5d5e2dcba8ed7f03e431f626ff38432c1c866bb7e2"),
  445. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  446. filepath.Join(p, "blobs", "sha256-e5dcffe836b6ec8a58e492419b550e65fb8cbdc308503979e5dacb33ac7ea3b7"),
  447. })
  448. mit, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-e5dcffe836b6ec8a58e492419b550e65fb8cbdc308503979e5dacb33ac7ea3b7"))
  449. if err != nil {
  450. t.Fatal(err)
  451. }
  452. if string(mit) != "MIT" {
  453. t.Errorf("expected MIT, actual %s", mit)
  454. }
  455. apache, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-2af71558e438db0b73a20beab92dc278a94e1bbe974c00c1a33e3ab62d53a608"))
  456. if err != nil {
  457. t.Fatal(err)
  458. }
  459. if string(apache) != "Apache-2.0" {
  460. t.Errorf("expected Apache-2.0, actual %s", apache)
  461. }
  462. }
  463. func TestCreateDetectTemplate(t *testing.T) {
  464. gin.SetMode(gin.TestMode)
  465. p := t.TempDir()
  466. t.Setenv("OLLAMA_MODELS", p)
  467. envconfig.LoadConfig()
  468. var s Server
  469. t.Run("matched", func(t *testing.T) {
  470. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  471. Name: "test",
  472. Modelfile: fmt.Sprintf("FROM %s", createBinFile(t, llm.KV{
  473. "tokenizer.chat_template": "{{ bos_token }}{% for message in messages %}{{'<|' + message['role'] + '|>' + '\n' + message['content'] + '<|end|>\n' }}{% endfor %}{% if add_generation_prompt %}{{ '<|assistant|>\n' }}{% else %}{{ eos_token }}{% endif %}",
  474. }, nil)),
  475. Stream: &stream,
  476. })
  477. if w.Code != http.StatusOK {
  478. t.Fatalf("expected status code 200, actual %d", w.Code)
  479. }
  480. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  481. filepath.Join(p, "blobs", "sha256-553c4a3f747b3d22a4946875f1cc8ed011c2930d83f864a0c7265f9ec0a20413"),
  482. filepath.Join(p, "blobs", "sha256-c608dc615584cd20d9d830363dabf8a4783ae5d34245c3d8c115edb3bc7b28e4"),
  483. filepath.Join(p, "blobs", "sha256-f836ee110db21567f826332e4cedd746c06d10664fd5a9ea3659e3683a944510"),
  484. })
  485. })
  486. t.Run("unmatched", func(t *testing.T) {
  487. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  488. Name: "test",
  489. Modelfile: fmt.Sprintf("FROM %s", createBinFile(t, nil, nil)),
  490. Stream: &stream,
  491. })
  492. if w.Code != http.StatusOK {
  493. t.Fatalf("expected status code 200, actual %d", w.Code)
  494. }
  495. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  496. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  497. filepath.Join(p, "blobs", "sha256-ca239d7bd8ea90e4a5d2e6bf88f8d74a47b14336e73eb4e18bed4dd325018116"),
  498. })
  499. })
  500. }