routes_test.go 6.7 KB

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