llm_test.go 5.2 KB

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