llm_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package model_entities
  2. import (
  3. "testing"
  4. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  5. )
  6. func TestFullFunctionPromptMessage(t *testing.T) {
  7. const (
  8. system_message = `
  9. {
  10. "role": "system",
  11. "content": "you are a helpful assistant"
  12. }
  13. `
  14. user_message = `
  15. {
  16. "role": "user",
  17. "content": "hello"
  18. }`
  19. assistant_message = `
  20. {
  21. "role": "assistant",
  22. "content": "you are a helpful assistant"
  23. }`
  24. image_message = `
  25. {
  26. "role": "user",
  27. "content": [
  28. {
  29. "type": "image",
  30. "data": "base64"
  31. }
  32. ]
  33. }`
  34. tool_message = `
  35. {
  36. "role": "tool",
  37. "content": "you are a helpful assistant",
  38. "tool_call_id": "123"
  39. }
  40. `
  41. )
  42. promptMessage, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(system_message))
  43. if err != nil {
  44. t.Error(err)
  45. }
  46. if promptMessage.Role != "system" {
  47. t.Error("role is not system")
  48. }
  49. promptMessage, err = parser.UnmarshalJsonBytes[PromptMessage]([]byte(user_message))
  50. if err != nil {
  51. t.Error(err)
  52. }
  53. if promptMessage.Role != "user" {
  54. t.Error("role is not user")
  55. }
  56. promptMessage, err = parser.UnmarshalJsonBytes[PromptMessage]([]byte(assistant_message))
  57. if err != nil {
  58. t.Error(err)
  59. }
  60. if promptMessage.Role != "assistant" {
  61. t.Error("role is not assistant")
  62. }
  63. promptMessage, err = parser.UnmarshalJsonBytes[PromptMessage]([]byte(image_message))
  64. if err != nil {
  65. t.Error(err)
  66. }
  67. if promptMessage.Role != "user" {
  68. t.Error("role is not user")
  69. }
  70. if promptMessage.Content.([]PromptMessageContent)[0].Type != "image" {
  71. t.Error("type is not image")
  72. }
  73. promptMessage, err = parser.UnmarshalJsonBytes[PromptMessage]([]byte(tool_message))
  74. if err != nil {
  75. t.Error(err)
  76. }
  77. if promptMessage.Role != "tool" {
  78. t.Error("role is not tool")
  79. }
  80. if promptMessage.ToolCallId != "123" {
  81. t.Error("tool call id is not 123")
  82. }
  83. }
  84. func TestWrongRole(t *testing.T) {
  85. const (
  86. wrong_role = `
  87. {
  88. "role": "wrong",
  89. "content": "you are a helpful assistant"
  90. }
  91. `
  92. )
  93. _, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(wrong_role))
  94. if err == nil {
  95. t.Error("error is nil")
  96. }
  97. }
  98. func TestWrongContent(t *testing.T) {
  99. const (
  100. wrong_content = `
  101. {
  102. "role": "user",
  103. "content": 123
  104. }
  105. `
  106. )
  107. _, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(wrong_content))
  108. if err == nil {
  109. t.Error("error is nil")
  110. }
  111. }
  112. func TestWrongContentArray(t *testing.T) {
  113. const (
  114. wrong_content_array = `
  115. {
  116. "role": "user",
  117. "content": [
  118. {
  119. "type": "image",
  120. "data": 123
  121. }
  122. ]
  123. }
  124. `
  125. )
  126. _, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(wrong_content_array))
  127. if err == nil {
  128. t.Error("error is nil")
  129. }
  130. }
  131. // func TestWrongContentArray2(t *testing.T) {
  132. // const (
  133. // wrong_content_array2 = `
  134. // {
  135. // "role": "user",
  136. // "content": [
  137. // {
  138. // "type": "image"
  139. // }
  140. // ]
  141. // }
  142. // `
  143. // )
  144. // _, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(wrong_content_array2))
  145. // if err == nil {
  146. // t.Error("error is nil")
  147. // }
  148. // }
  149. func TestWrongContentArray3(t *testing.T) {
  150. const (
  151. wrong_content_array3 = `
  152. {
  153. "role": "user",
  154. "content": [
  155. {
  156. "type": "wwww",
  157. "data": "base64"
  158. },
  159. {
  160. "type": "image",
  161. "data": "base64"
  162. }
  163. ]
  164. }
  165. `
  166. )
  167. _, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(wrong_content_array3))
  168. if err == nil {
  169. t.Error("error is nil")
  170. }
  171. }
  172. func TestFullFunctionLLMResultChunk(t *testing.T) {
  173. const (
  174. llm_result_chunk = `
  175. {
  176. "model": "gpt-3.5-turbo",
  177. "prompt_messages": [
  178. {
  179. "role": "system",
  180. "content": "you are a helpful assistant"
  181. },
  182. {
  183. "role": "user",
  184. "content": "hello"
  185. }
  186. ],
  187. "system_fingerprint": "123",
  188. "delta": {
  189. "index" : 0,
  190. "message" : {
  191. "role": "assistant",
  192. "content": "I am a helpful assistant"
  193. },
  194. "usage": {
  195. "prompt_tokens": 10,
  196. "prompt_unit_price": 0.1,
  197. "prompt_price_unit": 1,
  198. "prompt_price": 1,
  199. "completion_tokens": 10,
  200. "completion_unit_price": 0.1,
  201. "completion_price_unit": 1,
  202. "completion_price": 1,
  203. "total_tokens": 20,
  204. "total_price": 2,
  205. "currency": "usd",
  206. "latency": 0.1
  207. },
  208. "finish_reason": "completed"
  209. }
  210. }
  211. `
  212. )
  213. _, err := parser.UnmarshalJsonBytes[LLMResultChunk]([]byte(llm_result_chunk))
  214. if err != nil {
  215. t.Error(err)
  216. }
  217. }
  218. func TestZeroLLMUsage(t *testing.T) {
  219. const (
  220. llm_usage = `
  221. {
  222. "prompt_tokens": 0,
  223. "prompt_unit_price": 0,
  224. "prompt_price_unit": 0,
  225. "prompt_price": 0,
  226. "completion_tokens": 0,
  227. "completion_unit_price": 0,
  228. "completion_price_unit": 0,
  229. "completion_price": 0,
  230. "total_tokens": 0,
  231. "total_price": 0,
  232. "currency": "usd",
  233. "latency": 0
  234. }
  235. `
  236. )
  237. _, err := parser.UnmarshalJsonBytes[LLMUsage]([]byte(llm_usage))
  238. if err != nil {
  239. t.Error(err)
  240. }
  241. }
  242. func TestTextPromptMessage(t *testing.T) {
  243. const (
  244. promptMessage = `
  245. {
  246. "role": "user",
  247. "content": "hello"
  248. }
  249. `
  250. )
  251. _, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(promptMessage))
  252. if err != nil {
  253. t.Error(err)
  254. }
  255. }
  256. func TestImagePromptMessage(t *testing.T) {
  257. const (
  258. promptMessage = `
  259. {
  260. "role": "user",
  261. "content": [
  262. {
  263. "type": "image",
  264. "data": "base64"
  265. }
  266. ]
  267. }
  268. `
  269. )
  270. _, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(promptMessage))
  271. if err != nil {
  272. t.Error(err)
  273. }
  274. }