configkey.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package model
  2. import (
  3. "reflect"
  4. "strings"
  5. )
  6. type ModelConfigKey string
  7. const (
  8. ModelConfigMaxContextTokensKey ModelConfigKey = "max_context_tokens"
  9. ModelConfigMaxInputTokensKey ModelConfigKey = "max_input_tokens"
  10. ModelConfigMaxOutputTokensKey ModelConfigKey = "max_output_tokens"
  11. ModelConfigVisionKey ModelConfigKey = "vision"
  12. ModelConfigToolChoiceKey ModelConfigKey = "tool_choice"
  13. ModelConfigSupportFormatsKey ModelConfigKey = "support_formats"
  14. ModelConfigSupportVoicesKey ModelConfigKey = "support_voices"
  15. ModelConfigImageSizes ModelConfigKey = "image_sizes"
  16. ModelConfigImageQualitys ModelConfigKey = "image_qualitys"
  17. )
  18. type ModelConfigOption func(config map[ModelConfigKey]any)
  19. func WithModelConfigImageSizes(sizes ...string) ModelConfigOption {
  20. for _, size := range sizes {
  21. if !strings.Contains(size, "x") {
  22. panic("image size format error")
  23. }
  24. }
  25. return func(config map[ModelConfigKey]any) {
  26. config[ModelConfigImageSizes] = sizes
  27. }
  28. }
  29. func WithModelConfigImageQualitys(qualitys ...string) ModelConfigOption {
  30. return func(config map[ModelConfigKey]any) {
  31. config[ModelConfigImageQualitys] = qualitys
  32. }
  33. }
  34. func WithModelConfigMaxContextTokens(maxContextTokens int) ModelConfigOption {
  35. return func(config map[ModelConfigKey]any) {
  36. config[ModelConfigMaxContextTokensKey] = maxContextTokens
  37. }
  38. }
  39. func WithModelConfigMaxInputTokens(maxInputTokens int) ModelConfigOption {
  40. return func(config map[ModelConfigKey]any) {
  41. config[ModelConfigMaxInputTokensKey] = maxInputTokens
  42. }
  43. }
  44. func WithModelConfigMaxOutputTokens(maxOutputTokens int) ModelConfigOption {
  45. return func(config map[ModelConfigKey]any) {
  46. config[ModelConfigMaxOutputTokensKey] = maxOutputTokens
  47. }
  48. }
  49. func WithModelConfigVision(vision bool) ModelConfigOption {
  50. return func(config map[ModelConfigKey]any) {
  51. config[ModelConfigVisionKey] = vision
  52. }
  53. }
  54. func WithModelConfigToolChoice(toolChoice bool) ModelConfigOption {
  55. return func(config map[ModelConfigKey]any) {
  56. config[ModelConfigToolChoiceKey] = toolChoice
  57. }
  58. }
  59. func WithModelConfigSupportFormats(supportFormats []string) ModelConfigOption {
  60. return func(config map[ModelConfigKey]any) {
  61. config[ModelConfigSupportFormatsKey] = supportFormats
  62. }
  63. }
  64. func WithModelConfigSupportVoices(supportVoices []string) ModelConfigOption {
  65. return func(config map[ModelConfigKey]any) {
  66. config[ModelConfigSupportVoicesKey] = supportVoices
  67. }
  68. }
  69. func NewModelConfig(opts ...ModelConfigOption) map[ModelConfigKey]any {
  70. config := make(map[ModelConfigKey]any)
  71. for _, opt := range opts {
  72. opt(config)
  73. }
  74. return config
  75. }
  76. func GetModelConfigInt(config map[ModelConfigKey]any, key ModelConfigKey) (int, bool) {
  77. if v, ok := config[key]; ok {
  78. value := reflect.ValueOf(v)
  79. if value.CanInt() {
  80. return int(value.Int()), true
  81. }
  82. if value.CanFloat() {
  83. return int(value.Float()), true
  84. }
  85. }
  86. return 0, false
  87. }
  88. func GetModelConfigUint(config map[ModelConfigKey]any, key ModelConfigKey) (uint64, bool) {
  89. if v, ok := config[key]; ok {
  90. value := reflect.ValueOf(v)
  91. if value.CanUint() {
  92. return value.Uint(), true
  93. }
  94. if value.CanFloat() {
  95. return uint64(value.Float()), true
  96. }
  97. }
  98. return 0, false
  99. }
  100. func GetModelConfigFloat(config map[ModelConfigKey]any, key ModelConfigKey) (float64, bool) {
  101. if v, ok := config[key]; ok {
  102. value := reflect.ValueOf(v)
  103. if value.CanFloat() {
  104. return value.Float(), true
  105. }
  106. if value.CanInt() {
  107. return float64(value.Int()), true
  108. }
  109. if value.CanUint() {
  110. return float64(value.Uint()), true
  111. }
  112. }
  113. return 0, false
  114. }
  115. func GetModelConfigStringSlice(config map[ModelConfigKey]any, key ModelConfigKey) ([]string, bool) {
  116. v, ok := config[key]
  117. if !ok {
  118. return nil, false
  119. }
  120. if slice, ok := v.([]string); ok {
  121. return slice, true
  122. }
  123. if slice, ok := v.([]any); ok {
  124. result := make([]string, len(slice))
  125. for i, v := range slice {
  126. if s, ok := v.(string); ok {
  127. result[i] = s
  128. continue
  129. }
  130. return nil, false
  131. }
  132. return result, true
  133. }
  134. return nil, false
  135. }
  136. func GetModelConfigBool(config map[ModelConfigKey]any, key ModelConfigKey) (bool, bool) {
  137. if v, ok := config[key].(bool); ok {
  138. return v, true
  139. }
  140. return false, false
  141. }