You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

134 lines
3.2 KiB

  1. package types
  2. import (
  3. "fmt"
  4. "github.com/QuantumNous/new-api/common"
  5. )
  6. const (
  7. PricingSchemaVersion = 1
  8. PricingScopeModel = "model"
  9. BillingUnitPerCall = "per_call"
  10. BillingUnitPer1MTokens = "per_1m_tokens"
  11. PreconsumeStrategyExact = "exact"
  12. PreconsumeStrategyMinimum = "minimum"
  13. PricingFallbackReject = "reject"
  14. PricingFallbackMax = "max"
  15. PricingFallbackDefault = "default"
  16. PricingRowSourceManual = "manual"
  17. PricingRowSourceGenerated = "generated"
  18. BillingModeMatrix = "matrix"
  19. )
  20. type PricingConfig struct {
  21. SchemaVersion int `json:"schema_version"`
  22. Scope string `json:"scope"`
  23. BillingUnit string `json:"billing_unit,omitempty"`
  24. PreconsumeStrategy string `json:"preconsume_strategy,omitempty"`
  25. Dimensions []PricingDimension `json:"dimensions"`
  26. Table []PricingRow `json:"table"`
  27. Fallback PricingFallback `json:"fallback"`
  28. GeneratorSnapshot map[string]any `json:"generator_snapshot,omitempty"`
  29. }
  30. type PricingDimension struct {
  31. Key string `json:"key"`
  32. Source string `json:"source"`
  33. Type string `json:"type"`
  34. Optional bool `json:"optional,omitempty"`
  35. Default any `json:"default"`
  36. }
  37. type PricingRow map[string]any
  38. type PricingFallback struct {
  39. Strategy string `json:"strategy"`
  40. DefaultPrice float64 `json:"default_price,omitempty"`
  41. }
  42. type OriginPricingSnapshot struct {
  43. BillingMode string
  44. BillingUnit string
  45. PriceUSD float64
  46. TokenUnitPriceUSD float64
  47. Snapshot map[string]any
  48. OtherRatios map[string]float64
  49. PerCallBilling bool
  50. }
  51. func ClonePricingConfig(src *PricingConfig) *PricingConfig {
  52. if src == nil {
  53. return nil
  54. }
  55. return &PricingConfig{
  56. SchemaVersion: src.SchemaVersion,
  57. Scope: src.Scope,
  58. BillingUnit: src.BillingUnit,
  59. PreconsumeStrategy: src.PreconsumeStrategy,
  60. Dimensions: append([]PricingDimension(nil), src.Dimensions...),
  61. Table: ClonePricingRows(src.Table),
  62. Fallback: src.Fallback,
  63. GeneratorSnapshot: CloneMapAny(src.GeneratorSnapshot),
  64. }
  65. }
  66. func ClonePricingRows(src []PricingRow) []PricingRow {
  67. if src == nil {
  68. return nil
  69. }
  70. dst := make([]PricingRow, len(src))
  71. for i := range src {
  72. dst[i] = PricingRow(CloneMapAny(map[string]any(src[i])))
  73. }
  74. return dst
  75. }
  76. func CloneMapAny(src map[string]any) map[string]any {
  77. if src == nil {
  78. return nil
  79. }
  80. bytes, err := common.Marshal(src)
  81. if err != nil {
  82. dst := make(map[string]any, len(src))
  83. for k, v := range src {
  84. dst[k] = v
  85. }
  86. return dst
  87. }
  88. var dst map[string]any
  89. if err := common.Unmarshal(bytes, &dst); err != nil {
  90. panic(fmt.Sprintf("clone map failed after successful marshal: %v", err))
  91. }
  92. return dst
  93. }
  94. func CloneRatios(src map[string]float64) map[string]float64 {
  95. if src == nil {
  96. return nil
  97. }
  98. dst := make(map[string]float64, len(src))
  99. for k, v := range src {
  100. dst[k] = v
  101. }
  102. return dst
  103. }
  104. func NormalizePricingConfigDefaults(cfg *PricingConfig) {
  105. if cfg == nil {
  106. return
  107. }
  108. if cfg.BillingUnit == "" {
  109. cfg.BillingUnit = BillingUnitPerCall
  110. }
  111. if cfg.PreconsumeStrategy == "" {
  112. cfg.PreconsumeStrategy = PreconsumeStrategyExact
  113. }
  114. }