routes_create_test.go 21 KB

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