model_ratio.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. package ratio_setting
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "sync"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/setting/operation_setting"
  8. )
  9. // from songquanpeng/one-api
  10. const (
  11. USD2RMB = 7.3 // 暂定 1 USD = 7.3 RMB
  12. USD = 500 // $0.002 = 1 -> $1 = 500
  13. RMB = USD / USD2RMB
  14. )
  15. // modelRatio
  16. // https://platform.openai.com/docs/models/model-endpoint-compatibility
  17. // https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Blfmc9dlf
  18. // https://openai.com/pricing
  19. // TODO: when a new api is enabled, check the pricing here
  20. // 1 === $0.002 / 1K tokens
  21. // 1 === ¥0.014 / 1k tokens
  22. var defaultModelRatio = map[string]float64{
  23. //"midjourney": 50,
  24. "gpt-4-gizmo-*": 15,
  25. "gpt-4o-gizmo-*": 2.5,
  26. "gpt-4-all": 15,
  27. "gpt-4o-all": 15,
  28. "gpt-4": 15,
  29. //"gpt-4-0314": 15, //deprecated
  30. "gpt-4-0613": 15,
  31. "gpt-4-32k": 30,
  32. //"gpt-4-32k-0314": 30, //deprecated
  33. "gpt-4-32k-0613": 30,
  34. "gpt-4-1106-preview": 5, // $10 / 1M tokens
  35. "gpt-4-0125-preview": 5, // $10 / 1M tokens
  36. "gpt-4-turbo-preview": 5, // $10 / 1M tokens
  37. "gpt-4-vision-preview": 5, // $10 / 1M tokens
  38. "gpt-4-1106-vision-preview": 5, // $10 / 1M tokens
  39. "chatgpt-4o-latest": 2.5, // $5 / 1M tokens
  40. "gpt-4o": 1.25, // $2.5 / 1M tokens
  41. "gpt-4o-audio-preview": 1.25, // $2.5 / 1M tokens
  42. "gpt-4o-audio-preview-2024-10-01": 1.25, // $2.5 / 1M tokens
  43. "gpt-4o-2024-05-13": 2.5, // $5 / 1M tokens
  44. "gpt-4o-2024-08-06": 1.25, // $2.5 / 1M tokens
  45. "gpt-4o-2024-11-20": 1.25, // $2.5 / 1M tokens
  46. "gpt-4o-realtime-preview": 2.5,
  47. "gpt-4o-realtime-preview-2024-10-01": 2.5,
  48. "gpt-4o-realtime-preview-2024-12-17": 2.5,
  49. "gpt-4o-mini-realtime-preview": 0.3,
  50. "gpt-4o-mini-realtime-preview-2024-12-17": 0.3,
  51. "gpt-4.1": 1.0, // $2 / 1M tokens
  52. "gpt-4.1-2025-04-14": 1.0, // $2 / 1M tokens
  53. "gpt-4.1-mini": 0.2, // $0.4 / 1M tokens
  54. "gpt-4.1-mini-2025-04-14": 0.2, // $0.4 / 1M tokens
  55. "gpt-4.1-nano": 0.05, // $0.1 / 1M tokens
  56. "gpt-4.1-nano-2025-04-14": 0.05, // $0.1 / 1M tokens
  57. "gpt-image-1": 2.5, // $5 / 1M tokens
  58. "o1": 7.5, // $15 / 1M tokens
  59. "o1-2024-12-17": 7.5, // $15 / 1M tokens
  60. "o1-preview": 7.5, // $15 / 1M tokens
  61. "o1-preview-2024-09-12": 7.5, // $15 / 1M tokens
  62. "o1-mini": 0.55, // $1.1 / 1M tokens
  63. "o1-mini-2024-09-12": 0.55, // $1.1 / 1M tokens
  64. "o1-pro": 75.0, // $150 / 1M tokens
  65. "o1-pro-2025-03-19": 75.0, // $150 / 1M tokens
  66. "o3-mini": 0.55,
  67. "o3-mini-2025-01-31": 0.55,
  68. "o3-mini-high": 0.55,
  69. "o3-mini-2025-01-31-high": 0.55,
  70. "o3-mini-low": 0.55,
  71. "o3-mini-2025-01-31-low": 0.55,
  72. "o3-mini-medium": 0.55,
  73. "o3-mini-2025-01-31-medium": 0.55,
  74. "o3": 1.0, // $2 / 1M tokens
  75. "o3-2025-04-16": 1.0, // $2 / 1M tokens
  76. "o3-pro": 10.0, // $20 / 1M tokens
  77. "o3-pro-2025-06-10": 10.0, // $20 / 1M tokens
  78. "o3-deep-research": 5.0, // $10 / 1M tokens
  79. "o3-deep-research-2025-06-26": 5.0, // $10 / 1M tokens
  80. "o4-mini": 0.55, // $1.1 / 1M tokens
  81. "o4-mini-2025-04-16": 0.55, // $1.1 / 1M tokens
  82. "o4-mini-deep-research": 1.0, // $2 / 1M tokens
  83. "o4-mini-deep-research-2025-06-26": 1.0, // $2 / 1M tokens
  84. "gpt-4o-mini": 0.075,
  85. "gpt-4o-mini-2024-07-18": 0.075,
  86. "gpt-4-turbo": 5, // $0.01 / 1K tokens
  87. "gpt-4-turbo-2024-04-09": 5, // $0.01 / 1K tokens
  88. "gpt-4.5-preview": 37.5,
  89. "gpt-4.5-preview-2025-02-27": 37.5,
  90. "gpt-5": 0.625,
  91. "gpt-5-2025-08-07": 0.625,
  92. "gpt-5-chat-latest": 0.625,
  93. "gpt-5-mini": 0.125,
  94. "gpt-5-mini-2025-08-07": 0.125,
  95. "gpt-5-nano": 0.025,
  96. "gpt-5-nano-2025-08-07": 0.025,
  97. //"gpt-3.5-turbo-0301": 0.75, //deprecated
  98. "gpt-3.5-turbo": 0.25,
  99. "gpt-3.5-turbo-0613": 0.75,
  100. "gpt-3.5-turbo-16k": 1.5, // $0.003 / 1K tokens
  101. "gpt-3.5-turbo-16k-0613": 1.5,
  102. "gpt-3.5-turbo-instruct": 0.75, // $0.0015 / 1K tokens
  103. "gpt-3.5-turbo-1106": 0.5, // $0.001 / 1K tokens
  104. "gpt-3.5-turbo-0125": 0.25,
  105. "babbage-002": 0.2, // $0.0004 / 1K tokens
  106. "davinci-002": 1, // $0.002 / 1K tokens
  107. "text-ada-001": 0.2,
  108. "text-babbage-001": 0.25,
  109. "text-curie-001": 1,
  110. //"text-davinci-002": 10,
  111. //"text-davinci-003": 10,
  112. "text-davinci-edit-001": 10,
  113. "code-davinci-edit-001": 10,
  114. "whisper-1": 15, // $0.006 / minute -> $0.006 / 150 words -> $0.006 / 200 tokens -> $0.03 / 1k tokens
  115. "tts-1": 7.5, // 1k characters -> $0.015
  116. "tts-1-1106": 7.5, // 1k characters -> $0.015
  117. "tts-1-hd": 15, // 1k characters -> $0.03
  118. "tts-1-hd-1106": 15, // 1k characters -> $0.03
  119. "davinci": 10,
  120. "curie": 10,
  121. "babbage": 10,
  122. "ada": 10,
  123. "text-embedding-3-small": 0.01,
  124. "text-embedding-3-large": 0.065,
  125. "text-embedding-ada-002": 0.05,
  126. "text-search-ada-doc-001": 10,
  127. "text-moderation-stable": 0.1,
  128. "text-moderation-latest": 0.1,
  129. "claude-instant-1": 0.4, // $0.8 / 1M tokens
  130. "claude-2.0": 4, // $8 / 1M tokens
  131. "claude-2.1": 4, // $8 / 1M tokens
  132. "claude-3-haiku-20240307": 0.125, // $0.25 / 1M tokens
  133. "claude-3-5-haiku-20241022": 0.5, // $1 / 1M tokens
  134. "claude-haiku-4-5-20251001": 0.5, // $1 / 1M tokens
  135. "claude-3-sonnet-20240229": 1.5, // $3 / 1M tokens
  136. "claude-3-5-sonnet-20240620": 1.5,
  137. "claude-3-5-sonnet-20241022": 1.5,
  138. "claude-3-7-sonnet-20250219": 1.5,
  139. "claude-3-7-sonnet-20250219-thinking": 1.5,
  140. "claude-sonnet-4-20250514": 1.5,
  141. "claude-sonnet-4-5-20250929": 1.5,
  142. "claude-opus-4-5-20251101": 2.5,
  143. "claude-3-opus-20240229": 7.5, // $15 / 1M tokens
  144. "claude-opus-4-20250514": 7.5,
  145. "claude-opus-4-1-20250805": 7.5,
  146. "ERNIE-4.0-8K": 0.120 * RMB,
  147. "ERNIE-3.5-8K": 0.012 * RMB,
  148. "ERNIE-3.5-8K-0205": 0.024 * RMB,
  149. "ERNIE-3.5-8K-1222": 0.012 * RMB,
  150. "ERNIE-Bot-8K": 0.024 * RMB,
  151. "ERNIE-3.5-4K-0205": 0.012 * RMB,
  152. "ERNIE-Speed-8K": 0.004 * RMB,
  153. "ERNIE-Speed-128K": 0.004 * RMB,
  154. "ERNIE-Lite-8K-0922": 0.008 * RMB,
  155. "ERNIE-Lite-8K-0308": 0.003 * RMB,
  156. "ERNIE-Tiny-8K": 0.001 * RMB,
  157. "BLOOMZ-7B": 0.004 * RMB,
  158. "Embedding-V1": 0.002 * RMB,
  159. "bge-large-zh": 0.002 * RMB,
  160. "bge-large-en": 0.002 * RMB,
  161. "tao-8k": 0.002 * RMB,
  162. "PaLM-2": 1,
  163. "gemini-1.5-pro-latest": 1.25, // $3.5 / 1M tokens
  164. "gemini-1.5-flash-latest": 0.075,
  165. "gemini-2.0-flash": 0.05,
  166. "gemini-2.5-pro-exp-03-25": 0.625,
  167. "gemini-2.5-pro-preview-03-25": 0.625,
  168. "gemini-2.5-pro": 0.625,
  169. "gemini-2.5-flash-preview-04-17": 0.075,
  170. "gemini-2.5-flash-preview-04-17-thinking": 0.075,
  171. "gemini-2.5-flash-preview-04-17-nothinking": 0.075,
  172. "gemini-2.5-flash-preview-05-20": 0.075,
  173. "gemini-2.5-flash-preview-05-20-thinking": 0.075,
  174. "gemini-2.5-flash-preview-05-20-nothinking": 0.075,
  175. "gemini-2.5-flash-thinking-*": 0.075, // 用于为后续所有2.5 flash thinking budget 模型设置默认倍率
  176. "gemini-2.5-pro-thinking-*": 0.625, // 用于为后续所有2.5 pro thinking budget 模型设置默认倍率
  177. "gemini-2.5-flash-lite-preview-thinking-*": 0.05,
  178. "gemini-2.5-flash-lite-preview-06-17": 0.05,
  179. "gemini-2.5-flash": 0.15,
  180. "gemini-robotics-er-1.5-preview": 0.15,
  181. "gemini-embedding-001": 0.075,
  182. "text-embedding-004": 0.001,
  183. "chatglm_turbo": 0.3572, // ¥0.005 / 1k tokens
  184. "chatglm_pro": 0.7143, // ¥0.01 / 1k tokens
  185. "chatglm_std": 0.3572, // ¥0.005 / 1k tokens
  186. "chatglm_lite": 0.1429, // ¥0.002 / 1k tokens
  187. "glm-4": 7.143, // ¥0.1 / 1k tokens
  188. "glm-4v": 0.05 * RMB, // ¥0.05 / 1k tokens
  189. "glm-4-alltools": 0.1 * RMB, // ¥0.1 / 1k tokens
  190. "glm-3-turbo": 0.3572,
  191. "glm-4-plus": 0.05 * RMB,
  192. "glm-4-0520": 0.1 * RMB,
  193. "glm-4-air": 0.001 * RMB,
  194. "glm-4-airx": 0.01 * RMB,
  195. "glm-4-long": 0.001 * RMB,
  196. "glm-4-flash": 0,
  197. "glm-4v-plus": 0.01 * RMB,
  198. "qwen-turbo": 0.8572, // ¥0.012 / 1k tokens
  199. "qwen-plus": 10, // ¥0.14 / 1k tokens
  200. "text-embedding-v1": 0.05, // ¥0.0007 / 1k tokens
  201. "SparkDesk-v1.1": 1.2858, // ¥0.018 / 1k tokens
  202. "SparkDesk-v2.1": 1.2858, // ¥0.018 / 1k tokens
  203. "SparkDesk-v3.1": 1.2858, // ¥0.018 / 1k tokens
  204. "SparkDesk-v3.5": 1.2858, // ¥0.018 / 1k tokens
  205. "SparkDesk-v4.0": 1.2858,
  206. "360GPT_S2_V9": 0.8572, // ¥0.012 / 1k tokens
  207. "360gpt-turbo": 0.0858, // ¥0.0012 / 1k tokens
  208. "360gpt-turbo-responsibility-8k": 0.8572, // ¥0.012 / 1k tokens
  209. "360gpt-pro": 0.8572, // ¥0.012 / 1k tokens
  210. "360gpt2-pro": 0.8572, // ¥0.012 / 1k tokens
  211. "embedding-bert-512-v1": 0.0715, // ¥0.001 / 1k tokens
  212. "embedding_s1_v1": 0.0715, // ¥0.001 / 1k tokens
  213. "semantic_similarity_s1_v1": 0.0715, // ¥0.001 / 1k tokens
  214. "hunyuan": 7.143, // ¥0.1 / 1k tokens // https://cloud.tencent.com/document/product/1729/97731#e0e6be58-60c8-469f-bdeb-6c264ce3b4d0
  215. // https://platform.lingyiwanwu.com/docs#-计费单元
  216. // 已经按照 7.2 来换算美元价格
  217. "yi-34b-chat-0205": 0.18,
  218. "yi-34b-chat-200k": 0.864,
  219. "yi-vl-plus": 0.432,
  220. "yi-large": 20.0 / 1000 * RMB,
  221. "yi-medium": 2.5 / 1000 * RMB,
  222. "yi-vision": 6.0 / 1000 * RMB,
  223. "yi-medium-200k": 12.0 / 1000 * RMB,
  224. "yi-spark": 1.0 / 1000 * RMB,
  225. "yi-large-rag": 25.0 / 1000 * RMB,
  226. "yi-large-turbo": 12.0 / 1000 * RMB,
  227. "yi-large-preview": 20.0 / 1000 * RMB,
  228. "yi-large-rag-preview": 25.0 / 1000 * RMB,
  229. "command": 0.5,
  230. "command-nightly": 0.5,
  231. "command-light": 0.5,
  232. "command-light-nightly": 0.5,
  233. "command-r": 0.25,
  234. "command-r-plus": 1.5,
  235. "command-r-08-2024": 0.075,
  236. "command-r-plus-08-2024": 1.25,
  237. "deepseek-chat": 0.27 / 2,
  238. "deepseek-coder": 0.27 / 2,
  239. "deepseek-reasoner": 0.55 / 2, // 0.55 / 1k tokens
  240. // Perplexity online 模型对搜索额外收费,有需要应自行调整,此处不计入搜索费用
  241. "llama-3-sonar-small-32k-chat": 0.2 / 1000 * USD,
  242. "llama-3-sonar-small-32k-online": 0.2 / 1000 * USD,
  243. "llama-3-sonar-large-32k-chat": 1 / 1000 * USD,
  244. "llama-3-sonar-large-32k-online": 1 / 1000 * USD,
  245. // grok
  246. "grok-3-beta": 1.5,
  247. "grok-3-mini-beta": 0.15,
  248. "grok-2": 1,
  249. "grok-2-vision": 1,
  250. "grok-beta": 2.5,
  251. "grok-vision-beta": 2.5,
  252. "grok-3-fast-beta": 2.5,
  253. "grok-3-mini-fast-beta": 0.3,
  254. // submodel
  255. "NousResearch/Hermes-4-405B-FP8": 0.8,
  256. "Qwen/Qwen3-235B-A22B-Thinking-2507": 0.6,
  257. "Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8": 0.8,
  258. "Qwen/Qwen3-235B-A22B-Instruct-2507": 0.3,
  259. "zai-org/GLM-4.5-FP8": 0.8,
  260. "openai/gpt-oss-120b": 0.5,
  261. "deepseek-ai/DeepSeek-R1-0528": 0.8,
  262. "deepseek-ai/DeepSeek-R1": 0.8,
  263. "deepseek-ai/DeepSeek-V3-0324": 0.8,
  264. "deepseek-ai/DeepSeek-V3.1": 0.8,
  265. }
  266. var defaultModelPrice = map[string]float64{
  267. "suno_music": 0.1,
  268. "suno_lyrics": 0.01,
  269. "dall-e-3": 0.04,
  270. "imagen-3.0-generate-002": 0.03,
  271. "black-forest-labs/flux-1.1-pro": 0.04,
  272. "gpt-4-gizmo-*": 0.1,
  273. "mj_video": 0.8,
  274. "mj_imagine": 0.1,
  275. "mj_edits": 0.1,
  276. "mj_variation": 0.1,
  277. "mj_reroll": 0.1,
  278. "mj_blend": 0.1,
  279. "mj_modal": 0.1,
  280. "mj_zoom": 0.1,
  281. "mj_shorten": 0.1,
  282. "mj_high_variation": 0.1,
  283. "mj_low_variation": 0.1,
  284. "mj_pan": 0.1,
  285. "mj_inpaint": 0,
  286. "mj_custom_zoom": 0,
  287. "mj_describe": 0.05,
  288. "mj_upscale": 0.05,
  289. "swap_face": 0.05,
  290. "mj_upload": 0.05,
  291. "sora-2": 0.3,
  292. "sora-2-pro": 0.5,
  293. "gpt-4o-mini-tts": 0.3,
  294. }
  295. var defaultAudioRatio = map[string]float64{
  296. "gpt-4o-audio-preview": 16,
  297. "gpt-4o-mini-audio-preview": 66.67,
  298. "gpt-4o-realtime-preview": 8,
  299. "gpt-4o-mini-realtime-preview": 16.67,
  300. "gpt-4o-mini-tts": 25,
  301. }
  302. var defaultAudioCompletionRatio = map[string]float64{
  303. "gpt-4o-realtime": 2,
  304. "gpt-4o-mini-realtime": 2,
  305. "gpt-4o-mini-tts": 1,
  306. "tts-1": 0,
  307. "tts-1-hd": 0,
  308. "tts-1-1106": 0,
  309. "tts-1-hd-1106": 0,
  310. }
  311. var (
  312. modelPriceMap map[string]float64 = nil
  313. modelPriceMapMutex = sync.RWMutex{}
  314. )
  315. var (
  316. modelRatioMap map[string]float64 = nil
  317. modelRatioMapMutex = sync.RWMutex{}
  318. )
  319. var (
  320. CompletionRatio map[string]float64 = nil
  321. CompletionRatioMutex = sync.RWMutex{}
  322. )
  323. var defaultCompletionRatio = map[string]float64{
  324. "gpt-4-gizmo-*": 2,
  325. "gpt-4o-gizmo-*": 3,
  326. "gpt-4-all": 2,
  327. "gpt-image-1": 8,
  328. }
  329. // InitRatioSettings initializes all model related settings maps
  330. func InitRatioSettings() {
  331. // Initialize modelPriceMap
  332. modelPriceMapMutex.Lock()
  333. modelPriceMap = defaultModelPrice
  334. modelPriceMapMutex.Unlock()
  335. // Initialize modelRatioMap
  336. modelRatioMapMutex.Lock()
  337. modelRatioMap = defaultModelRatio
  338. modelRatioMapMutex.Unlock()
  339. // Initialize CompletionRatio
  340. CompletionRatioMutex.Lock()
  341. CompletionRatio = defaultCompletionRatio
  342. CompletionRatioMutex.Unlock()
  343. // Initialize cacheRatioMap
  344. cacheRatioMapMutex.Lock()
  345. cacheRatioMap = defaultCacheRatio
  346. cacheRatioMapMutex.Unlock()
  347. // initialize imageRatioMap
  348. imageRatioMapMutex.Lock()
  349. imageRatioMap = defaultImageRatio
  350. imageRatioMapMutex.Unlock()
  351. // initialize audioRatioMap
  352. audioRatioMapMutex.Lock()
  353. audioRatioMap = defaultAudioRatio
  354. audioRatioMapMutex.Unlock()
  355. // initialize audioCompletionRatioMap
  356. audioCompletionRatioMapMutex.Lock()
  357. audioCompletionRatioMap = defaultAudioCompletionRatio
  358. audioCompletionRatioMapMutex.Unlock()
  359. }
  360. func GetModelPriceMap() map[string]float64 {
  361. modelPriceMapMutex.RLock()
  362. defer modelPriceMapMutex.RUnlock()
  363. return modelPriceMap
  364. }
  365. func ModelPrice2JSONString() string {
  366. modelPriceMapMutex.RLock()
  367. defer modelPriceMapMutex.RUnlock()
  368. jsonBytes, err := common.Marshal(modelPriceMap)
  369. if err != nil {
  370. common.SysError("error marshalling model price: " + err.Error())
  371. }
  372. return string(jsonBytes)
  373. }
  374. func UpdateModelPriceByJSONString(jsonStr string) error {
  375. modelPriceMapMutex.Lock()
  376. defer modelPriceMapMutex.Unlock()
  377. modelPriceMap = make(map[string]float64)
  378. err := json.Unmarshal([]byte(jsonStr), &modelPriceMap)
  379. if err == nil {
  380. InvalidateExposedDataCache()
  381. }
  382. return err
  383. }
  384. // GetModelPrice 返回模型的价格,如果模型不存在则返回-1,false
  385. func GetModelPrice(name string, printErr bool) (float64, bool) {
  386. modelPriceMapMutex.RLock()
  387. defer modelPriceMapMutex.RUnlock()
  388. name = FormatMatchingModelName(name)
  389. price, ok := modelPriceMap[name]
  390. if !ok {
  391. if printErr {
  392. common.SysError("model price not found: " + name)
  393. }
  394. return -1, false
  395. }
  396. return price, true
  397. }
  398. func UpdateModelRatioByJSONString(jsonStr string) error {
  399. modelRatioMapMutex.Lock()
  400. defer modelRatioMapMutex.Unlock()
  401. modelRatioMap = make(map[string]float64)
  402. err := common.Unmarshal([]byte(jsonStr), &modelRatioMap)
  403. if err == nil {
  404. InvalidateExposedDataCache()
  405. }
  406. return err
  407. }
  408. // 处理带有思考预算的模型名称,方便统一定价
  409. func handleThinkingBudgetModel(name, prefix, wildcard string) string {
  410. if strings.HasPrefix(name, prefix) && strings.Contains(name, "-thinking-") {
  411. return wildcard
  412. }
  413. return name
  414. }
  415. func GetModelRatio(name string) (float64, bool, string) {
  416. modelRatioMapMutex.RLock()
  417. defer modelRatioMapMutex.RUnlock()
  418. name = FormatMatchingModelName(name)
  419. ratio, ok := modelRatioMap[name]
  420. if !ok {
  421. return 37.5, operation_setting.SelfUseModeEnabled, name
  422. }
  423. return ratio, true, name
  424. }
  425. func DefaultModelRatio2JSONString() string {
  426. jsonBytes, err := common.Marshal(defaultModelRatio)
  427. if err != nil {
  428. common.SysError("error marshalling model ratio: " + err.Error())
  429. }
  430. return string(jsonBytes)
  431. }
  432. func GetDefaultModelRatioMap() map[string]float64 {
  433. return defaultModelRatio
  434. }
  435. func GetDefaultModelPriceMap() map[string]float64 {
  436. return defaultModelPrice
  437. }
  438. func GetDefaultImageRatioMap() map[string]float64 {
  439. return defaultImageRatio
  440. }
  441. func GetDefaultAudioRatioMap() map[string]float64 {
  442. return defaultAudioRatio
  443. }
  444. func GetDefaultAudioCompletionRatioMap() map[string]float64 {
  445. return defaultAudioCompletionRatio
  446. }
  447. func GetCompletionRatioMap() map[string]float64 {
  448. CompletionRatioMutex.RLock()
  449. defer CompletionRatioMutex.RUnlock()
  450. return CompletionRatio
  451. }
  452. func CompletionRatio2JSONString() string {
  453. CompletionRatioMutex.RLock()
  454. defer CompletionRatioMutex.RUnlock()
  455. jsonBytes, err := json.Marshal(CompletionRatio)
  456. if err != nil {
  457. common.SysError("error marshalling completion ratio: " + err.Error())
  458. }
  459. return string(jsonBytes)
  460. }
  461. func UpdateCompletionRatioByJSONString(jsonStr string) error {
  462. CompletionRatioMutex.Lock()
  463. defer CompletionRatioMutex.Unlock()
  464. CompletionRatio = make(map[string]float64)
  465. err := common.Unmarshal([]byte(jsonStr), &CompletionRatio)
  466. if err == nil {
  467. InvalidateExposedDataCache()
  468. }
  469. return err
  470. }
  471. func GetCompletionRatio(name string) float64 {
  472. CompletionRatioMutex.RLock()
  473. defer CompletionRatioMutex.RUnlock()
  474. name = FormatMatchingModelName(name)
  475. if strings.Contains(name, "/") {
  476. if ratio, ok := CompletionRatio[name]; ok {
  477. return ratio
  478. }
  479. }
  480. hardCodedRatio, contain := getHardcodedCompletionModelRatio(name)
  481. if contain {
  482. return hardCodedRatio
  483. }
  484. if ratio, ok := CompletionRatio[name]; ok {
  485. return ratio
  486. }
  487. return hardCodedRatio
  488. }
  489. func getHardcodedCompletionModelRatio(name string) (float64, bool) {
  490. isReservedModel := strings.HasSuffix(name, "-all") || strings.HasSuffix(name, "-gizmo-*")
  491. if isReservedModel {
  492. return 2, false
  493. }
  494. if strings.HasPrefix(name, "gpt-") {
  495. if strings.HasPrefix(name, "gpt-4o") {
  496. if name == "gpt-4o-2024-05-13" {
  497. return 3, true
  498. }
  499. if strings.HasPrefix(name, "gpt-4o-mini-tts") {
  500. return 20, false
  501. }
  502. return 4, false
  503. }
  504. // gpt-5 匹配
  505. if strings.HasPrefix(name, "gpt-5") {
  506. return 8, true
  507. }
  508. // gpt-4.5-preview匹配
  509. if strings.HasPrefix(name, "gpt-4.5-preview") {
  510. return 2, true
  511. }
  512. if strings.HasPrefix(name, "gpt-4-turbo") || strings.HasSuffix(name, "gpt-4-1106") || strings.HasSuffix(name, "gpt-4-1105") {
  513. return 3, true
  514. }
  515. // 没有特殊标记的 gpt-4 模型默认倍率为 2
  516. return 2, false
  517. }
  518. if strings.HasPrefix(name, "o1") || strings.HasPrefix(name, "o3") {
  519. return 4, true
  520. }
  521. if name == "chatgpt-4o-latest" {
  522. return 3, true
  523. }
  524. if strings.Contains(name, "claude-3") {
  525. return 5, true
  526. } else if strings.Contains(name, "claude-sonnet-4") || strings.Contains(name, "claude-opus-4") || strings.Contains(name, "claude-haiku-4") {
  527. return 5, true
  528. } else if strings.Contains(name, "claude-instant-1") || strings.Contains(name, "claude-2") {
  529. return 3, true
  530. }
  531. if strings.HasPrefix(name, "gpt-3.5") {
  532. if name == "gpt-3.5-turbo" || strings.HasSuffix(name, "0125") {
  533. // https://openai.com/blog/new-embedding-models-and-api-updates
  534. // Updated GPT-3.5 Turbo model and lower pricing
  535. return 3, true
  536. }
  537. if strings.HasSuffix(name, "1106") {
  538. return 2, true
  539. }
  540. return 4.0 / 3.0, true
  541. }
  542. if strings.HasPrefix(name, "mistral-") {
  543. return 3, true
  544. }
  545. if strings.HasPrefix(name, "gemini-") {
  546. if strings.HasPrefix(name, "gemini-1.5") {
  547. return 4, true
  548. } else if strings.HasPrefix(name, "gemini-2.0") {
  549. return 4, true
  550. } else if strings.HasPrefix(name, "gemini-2.5-pro") { // 移除preview来增加兼容性,这里假设正式版的倍率和preview一致
  551. return 8, false
  552. } else if strings.HasPrefix(name, "gemini-2.5-flash") { // 处理不同的flash模型倍率
  553. if strings.HasPrefix(name, "gemini-2.5-flash-preview") {
  554. if strings.HasSuffix(name, "-nothinking") {
  555. return 4, false
  556. }
  557. return 3.5 / 0.15, false
  558. }
  559. if strings.HasPrefix(name, "gemini-2.5-flash-lite") {
  560. return 4, false
  561. }
  562. return 2.5 / 0.3, false
  563. } else if strings.HasPrefix(name, "gemini-robotics-er-1.5") {
  564. return 2.5 / 0.3, false
  565. } else if strings.HasPrefix(name, "gemini-3-pro") {
  566. if strings.HasPrefix(name, "gemini-3-pro-image") {
  567. return 60, false
  568. }
  569. return 6, false
  570. }
  571. return 4, false
  572. }
  573. if strings.HasPrefix(name, "command") {
  574. switch name {
  575. case "command-r":
  576. return 3, true
  577. case "command-r-plus":
  578. return 5, true
  579. case "command-r-08-2024":
  580. return 4, true
  581. case "command-r-plus-08-2024":
  582. return 4, true
  583. default:
  584. return 4, false
  585. }
  586. }
  587. // hint 只给官方上4倍率,由于开源模型供应商自行定价,不对其进行补全倍率进行强制对齐
  588. if strings.HasPrefix(name, "ERNIE-Speed-") {
  589. return 2, true
  590. } else if strings.HasPrefix(name, "ERNIE-Lite-") {
  591. return 2, true
  592. } else if strings.HasPrefix(name, "ERNIE-Character") {
  593. return 2, true
  594. } else if strings.HasPrefix(name, "ERNIE-Functions") {
  595. return 2, true
  596. }
  597. switch name {
  598. case "llama2-70b-4096":
  599. return 0.8 / 0.64, true
  600. case "llama3-8b-8192":
  601. return 2, true
  602. case "llama3-70b-8192":
  603. return 0.79 / 0.59, true
  604. }
  605. return 1, false
  606. }
  607. func GetAudioRatio(name string) float64 {
  608. audioRatioMapMutex.RLock()
  609. defer audioRatioMapMutex.RUnlock()
  610. name = FormatMatchingModelName(name)
  611. if ratio, ok := audioRatioMap[name]; ok {
  612. return ratio
  613. }
  614. return 1
  615. }
  616. func GetAudioCompletionRatio(name string) float64 {
  617. audioCompletionRatioMapMutex.RLock()
  618. defer audioCompletionRatioMapMutex.RUnlock()
  619. name = FormatMatchingModelName(name)
  620. if ratio, ok := audioCompletionRatioMap[name]; ok {
  621. return ratio
  622. }
  623. return 1
  624. }
  625. func ContainsAudioRatio(name string) bool {
  626. audioRatioMapMutex.RLock()
  627. defer audioRatioMapMutex.RUnlock()
  628. name = FormatMatchingModelName(name)
  629. _, ok := audioRatioMap[name]
  630. return ok
  631. }
  632. func ContainsAudioCompletionRatio(name string) bool {
  633. audioCompletionRatioMapMutex.RLock()
  634. defer audioCompletionRatioMapMutex.RUnlock()
  635. name = FormatMatchingModelName(name)
  636. _, ok := audioCompletionRatioMap[name]
  637. return ok
  638. }
  639. func ModelRatio2JSONString() string {
  640. modelRatioMapMutex.RLock()
  641. defer modelRatioMapMutex.RUnlock()
  642. jsonBytes, err := common.Marshal(modelRatioMap)
  643. if err != nil {
  644. common.SysError("error marshalling model ratio: " + err.Error())
  645. }
  646. return string(jsonBytes)
  647. }
  648. var defaultImageRatio = map[string]float64{
  649. "gpt-image-1": 2,
  650. }
  651. var imageRatioMap map[string]float64
  652. var imageRatioMapMutex sync.RWMutex
  653. var (
  654. audioRatioMap map[string]float64 = nil
  655. audioRatioMapMutex = sync.RWMutex{}
  656. )
  657. var (
  658. audioCompletionRatioMap map[string]float64 = nil
  659. audioCompletionRatioMapMutex = sync.RWMutex{}
  660. )
  661. func ImageRatio2JSONString() string {
  662. imageRatioMapMutex.RLock()
  663. defer imageRatioMapMutex.RUnlock()
  664. jsonBytes, err := common.Marshal(imageRatioMap)
  665. if err != nil {
  666. common.SysError("error marshalling cache ratio: " + err.Error())
  667. }
  668. return string(jsonBytes)
  669. }
  670. func UpdateImageRatioByJSONString(jsonStr string) error {
  671. imageRatioMapMutex.Lock()
  672. defer imageRatioMapMutex.Unlock()
  673. imageRatioMap = make(map[string]float64)
  674. return common.Unmarshal([]byte(jsonStr), &imageRatioMap)
  675. }
  676. func GetImageRatio(name string) (float64, bool) {
  677. imageRatioMapMutex.RLock()
  678. defer imageRatioMapMutex.RUnlock()
  679. ratio, ok := imageRatioMap[name]
  680. if !ok {
  681. return 1, false // Default to 1 if not found
  682. }
  683. return ratio, true
  684. }
  685. func AudioRatio2JSONString() string {
  686. audioRatioMapMutex.RLock()
  687. defer audioRatioMapMutex.RUnlock()
  688. jsonBytes, err := common.Marshal(audioRatioMap)
  689. if err != nil {
  690. common.SysError("error marshalling audio ratio: " + err.Error())
  691. }
  692. return string(jsonBytes)
  693. }
  694. func UpdateAudioRatioByJSONString(jsonStr string) error {
  695. tmp := make(map[string]float64)
  696. if err := common.Unmarshal([]byte(jsonStr), &tmp); err != nil {
  697. return err
  698. }
  699. audioRatioMapMutex.Lock()
  700. audioRatioMap = tmp
  701. audioRatioMapMutex.Unlock()
  702. InvalidateExposedDataCache()
  703. return nil
  704. }
  705. func AudioCompletionRatio2JSONString() string {
  706. audioCompletionRatioMapMutex.RLock()
  707. defer audioCompletionRatioMapMutex.RUnlock()
  708. jsonBytes, err := common.Marshal(audioCompletionRatioMap)
  709. if err != nil {
  710. common.SysError("error marshalling audio completion ratio: " + err.Error())
  711. }
  712. return string(jsonBytes)
  713. }
  714. func UpdateAudioCompletionRatioByJSONString(jsonStr string) error {
  715. tmp := make(map[string]float64)
  716. if err := common.Unmarshal([]byte(jsonStr), &tmp); err != nil {
  717. return err
  718. }
  719. audioCompletionRatioMapMutex.Lock()
  720. audioCompletionRatioMap = tmp
  721. audioCompletionRatioMapMutex.Unlock()
  722. InvalidateExposedDataCache()
  723. return nil
  724. }
  725. func GetModelRatioCopy() map[string]float64 {
  726. modelRatioMapMutex.RLock()
  727. defer modelRatioMapMutex.RUnlock()
  728. copyMap := make(map[string]float64, len(modelRatioMap))
  729. for k, v := range modelRatioMap {
  730. copyMap[k] = v
  731. }
  732. return copyMap
  733. }
  734. func GetModelPriceCopy() map[string]float64 {
  735. modelPriceMapMutex.RLock()
  736. defer modelPriceMapMutex.RUnlock()
  737. copyMap := make(map[string]float64, len(modelPriceMap))
  738. for k, v := range modelPriceMap {
  739. copyMap[k] = v
  740. }
  741. return copyMap
  742. }
  743. func GetCompletionRatioCopy() map[string]float64 {
  744. CompletionRatioMutex.RLock()
  745. defer CompletionRatioMutex.RUnlock()
  746. copyMap := make(map[string]float64, len(CompletionRatio))
  747. for k, v := range CompletionRatio {
  748. copyMap[k] = v
  749. }
  750. return copyMap
  751. }
  752. // 转换模型名,减少渠道必须配置各种带参数模型
  753. func FormatMatchingModelName(name string) string {
  754. if strings.HasPrefix(name, "gemini-2.5-flash-lite") {
  755. name = handleThinkingBudgetModel(name, "gemini-2.5-flash-lite", "gemini-2.5-flash-lite-thinking-*")
  756. } else if strings.HasPrefix(name, "gemini-2.5-flash") {
  757. name = handleThinkingBudgetModel(name, "gemini-2.5-flash", "gemini-2.5-flash-thinking-*")
  758. } else if strings.HasPrefix(name, "gemini-2.5-pro") {
  759. name = handleThinkingBudgetModel(name, "gemini-2.5-pro", "gemini-2.5-pro-thinking-*")
  760. }
  761. if strings.HasPrefix(name, "gpt-4-gizmo") {
  762. name = "gpt-4-gizmo-*"
  763. }
  764. if strings.HasPrefix(name, "gpt-4o-gizmo") {
  765. name = "gpt-4o-gizmo-*"
  766. }
  767. return name
  768. }
  769. // result: 倍率or价格, usePrice, exist
  770. func GetModelRatioOrPrice(model string) (float64, bool, bool) { // price or ratio
  771. price, usePrice := GetModelPrice(model, false)
  772. if usePrice {
  773. return price, true, true
  774. }
  775. modelRatio, success, _ := GetModelRatio(model)
  776. if success {
  777. return modelRatio, false, true
  778. }
  779. return 37.5, false, false
  780. }