prompt_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package server
  2. import (
  3. "bytes"
  4. "context"
  5. "testing"
  6. "github.com/google/go-cmp/cmp"
  7. "github.com/ollama/ollama/api"
  8. "github.com/ollama/ollama/template"
  9. )
  10. func TestChatPrompt(t *testing.T) {
  11. type expect struct {
  12. prompt string
  13. images [][]byte
  14. }
  15. cases := []struct {
  16. name string
  17. limit int
  18. msgs []api.Message
  19. expect
  20. }{
  21. {
  22. name: "messages",
  23. limit: 64,
  24. msgs: []api.Message{
  25. {Role: "user", Content: "You're a test, Harry!"},
  26. {Role: "assistant", Content: "I-I'm a what?"},
  27. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
  28. },
  29. expect: expect{
  30. prompt: "You're a test, Harry! I-I'm a what? A test. And a thumping good one at that, I'd wager. ",
  31. },
  32. },
  33. {
  34. name: "truncate messages",
  35. limit: 1,
  36. msgs: []api.Message{
  37. {Role: "user", Content: "You're a test, Harry!"},
  38. {Role: "assistant", Content: "I-I'm a what?"},
  39. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
  40. },
  41. expect: expect{
  42. prompt: "A test. And a thumping good one at that, I'd wager. ",
  43. },
  44. },
  45. {
  46. name: "truncate messages with image",
  47. limit: 64,
  48. msgs: []api.Message{
  49. {Role: "user", Content: "You're a test, Harry!"},
  50. {Role: "assistant", Content: "I-I'm a what?"},
  51. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("something")}},
  52. },
  53. expect: expect{
  54. prompt: "[img-0] A test. And a thumping good one at that, I'd wager. ",
  55. images: [][]byte{
  56. []byte("something"),
  57. },
  58. },
  59. },
  60. {
  61. name: "truncate messages with images",
  62. limit: 64,
  63. msgs: []api.Message{
  64. {Role: "user", Content: "You're a test, Harry!", Images: []api.ImageData{[]byte("something")}},
  65. {Role: "assistant", Content: "I-I'm a what?"},
  66. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}},
  67. },
  68. expect: expect{
  69. prompt: "[img-0] A test. And a thumping good one at that, I'd wager. ",
  70. images: [][]byte{
  71. []byte("somethingelse"),
  72. },
  73. },
  74. },
  75. {
  76. name: "messages with images",
  77. limit: 2048,
  78. msgs: []api.Message{
  79. {Role: "user", Content: "You're a test, Harry!", Images: []api.ImageData{[]byte("something")}},
  80. {Role: "assistant", Content: "I-I'm a what?"},
  81. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}},
  82. },
  83. expect: expect{
  84. prompt: "[img-0] You're a test, Harry! I-I'm a what? [img-1] A test. And a thumping good one at that, I'd wager. ",
  85. images: [][]byte{
  86. []byte("something"),
  87. []byte("somethingelse"),
  88. },
  89. },
  90. },
  91. {
  92. name: "message with image tag",
  93. limit: 2048,
  94. msgs: []api.Message{
  95. {Role: "user", Content: "You're a test, Harry! [img]", Images: []api.ImageData{[]byte("something")}},
  96. {Role: "assistant", Content: "I-I'm a what?"},
  97. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}},
  98. },
  99. expect: expect{
  100. prompt: "You're a test, Harry! [img-0] I-I'm a what? [img-1] A test. And a thumping good one at that, I'd wager. ",
  101. images: [][]byte{
  102. []byte("something"),
  103. []byte("somethingelse"),
  104. },
  105. },
  106. },
  107. {
  108. name: "messages with interleaved images",
  109. limit: 2048,
  110. msgs: []api.Message{
  111. {Role: "user", Content: "You're a test, Harry!"},
  112. {Role: "user", Images: []api.ImageData{[]byte("something")}},
  113. {Role: "user", Images: []api.ImageData{[]byte("somethingelse")}},
  114. {Role: "assistant", Content: "I-I'm a what?"},
  115. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
  116. },
  117. expect: expect{
  118. prompt: "You're a test, Harry!\n\n[img-0]\n\n[img-1] I-I'm a what? A test. And a thumping good one at that, I'd wager. ",
  119. images: [][]byte{
  120. []byte("something"),
  121. []byte("somethingelse"),
  122. },
  123. },
  124. },
  125. {
  126. name: "truncate message with interleaved images",
  127. limit: 1024,
  128. msgs: []api.Message{
  129. {Role: "user", Content: "You're a test, Harry!"},
  130. {Role: "user", Images: []api.ImageData{[]byte("something")}},
  131. {Role: "user", Images: []api.ImageData{[]byte("somethingelse")}},
  132. {Role: "assistant", Content: "I-I'm a what?"},
  133. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
  134. },
  135. expect: expect{
  136. prompt: "[img-0] I-I'm a what? A test. And a thumping good one at that, I'd wager. ",
  137. images: [][]byte{
  138. []byte("somethingelse"),
  139. },
  140. },
  141. },
  142. {
  143. name: "message with system prompt",
  144. limit: 2048,
  145. msgs: []api.Message{
  146. {Role: "system", Content: "You are the Test Who Lived."},
  147. {Role: "user", Content: "You're a test, Harry!"},
  148. {Role: "assistant", Content: "I-I'm a what?"},
  149. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
  150. },
  151. expect: expect{
  152. prompt: "You are the Test Who Lived. You're a test, Harry! I-I'm a what? A test. And a thumping good one at that, I'd wager. ",
  153. },
  154. },
  155. {
  156. name: "out of order system",
  157. limit: 2048,
  158. msgs: []api.Message{
  159. {Role: "user", Content: "You're a test, Harry!"},
  160. {Role: "assistant", Content: "I-I'm a what?"},
  161. {Role: "system", Content: "You are the Test Who Lived."},
  162. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
  163. },
  164. expect: expect{
  165. prompt: "You're a test, Harry! I-I'm a what? You are the Test Who Lived. A test. And a thumping good one at that, I'd wager. ",
  166. },
  167. },
  168. }
  169. tmpl, err := template.Parse(`
  170. {{- if .System }}{{ .System }} {{ end }}
  171. {{- if .Prompt }}{{ .Prompt }} {{ end }}
  172. {{- if .Response }}{{ .Response }} {{ end }}`)
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. for _, tt := range cases {
  177. t.Run(tt.name, func(t *testing.T) {
  178. model := Model{Template: tmpl, ProjectorPaths: []string{"vision"}}
  179. opts := api.Options{Runner: api.Runner{NumCtx: tt.limit}}
  180. prompt, images, err := chatPrompt(context.TODO(), &model, mockRunner{}.Tokenize, &opts, tt.msgs, nil)
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. if diff := cmp.Diff(prompt, tt.prompt); diff != "" {
  185. t.Errorf("mismatch (-got +want):\n%s", diff)
  186. }
  187. if len(images) != len(tt.images) {
  188. t.Fatalf("expected %d images, got %d", len(tt.images), len(images))
  189. }
  190. for i := range images {
  191. if images[i].ID != i {
  192. t.Errorf("expected ID %d, got %d", i, images[i].ID)
  193. }
  194. if !bytes.Equal(images[i].Data, tt.images[i]) {
  195. t.Errorf("expected %q, got %q", tt.images[i], images[i])
  196. }
  197. }
  198. })
  199. }
  200. }