routes_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. func Test_ChatPrompt(t *testing.T) {
  212. tests := []struct {
  213. name string
  214. template string
  215. chat *ChatHistory
  216. numCtx int
  217. runner MockLLM
  218. want string
  219. wantErr string
  220. }{
  221. {
  222. name: "Single Message",
  223. template: "[INST] {{ .System }} {{ .Prompt }} [/INST]",
  224. chat: &ChatHistory{
  225. Prompts: []PromptVars{
  226. {
  227. System: "You are a Wizard.",
  228. Prompt: "What are the potion ingredients?",
  229. First: true,
  230. },
  231. },
  232. LastSystem: "You are a Wizard.",
  233. },
  234. numCtx: 1,
  235. runner: MockLLM{
  236. encoding: []int{1}, // fit the ctxLen
  237. },
  238. want: "[INST] You are a Wizard. What are the potion ingredients? [/INST]",
  239. },
  240. {
  241. name: "First Message",
  242. template: "[INST] {{if .First}}Hello!{{end}} {{ .System }} {{ .Prompt }} [/INST]",
  243. chat: &ChatHistory{
  244. Prompts: []PromptVars{
  245. {
  246. System: "You are a Wizard.",
  247. Prompt: "What are the potion ingredients?",
  248. Response: "eye of newt",
  249. First: true,
  250. },
  251. {
  252. Prompt: "Anything else?",
  253. },
  254. },
  255. LastSystem: "You are a Wizard.",
  256. },
  257. numCtx: 2,
  258. runner: MockLLM{
  259. encoding: []int{1}, // fit the ctxLen
  260. },
  261. want: "[INST] Hello! You are a Wizard. What are the potion ingredients? [/INST]eye of newt[INST] Anything else? [/INST]",
  262. },
  263. {
  264. name: "Message History",
  265. template: "[INST] {{ .System }} {{ .Prompt }} [/INST]",
  266. chat: &ChatHistory{
  267. Prompts: []PromptVars{
  268. {
  269. System: "You are a Wizard.",
  270. Prompt: "What are the potion ingredients?",
  271. Response: "sugar",
  272. First: true,
  273. },
  274. {
  275. Prompt: "Anything else?",
  276. },
  277. },
  278. LastSystem: "You are a Wizard.",
  279. },
  280. numCtx: 4,
  281. runner: MockLLM{
  282. encoding: []int{1}, // fit the ctxLen, 1 for each message
  283. },
  284. want: "[INST] You are a Wizard. What are the potion ingredients? [/INST]sugar[INST] Anything else? [/INST]",
  285. },
  286. {
  287. name: "Assistant Only",
  288. template: "[INST] {{ .System }} {{ .Prompt }} [/INST]",
  289. chat: &ChatHistory{
  290. Prompts: []PromptVars{
  291. {
  292. Response: "everything nice",
  293. First: true,
  294. },
  295. },
  296. },
  297. numCtx: 1,
  298. runner: MockLLM{
  299. encoding: []int{1},
  300. },
  301. want: "[INST] [/INST]everything nice",
  302. },
  303. {
  304. name: "Message History Truncated, No System",
  305. template: "[INST] {{ .System }} {{ .Prompt }} [/INST]",
  306. chat: &ChatHistory{
  307. Prompts: []PromptVars{
  308. {
  309. Prompt: "What are the potion ingredients?",
  310. Response: "sugar",
  311. First: true,
  312. },
  313. {
  314. Prompt: "Anything else?",
  315. Response: "spice",
  316. },
  317. {
  318. Prompt: "... and?",
  319. },
  320. },
  321. },
  322. numCtx: 2, // only 1 message from history and most recent message
  323. runner: MockLLM{
  324. encoding: []int{1},
  325. },
  326. want: "[INST] Anything else? [/INST]spice[INST] ... and? [/INST]",
  327. },
  328. {
  329. name: "System is Preserved when Truncated",
  330. template: "[INST] {{ .System }} {{ .Prompt }} [/INST]",
  331. chat: &ChatHistory{
  332. Prompts: []PromptVars{
  333. {
  334. Prompt: "What are the magic words?",
  335. Response: "abracadabra",
  336. },
  337. {
  338. Prompt: "What is the spell for invisibility?",
  339. },
  340. },
  341. LastSystem: "You are a wizard.",
  342. },
  343. numCtx: 2,
  344. runner: MockLLM{
  345. encoding: []int{1},
  346. },
  347. want: "[INST] You are a wizard. What is the spell for invisibility? [/INST]",
  348. },
  349. {
  350. name: "System is Preserved when Length Exceeded",
  351. template: "[INST] {{ .System }} {{ .Prompt }} [/INST]",
  352. chat: &ChatHistory{
  353. Prompts: []PromptVars{
  354. {
  355. Prompt: "What are the magic words?",
  356. Response: "abracadabra",
  357. },
  358. {
  359. Prompt: "What is the spell for invisibility?",
  360. },
  361. },
  362. LastSystem: "You are a wizard.",
  363. },
  364. numCtx: 1,
  365. runner: MockLLM{
  366. encoding: []int{1},
  367. },
  368. want: "[INST] You are a wizard. What is the spell for invisibility? [/INST]",
  369. },
  370. {
  371. name: "First is Preserved when Truncated",
  372. template: "[INST] {{ if .First }}{{ .System }} {{ end }}{{ .Prompt }} [/INST]",
  373. chat: &ChatHistory{
  374. Prompts: []PromptVars{
  375. // first message omitted for test
  376. {
  377. Prompt: "Do you have a magic hat?",
  378. Response: "Of course.",
  379. },
  380. {
  381. Prompt: "What is the spell for invisibility?",
  382. },
  383. },
  384. LastSystem: "You are a wizard.",
  385. },
  386. numCtx: 3, // two most recent messages and room for system message
  387. runner: MockLLM{
  388. encoding: []int{1},
  389. },
  390. want: "[INST] You are a wizard. Do you have a magic hat? [/INST]Of course.[INST] What is the spell for invisibility? [/INST]",
  391. },
  392. {
  393. name: "Most recent message is returned when longer than ctxLen",
  394. template: "[INST] {{ .Prompt }} [/INST]",
  395. chat: &ChatHistory{
  396. Prompts: []PromptVars{
  397. {
  398. Prompt: "What is the spell for invisibility?",
  399. First: true,
  400. },
  401. },
  402. },
  403. numCtx: 1, // two most recent messages
  404. runner: MockLLM{
  405. encoding: []int{1, 2},
  406. },
  407. want: "[INST] What is the spell for invisibility? [/INST]",
  408. },
  409. }
  410. for _, testCase := range tests {
  411. tt := testCase
  412. m := &Model{
  413. Template: tt.template,
  414. }
  415. t.Run(tt.name, func(t *testing.T) {
  416. loaded.runner = &tt.runner
  417. loaded.Options = &api.Options{
  418. Runner: api.Runner{
  419. NumCtx: tt.numCtx,
  420. },
  421. }
  422. // TODO: add tests for trimming images
  423. got, _, err := trimmedPrompt(context.Background(), tt.chat, m)
  424. if tt.wantErr != "" {
  425. if err == nil {
  426. t.Errorf("ChatPrompt() expected error, got nil")
  427. }
  428. if !strings.Contains(err.Error(), tt.wantErr) {
  429. t.Errorf("ChatPrompt() error = %v, wantErr %v", err, tt.wantErr)
  430. }
  431. }
  432. if got != tt.want {
  433. t.Errorf("ChatPrompt() got = %v, want %v", got, tt.want)
  434. }
  435. })
  436. }
  437. }
  438. type MockLLM struct {
  439. encoding []int
  440. }
  441. func (llm *MockLLM) Predict(ctx context.Context, pred llm.PredictOpts, fn func(llm.PredictResult)) error {
  442. return nil
  443. }
  444. func (llm *MockLLM) Encode(ctx context.Context, prompt string) ([]int, error) {
  445. return llm.encoding, nil
  446. }
  447. func (llm *MockLLM) Decode(ctx context.Context, tokens []int) (string, error) {
  448. return "", nil
  449. }
  450. func (llm *MockLLM) Embedding(ctx context.Context, input string) ([]float64, error) {
  451. return []float64{}, nil
  452. }
  453. func (llm *MockLLM) Close() {
  454. // do nothing
  455. }