model-ratio.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package common
  2. import (
  3. "encoding/json"
  4. "strings"
  5. )
  6. // modelRatio
  7. // https://platform.openai.com/docs/models/model-endpoint-compatibility
  8. // https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Blfmc9dlf
  9. // https://openai.com/pricing
  10. // TODO: when a new api is enabled, check the pricing here
  11. // 1 === $0.002 / 1K tokens
  12. // 1 === ¥0.014 / 1k tokens
  13. var DefaultModelRatio = map[string]float64{
  14. //"midjourney": 50,
  15. "gpt-4-gizmo-*": 15,
  16. "gpt-4": 15,
  17. //"gpt-4-0314": 15, //deprecated
  18. "gpt-4-0613": 15,
  19. "gpt-4-32k": 30,
  20. //"gpt-4-32k-0314": 30, //deprecated
  21. "gpt-4-32k-0613": 30,
  22. "gpt-4-1106-preview": 5, // $0.01 / 1K tokens
  23. "gpt-4-0125-preview": 5, // $0.01 / 1K tokens
  24. "gpt-4-turbo-preview": 5, // $0.01 / 1K tokens
  25. "gpt-4-vision-preview": 5, // $0.01 / 1K tokens
  26. "gpt-4-1106-vision-preview": 5, // $0.01 / 1K tokens
  27. "gpt-4-turbo": 5, // $0.01 / 1K tokens
  28. "gpt-4-turbo-2024-04-09": 5, // $0.01 / 1K tokens
  29. "gpt-3.5-turbo": 0.25, // $0.0015 / 1K tokens
  30. //"gpt-3.5-turbo-0301": 0.75, //deprecated
  31. "gpt-3.5-turbo-0613": 0.75,
  32. "gpt-3.5-turbo-16k": 1.5, // $0.003 / 1K tokens
  33. "gpt-3.5-turbo-16k-0613": 1.5,
  34. "gpt-3.5-turbo-instruct": 0.75, // $0.0015 / 1K tokens
  35. "gpt-3.5-turbo-1106": 0.5, // $0.001 / 1K tokens
  36. "gpt-3.5-turbo-0125": 0.25,
  37. "babbage-002": 0.2, // $0.0004 / 1K tokens
  38. "davinci-002": 1, // $0.002 / 1K tokens
  39. "text-ada-001": 0.2,
  40. "text-babbage-001": 0.25,
  41. "text-curie-001": 1,
  42. "text-davinci-002": 10,
  43. "text-davinci-003": 10,
  44. "text-davinci-edit-001": 10,
  45. "code-davinci-edit-001": 10,
  46. "whisper-1": 15, // $0.006 / minute -> $0.006 / 150 words -> $0.006 / 200 tokens -> $0.03 / 1k tokens
  47. "tts-1": 7.5, // 1k characters -> $0.015
  48. "tts-1-1106": 7.5, // 1k characters -> $0.015
  49. "tts-1-hd": 15, // 1k characters -> $0.03
  50. "tts-1-hd-1106": 15, // 1k characters -> $0.03
  51. "davinci": 10,
  52. "curie": 10,
  53. "babbage": 10,
  54. "ada": 10,
  55. "text-embedding-3-small": 0.01,
  56. "text-embedding-3-large": 0.065,
  57. "text-embedding-ada-002": 0.05,
  58. "text-search-ada-doc-001": 10,
  59. "text-moderation-stable": 0.1,
  60. "text-moderation-latest": 0.1,
  61. "dall-e-2": 8,
  62. "dall-e-3": 16,
  63. "claude-instant-1": 0.4, // $0.8 / 1M tokens
  64. "claude-2.0": 4, // $8 / 1M tokens
  65. "claude-2.1": 4, // $8 / 1M tokens
  66. "claude-3-haiku-20240307": 0.125, // $0.25 / 1M tokens
  67. "claude-3-sonnet-20240229": 1.5, // $3 / 1M tokens
  68. "claude-3-opus-20240229": 7.5, // $15 / 1M tokens
  69. "ERNIE-Bot": 0.8572, // ¥0.012 / 1k tokens
  70. "ERNIE-Bot-turbo": 0.5715, // ¥0.008 / 1k tokens
  71. "ERNIE-Bot-4": 8.572, // ¥0.12 / 1k tokens
  72. "Embedding-V1": 0.1429, // ¥0.002 / 1k tokens
  73. "PaLM-2": 1,
  74. "gemini-pro": 1, // $0.00025 / 1k characters -> $0.001 / 1k tokens
  75. "gemini-pro-vision": 1, // $0.00025 / 1k characters -> $0.001 / 1k tokens
  76. "gemini-1.0-pro-vision-001": 1,
  77. "gemini-1.0-pro-001": 1,
  78. "gemini-1.5-pro-latest": 1,
  79. "gemini-1.0-pro-latest": 1,
  80. "gemini-1.0-pro-vision-latest": 1,
  81. "gemini-ultra": 1,
  82. "chatglm_turbo": 0.3572, // ¥0.005 / 1k tokens
  83. "chatglm_pro": 0.7143, // ¥0.01 / 1k tokens
  84. "chatglm_std": 0.3572, // ¥0.005 / 1k tokens
  85. "chatglm_lite": 0.1429, // ¥0.002 / 1k tokens
  86. "glm-4": 7.143, // ¥0.1 / 1k tokens
  87. "glm-4v": 7.143, // ¥0.1 / 1k tokens
  88. "glm-3-turbo": 0.3572,
  89. "qwen-turbo": 0.8572, // ¥0.012 / 1k tokens
  90. "qwen-plus": 10, // ¥0.14 / 1k tokens
  91. "text-embedding-v1": 0.05, // ¥0.0007 / 1k tokens
  92. "SparkDesk-v1.1": 1.2858, // ¥0.018 / 1k tokens
  93. "SparkDesk-v2.1": 1.2858, // ¥0.018 / 1k tokens
  94. "SparkDesk-v3.1": 1.2858, // ¥0.018 / 1k tokens
  95. "SparkDesk-v3.5": 1.2858, // ¥0.018 / 1k tokens
  96. "360GPT_S2_V9": 0.8572, // ¥0.012 / 1k tokens
  97. "embedding-bert-512-v1": 0.0715, // ¥0.001 / 1k tokens
  98. "embedding_s1_v1": 0.0715, // ¥0.001 / 1k tokens
  99. "semantic_similarity_s1_v1": 0.0715, // ¥0.001 / 1k tokens
  100. "hunyuan": 7.143, // ¥0.1 / 1k tokens // https://cloud.tencent.com/document/product/1729/97731#e0e6be58-60c8-469f-bdeb-6c264ce3b4d0
  101. // https://platform.lingyiwanwu.com/docs#-计费单元
  102. // 已经按照 7.2 来换算美元价格
  103. "yi-34b-chat-0205": 0.018,
  104. "yi-34b-chat-200k": 0.0864,
  105. "yi-vl-plus": 0.0432,
  106. "command": 0.5,
  107. "command-nightly": 0.5,
  108. "command-light": 0.5,
  109. "command-light-nightly": 0.5,
  110. "command-r": 0.25,
  111. "command-r-plus ": 1.5,
  112. "deepseek-chat": 0.07,
  113. "deepseek-coder": 0.07,
  114. }
  115. var DefaultModelPrice = map[string]float64{
  116. "gpt-4-gizmo-*": 0.1,
  117. "mj_imagine": 0.1,
  118. "mj_variation": 0.1,
  119. "mj_reroll": 0.1,
  120. "mj_blend": 0.1,
  121. "mj_modal": 0.1,
  122. "mj_zoom": 0.1,
  123. "mj_shorten": 0.1,
  124. "mj_high_variation": 0.1,
  125. "mj_low_variation": 0.1,
  126. "mj_pan": 0.1,
  127. "mj_inpaint": 0,
  128. "mj_custom_zoom": 0,
  129. "mj_describe": 0.05,
  130. "mj_upscale": 0.05,
  131. "swap_face": 0.05,
  132. }
  133. var modelPrice map[string]float64 = nil
  134. var modelRatio map[string]float64 = nil
  135. var CompletionRatio map[string]float64 = nil
  136. var DefaultCompletionRatio = map[string]float64{
  137. "gpt-4-gizmo-*": 2,
  138. "gpt-4-all": 2,
  139. }
  140. func ModelPrice2JSONString() string {
  141. if modelPrice == nil {
  142. modelPrice = DefaultModelPrice
  143. }
  144. jsonBytes, err := json.Marshal(modelPrice)
  145. if err != nil {
  146. SysError("error marshalling model price: " + err.Error())
  147. }
  148. return string(jsonBytes)
  149. }
  150. func UpdateModelPriceByJSONString(jsonStr string) error {
  151. modelPrice = make(map[string]float64)
  152. return json.Unmarshal([]byte(jsonStr), &modelPrice)
  153. }
  154. func GetModelPrice(name string, printErr bool) float64 {
  155. if modelPrice == nil {
  156. modelPrice = DefaultModelPrice
  157. }
  158. if strings.HasPrefix(name, "gpt-4-gizmo") {
  159. name = "gpt-4-gizmo-*"
  160. }
  161. price, ok := modelPrice[name]
  162. if !ok {
  163. if printErr {
  164. SysError("model price not found: " + name)
  165. }
  166. return -1
  167. }
  168. return price
  169. }
  170. func ModelRatio2JSONString() string {
  171. if modelRatio == nil {
  172. modelRatio = DefaultModelRatio
  173. }
  174. jsonBytes, err := json.Marshal(modelRatio)
  175. if err != nil {
  176. SysError("error marshalling model ratio: " + err.Error())
  177. }
  178. return string(jsonBytes)
  179. }
  180. func UpdateModelRatioByJSONString(jsonStr string) error {
  181. modelRatio = make(map[string]float64)
  182. return json.Unmarshal([]byte(jsonStr), &modelRatio)
  183. }
  184. func GetModelRatio(name string) float64 {
  185. if modelRatio == nil {
  186. modelRatio = DefaultModelRatio
  187. }
  188. if strings.HasPrefix(name, "gpt-4-gizmo") {
  189. name = "gpt-4-gizmo-*"
  190. }
  191. ratio, ok := modelRatio[name]
  192. if !ok {
  193. SysError("model ratio not found: " + name)
  194. return 30
  195. }
  196. return ratio
  197. }
  198. func CompletionRatio2JSONString() string {
  199. if CompletionRatio == nil {
  200. CompletionRatio = DefaultCompletionRatio
  201. }
  202. jsonBytes, err := json.Marshal(CompletionRatio)
  203. if err != nil {
  204. SysError("error marshalling completion ratio: " + err.Error())
  205. }
  206. return string(jsonBytes)
  207. }
  208. func UpdateCompletionRatioByJSONString(jsonStr string) error {
  209. CompletionRatio = make(map[string]float64)
  210. return json.Unmarshal([]byte(jsonStr), &CompletionRatio)
  211. }
  212. func GetCompletionRatio(name string) float64 {
  213. if strings.HasPrefix(name, "gpt-3.5") {
  214. if name == "gpt-3.5-turbo" || strings.HasSuffix(name, "0125") {
  215. // https://openai.com/blog/new-embedding-models-and-api-updates
  216. // Updated GPT-3.5 Turbo model and lower pricing
  217. return 3
  218. }
  219. if strings.HasSuffix(name, "1106") {
  220. return 2
  221. }
  222. return 4.0 / 3.0
  223. }
  224. if strings.HasPrefix(name, "gpt-4") && name != "gpt-4-all" && !strings.HasPrefix(name, "gpt-4-gizmo") {
  225. if strings.HasPrefix(name, "gpt-4-turbo") || strings.HasSuffix(name, "preview") {
  226. return 3
  227. }
  228. return 2
  229. }
  230. if strings.Contains(name, "claude-instant-1") {
  231. return 3
  232. } else if strings.Contains(name, "claude-2") {
  233. return 3
  234. } else if strings.Contains(name, "claude-3") {
  235. return 5
  236. }
  237. if strings.HasPrefix(name, "mistral-") {
  238. return 3
  239. }
  240. if strings.HasPrefix(name, "gemini-") {
  241. return 3
  242. }
  243. if strings.HasPrefix(name, "command") {
  244. switch name {
  245. case "command-r":
  246. return 3
  247. case "command-r-plus":
  248. return 5
  249. default:
  250. return 2
  251. }
  252. }
  253. if strings.HasPrefix(name, "deepseek") {
  254. return 2
  255. }
  256. switch name {
  257. case "llama2-70b-4096":
  258. return 0.8 / 0.64
  259. case "llama3-8b-8192":
  260. return 2
  261. case "llama3-70b-8192":
  262. return 0.79 / 0.59
  263. }
  264. if ratio, ok := CompletionRatio[name]; ok {
  265. return ratio
  266. }
  267. return 1
  268. }