routes_test.go 6.8 KB

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