prompt_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package server
  2. import (
  3. "bytes"
  4. "context"
  5. "image"
  6. "image/png"
  7. "testing"
  8. "github.com/google/go-cmp/cmp"
  9. "github.com/ollama/ollama/api"
  10. "github.com/ollama/ollama/template"
  11. )
  12. func TestChatPrompt(t *testing.T) {
  13. type expect struct {
  14. prompt string
  15. images [][]byte
  16. aspectRatioID int
  17. }
  18. tmpl, err := template.Parse(`
  19. {{- if .System }}{{ .System }} {{ end }}
  20. {{- if .Prompt }}{{ .Prompt }} {{ end }}
  21. {{- if .Response }}{{ .Response }} {{ end }}`)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. visionModel := Model{Template: tmpl, ProjectorPaths: []string{"vision"}}
  26. mllamaModel := Model{Template: tmpl, ProjectorPaths: []string{"vision"}, Config: ConfigV2{ModelFamilies: []string{"mllama"}}}
  27. img := image.NewRGBA(image.Rect(0, 0, 5, 5))
  28. var buf bytes.Buffer
  29. err = png.Encode(&buf, img)
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. imgBuf := buf.Bytes()
  34. cases := []struct {
  35. name string
  36. model Model
  37. limit int
  38. msgs []api.Message
  39. expect
  40. }{
  41. {
  42. name: "messages",
  43. model: visionModel,
  44. limit: 64,
  45. msgs: []api.Message{
  46. {Role: "user", Content: "You're a test, Harry!"},
  47. {Role: "assistant", Content: "I-I'm a what?"},
  48. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
  49. },
  50. expect: expect{
  51. prompt: "You're a test, Harry! I-I'm a what? A test. And a thumping good one at that, I'd wager. ",
  52. },
  53. },
  54. {
  55. name: "truncate messages",
  56. model: visionModel,
  57. limit: 1,
  58. msgs: []api.Message{
  59. {Role: "user", Content: "You're a test, Harry!"},
  60. {Role: "assistant", Content: "I-I'm a what?"},
  61. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
  62. },
  63. expect: expect{
  64. prompt: "A test. And a thumping good one at that, I'd wager. ",
  65. },
  66. },
  67. {
  68. name: "truncate messages with image",
  69. model: visionModel,
  70. limit: 64,
  71. msgs: []api.Message{
  72. {Role: "user", Content: "You're a test, Harry!"},
  73. {Role: "assistant", Content: "I-I'm a what?"},
  74. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("something")}},
  75. },
  76. expect: expect{
  77. prompt: "[img-0] A test. And a thumping good one at that, I'd wager. ",
  78. images: [][]byte{
  79. []byte("something"),
  80. },
  81. },
  82. },
  83. {
  84. name: "truncate messages with images",
  85. model: visionModel,
  86. limit: 64,
  87. msgs: []api.Message{
  88. {Role: "user", Content: "You're a test, Harry!", Images: []api.ImageData{[]byte("something")}},
  89. {Role: "assistant", Content: "I-I'm a what?"},
  90. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}},
  91. },
  92. expect: expect{
  93. prompt: "[img-0] A test. And a thumping good one at that, I'd wager. ",
  94. images: [][]byte{
  95. []byte("somethingelse"),
  96. },
  97. },
  98. },
  99. {
  100. name: "messages with images",
  101. model: visionModel,
  102. limit: 2048,
  103. msgs: []api.Message{
  104. {Role: "user", Content: "You're a test, Harry!", Images: []api.ImageData{[]byte("something")}},
  105. {Role: "assistant", Content: "I-I'm a what?"},
  106. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}},
  107. },
  108. expect: expect{
  109. 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. ",
  110. images: [][]byte{
  111. []byte("something"),
  112. []byte("somethingelse"),
  113. },
  114. },
  115. },
  116. {
  117. name: "message with image tag",
  118. model: visionModel,
  119. limit: 2048,
  120. msgs: []api.Message{
  121. {Role: "user", Content: "You're a test, Harry! [img]", Images: []api.ImageData{[]byte("something")}},
  122. {Role: "assistant", Content: "I-I'm a what?"},
  123. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}},
  124. },
  125. expect: expect{
  126. 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. ",
  127. images: [][]byte{
  128. []byte("something"),
  129. []byte("somethingelse"),
  130. },
  131. },
  132. },
  133. {
  134. name: "messages with interleaved images",
  135. model: visionModel,
  136. limit: 2048,
  137. msgs: []api.Message{
  138. {Role: "user", Content: "You're a test, Harry!"},
  139. {Role: "user", Images: []api.ImageData{[]byte("something")}},
  140. {Role: "user", Images: []api.ImageData{[]byte("somethingelse")}},
  141. {Role: "assistant", Content: "I-I'm a what?"},
  142. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
  143. },
  144. expect: expect{
  145. 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. ",
  146. images: [][]byte{
  147. []byte("something"),
  148. []byte("somethingelse"),
  149. },
  150. },
  151. },
  152. {
  153. name: "truncate message with interleaved images",
  154. model: visionModel,
  155. limit: 1024,
  156. msgs: []api.Message{
  157. {Role: "user", Content: "You're a test, Harry!"},
  158. {Role: "user", Images: []api.ImageData{[]byte("something")}},
  159. {Role: "user", Images: []api.ImageData{[]byte("somethingelse")}},
  160. {Role: "assistant", Content: "I-I'm a what?"},
  161. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
  162. },
  163. expect: expect{
  164. prompt: "[img-0] I-I'm a what? A test. And a thumping good one at that, I'd wager. ",
  165. images: [][]byte{
  166. []byte("somethingelse"),
  167. },
  168. },
  169. },
  170. {
  171. name: "message with system prompt",
  172. model: visionModel,
  173. limit: 2048,
  174. msgs: []api.Message{
  175. {Role: "system", Content: "You are the Test Who Lived."},
  176. {Role: "user", Content: "You're a test, Harry!"},
  177. {Role: "assistant", Content: "I-I'm a what?"},
  178. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
  179. },
  180. expect: expect{
  181. 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. ",
  182. },
  183. },
  184. {
  185. name: "out of order system",
  186. model: visionModel,
  187. limit: 2048,
  188. msgs: []api.Message{
  189. {Role: "user", Content: "You're a test, Harry!"},
  190. {Role: "assistant", Content: "I-I'm a what?"},
  191. {Role: "system", Content: "You are the Test Who Lived."},
  192. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
  193. },
  194. expect: expect{
  195. 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. ",
  196. },
  197. },
  198. {
  199. name: "messages with mllama (no images)",
  200. model: mllamaModel,
  201. limit: 2048,
  202. msgs: []api.Message{
  203. {Role: "user", Content: "You're a test, Harry!"},
  204. {Role: "assistant", Content: "I-I'm a what?"},
  205. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
  206. },
  207. expect: expect{
  208. prompt: "You're a test, Harry! I-I'm a what? A test. And a thumping good one at that, I'd wager. ",
  209. },
  210. },
  211. {
  212. name: "messages with mllama",
  213. model: mllamaModel,
  214. limit: 2048,
  215. msgs: []api.Message{
  216. {Role: "user", Content: "You're a test, Harry!"},
  217. {Role: "assistant", Content: "I-I'm a what?"},
  218. {Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{imgBuf}},
  219. },
  220. expect: expect{
  221. prompt: "You're a test, Harry! I-I'm a what? <|image|>A test. And a thumping good one at that, I'd wager. ",
  222. images: [][]byte{imgBuf},
  223. aspectRatioID: 1,
  224. },
  225. },
  226. }
  227. for _, tt := range cases {
  228. t.Run(tt.name, func(t *testing.T) {
  229. model := tt.model
  230. opts := api.Options{Runner: api.Runner{NumCtx: tt.limit}}
  231. prompt, images, err := chatPrompt(context.TODO(), &model, mockRunner{}.Tokenize, &opts, tt.msgs, nil)
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. if diff := cmp.Diff(prompt, tt.prompt); diff != "" {
  236. t.Errorf("mismatch (-got +want):\n%s", diff)
  237. }
  238. if len(images) != len(tt.images) {
  239. t.Fatalf("expected %d images, got %d", len(tt.images), len(images))
  240. }
  241. for i := range images {
  242. if images[i].ID != i {
  243. t.Errorf("expected ID %d, got %d", i, images[i].ID)
  244. }
  245. if len(model.Config.ModelFamilies) == 0 {
  246. if !bytes.Equal(images[i].Data, tt.images[i]) {
  247. t.Errorf("expected %q, got %q", tt.images[i], images[i].Data)
  248. }
  249. } else {
  250. if images[i].AspectRatioID != tt.aspectRatioID {
  251. t.Errorf("expected aspect ratio %d, got %d", tt.aspectRatioID, images[i].AspectRatioID)
  252. }
  253. }
  254. }
  255. })
  256. }
  257. }