routes_create_test.go 19 KB

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