routes_create_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. package server
  2. import (
  3. "bytes"
  4. "cmp"
  5. "crypto/sha256"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "net/http/httptest"
  11. "os"
  12. "path/filepath"
  13. "slices"
  14. "strings"
  15. "testing"
  16. "github.com/gin-gonic/gin"
  17. "github.com/ollama/ollama/api"
  18. "github.com/ollama/ollama/envconfig"
  19. "github.com/ollama/ollama/fs/ggml"
  20. )
  21. var stream bool = false
  22. func createBinFile(t *testing.T, kv map[string]any, ti []ggml.Tensor) (string, string) {
  23. t.Helper()
  24. t.Setenv("OLLAMA_MODELS", cmp.Or(os.Getenv("OLLAMA_MODELS"), t.TempDir()))
  25. modelDir := envconfig.Models()
  26. f, err := os.CreateTemp(t.TempDir(), "")
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. defer f.Close()
  31. if err := ggml.WriteGGUF(f, kv, ti); err != nil {
  32. t.Fatal(err)
  33. }
  34. // Calculate sha256 of file
  35. if _, err := f.Seek(0, 0); err != nil {
  36. t.Fatal(err)
  37. }
  38. digest, _ := GetSHA256Digest(f)
  39. if err := f.Close(); err != nil {
  40. t.Fatal(err)
  41. }
  42. if err := createLink(f.Name(), filepath.Join(modelDir, "blobs", fmt.Sprintf("sha256-%s", strings.TrimPrefix(digest, "sha256:")))); err != nil {
  43. t.Fatal(err)
  44. }
  45. return f.Name(), digest
  46. }
  47. type responseRecorder struct {
  48. *httptest.ResponseRecorder
  49. http.CloseNotifier
  50. }
  51. func NewRecorder() *responseRecorder {
  52. return &responseRecorder{
  53. ResponseRecorder: httptest.NewRecorder(),
  54. }
  55. }
  56. func (t *responseRecorder) CloseNotify() <-chan bool {
  57. return make(chan bool)
  58. }
  59. func createRequest(t *testing.T, fn func(*gin.Context), body any) *httptest.ResponseRecorder {
  60. t.Helper()
  61. // if OLLAMA_MODELS is not set, set it to the temp directory
  62. t.Setenv("OLLAMA_MODELS", cmp.Or(os.Getenv("OLLAMA_MODELS"), t.TempDir()))
  63. w := NewRecorder()
  64. c, _ := gin.CreateTestContext(w)
  65. var b bytes.Buffer
  66. if err := json.NewEncoder(&b).Encode(body); err != nil {
  67. t.Fatal(err)
  68. }
  69. c.Request = &http.Request{
  70. Body: io.NopCloser(&b),
  71. }
  72. fn(c)
  73. return w.ResponseRecorder
  74. }
  75. func checkFileExists(t *testing.T, p string, expect []string) {
  76. t.Helper()
  77. actual, err := filepath.Glob(p)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. if !slices.Equal(actual, expect) {
  82. t.Fatalf("expected slices to be equal %v", actual)
  83. }
  84. }
  85. func TestCreateFromBin(t *testing.T) {
  86. gin.SetMode(gin.TestMode)
  87. p := t.TempDir()
  88. t.Setenv("OLLAMA_MODELS", p)
  89. var s Server
  90. _, digest := createBinFile(t, nil, nil)
  91. w := createRequest(t, s.CreateHandler, api.CreateRequest{
  92. Name: "test",
  93. Files: map[string]string{"test.gguf": digest},
  94. Stream: &stream,
  95. })
  96. if w.Code != http.StatusOK {
  97. fmt.Println(w)
  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. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  104. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  105. filepath.Join(p, "blobs", "sha256-ca239d7bd8ea90e4a5d2e6bf88f8d74a47b14336e73eb4e18bed4dd325018116"),
  106. })
  107. }
  108. func TestCreateFromModel(t *testing.T) {
  109. gin.SetMode(gin.TestMode)
  110. p := t.TempDir()
  111. t.Setenv("OLLAMA_MODELS", p)
  112. var s Server
  113. _, digest := createBinFile(t, nil, nil)
  114. w := createRequest(t, s.CreateHandler, api.CreateRequest{
  115. Name: "test",
  116. Files: map[string]string{"test.gguf": digest},
  117. Stream: &stream,
  118. })
  119. if w.Code != http.StatusOK {
  120. t.Fatalf("expected status code 200, actual %d", w.Code)
  121. }
  122. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  123. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  124. })
  125. w = createRequest(t, s.CreateHandler, api.CreateRequest{
  126. Name: "test2",
  127. From: "test",
  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. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test2", "latest"),
  136. })
  137. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  138. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  139. filepath.Join(p, "blobs", "sha256-ca239d7bd8ea90e4a5d2e6bf88f8d74a47b14336e73eb4e18bed4dd325018116"),
  140. })
  141. }
  142. func TestCreateRemovesLayers(t *testing.T) {
  143. gin.SetMode(gin.TestMode)
  144. p := t.TempDir()
  145. t.Setenv("OLLAMA_MODELS", p)
  146. var s Server
  147. _, digest := createBinFile(t, nil, nil)
  148. w := createRequest(t, s.CreateHandler, api.CreateRequest{
  149. Name: "test",
  150. Files: map[string]string{"test.gguf": digest},
  151. Template: "{{ .Prompt }}",
  152. Stream: &stream,
  153. })
  154. if w.Code != http.StatusOK {
  155. t.Fatalf("expected status code 200, actual %d", w.Code)
  156. }
  157. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  158. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  159. })
  160. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  161. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  162. filepath.Join(p, "blobs", "sha256-b507b9c2f6ca642bffcd06665ea7c91f235fd32daeefdf875a0f938db05fb315"),
  163. filepath.Join(p, "blobs", "sha256-bc80b03733773e0728011b2f4adf34c458b400e1aad48cb28d61170f3a2ad2d6"),
  164. })
  165. w = createRequest(t, s.CreateHandler, api.CreateRequest{
  166. Name: "test",
  167. Files: map[string]string{"test.gguf": digest},
  168. Template: "{{ .System }} {{ .Prompt }}",
  169. Stream: &stream,
  170. })
  171. if w.Code != http.StatusOK {
  172. t.Fatalf("expected status code 200, actual %d", w.Code)
  173. }
  174. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  175. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  176. })
  177. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  178. filepath.Join(p, "blobs", "sha256-8f2c2167d789c6b2302dff965160fa5029f6a24096d262c1cbb469f21a045382"),
  179. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  180. filepath.Join(p, "blobs", "sha256-fe7ac77b725cda2ccad03f88a880ecdfd7a33192d6cae08fce2c0ee1455991ed"),
  181. })
  182. }
  183. func TestCreateUnsetsSystem(t *testing.T) {
  184. gin.SetMode(gin.TestMode)
  185. p := t.TempDir()
  186. t.Setenv("OLLAMA_MODELS", p)
  187. var s Server
  188. _, digest := createBinFile(t, nil, nil)
  189. w := createRequest(t, s.CreateHandler, api.CreateRequest{
  190. Name: "test",
  191. Files: map[string]string{"test.gguf": digest},
  192. System: "Say hi!",
  193. Stream: &stream,
  194. })
  195. if w.Code != http.StatusOK {
  196. t.Fatalf("expected status code 200, actual %d", w.Code)
  197. }
  198. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  199. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  200. })
  201. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  202. filepath.Join(p, "blobs", "sha256-8585df945d1069bc78b79bd10bb73ba07fbc29b0f5479a31a601c0d12731416e"),
  203. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  204. filepath.Join(p, "blobs", "sha256-f29e82a8284dbdf5910b1555580ff60b04238b8da9d5e51159ada67a4d0d5851"),
  205. })
  206. w = createRequest(t, s.CreateHandler, api.CreateRequest{
  207. Name: "test",
  208. Files: map[string]string{"test.gguf": digest},
  209. System: "",
  210. Stream: &stream,
  211. })
  212. if w.Code != http.StatusOK {
  213. t.Fatalf("expected status code 200, actual %d", w.Code)
  214. }
  215. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  216. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  217. })
  218. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  219. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  220. filepath.Join(p, "blobs", "sha256-ca239d7bd8ea90e4a5d2e6bf88f8d74a47b14336e73eb4e18bed4dd325018116"),
  221. })
  222. }
  223. func TestCreateMergeParameters(t *testing.T) {
  224. gin.SetMode(gin.TestMode)
  225. p := t.TempDir()
  226. t.Setenv("OLLAMA_MODELS", p)
  227. var s Server
  228. _, digest := createBinFile(t, nil, nil)
  229. w := createRequest(t, s.CreateHandler, api.CreateRequest{
  230. Name: "test",
  231. Files: map[string]string{"test.gguf": digest},
  232. Parameters: map[string]any{
  233. "temperature": 1,
  234. "top_k": 10,
  235. "stop": []string{"USER:", "ASSISTANT:"},
  236. },
  237. Stream: &stream,
  238. })
  239. if w.Code != http.StatusOK {
  240. t.Fatalf("expected status code 200, actual %d", w.Code)
  241. }
  242. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  243. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  244. })
  245. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  246. filepath.Join(p, "blobs", "sha256-1d0ad71299d48c2fb7ae2b98e683643e771f8a5b72be34942af90d97a91c1e37"),
  247. filepath.Join(p, "blobs", "sha256-4a384beaf47a9cbe452dfa5ab70eea691790f3b35a832d12933a1996685bf2b6"),
  248. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  249. })
  250. // in order to merge parameters, the second model must be created FROM the first
  251. w = createRequest(t, s.CreateHandler, api.CreateRequest{
  252. Name: "test2",
  253. From: "test",
  254. Parameters: map[string]any{
  255. "temperature": 0.6,
  256. "top_p": 0.7,
  257. },
  258. Stream: &stream,
  259. })
  260. if w.Code != http.StatusOK {
  261. t.Fatalf("expected status code 200, actual %d", w.Code)
  262. }
  263. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  264. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  265. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test2", "latest"),
  266. })
  267. // Display contents of each blob in the directory
  268. blobDir := filepath.Join(p, "blobs")
  269. entries, err := os.ReadDir(blobDir)
  270. if err != nil {
  271. t.Fatalf("failed to read blobs directory: %v", err)
  272. }
  273. for _, entry := range entries {
  274. blobPath := filepath.Join(blobDir, entry.Name())
  275. content, err := os.ReadFile(blobPath)
  276. if err != nil {
  277. t.Fatalf("failed to read blob %s: %v", entry.Name(), err)
  278. }
  279. t.Logf("Contents of %s:\n%s", entry.Name(), string(content))
  280. }
  281. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  282. filepath.Join(p, "blobs", "sha256-1d0ad71299d48c2fb7ae2b98e683643e771f8a5b72be34942af90d97a91c1e37"),
  283. filepath.Join(p, "blobs", "sha256-4a384beaf47a9cbe452dfa5ab70eea691790f3b35a832d12933a1996685bf2b6"),
  284. filepath.Join(p, "blobs", "sha256-4cd9d4ba6b734d9b4cbd1e5caa60374c00722e993fce5e1e2d15a33698f71187"),
  285. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  286. filepath.Join(p, "blobs", "sha256-e29a7b3c47287a2489c895d21fe413c20f859a85d20e749492f52a838e36e1ba"),
  287. })
  288. actual, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-e29a7b3c47287a2489c895d21fe413c20f859a85d20e749492f52a838e36e1ba"))
  289. if err != nil {
  290. t.Fatal(err)
  291. }
  292. expect, err := json.Marshal(map[string]any{"temperature": 0.6, "top_k": 10, "top_p": 0.7, "stop": []string{"USER:", "ASSISTANT:"}})
  293. if err != nil {
  294. t.Fatal(err)
  295. }
  296. if !bytes.Equal(bytes.TrimSpace(expect), bytes.TrimSpace(actual)) {
  297. t.Errorf("expected %s, actual %s", string(expect), string(actual))
  298. }
  299. // slices are replaced
  300. w = createRequest(t, s.CreateHandler, api.CreateRequest{
  301. Name: "test2",
  302. From: "test",
  303. Parameters: map[string]any{
  304. "temperature": 0.6,
  305. "top_p": 0.7,
  306. "stop": []string{"<|endoftext|>"},
  307. },
  308. Stream: &stream,
  309. })
  310. if w.Code != http.StatusOK {
  311. t.Fatalf("expected status code 200, actual %d", w.Code)
  312. }
  313. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  314. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  315. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test2", "latest"),
  316. })
  317. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  318. filepath.Join(p, "blobs", "sha256-12f58bb75cb3042d69a7e013ab87fb3c3c7088f50ddc62f0c77bd332f0d44d35"),
  319. filepath.Join(p, "blobs", "sha256-1d0ad71299d48c2fb7ae2b98e683643e771f8a5b72be34942af90d97a91c1e37"),
  320. filepath.Join(p, "blobs", "sha256-257aa726584f24970a4f240765e75a7169bfbe7f4966c1f04513d6b6c860583a"),
  321. filepath.Join(p, "blobs", "sha256-4a384beaf47a9cbe452dfa5ab70eea691790f3b35a832d12933a1996685bf2b6"),
  322. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  323. })
  324. actual, err = os.ReadFile(filepath.Join(p, "blobs", "sha256-12f58bb75cb3042d69a7e013ab87fb3c3c7088f50ddc62f0c77bd332f0d44d35"))
  325. if err != nil {
  326. t.Fatal(err)
  327. }
  328. expect, err = json.Marshal(map[string]any{"temperature": 0.6, "top_k": 10, "top_p": 0.7, "stop": []string{"<|endoftext|>"}})
  329. if err != nil {
  330. t.Fatal(err)
  331. }
  332. if !bytes.Equal(bytes.TrimSpace(expect), bytes.TrimSpace(actual)) {
  333. t.Errorf("expected %s, actual %s", string(expect), string(actual))
  334. }
  335. }
  336. func TestCreateReplacesMessages(t *testing.T) {
  337. gin.SetMode(gin.TestMode)
  338. p := t.TempDir()
  339. t.Setenv("OLLAMA_MODELS", p)
  340. var s Server
  341. _, digest := createBinFile(t, nil, nil)
  342. w := createRequest(t, s.CreateHandler, api.CreateRequest{
  343. Name: "test",
  344. Files: map[string]string{"test.gguf": digest},
  345. Messages: []api.Message{
  346. {
  347. Role: "assistant",
  348. Content: "What is my purpose?",
  349. },
  350. {
  351. Role: "user",
  352. Content: "You run tests.",
  353. },
  354. {
  355. Role: "assistant",
  356. Content: "Oh, my god.",
  357. },
  358. },
  359. Stream: &stream,
  360. })
  361. if w.Code != http.StatusOK {
  362. t.Fatalf("expected status code 200, actual %d", w.Code)
  363. }
  364. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  365. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  366. })
  367. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  368. filepath.Join(p, "blobs", "sha256-298baeaf6928a60cf666d88d64a1ba606feb43a2865687c39e40652e407bffc4"),
  369. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  370. filepath.Join(p, "blobs", "sha256-e0e27d47045063ccb167ae852c51d49a98eab33fabaee4633fdddf97213e40b5"),
  371. })
  372. w = createRequest(t, s.CreateHandler, api.CreateRequest{
  373. Name: "test2",
  374. From: "test",
  375. Messages: []api.Message{
  376. {
  377. Role: "assistant",
  378. Content: "You're a test, Harry.",
  379. },
  380. {
  381. Role: "user",
  382. Content: "I-I'm a what?",
  383. },
  384. {
  385. Role: "assistant",
  386. Content: "A test. And a thumping good one at that, I'd wager.",
  387. },
  388. },
  389. Stream: &stream,
  390. })
  391. if w.Code != http.StatusOK {
  392. t.Fatalf("expected status code 200, actual %d", w.Code)
  393. }
  394. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  395. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  396. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test2", "latest"),
  397. })
  398. // Old layers will not have been pruned
  399. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  400. filepath.Join(p, "blobs", "sha256-298baeaf6928a60cf666d88d64a1ba606feb43a2865687c39e40652e407bffc4"),
  401. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  402. filepath.Join(p, "blobs", "sha256-a60ecc9da299ec7ede453f99236e5577fd125e143689b646d9f0ddc9971bf4db"),
  403. filepath.Join(p, "blobs", "sha256-e0e27d47045063ccb167ae852c51d49a98eab33fabaee4633fdddf97213e40b5"),
  404. filepath.Join(p, "blobs", "sha256-f4e2c3690efef1b4b63ba1e1b2744ffeb6a7438a0110b86596069f6d9999c80b"),
  405. })
  406. type message struct {
  407. Role string `json:"role"`
  408. Content string `json:"content"`
  409. }
  410. f, err := os.Open(filepath.Join(p, "blobs", "sha256-a60ecc9da299ec7ede453f99236e5577fd125e143689b646d9f0ddc9971bf4db"))
  411. if err != nil {
  412. t.Fatal(err)
  413. }
  414. defer f.Close()
  415. var actual []message
  416. if err := json.NewDecoder(f).Decode(&actual); err != nil {
  417. t.Fatal(err)
  418. }
  419. expect := []message{
  420. {Role: "assistant", Content: "You're a test, Harry."},
  421. {Role: "user", Content: "I-I'm a what?"},
  422. {Role: "assistant", Content: "A test. And a thumping good one at that, I'd wager."},
  423. }
  424. if !slices.Equal(actual, expect) {
  425. t.Errorf("expected %s, actual %s", expect, actual)
  426. }
  427. }
  428. func TestCreateTemplateSystem(t *testing.T) {
  429. gin.SetMode(gin.TestMode)
  430. p := t.TempDir()
  431. t.Setenv("OLLAMA_MODELS", p)
  432. var s Server
  433. _, digest := createBinFile(t, nil, nil)
  434. w := createRequest(t, s.CreateHandler, api.CreateRequest{
  435. Name: "test",
  436. Files: map[string]string{"test.gguf": digest},
  437. Template: "{{ .System }} {{ .Prompt }}",
  438. System: "Say bye!",
  439. Stream: &stream,
  440. })
  441. if w.Code != http.StatusOK {
  442. t.Fatalf("expected status code 200, actual %d", w.Code)
  443. }
  444. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  445. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  446. })
  447. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  448. filepath.Join(p, "blobs", "sha256-2b5e330885117c82f3fd75169ea323e141070a2947c11ddb9f79ee0b01c589c1"),
  449. filepath.Join(p, "blobs", "sha256-4c5f51faac758fecaff8db42f0b7382891a4d0c0bb885f7b86be88c814a7cc86"),
  450. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  451. filepath.Join(p, "blobs", "sha256-fe7ac77b725cda2ccad03f88a880ecdfd7a33192d6cae08fce2c0ee1455991ed"),
  452. })
  453. template, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-fe7ac77b725cda2ccad03f88a880ecdfd7a33192d6cae08fce2c0ee1455991ed"))
  454. if err != nil {
  455. t.Fatal(err)
  456. }
  457. if string(template) != "{{ .System }} {{ .Prompt }}" {
  458. t.Errorf("expected \"{{ .System }} {{ .Prompt }}\", actual %s", template)
  459. }
  460. system, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-4c5f51faac758fecaff8db42f0b7382891a4d0c0bb885f7b86be88c814a7cc86"))
  461. if err != nil {
  462. t.Fatal(err)
  463. }
  464. if string(system) != "Say bye!" {
  465. t.Errorf("expected \"Say bye!\", actual %s", system)
  466. }
  467. t.Run("incomplete template", func(t *testing.T) {
  468. _, digest := createBinFile(t, nil, nil)
  469. w := createRequest(t, s.CreateHandler, api.CreateRequest{
  470. Name: "test",
  471. Files: map[string]string{"test.gguf": digest},
  472. Template: "{{ .Prompt",
  473. Stream: &stream,
  474. })
  475. if w.Code != http.StatusBadRequest {
  476. t.Fatalf("expected status code 400, actual %d", w.Code)
  477. }
  478. })
  479. t.Run("template with unclosed if", func(t *testing.T) {
  480. _, digest := createBinFile(t, nil, nil)
  481. w := createRequest(t, s.CreateHandler, api.CreateRequest{
  482. Name: "test",
  483. Files: map[string]string{"test.gguf": digest},
  484. Template: "{{ if .Prompt }}",
  485. Stream: &stream,
  486. })
  487. if w.Code != http.StatusBadRequest {
  488. t.Fatalf("expected status code 400, actual %d", w.Code)
  489. }
  490. })
  491. t.Run("template with undefined function", func(t *testing.T) {
  492. _, digest := createBinFile(t, nil, nil)
  493. w := createRequest(t, s.CreateHandler, api.CreateRequest{
  494. Name: "test",
  495. Files: map[string]string{"test.gguf": digest},
  496. Template: "{{ Prompt }}",
  497. Stream: &stream,
  498. })
  499. if w.Code != http.StatusBadRequest {
  500. t.Fatalf("expected status code 400, actual %d", w.Code)
  501. }
  502. })
  503. }
  504. func TestCreateLicenses(t *testing.T) {
  505. gin.SetMode(gin.TestMode)
  506. p := t.TempDir()
  507. t.Setenv("OLLAMA_MODELS", p)
  508. var s Server
  509. _, digest := createBinFile(t, nil, nil)
  510. w := createRequest(t, s.CreateHandler, api.CreateRequest{
  511. Name: "test",
  512. Files: map[string]string{"test.gguf": digest},
  513. License: []string{"MIT", "Apache-2.0"},
  514. Stream: &stream,
  515. })
  516. if w.Code != http.StatusOK {
  517. t.Fatalf("expected status code 200, actual %d", w.Code)
  518. }
  519. checkFileExists(t, filepath.Join(p, "manifests", "*", "*", "*", "*"), []string{
  520. filepath.Join(p, "manifests", "registry.ollama.ai", "library", "test", "latest"),
  521. })
  522. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  523. filepath.Join(p, "blobs", "sha256-2af71558e438db0b73a20beab92dc278a94e1bbe974c00c1a33e3ab62d53a608"),
  524. filepath.Join(p, "blobs", "sha256-79a39c37536ddee29cbadd5d5e2dcba8ed7f03e431f626ff38432c1c866bb7e2"),
  525. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  526. filepath.Join(p, "blobs", "sha256-e5dcffe836b6ec8a58e492419b550e65fb8cbdc308503979e5dacb33ac7ea3b7"),
  527. })
  528. mit, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-e5dcffe836b6ec8a58e492419b550e65fb8cbdc308503979e5dacb33ac7ea3b7"))
  529. if err != nil {
  530. t.Fatal(err)
  531. }
  532. if string(mit) != "MIT" {
  533. t.Errorf("expected MIT, actual %s", mit)
  534. }
  535. apache, err := os.ReadFile(filepath.Join(p, "blobs", "sha256-2af71558e438db0b73a20beab92dc278a94e1bbe974c00c1a33e3ab62d53a608"))
  536. if err != nil {
  537. t.Fatal(err)
  538. }
  539. if string(apache) != "Apache-2.0" {
  540. t.Errorf("expected Apache-2.0, actual %s", apache)
  541. }
  542. }
  543. func TestCreateDetectTemplate(t *testing.T) {
  544. gin.SetMode(gin.TestMode)
  545. p := t.TempDir()
  546. t.Setenv("OLLAMA_MODELS", p)
  547. var s Server
  548. t.Run("matched", func(t *testing.T) {
  549. _, digest := createBinFile(t, ggml.KV{
  550. "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 %}",
  551. }, nil)
  552. w := createRequest(t, s.CreateHandler, api.CreateRequest{
  553. Name: "test",
  554. Files: map[string]string{"test.gguf": digest},
  555. Stream: &stream,
  556. })
  557. if w.Code != http.StatusOK {
  558. t.Fatalf("expected status code 200, actual %d", w.Code)
  559. }
  560. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  561. filepath.Join(p, "blobs", "sha256-0d79f567714c62c048378f2107fb332dabee0135d080c302d884317da9433cc5"),
  562. filepath.Join(p, "blobs", "sha256-35360843d0c84fb1506952a131bbef13cd2bb4a541251f22535170c05b56e672"),
  563. filepath.Join(p, "blobs", "sha256-553c4a3f747b3d22a4946875f1cc8ed011c2930d83f864a0c7265f9ec0a20413"),
  564. filepath.Join(p, "blobs", "sha256-de3959f841e9ef6b4b6255fa41cb9e0a45da89c3066aa72bdd07a4747f848990"),
  565. })
  566. })
  567. t.Run("unmatched", func(t *testing.T) {
  568. _, digest := createBinFile(t, nil, nil)
  569. w := createRequest(t, s.CreateHandler, api.CreateRequest{
  570. Name: "test",
  571. Files: map[string]string{"test.gguf": digest},
  572. Stream: &stream,
  573. })
  574. if w.Code != http.StatusOK {
  575. t.Fatalf("expected status code 200, actual %d", w.Code)
  576. }
  577. checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{
  578. filepath.Join(p, "blobs", "sha256-a4e5e156ddec27e286f75328784d7106b60a4eb1d246e950a001a3f944fbda99"),
  579. filepath.Join(p, "blobs", "sha256-ca239d7bd8ea90e4a5d2e6bf88f8d74a47b14336e73eb4e18bed4dd325018116"),
  580. })
  581. })
  582. }
  583. func TestDetectModelTypeFromFiles(t *testing.T) {
  584. t.Run("gguf file", func(t *testing.T) {
  585. _, digest := createBinFile(t, nil, nil)
  586. files := map[string]string{
  587. "model.gguf": digest,
  588. }
  589. modelType := detectModelTypeFromFiles(files)
  590. if modelType != "gguf" {
  591. t.Fatalf("expected model type 'gguf', got %q", modelType)
  592. }
  593. })
  594. t.Run("gguf file w/o extension", func(t *testing.T) {
  595. _, digest := createBinFile(t, nil, nil)
  596. files := map[string]string{
  597. fmt.Sprintf("%x", digest): digest,
  598. }
  599. modelType := detectModelTypeFromFiles(files)
  600. if modelType != "gguf" {
  601. t.Fatalf("expected model type 'gguf', got %q", modelType)
  602. }
  603. })
  604. t.Run("safetensors file", func(t *testing.T) {
  605. files := map[string]string{
  606. "model.safetensors": "sha256:abc123",
  607. }
  608. modelType := detectModelTypeFromFiles(files)
  609. if modelType != "safetensors" {
  610. t.Fatalf("expected model type 'safetensors', got %q", modelType)
  611. }
  612. })
  613. t.Run("unsupported file type", func(t *testing.T) {
  614. p := t.TempDir()
  615. t.Setenv("OLLAMA_MODELS", p)
  616. data := []byte("12345678")
  617. digest := fmt.Sprintf("sha256:%x", sha256.Sum256(data))
  618. if err := os.MkdirAll(filepath.Join(p, "blobs"), 0o755); err != nil {
  619. t.Fatal(err)
  620. }
  621. f, err := os.Create(filepath.Join(p, "blobs", fmt.Sprintf("sha256-%s", strings.TrimPrefix(digest, "sha256:"))))
  622. if err != nil {
  623. t.Fatal(err)
  624. }
  625. defer f.Close()
  626. if _, err := f.Write(data); err != nil {
  627. t.Fatal(err)
  628. }
  629. files := map[string]string{
  630. "model.bin": digest,
  631. }
  632. modelType := detectModelTypeFromFiles(files)
  633. if modelType != "" {
  634. t.Fatalf("expected empty model type for unsupported file, got %q", modelType)
  635. }
  636. })
  637. t.Run("file with less than 4 bytes", func(t *testing.T) {
  638. p := t.TempDir()
  639. t.Setenv("OLLAMA_MODELS", p)
  640. data := []byte("123")
  641. digest := fmt.Sprintf("sha256:%x", sha256.Sum256(data))
  642. if err := os.MkdirAll(filepath.Join(p, "blobs"), 0o755); err != nil {
  643. t.Fatal(err)
  644. }
  645. f, err := os.Create(filepath.Join(p, "blobs", fmt.Sprintf("sha256-%s", strings.TrimPrefix(digest, "sha256:"))))
  646. if err != nil {
  647. t.Fatal(err)
  648. }
  649. defer f.Close()
  650. if _, err := f.Write(data); err != nil {
  651. t.Fatal(err)
  652. }
  653. files := map[string]string{
  654. "noext": digest,
  655. }
  656. modelType := detectModelTypeFromFiles(files)
  657. if modelType != "" {
  658. t.Fatalf("expected empty model type for small file, got %q", modelType)
  659. }
  660. })
  661. }