routes_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package server
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "sort"
  12. "strings"
  13. "testing"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/ollama/ollama/api"
  16. "github.com/ollama/ollama/llm"
  17. "github.com/ollama/ollama/parser"
  18. "github.com/ollama/ollama/version"
  19. )
  20. func Test_Routes(t *testing.T) {
  21. type testCase struct {
  22. Name string
  23. Method string
  24. Path string
  25. Setup func(t *testing.T, req *http.Request)
  26. Expected func(t *testing.T, resp *http.Response)
  27. }
  28. createTestFile := func(t *testing.T, name string) string {
  29. f, err := os.CreateTemp(t.TempDir(), name)
  30. assert.Nil(t, err)
  31. defer f.Close()
  32. _, err = f.Write([]byte("GGUF"))
  33. assert.Nil(t, err)
  34. _, err = f.Write([]byte{0x2, 0})
  35. assert.Nil(t, err)
  36. return f.Name()
  37. }
  38. createTestModel := func(t *testing.T, name string) {
  39. fname := createTestFile(t, "ollama-model")
  40. modelfile := strings.NewReader(fmt.Sprintf("FROM %s\nPARAMETER seed 42\nPARAMETER top_p 0.9\nPARAMETER stop foo\nPARAMETER stop bar", fname))
  41. commands, err := parser.Parse(modelfile)
  42. assert.Nil(t, err)
  43. fn := func(resp api.ProgressResponse) {
  44. t.Logf("Status: %s", resp.Status)
  45. }
  46. err = CreateModel(context.TODO(), name, "", commands, fn)
  47. assert.Nil(t, err)
  48. }
  49. testCases := []testCase{
  50. {
  51. Name: "Version Handler",
  52. Method: http.MethodGet,
  53. Path: "/api/version",
  54. Setup: func(t *testing.T, req *http.Request) {
  55. },
  56. Expected: func(t *testing.T, resp *http.Response) {
  57. contentType := resp.Header.Get("Content-Type")
  58. assert.Equal(t, contentType, "application/json; charset=utf-8")
  59. body, err := io.ReadAll(resp.Body)
  60. assert.Nil(t, err)
  61. assert.Equal(t, fmt.Sprintf(`{"version":"%s"}`, version.Version), string(body))
  62. },
  63. },
  64. {
  65. Name: "Tags Handler (no tags)",
  66. Method: http.MethodGet,
  67. Path: "/api/tags",
  68. Expected: func(t *testing.T, resp *http.Response) {
  69. contentType := resp.Header.Get("Content-Type")
  70. assert.Equal(t, contentType, "application/json; charset=utf-8")
  71. body, err := io.ReadAll(resp.Body)
  72. assert.Nil(t, err)
  73. var modelList api.ListResponse
  74. err = json.Unmarshal(body, &modelList)
  75. assert.Nil(t, err)
  76. assert.Equal(t, 0, len(modelList.Models))
  77. },
  78. },
  79. {
  80. Name: "Tags Handler (yes tags)",
  81. Method: http.MethodGet,
  82. Path: "/api/tags",
  83. Setup: func(t *testing.T, req *http.Request) {
  84. createTestModel(t, "test-model")
  85. },
  86. Expected: func(t *testing.T, resp *http.Response) {
  87. contentType := resp.Header.Get("Content-Type")
  88. assert.Equal(t, contentType, "application/json; charset=utf-8")
  89. body, err := io.ReadAll(resp.Body)
  90. assert.Nil(t, err)
  91. var modelList api.ListResponse
  92. err = json.Unmarshal(body, &modelList)
  93. assert.Nil(t, err)
  94. assert.Equal(t, 1, len(modelList.Models))
  95. assert.Equal(t, modelList.Models[0].Name, "test-model:latest")
  96. },
  97. },
  98. {
  99. Name: "Create Model Handler",
  100. Method: http.MethodPost,
  101. Path: "/api/create",
  102. Setup: func(t *testing.T, req *http.Request) {
  103. f, err := os.CreateTemp(t.TempDir(), "ollama-model")
  104. assert.Nil(t, err)
  105. defer f.Close()
  106. stream := false
  107. createReq := api.CreateRequest{
  108. Name: "t-bone",
  109. Modelfile: fmt.Sprintf("FROM %s", f.Name()),
  110. Stream: &stream,
  111. }
  112. jsonData, err := json.Marshal(createReq)
  113. assert.Nil(t, err)
  114. req.Body = io.NopCloser(bytes.NewReader(jsonData))
  115. },
  116. Expected: func(t *testing.T, resp *http.Response) {
  117. contentType := resp.Header.Get("Content-Type")
  118. assert.Equal(t, "application/json", contentType)
  119. _, err := io.ReadAll(resp.Body)
  120. assert.Nil(t, err)
  121. assert.Equal(t, resp.StatusCode, 200)
  122. model, err := GetModel("t-bone")
  123. assert.Nil(t, err)
  124. assert.Equal(t, "t-bone:latest", model.ShortName)
  125. },
  126. },
  127. {
  128. Name: "Copy Model Handler",
  129. Method: http.MethodPost,
  130. Path: "/api/copy",
  131. Setup: func(t *testing.T, req *http.Request) {
  132. createTestModel(t, "hamshank")
  133. copyReq := api.CopyRequest{
  134. Source: "hamshank",
  135. Destination: "beefsteak",
  136. }
  137. jsonData, err := json.Marshal(copyReq)
  138. assert.Nil(t, err)
  139. req.Body = io.NopCloser(bytes.NewReader(jsonData))
  140. },
  141. Expected: func(t *testing.T, resp *http.Response) {
  142. model, err := GetModel("beefsteak")
  143. assert.Nil(t, err)
  144. assert.Equal(t, "beefsteak:latest", model.ShortName)
  145. },
  146. },
  147. {
  148. Name: "Show Model Handler",
  149. Method: http.MethodPost,
  150. Path: "/api/show",
  151. Setup: func(t *testing.T, req *http.Request) {
  152. createTestModel(t, "show-model")
  153. showReq := api.ShowRequest{Model: "show-model"}
  154. jsonData, err := json.Marshal(showReq)
  155. assert.Nil(t, err)
  156. req.Body = io.NopCloser(bytes.NewReader(jsonData))
  157. },
  158. Expected: func(t *testing.T, resp *http.Response) {
  159. contentType := resp.Header.Get("Content-Type")
  160. assert.Equal(t, contentType, "application/json; charset=utf-8")
  161. body, err := io.ReadAll(resp.Body)
  162. assert.Nil(t, err)
  163. var showResp api.ShowResponse
  164. err = json.Unmarshal(body, &showResp)
  165. assert.Nil(t, err)
  166. var params []string
  167. paramsSplit := strings.Split(showResp.Parameters, "\n")
  168. for _, p := range paramsSplit {
  169. params = append(params, strings.Join(strings.Fields(p), " "))
  170. }
  171. sort.Strings(params)
  172. expectedParams := []string{
  173. "seed 42",
  174. "stop \"bar\"",
  175. "stop \"foo\"",
  176. "top_p 0.9",
  177. }
  178. assert.Equal(t, expectedParams, params)
  179. },
  180. },
  181. }
  182. s := Server{}
  183. router := s.GenerateRoutes()
  184. httpSrv := httptest.NewServer(router)
  185. t.Cleanup(httpSrv.Close)
  186. workDir, err := os.MkdirTemp("", "ollama-test")
  187. assert.Nil(t, err)
  188. defer os.RemoveAll(workDir)
  189. os.Setenv("OLLAMA_MODELS", workDir)
  190. for _, tc := range testCases {
  191. t.Logf("Running Test: [%s]", tc.Name)
  192. u := httpSrv.URL + tc.Path
  193. req, err := http.NewRequestWithContext(context.TODO(), tc.Method, u, nil)
  194. assert.Nil(t, err)
  195. if tc.Setup != nil {
  196. tc.Setup(t, req)
  197. }
  198. resp, err := httpSrv.Client().Do(req)
  199. assert.Nil(t, err)
  200. defer resp.Body.Close()
  201. if tc.Expected != nil {
  202. tc.Expected(t, resp)
  203. }
  204. }
  205. }
  206. type MockLLM struct {
  207. encoding []int
  208. }
  209. func (llm *MockLLM) Predict(ctx context.Context, pred llm.PredictOpts, fn func(llm.PredictResult)) error {
  210. return nil
  211. }
  212. func (llm *MockLLM) Encode(ctx context.Context, prompt string) ([]int, error) {
  213. return llm.encoding, nil
  214. }
  215. func (llm *MockLLM) Decode(ctx context.Context, tokens []int) (string, error) {
  216. return "", nil
  217. }
  218. func (llm *MockLLM) Embedding(ctx context.Context, input string) ([]float64, error) {
  219. return []float64{}, nil
  220. }
  221. func (llm *MockLLM) Close() {
  222. // do nothing
  223. }