token_calculation_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package tests
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "testing"
  9. )
  10. // TestQwenTokenCalculation 测试千问模型的token计算规则
  11. func TestQwenTokenCalculation(t *testing.T) {
  12. // 加载测试配置
  13. config := LoadConfig()
  14. baseURL := config.Qwen3BaseURL
  15. model := config.Qwen3Model
  16. // 构建询问token计算规则的请求
  17. request := Qwen3ChatRequest{
  18. Model: model,
  19. Stream: false,
  20. Messages: []ChatMessage{
  21. {
  22. Role: "user",
  23. Content: "请详细解释一下你的token计算规则。具体来说:1. 中文字符如何计算token?2. 英文字符如何计算token?3. 标点符号如何计算token?4. JSON结构字符(如{、}、[、]、\"、:、,)如何计算token?5. 换行符和空格如何计算token?请给出具体的比例或规则。",
  24. },
  25. },
  26. }
  27. jsonData, err := json.Marshal(request)
  28. if err != nil {
  29. t.Fatalf("序列化请求失败: %v", err)
  30. }
  31. t.Logf("发送的请求: %s", string(jsonData))
  32. // 发送POST请求
  33. resp, err := http.Post(
  34. fmt.Sprintf("%s/v1/chat/completions", baseURL),
  35. "application/json",
  36. bytes.NewBuffer(jsonData),
  37. )
  38. if err != nil {
  39. t.Fatalf("发送请求失败: %v", err)
  40. }
  41. defer resp.Body.Close()
  42. t.Logf("响应状态码: %d", resp.StatusCode)
  43. if resp.StatusCode != http.StatusOK {
  44. body, _ := io.ReadAll(resp.Body)
  45. t.Fatalf("请求失败,状态码: %d, 响应: %s", resp.StatusCode, string(body))
  46. }
  47. // 读取完整的响应内容
  48. body, err := io.ReadAll(resp.Body)
  49. if err != nil {
  50. t.Fatalf("读取响应失败: %v", err)
  51. }
  52. // 解析JSON响应
  53. var response Qwen3ChatResponse
  54. if err := json.Unmarshal(body, &response); err != nil {
  55. t.Fatalf("解析JSON响应失败: %v", err)
  56. }
  57. t.Logf("✅ Token计算规则查询成功!")
  58. t.Logf("千问模型的回复:")
  59. t.Logf("==========================================")
  60. t.Logf("%s", response.Choices[0].Message.Content)
  61. t.Logf("==========================================")
  62. }
  63. // TestQwenTokenLimit 测试千问模型的具体token限制
  64. func TestQwenTokenLimit(t *testing.T) {
  65. // 加载测试配置
  66. config := LoadConfig()
  67. baseURL := config.Qwen3BaseURL
  68. model := config.Qwen3Model
  69. // 构建询问token限制的请求
  70. request := Qwen3ChatRequest{
  71. Model: model,
  72. Stream: false,
  73. Messages: []ChatMessage{
  74. {
  75. Role: "user",
  76. Content: "请告诉我你的最大输入token限制是多少?包括输入和输出的总限制。",
  77. },
  78. },
  79. }
  80. jsonData, err := json.Marshal(request)
  81. if err != nil {
  82. t.Fatalf("序列化请求失败: %v", err)
  83. }
  84. t.Logf("发送的请求: %s", string(jsonData))
  85. // 发送POST请求
  86. resp, err := http.Post(
  87. fmt.Sprintf("%s/v1/chat/completions", baseURL),
  88. "application/json",
  89. bytes.NewBuffer(jsonData),
  90. )
  91. if err != nil {
  92. t.Fatalf("发送请求失败: %v", err)
  93. }
  94. defer resp.Body.Close()
  95. t.Logf("响应状态码: %d", resp.StatusCode)
  96. if resp.StatusCode != http.StatusOK {
  97. body, _ := io.ReadAll(resp.Body)
  98. t.Fatalf("请求失败,状态码: %d, 响应: %s", resp.StatusCode, string(body))
  99. }
  100. // 读取完整的响应内容
  101. body, err := io.ReadAll(resp.Body)
  102. if err != nil {
  103. t.Fatalf("读取响应失败: %v", err)
  104. }
  105. // 解析JSON响应
  106. var response Qwen3ChatResponse
  107. if err := json.Unmarshal(body, &response); err != nil {
  108. t.Fatalf("解析JSON响应失败: %v", err)
  109. }
  110. t.Logf("✅ Token限制查询成功!")
  111. t.Logf("千问模型的回复:")
  112. t.Logf("==========================================")
  113. t.Logf("%s", response.Choices[0].Message.Content)
  114. t.Logf("==========================================")
  115. }
  116. // TestQwenTokenEstimation 测试具体的token估算
  117. func TestQwenTokenEstimation(t *testing.T) {
  118. // 加载测试配置
  119. config := LoadConfig()
  120. baseURL := config.Qwen3BaseURL
  121. model := config.Qwen3Model
  122. // 测试文本
  123. testTexts := []string{
  124. "你好世界", // 4个中文字符
  125. "Hello World", // 11个英文字符
  126. "你好,Hello World!", // 混合文本
  127. `{"name": "张三", "age": 25}`, // JSON格式
  128. "这是一个测试\n包含换行符\t和制表符", // 包含特殊字符
  129. }
  130. for i, testText := range testTexts {
  131. t.Run(fmt.Sprintf("测试文本%d", i+1), func(t *testing.T) {
  132. request := Qwen3ChatRequest{
  133. Model: model,
  134. Stream: false,
  135. Messages: []ChatMessage{
  136. {
  137. Role: "user",
  138. Content: fmt.Sprintf("请告诉我这段文本有多少个token:\"%s\"。请只回答数字,不要其他解释。", testText),
  139. },
  140. },
  141. }
  142. jsonData, err := json.Marshal(request)
  143. if err != nil {
  144. t.Fatalf("序列化请求失败: %v", err)
  145. }
  146. // 发送POST请求
  147. resp, err := http.Post(
  148. fmt.Sprintf("%s/v1/chat/completions", baseURL),
  149. "application/json",
  150. bytes.NewBuffer(jsonData),
  151. )
  152. if err != nil {
  153. t.Fatalf("发送请求失败: %v", err)
  154. }
  155. defer resp.Body.Close()
  156. if resp.StatusCode != http.StatusOK {
  157. body, _ := io.ReadAll(resp.Body)
  158. t.Fatalf("请求失败,状态码: %d, 响应: %s", resp.StatusCode, string(body))
  159. }
  160. // 读取完整的响应内容
  161. body, err := io.ReadAll(resp.Body)
  162. if err != nil {
  163. t.Fatalf("读取响应失败: %v", err)
  164. }
  165. // 解析JSON响应
  166. var response Qwen3ChatResponse
  167. if err := json.Unmarshal(body, &response); err != nil {
  168. t.Fatalf("解析JSON响应失败: %v", err)
  169. }
  170. t.Logf("测试文本: \"%s\"", testText)
  171. t.Logf("字符长度: %d", len(testText))
  172. t.Logf("千问估算token数: %s", response.Choices[0].Message.Content)
  173. t.Logf("---")
  174. })
  175. }
  176. }