model-ratio.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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-4o": 2.5, // $0.01 / 1K tokens
  28. "gpt-4o-2024-05-13": 2.5, // $0.01 / 1K tokens
  29. "gpt-4-turbo": 5, // $0.01 / 1K tokens
  30. "gpt-4-turbo-2024-04-09": 5, // $0.01 / 1K tokens
  31. "gpt-3.5-turbo": 0.25, // $0.0015 / 1K tokens
  32. //"gpt-3.5-turbo-0301": 0.75, //deprecated
  33. "gpt-3.5-turbo-0613": 0.75,
  34. "gpt-3.5-turbo-16k": 1.5, // $0.003 / 1K tokens
  35. "gpt-3.5-turbo-16k-0613": 1.5,
  36. "gpt-3.5-turbo-instruct": 0.75, // $0.0015 / 1K tokens
  37. "gpt-3.5-turbo-1106": 0.5, // $0.001 / 1K tokens
  38. "gpt-3.5-turbo-0125": 0.25,
  39. "babbage-002": 0.2, // $0.0004 / 1K tokens
  40. "davinci-002": 1, // $0.002 / 1K tokens
  41. "text-ada-001": 0.2,
  42. "text-babbage-001": 0.25,
  43. "text-curie-001": 1,
  44. "text-davinci-002": 10,
  45. "text-davinci-003": 10,
  46. "text-davinci-edit-001": 10,
  47. "code-davinci-edit-001": 10,
  48. "whisper-1": 15, // $0.006 / minute -> $0.006 / 150 words -> $0.006 / 200 tokens -> $0.03 / 1k tokens
  49. "tts-1": 7.5, // 1k characters -> $0.015
  50. "tts-1-1106": 7.5, // 1k characters -> $0.015
  51. "tts-1-hd": 15, // 1k characters -> $0.03
  52. "tts-1-hd-1106": 15, // 1k characters -> $0.03
  53. "davinci": 10,
  54. "curie": 10,
  55. "babbage": 10,
  56. "ada": 10,
  57. "text-embedding-3-small": 0.01,
  58. "text-embedding-3-large": 0.065,
  59. "text-embedding-ada-002": 0.05,
  60. "text-search-ada-doc-001": 10,
  61. "text-moderation-stable": 0.1,
  62. "text-moderation-latest": 0.1,
  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 //renamed to ERNIE-3.5-8K
  70. "ERNIE-Bot-turbo": 0.5715, // ¥0.008 / 1k tokens //renamed to ERNIE-Lite-8K
  71. "ERNIE-Bot-4": 8.572, // ¥0.12 / 1k tokens //renamed to ERNIE-4.0-8K
  72. "ERNIE-4.0-8K": 8.572, // ¥0.12 / 1k tokens
  73. "ERNIE-3.5-8K": 0.8572, // ¥0.012 / 1k tokens
  74. "ERNIE-Speed-8K": 0.2858, // ¥0.004 / 1k tokens
  75. "ERNIE-Speed-128K": 0.2858, // ¥0.004 / 1k tokens
  76. "ERNIE-Lite-8K": 0.2143, // ¥0.003 / 1k tokens
  77. "ERNIE-Tiny-8K": 0.0715, // ¥0.001 / 1k tokens
  78. "ERNIE-Character-8K": 0.2858, // ¥0.004 / 1k tokens
  79. "ERNIE-Functions-8K": 0.2858, // ¥0.004 / 1k tokens
  80. "Embedding-V1": 0.1429, // ¥0.002 / 1k tokens
  81. "PaLM-2": 1,
  82. "gemini-pro": 1, // $0.00025 / 1k characters -> $0.001 / 1k tokens
  83. "gemini-pro-vision": 1, // $0.00025 / 1k characters -> $0.001 / 1k tokens
  84. "gemini-1.0-pro-vision-001": 1,
  85. "gemini-1.0-pro-001": 1,
  86. "gemini-1.5-pro-latest": 1,
  87. "gemini-1.5-flash-latest": 1,
  88. "gemini-1.0-pro-latest": 1,
  89. "gemini-1.0-pro-vision-latest": 1,
  90. "gemini-ultra": 1,
  91. "chatglm_turbo": 0.3572, // ¥0.005 / 1k tokens
  92. "chatglm_pro": 0.7143, // ¥0.01 / 1k tokens
  93. "chatglm_std": 0.3572, // ¥0.005 / 1k tokens
  94. "chatglm_lite": 0.1429, // ¥0.002 / 1k tokens
  95. "glm-4": 7.143, // ¥0.1 / 1k tokens
  96. "glm-4v": 7.143, // ¥0.1 / 1k tokens
  97. "glm-3-turbo": 0.3572,
  98. "qwen-turbo": 0.8572, // ¥0.012 / 1k tokens
  99. "qwen-plus": 10, // ¥0.14 / 1k tokens
  100. "text-embedding-v1": 0.05, // ¥0.0007 / 1k tokens
  101. "SparkDesk-v1.1": 1.2858, // ¥0.018 / 1k tokens
  102. "SparkDesk-v2.1": 1.2858, // ¥0.018 / 1k tokens
  103. "SparkDesk-v3.1": 1.2858, // ¥0.018 / 1k tokens
  104. "SparkDesk-v3.5": 1.2858, // ¥0.018 / 1k tokens
  105. "360GPT_S2_V9": 0.8572, // ¥0.012 / 1k tokens
  106. "360gpt-turbo": 0.0858, // ¥0.0012 / 1k tokens
  107. "360gpt-turbo-responsibility-8k": 0.8572, // ¥0.012 / 1k tokens
  108. "360gpt-pro": 0.8572, // ¥0.012 / 1k tokens
  109. "embedding-bert-512-v1": 0.0715, // ¥0.001 / 1k tokens
  110. "embedding_s1_v1": 0.0715, // ¥0.001 / 1k tokens
  111. "semantic_similarity_s1_v1": 0.0715, // ¥0.001 / 1k tokens
  112. "hunyuan": 7.143, // ¥0.1 / 1k tokens // https://cloud.tencent.com/document/product/1729/97731#e0e6be58-60c8-469f-bdeb-6c264ce3b4d0
  113. // https://platform.lingyiwanwu.com/docs#-计费单元
  114. // 已经按照 7.2 来换算美元价格
  115. "yi-34b-chat-0205": 0.018,
  116. "yi-34b-chat-200k": 0.0864,
  117. "yi-vl-plus": 0.0432,
  118. "command": 0.5,
  119. "command-nightly": 0.5,
  120. "command-light": 0.5,
  121. "command-light-nightly": 0.5,
  122. "command-r": 0.25,
  123. "command-r-plus ": 1.5,
  124. "deepseek-chat": 0.07,
  125. "deepseek-coder": 0.07,
  126. }
  127. var DefaultModelPrice = map[string]float64{
  128. "dall-e-2": 0.02,
  129. "dall-e-3": 0.04,
  130. "gpt-4-gizmo-*": 0.1,
  131. "mj_imagine": 0.1,
  132. "mj_variation": 0.1,
  133. "mj_reroll": 0.1,
  134. "mj_blend": 0.1,
  135. "mj_modal": 0.1,
  136. "mj_zoom": 0.1,
  137. "mj_shorten": 0.1,
  138. "mj_high_variation": 0.1,
  139. "mj_low_variation": 0.1,
  140. "mj_pan": 0.1,
  141. "mj_inpaint": 0,
  142. "mj_custom_zoom": 0,
  143. "mj_describe": 0.05,
  144. "mj_upscale": 0.05,
  145. "swap_face": 0.05,
  146. }
  147. var modelPrice map[string]float64 = nil
  148. var modelRatio map[string]float64 = nil
  149. var CompletionRatio map[string]float64 = nil
  150. var DefaultCompletionRatio = map[string]float64{
  151. "gpt-4-gizmo-*": 2,
  152. "gpt-4-all": 2,
  153. }
  154. func ModelPrice2JSONString() string {
  155. if modelPrice == nil {
  156. modelPrice = DefaultModelPrice
  157. }
  158. jsonBytes, err := json.Marshal(modelPrice)
  159. if err != nil {
  160. SysError("error marshalling model price: " + err.Error())
  161. }
  162. return string(jsonBytes)
  163. }
  164. func UpdateModelPriceByJSONString(jsonStr string) error {
  165. modelPrice = make(map[string]float64)
  166. return json.Unmarshal([]byte(jsonStr), &modelPrice)
  167. }
  168. // GetModelPrice 返回模型的价格,如果模型不存在则返回-1,false
  169. func GetModelPrice(name string, printErr bool) (float64, bool) {
  170. if modelPrice == nil {
  171. modelPrice = DefaultModelPrice
  172. }
  173. if strings.HasPrefix(name, "gpt-4-gizmo") {
  174. name = "gpt-4-gizmo-*"
  175. }
  176. price, ok := modelPrice[name]
  177. if !ok {
  178. if printErr {
  179. SysError("model price not found: " + name)
  180. }
  181. return -1, false
  182. }
  183. return price, true
  184. }
  185. func GetModelPrices() map[string]float64 {
  186. if modelPrice == nil {
  187. modelPrice = DefaultModelPrice
  188. }
  189. return modelPrice
  190. }
  191. func ModelRatio2JSONString() string {
  192. if modelRatio == nil {
  193. modelRatio = DefaultModelRatio
  194. }
  195. jsonBytes, err := json.Marshal(modelRatio)
  196. if err != nil {
  197. SysError("error marshalling model ratio: " + err.Error())
  198. }
  199. return string(jsonBytes)
  200. }
  201. func UpdateModelRatioByJSONString(jsonStr string) error {
  202. modelRatio = make(map[string]float64)
  203. return json.Unmarshal([]byte(jsonStr), &modelRatio)
  204. }
  205. func GetModelRatio(name string) float64 {
  206. if modelRatio == nil {
  207. modelRatio = DefaultModelRatio
  208. }
  209. if strings.HasPrefix(name, "gpt-4-gizmo") {
  210. name = "gpt-4-gizmo-*"
  211. }
  212. ratio, ok := modelRatio[name]
  213. if !ok {
  214. SysError("model ratio not found: " + name)
  215. return 30
  216. }
  217. return ratio
  218. }
  219. func GetModelRatios() map[string]float64 {
  220. if modelRatio == nil {
  221. modelRatio = DefaultModelRatio
  222. }
  223. return modelRatio
  224. }
  225. func CompletionRatio2JSONString() string {
  226. if CompletionRatio == nil {
  227. CompletionRatio = DefaultCompletionRatio
  228. }
  229. jsonBytes, err := json.Marshal(CompletionRatio)
  230. if err != nil {
  231. SysError("error marshalling completion ratio: " + err.Error())
  232. }
  233. return string(jsonBytes)
  234. }
  235. func UpdateCompletionRatioByJSONString(jsonStr string) error {
  236. CompletionRatio = make(map[string]float64)
  237. return json.Unmarshal([]byte(jsonStr), &CompletionRatio)
  238. }
  239. func GetCompletionRatio(name string) float64 {
  240. if strings.HasPrefix(name, "gpt-4-gizmo") {
  241. name = "gpt-4-gizmo-*"
  242. }
  243. if strings.HasPrefix(name, "gpt-3.5") {
  244. if name == "gpt-3.5-turbo" || strings.HasSuffix(name, "0125") {
  245. // https://openai.com/blog/new-embedding-models-and-api-updates
  246. // Updated GPT-3.5 Turbo model and lower pricing
  247. return 3
  248. }
  249. if strings.HasSuffix(name, "1106") {
  250. return 2
  251. }
  252. return 4.0 / 3.0
  253. }
  254. if strings.HasPrefix(name, "gpt-4") && name != "gpt-4-all" && name != "gpt-4-gizmo-*" {
  255. if strings.HasPrefix(name, "gpt-4-turbo") || strings.HasSuffix(name, "preview") || strings.HasPrefix(name, "gpt-4o") {
  256. return 3
  257. }
  258. return 2
  259. }
  260. if strings.Contains(name, "claude-instant-1") {
  261. return 3
  262. } else if strings.Contains(name, "claude-2") {
  263. return 3
  264. } else if strings.Contains(name, "claude-3") {
  265. return 5
  266. }
  267. if strings.HasPrefix(name, "mistral-") {
  268. return 3
  269. }
  270. if strings.HasPrefix(name, "gemini-") {
  271. return 3
  272. }
  273. if strings.HasPrefix(name, "command") {
  274. switch name {
  275. case "command-r":
  276. return 3
  277. case "command-r-plus":
  278. return 5
  279. default:
  280. return 2
  281. }
  282. }
  283. if strings.HasPrefix(name, "deepseek") {
  284. return 2
  285. }
  286. if strings.HasPrefix(name, "ERNIE-Speed-") {
  287. return 2
  288. } else if strings.HasPrefix(name, "ERNIE-Lite-") {
  289. return 2
  290. } else if strings.HasPrefix(name, "ERNIE-Character") {
  291. return 2
  292. } else if strings.HasPrefix(name, "ERNIE-Functions") {
  293. return 2
  294. }
  295. switch name {
  296. case "llama2-70b-4096":
  297. return 0.8 / 0.64
  298. case "llama3-8b-8192":
  299. return 2
  300. case "llama3-70b-8192":
  301. return 0.79 / 0.59
  302. }
  303. if ratio, ok := CompletionRatio[name]; ok {
  304. return ratio
  305. }
  306. return 1
  307. }
  308. func GetCompletionRatios() map[string]float64 {
  309. if CompletionRatio == nil {
  310. CompletionRatio = DefaultCompletionRatio
  311. }
  312. return CompletionRatio
  313. }