routes_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package server
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/binary"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "net/http/httptest"
  11. "os"
  12. "sort"
  13. "strings"
  14. "testing"
  15. "github.com/stretchr/testify/assert"
  16. "github.com/stretchr/testify/require"
  17. "github.com/ollama/ollama/api"
  18. "github.com/ollama/ollama/envconfig"
  19. "github.com/ollama/ollama/llm"
  20. "github.com/ollama/ollama/openai"
  21. "github.com/ollama/ollama/parser"
  22. "github.com/ollama/ollama/types/model"
  23. "github.com/ollama/ollama/version"
  24. )
  25. func createTestFile(t *testing.T, name string) string {
  26. t.Helper()
  27. f, err := os.CreateTemp(t.TempDir(), name)
  28. require.NoError(t, err)
  29. defer f.Close()
  30. err = binary.Write(f, binary.LittleEndian, []byte("GGUF"))
  31. require.NoError(t, err)
  32. err = binary.Write(f, binary.LittleEndian, uint32(3))
  33. require.NoError(t, err)
  34. err = binary.Write(f, binary.LittleEndian, uint64(0))
  35. require.NoError(t, err)
  36. err = binary.Write(f, binary.LittleEndian, uint64(0))
  37. require.NoError(t, err)
  38. return f.Name()
  39. }
  40. func Test_Routes(t *testing.T) {
  41. type testCase struct {
  42. Name string
  43. Method string
  44. Path string
  45. Setup func(t *testing.T, req *http.Request)
  46. Expected func(t *testing.T, resp *http.Response)
  47. }
  48. createTestModel := func(t *testing.T, name string) {
  49. t.Helper()
  50. fname := createTestFile(t, "ollama-model")
  51. r := strings.NewReader(fmt.Sprintf("FROM %s\nPARAMETER seed 42\nPARAMETER top_p 0.9\nPARAMETER stop foo\nPARAMETER stop bar", fname))
  52. modelfile, err := parser.ParseFile(r)
  53. require.NoError(t, err)
  54. fn := func(resp api.ProgressResponse) {
  55. t.Logf("Status: %s", resp.Status)
  56. }
  57. err = CreateModel(context.TODO(), model.ParseName(name), "", "", modelfile, fn)
  58. require.NoError(t, err)
  59. }
  60. testCases := []testCase{
  61. {
  62. Name: "Version Handler",
  63. Method: http.MethodGet,
  64. Path: "/api/version",
  65. Setup: func(t *testing.T, req *http.Request) {
  66. },
  67. Expected: func(t *testing.T, resp *http.Response) {
  68. contentType := resp.Header.Get("Content-Type")
  69. assert.Equal(t, "application/json; charset=utf-8", contentType)
  70. body, err := io.ReadAll(resp.Body)
  71. require.NoError(t, err)
  72. assert.Equal(t, fmt.Sprintf(`{"version":"%s"}`, version.Version), string(body))
  73. },
  74. },
  75. {
  76. Name: "Tags Handler (no tags)",
  77. Method: http.MethodGet,
  78. Path: "/api/tags",
  79. Expected: func(t *testing.T, resp *http.Response) {
  80. contentType := resp.Header.Get("Content-Type")
  81. assert.Equal(t, "application/json; charset=utf-8", contentType)
  82. body, err := io.ReadAll(resp.Body)
  83. require.NoError(t, err)
  84. var modelList api.ListResponse
  85. err = json.Unmarshal(body, &modelList)
  86. require.NoError(t, err)
  87. assert.NotNil(t, modelList.Models)
  88. assert.Empty(t, len(modelList.Models))
  89. },
  90. },
  91. {
  92. Name: "openai empty list",
  93. Method: http.MethodGet,
  94. Path: "/v1/models",
  95. Expected: func(t *testing.T, resp *http.Response) {
  96. contentType := resp.Header.Get("Content-Type")
  97. assert.Equal(t, "application/json", contentType)
  98. body, err := io.ReadAll(resp.Body)
  99. require.NoError(t, err)
  100. var modelList openai.ListCompletion
  101. err = json.Unmarshal(body, &modelList)
  102. require.NoError(t, err)
  103. assert.Equal(t, "list", modelList.Object)
  104. assert.Empty(t, modelList.Data)
  105. },
  106. },
  107. {
  108. Name: "Tags Handler (yes tags)",
  109. Method: http.MethodGet,
  110. Path: "/api/tags",
  111. Setup: func(t *testing.T, req *http.Request) {
  112. createTestModel(t, "test-model")
  113. },
  114. Expected: func(t *testing.T, resp *http.Response) {
  115. contentType := resp.Header.Get("Content-Type")
  116. assert.Equal(t, "application/json; charset=utf-8", contentType)
  117. body, err := io.ReadAll(resp.Body)
  118. require.NoError(t, err)
  119. assert.NotContains(t, string(body), "expires_at")
  120. var modelList api.ListResponse
  121. err = json.Unmarshal(body, &modelList)
  122. require.NoError(t, err)
  123. assert.Len(t, modelList.Models, 1)
  124. assert.Equal(t, "test-model:latest", modelList.Models[0].Name)
  125. },
  126. },
  127. {
  128. Name: "openai list models with tags",
  129. Method: http.MethodGet,
  130. Path: "/v1/models",
  131. Expected: func(t *testing.T, resp *http.Response) {
  132. contentType := resp.Header.Get("Content-Type")
  133. assert.Equal(t, "application/json", contentType)
  134. body, err := io.ReadAll(resp.Body)
  135. require.NoError(t, err)
  136. var modelList openai.ListCompletion
  137. err = json.Unmarshal(body, &modelList)
  138. require.NoError(t, err)
  139. assert.Len(t, modelList.Data, 1)
  140. assert.Equal(t, "test-model:latest", modelList.Data[0].Id)
  141. assert.Equal(t, "library", modelList.Data[0].OwnedBy)
  142. },
  143. },
  144. {
  145. Name: "Create Model Handler",
  146. Method: http.MethodPost,
  147. Path: "/api/create",
  148. Setup: func(t *testing.T, req *http.Request) {
  149. fname := createTestFile(t, "ollama-model")
  150. stream := false
  151. createReq := api.CreateRequest{
  152. Name: "t-bone",
  153. Modelfile: fmt.Sprintf("FROM %s", fname),
  154. Stream: &stream,
  155. }
  156. jsonData, err := json.Marshal(createReq)
  157. require.NoError(t, err)
  158. req.Body = io.NopCloser(bytes.NewReader(jsonData))
  159. },
  160. Expected: func(t *testing.T, resp *http.Response) {
  161. contentType := resp.Header.Get("Content-Type")
  162. assert.Equal(t, "application/json", contentType)
  163. _, err := io.ReadAll(resp.Body)
  164. require.NoError(t, err)
  165. assert.Equal(t, 200, resp.StatusCode)
  166. model, err := GetModel("t-bone")
  167. require.NoError(t, err)
  168. assert.Equal(t, "t-bone:latest", model.ShortName)
  169. },
  170. },
  171. {
  172. Name: "Copy Model Handler",
  173. Method: http.MethodPost,
  174. Path: "/api/copy",
  175. Setup: func(t *testing.T, req *http.Request) {
  176. createTestModel(t, "hamshank")
  177. copyReq := api.CopyRequest{
  178. Source: "hamshank",
  179. Destination: "beefsteak",
  180. }
  181. jsonData, err := json.Marshal(copyReq)
  182. require.NoError(t, err)
  183. req.Body = io.NopCloser(bytes.NewReader(jsonData))
  184. },
  185. Expected: func(t *testing.T, resp *http.Response) {
  186. model, err := GetModel("beefsteak")
  187. require.NoError(t, err)
  188. assert.Equal(t, "beefsteak:latest", model.ShortName)
  189. },
  190. },
  191. {
  192. Name: "Show Model Handler",
  193. Method: http.MethodPost,
  194. Path: "/api/show",
  195. Setup: func(t *testing.T, req *http.Request) {
  196. createTestModel(t, "show-model")
  197. showReq := api.ShowRequest{Model: "show-model"}
  198. jsonData, err := json.Marshal(showReq)
  199. require.NoError(t, err)
  200. req.Body = io.NopCloser(bytes.NewReader(jsonData))
  201. },
  202. Expected: func(t *testing.T, resp *http.Response) {
  203. contentType := resp.Header.Get("Content-Type")
  204. assert.Equal(t, "application/json; charset=utf-8", contentType)
  205. body, err := io.ReadAll(resp.Body)
  206. require.NoError(t, err)
  207. var showResp api.ShowResponse
  208. err = json.Unmarshal(body, &showResp)
  209. require.NoError(t, err)
  210. var params []string
  211. paramsSplit := strings.Split(showResp.Parameters, "\n")
  212. for _, p := range paramsSplit {
  213. params = append(params, strings.Join(strings.Fields(p), " "))
  214. }
  215. sort.Strings(params)
  216. expectedParams := []string{
  217. "seed 42",
  218. "stop \"bar\"",
  219. "stop \"foo\"",
  220. "top_p 0.9",
  221. }
  222. assert.Equal(t, expectedParams, params)
  223. assert.InDelta(t, 0, showResp.ModelInfo["general.parameter_count"], 1e-9, "Parameter count should be 0")
  224. },
  225. },
  226. {
  227. Name: "openai retrieve model handler",
  228. Method: http.MethodGet,
  229. Path: "/v1/models/show-model",
  230. Expected: func(t *testing.T, resp *http.Response) {
  231. contentType := resp.Header.Get("Content-Type")
  232. assert.Equal(t, "application/json", contentType)
  233. body, err := io.ReadAll(resp.Body)
  234. require.NoError(t, err)
  235. var retrieveResp api.RetrieveModelResponse
  236. err = json.Unmarshal(body, &retrieveResp)
  237. require.NoError(t, err)
  238. assert.Equal(t, "show-model", retrieveResp.Id)
  239. assert.Equal(t, "library", retrieveResp.OwnedBy)
  240. },
  241. },
  242. }
  243. t.Setenv("OLLAMA_MODELS", t.TempDir())
  244. envconfig.LoadConfig()
  245. s := &Server{}
  246. router := s.GenerateRoutes()
  247. httpSrv := httptest.NewServer(router)
  248. t.Cleanup(httpSrv.Close)
  249. for _, tc := range testCases {
  250. t.Run(tc.Name, func(t *testing.T) {
  251. u := httpSrv.URL + tc.Path
  252. req, err := http.NewRequestWithContext(context.TODO(), tc.Method, u, nil)
  253. require.NoError(t, err)
  254. if tc.Setup != nil {
  255. tc.Setup(t, req)
  256. }
  257. resp, err := httpSrv.Client().Do(req)
  258. require.NoError(t, err)
  259. defer resp.Body.Close()
  260. if tc.Expected != nil {
  261. tc.Expected(t, resp)
  262. }
  263. })
  264. }
  265. }
  266. func TestCase(t *testing.T) {
  267. t.Setenv("OLLAMA_MODELS", t.TempDir())
  268. envconfig.LoadConfig()
  269. cases := []string{
  270. "mistral",
  271. "llama3:latest",
  272. "library/phi3:q4_0",
  273. "registry.ollama.ai/library/gemma:q5_K_M",
  274. // TODO: host:port currently fails on windows (#4107)
  275. // "localhost:5000/alice/bob:latest",
  276. }
  277. var s Server
  278. for _, tt := range cases {
  279. t.Run(tt, func(t *testing.T) {
  280. w := createRequest(t, s.CreateModelHandler, api.CreateRequest{
  281. Name: tt,
  282. Modelfile: fmt.Sprintf("FROM %s", createBinFile(t, nil, nil)),
  283. Stream: &stream,
  284. })
  285. if w.Code != http.StatusOK {
  286. t.Fatalf("expected status 200 got %d", w.Code)
  287. }
  288. expect, err := json.Marshal(map[string]string{"error": "a model with that name already exists"})
  289. if err != nil {
  290. t.Fatal(err)
  291. }
  292. t.Run("create", func(t *testing.T) {
  293. w = createRequest(t, s.CreateModelHandler, api.CreateRequest{
  294. Name: strings.ToUpper(tt),
  295. Modelfile: fmt.Sprintf("FROM %s", createBinFile(t, nil, nil)),
  296. Stream: &stream,
  297. })
  298. if w.Code != http.StatusBadRequest {
  299. t.Fatalf("expected status 500 got %d", w.Code)
  300. }
  301. if !bytes.Equal(w.Body.Bytes(), expect) {
  302. t.Fatalf("expected error %s got %s", expect, w.Body.String())
  303. }
  304. })
  305. t.Run("pull", func(t *testing.T) {
  306. w := createRequest(t, s.PullModelHandler, api.PullRequest{
  307. Name: strings.ToUpper(tt),
  308. Stream: &stream,
  309. })
  310. if w.Code != http.StatusBadRequest {
  311. t.Fatalf("expected status 500 got %d", w.Code)
  312. }
  313. if !bytes.Equal(w.Body.Bytes(), expect) {
  314. t.Fatalf("expected error %s got %s", expect, w.Body.String())
  315. }
  316. })
  317. t.Run("copy", func(t *testing.T) {
  318. w := createRequest(t, s.CopyModelHandler, api.CopyRequest{
  319. Source: tt,
  320. Destination: strings.ToUpper(tt),
  321. })
  322. if w.Code != http.StatusBadRequest {
  323. t.Fatalf("expected status 500 got %d", w.Code)
  324. }
  325. if !bytes.Equal(w.Body.Bytes(), expect) {
  326. t.Fatalf("expected error %s got %s", expect, w.Body.String())
  327. }
  328. })
  329. })
  330. }
  331. }
  332. func TestShow(t *testing.T) {
  333. t.Setenv("OLLAMA_MODELS", t.TempDir())
  334. envconfig.LoadConfig()
  335. var s Server
  336. createRequest(t, s.CreateModelHandler, api.CreateRequest{
  337. Name: "show-model",
  338. Modelfile: fmt.Sprintf(
  339. "FROM %s\nFROM %s",
  340. createBinFile(t, llm.KV{"general.architecture": "test"}, nil),
  341. createBinFile(t, llm.KV{"general.architecture": "clip"}, nil),
  342. ),
  343. })
  344. w := createRequest(t, s.ShowModelHandler, api.ShowRequest{
  345. Name: "show-model",
  346. })
  347. if w.Code != http.StatusOK {
  348. t.Fatalf("expected status code 200, actual %d", w.Code)
  349. }
  350. var resp api.ShowResponse
  351. if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
  352. t.Fatal(err)
  353. }
  354. if resp.ModelInfo["general.architecture"] != "test" {
  355. t.Fatal("Expected model architecture to be 'test', but got", resp.ModelInfo["general.architecture"])
  356. }
  357. if resp.ProjectorInfo["general.architecture"] != "clip" {
  358. t.Fatal("Expected projector architecture to be 'clip', but got", resp.ProjectorInfo["general.architecture"])
  359. }
  360. }