configkey.go 3.5 KB

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