model-ratio.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package common
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "time"
  6. )
  7. // ModelRatio
  8. // https://platform.openai.com/docs/models/model-endpoint-compatibility
  9. // https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Blfmc9dlf
  10. // https://openai.com/pricing
  11. // TODO: when a new api is enabled, check the pricing here
  12. // 1 === $0.002 / 1K tokens
  13. // 1 === ¥0.014 / 1k tokens
  14. var DefaultModelRatio = map[string]float64{
  15. //"midjourney": 50,
  16. "gpt-4-gizmo-*": 15,
  17. "gpt-4": 15,
  18. "gpt-4-0314": 15,
  19. "gpt-4-0613": 15,
  20. "gpt-4-32k": 30,
  21. "gpt-4-32k-0314": 30,
  22. "gpt-4-32k-0613": 30,
  23. "gpt-4-1106-preview": 5, // $0.01 / 1K tokens
  24. "gpt-4-0125-preview": 5, // $0.01 / 1K tokens
  25. "gpt-4-turbo-preview": 5, // $0.01 / 1K tokens
  26. "gpt-4-vision-preview": 5, // $0.01 / 1K tokens
  27. "gpt-4-1106-vision-preview": 5, // $0.01 / 1K tokens
  28. "gpt-3.5-turbo": 0.75, // $0.0015 / 1K tokens
  29. "gpt-3.5-turbo-0301": 0.75,
  30. "gpt-3.5-turbo-0613": 0.75,
  31. "gpt-3.5-turbo-16k": 1.5, // $0.003 / 1K tokens
  32. "gpt-3.5-turbo-16k-0613": 1.5,
  33. "gpt-3.5-turbo-instruct": 0.75, // $0.0015 / 1K tokens
  34. "gpt-3.5-turbo-1106": 0.5, // $0.001 / 1K tokens
  35. "gpt-3.5-turbo-0125": 0.25,
  36. "babbage-002": 0.2, // $0.0004 / 1K tokens
  37. "davinci-002": 1, // $0.002 / 1K tokens
  38. "text-ada-001": 0.2,
  39. "text-babbage-001": 0.25,
  40. "text-curie-001": 1,
  41. "text-davinci-002": 10,
  42. "text-davinci-003": 10,
  43. "text-davinci-edit-001": 10,
  44. "code-davinci-edit-001": 10,
  45. "whisper-1": 15, // $0.006 / minute -> $0.006 / 150 words -> $0.006 / 200 tokens -> $0.03 / 1k tokens
  46. "tts-1": 7.5, // 1k characters -> $0.015
  47. "tts-1-1106": 7.5, // 1k characters -> $0.015
  48. "tts-1-hd": 15, // 1k characters -> $0.03
  49. "tts-1-hd-1106": 15, // 1k characters -> $0.03
  50. "davinci": 10,
  51. "curie": 10,
  52. "babbage": 10,
  53. "ada": 10,
  54. "text-embedding-3-small": 0.01,
  55. "text-embedding-3-large": 0.065,
  56. "text-embedding-ada-002": 0.05,
  57. "text-search-ada-doc-001": 10,
  58. "text-moderation-stable": 0.1,
  59. "text-moderation-latest": 0.1,
  60. "dall-e-2": 8,
  61. "dall-e-3": 16,
  62. "claude-instant-1": 0.4, // $0.8 / 1M tokens
  63. "claude-2.0": 4, // $8 / 1M tokens
  64. "claude-2.1": 4, // $8 / 1M tokens
  65. "claude-3-haiku-20240307": 0.125, // $0.25 / 1M tokens
  66. "claude-3-sonnet-20240229": 1.5, // $3 / 1M tokens
  67. "claude-3-opus-20240229": 7.5, // $15 / 1M tokens
  68. "ERNIE-Bot": 0.8572, // ¥0.012 / 1k tokens
  69. "ERNIE-Bot-turbo": 0.5715, // ¥0.008 / 1k tokens
  70. "ERNIE-Bot-4": 8.572, // ¥0.12 / 1k tokens
  71. "Embedding-V1": 0.1429, // ¥0.002 / 1k tokens
  72. "PaLM-2": 1,
  73. "gemini-pro": 1, // $0.00025 / 1k characters -> $0.001 / 1k tokens
  74. "gemini-pro-vision": 1, // $0.00025 / 1k characters -> $0.001 / 1k tokens
  75. "chatglm_turbo": 0.3572, // ¥0.005 / 1k tokens
  76. "chatglm_pro": 0.7143, // ¥0.01 / 1k tokens
  77. "chatglm_std": 0.3572, // ¥0.005 / 1k tokens
  78. "chatglm_lite": 0.1429, // ¥0.002 / 1k tokens
  79. "glm-4": 7.143, // ¥0.1 / 1k tokens
  80. "glm-4v": 7.143, // ¥0.1 / 1k tokens
  81. "glm-3-turbo": 0.3572,
  82. "qwen-turbo": 0.8572, // ¥0.012 / 1k tokens
  83. "qwen-plus": 10, // ¥0.14 / 1k tokens
  84. "text-embedding-v1": 0.05, // ¥0.0007 / 1k tokens
  85. "SparkDesk-v1.1": 1.2858, // ¥0.018 / 1k tokens
  86. "SparkDesk-v2.1": 1.2858, // ¥0.018 / 1k tokens
  87. "SparkDesk-v3.1": 1.2858, // ¥0.018 / 1k tokens
  88. "SparkDesk-v3.5": 1.2858, // ¥0.018 / 1k tokens
  89. "360GPT_S2_V9": 0.8572, // ¥0.012 / 1k tokens
  90. "embedding-bert-512-v1": 0.0715, // ¥0.001 / 1k tokens
  91. "embedding_s1_v1": 0.0715, // ¥0.001 / 1k tokens
  92. "semantic_similarity_s1_v1": 0.0715, // ¥0.001 / 1k tokens
  93. "hunyuan": 7.143, // ¥0.1 / 1k tokens // https://cloud.tencent.com/document/product/1729/97731#e0e6be58-60c8-469f-bdeb-6c264ce3b4d0
  94. }
  95. var DefaultModelPrice = map[string]float64{
  96. "gpt-4-gizmo-*": 0.1,
  97. "mj_imagine": 0.1,
  98. "mj_variation": 0.1,
  99. "mj_reroll": 0.1,
  100. "mj_blend": 0.1,
  101. "mj_modal": 0.1,
  102. "mj_zoom": 0.1,
  103. "mj_shorten": 0.1,
  104. "mj_high_variation": 0.1,
  105. "mj_low_variation": 0.1,
  106. "mj_pan": 0.1,
  107. "mj_inpaint": 0,
  108. "mj_custom_zoom": 0,
  109. "mj_describe": 0.05,
  110. "mj_upscale": 0.05,
  111. "swap_face": 0.05,
  112. }
  113. var ModelPrice = map[string]float64{}
  114. var ModelRatio = map[string]float64{}
  115. func ModelPrice2JSONString() string {
  116. if len(ModelPrice) == 0 {
  117. ModelPrice = DefaultModelPrice
  118. }
  119. jsonBytes, err := json.Marshal(ModelPrice)
  120. if err != nil {
  121. SysError("error marshalling model price: " + err.Error())
  122. }
  123. return string(jsonBytes)
  124. }
  125. func UpdateModelPriceByJSONString(jsonStr string) error {
  126. ModelPrice = make(map[string]float64)
  127. return json.Unmarshal([]byte(jsonStr), &ModelPrice)
  128. }
  129. func GetModelPrice(name string, printErr bool) float64 {
  130. if len(ModelPrice) == 0 {
  131. ModelPrice = DefaultModelPrice
  132. }
  133. if strings.HasPrefix(name, "gpt-4-gizmo") {
  134. name = "gpt-4-gizmo-*"
  135. }
  136. price, ok := ModelPrice[name]
  137. if !ok {
  138. if printErr {
  139. SysError("model price not found: " + name)
  140. }
  141. return -1
  142. }
  143. return price
  144. }
  145. func ModelRatio2JSONString() string {
  146. if len(ModelRatio) == 0 {
  147. ModelRatio = DefaultModelRatio
  148. }
  149. jsonBytes, err := json.Marshal(ModelRatio)
  150. if err != nil {
  151. SysError("error marshalling model ratio: " + err.Error())
  152. }
  153. return string(jsonBytes)
  154. }
  155. func UpdateModelRatioByJSONString(jsonStr string) error {
  156. ModelRatio = make(map[string]float64)
  157. return json.Unmarshal([]byte(jsonStr), &ModelRatio)
  158. }
  159. func GetModelRatio(name string) float64 {
  160. if len(ModelRatio) == 0 {
  161. ModelRatio = DefaultModelRatio
  162. }
  163. if strings.HasPrefix(name, "gpt-4-gizmo") {
  164. name = "gpt-4-gizmo-*"
  165. }
  166. ratio, ok := ModelRatio[name]
  167. if !ok {
  168. SysError("model ratio not found: " + name)
  169. return 30
  170. }
  171. return ratio
  172. }
  173. func GetCompletionRatio(name string) float64 {
  174. if strings.HasPrefix(name, "gpt-3.5") {
  175. if strings.HasSuffix(name, "0125") {
  176. return 3
  177. }
  178. if strings.HasSuffix(name, "1106") {
  179. return 2
  180. }
  181. if name == "gpt-3.5-turbo" || name == "gpt-3.5-turbo-16k" {
  182. // TODO: clear this after 2023-12-11
  183. now := time.Now()
  184. // https://platform.openai.com/docs/models/continuous-model-upgrades
  185. // if after 2023-12-11, use 2
  186. if now.After(time.Date(2023, 12, 11, 0, 0, 0, 0, time.UTC)) {
  187. return 2
  188. }
  189. }
  190. return 1.333333
  191. }
  192. if strings.HasPrefix(name, "gpt-4") {
  193. if strings.HasSuffix(name, "preview") {
  194. return 3
  195. }
  196. return 2
  197. }
  198. if strings.HasPrefix(name, "claude-instant-1") {
  199. return 3
  200. } else if strings.HasPrefix(name, "claude-2") {
  201. return 3
  202. } else if strings.HasPrefix(name, "claude-3") {
  203. return 5
  204. }
  205. return 1
  206. }