model-ratio.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 ModelRatio = map[string]float64{
  15. "gpt-4": 15,
  16. "gpt-4-0314": 15,
  17. "gpt-4-0613": 15,
  18. "gpt-4-32k": 30,
  19. "gpt-4-32k-0314": 30,
  20. "gpt-4-32k-0613": 30,
  21. "gpt-4-1106-preview": 5, // $0.01 / 1K tokens
  22. "gpt-4-vision-preview": 5, // $0.01 / 1K tokens
  23. "gpt-3.5-turbo": 0.75, // $0.0015 / 1K tokens
  24. "gpt-3.5-turbo-0301": 0.75,
  25. "gpt-3.5-turbo-0613": 0.75,
  26. "gpt-3.5-turbo-16k": 1.5, // $0.003 / 1K tokens
  27. "gpt-3.5-turbo-16k-0613": 1.5,
  28. "gpt-3.5-turbo-instruct": 0.75, // $0.0015 / 1K tokens
  29. "gpt-3.5-turbo-1106": 0.5, // $0.001 / 1K tokens
  30. "text-ada-001": 0.2,
  31. "text-babbage-001": 0.25,
  32. "text-curie-001": 1,
  33. "text-davinci-002": 10,
  34. "text-davinci-003": 10,
  35. "text-davinci-edit-001": 10,
  36. "code-davinci-edit-001": 10,
  37. "whisper-1": 15, // $0.006 / minute -> $0.006 / 150 words -> $0.006 / 200 tokens -> $0.03 / 1k tokens
  38. "davinci": 10,
  39. "curie": 10,
  40. "babbage": 10,
  41. "ada": 10,
  42. "text-embedding-ada-002": 0.05,
  43. "text-search-ada-doc-001": 10,
  44. "text-moderation-stable": 0.1,
  45. "text-moderation-latest": 0.1,
  46. "dall-e": 8,
  47. "claude-instant-1": 0.815, // $1.63 / 1M tokens
  48. "claude-2": 5.51, // $11.02 / 1M tokens
  49. "ERNIE-Bot": 0.8572, // ¥0.012 / 1k tokens
  50. "ERNIE-Bot-turbo": 0.5715, // ¥0.008 / 1k tokens
  51. "ERNIE-Bot-4": 8.572, // ¥0.12 / 1k tokens
  52. "Embedding-V1": 0.1429, // ¥0.002 / 1k tokens
  53. "PaLM-2": 1,
  54. "chatglm_turbo": 0.3572, // ¥0.005 / 1k tokens
  55. "chatglm_pro": 0.7143, // ¥0.01 / 1k tokens
  56. "chatglm_std": 0.3572, // ¥0.005 / 1k tokens
  57. "chatglm_lite": 0.1429, // ¥0.002 / 1k tokens
  58. "qwen-turbo": 0.8572, // ¥0.012 / 1k tokens
  59. "qwen-plus": 10, // ¥0.14 / 1k tokens
  60. "text-embedding-v1": 0.05, // ¥0.0007 / 1k tokens
  61. "SparkDesk": 1.2858, // ¥0.018 / 1k tokens
  62. "360GPT_S2_V9": 0.8572, // ¥0.012 / 1k tokens
  63. "embedding-bert-512-v1": 0.0715, // ¥0.001 / 1k tokens
  64. "embedding_s1_v1": 0.0715, // ¥0.001 / 1k tokens
  65. "semantic_similarity_s1_v1": 0.0715, // ¥0.001 / 1k tokens
  66. "hunyuan": 7.143, // ¥0.1 / 1k tokens // https://cloud.tencent.com/document/product/1729/97731#e0e6be58-60c8-469f-bdeb-6c264ce3b4d0
  67. }
  68. func ModelRatio2JSONString() string {
  69. jsonBytes, err := json.Marshal(ModelRatio)
  70. if err != nil {
  71. SysError("error marshalling model ratio: " + err.Error())
  72. }
  73. return string(jsonBytes)
  74. }
  75. func UpdateModelRatioByJSONString(jsonStr string) error {
  76. ModelRatio = make(map[string]float64)
  77. return json.Unmarshal([]byte(jsonStr), &ModelRatio)
  78. }
  79. func GetModelRatio(name string) float64 {
  80. ratio, ok := ModelRatio[name]
  81. if !ok {
  82. SysError("model ratio not found: " + name)
  83. return 30
  84. }
  85. return ratio
  86. }
  87. func GetCompletionRatio(name string) float64 {
  88. if strings.HasPrefix(name, "gpt-3.5") {
  89. if strings.HasSuffix(name, "1106") {
  90. return 2
  91. }
  92. if name == "gpt-3.5-turbo" || name == "gpt-3.5-turbo-16k" {
  93. // TODO: clear this after 2023-12-11
  94. now := time.Now()
  95. // https://platform.openai.com/docs/models/continuous-model-upgrades
  96. // if after 2023-12-11, use 2
  97. if now.After(time.Date(2023, 12, 11, 0, 0, 0, 0, time.UTC)) {
  98. return 2
  99. }
  100. }
  101. return 1.333333
  102. }
  103. if strings.HasPrefix(name, "gpt-4") {
  104. if strings.HasSuffix(name, "preview") {
  105. return 3
  106. }
  107. return 2
  108. }
  109. if strings.HasPrefix(name, "claude-instant-1") {
  110. return 3.38
  111. }
  112. if strings.HasPrefix(name, "claude-2") {
  113. return 2.965517
  114. }
  115. return 1
  116. }