routes_create_test.go 19 KB

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