config.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Package patch provides configuration types and structures for the patch plugin.
  2. package patch
  3. import "github.com/bytedance/sonic/ast"
  4. // Config holds the configuration for the patch plugin
  5. type Config struct {
  6. // Force Enabled
  7. // UserPatches are user-defined custom patches
  8. UserPatches []PatchRule `json:"user_patches,omitempty"`
  9. }
  10. // PatchRule defines a complete patch rule with conditions and operations
  11. type PatchRule struct {
  12. // Name is a descriptive name for this patch rule
  13. Name string `json:"name"`
  14. // Description explains what this patch does
  15. Description string `json:"description,omitempty"`
  16. // Conditions determine when this patch should be applied
  17. Conditions []PatchCondition `json:"conditions,omitempty"`
  18. // ConditionLogic defines how conditions are combined (default: "and")
  19. ConditionLogic LogicOperator `json:"condition_logic,omitempty"`
  20. // Operations define what modifications to make
  21. Operations []PatchOperation `json:"operations"`
  22. }
  23. // PatchCondition defines when a patch should be applied
  24. type PatchCondition struct {
  25. // Key is the field to check (supports dot notation for nested fields)
  26. // Special keys: "model", "original_model"
  27. Key string `json:"key"`
  28. // Operator defines how to compare the value
  29. Operator ConditionOperator `json:"operator"`
  30. // Value is the value to compare against
  31. Value string `json:"value"`
  32. // Values is an array of values for 'in' and 'not_in' operators
  33. Values []string `json:"values,omitempty"`
  34. // Negate inverts the result of this condition (for "not" logic)
  35. Negate bool `json:"negate,omitempty"`
  36. }
  37. type PatchFunction func(root *ast.Node) (bool, error)
  38. // PatchOperation defines a modification to make to the JSON
  39. type PatchOperation struct {
  40. // Op is the operation type
  41. Op OperationType `json:"op"`
  42. // Key is the field to modify (supports dot notation for nested fields)
  43. Key string `json:"key"`
  44. // Value is the new value to set (not used for delete operations)
  45. Value any `json:"value,omitempty"`
  46. // Function is the inline Function code for OpFunction operations
  47. Function PatchFunction `json:"-"`
  48. }
  49. // ConditionOperator defines how to evaluate a condition
  50. type ConditionOperator string
  51. const (
  52. OperatorEquals ConditionOperator = "equals"
  53. OperatorNotEquals ConditionOperator = "not_equals"
  54. OperatorContains ConditionOperator = "contains"
  55. OperatorNotContains ConditionOperator = "not_contains"
  56. OperatorRegex ConditionOperator = "regex"
  57. OperatorExists ConditionOperator = "exists"
  58. OperatorNotExists ConditionOperator = "not_exists"
  59. OperatorHasPrefix ConditionOperator = "has_prefix"
  60. OperatorHasSuffix ConditionOperator = "has_suffix"
  61. OperatorGreaterThan ConditionOperator = "greater_than"
  62. OperatorLessThan ConditionOperator = "less_than"
  63. OperatorGreaterEq ConditionOperator = "greater_eq"
  64. OperatorLessEq ConditionOperator = "less_eq"
  65. OperatorIn ConditionOperator = "in"
  66. OperatorNotIn ConditionOperator = "not_in"
  67. )
  68. // LogicOperator defines how multiple conditions are combined
  69. type LogicOperator string
  70. const (
  71. // LogicAnd requires all conditions to be true (default)
  72. LogicAnd LogicOperator = "and"
  73. // LogicOr requires at least one condition to be true
  74. LogicOr LogicOperator = "or"
  75. )
  76. // OperationType defines the type of operation to perform
  77. type OperationType string
  78. const (
  79. // OpSet sets a field to a specific value
  80. OpSet OperationType = "set"
  81. // OpDelete removes a field
  82. OpDelete OperationType = "delete"
  83. // OpAdd adds a field only if it doesn't exist
  84. OpAdd OperationType = "add"
  85. // OpLimit limits a numeric field to a maximum value
  86. OpLimit OperationType = "limit"
  87. // OpIncrement increments a numeric field by a value
  88. OpIncrement OperationType = "increment"
  89. // OpDecrement decrements a numeric field by a value
  90. OpDecrement OperationType = "decrement"
  91. // OpMultiply multiplies a numeric field by a value
  92. OpMultiply OperationType = "multiply"
  93. // OpDivide divides a numeric field by a value
  94. OpDivide OperationType = "divide"
  95. // OpAppend appends value to an array field
  96. OpAppend OperationType = "append"
  97. // OpPrepend prepends value to an array field
  98. OpPrepend OperationType = "prepend"
  99. // OpFunction executes an inline function on the field
  100. OpFunction OperationType = "function"
  101. )
  102. // DefaultPredefinedPatches are built-in patches that are always available
  103. var DefaultPredefinedPatches = []PatchRule{
  104. {
  105. Name: "deepseek_max_tokens_limit",
  106. Description: "Limit max_tokens to 16000 for DeepSeek models",
  107. ConditionLogic: LogicOr,
  108. Conditions: []PatchCondition{
  109. {
  110. Key: "model",
  111. Operator: OperatorContains,
  112. Value: "deepseek-v3",
  113. },
  114. },
  115. Operations: []PatchOperation{
  116. {
  117. Op: OpLimit,
  118. Key: "max_tokens",
  119. Value: 16384,
  120. },
  121. },
  122. },
  123. {
  124. Name: "deepseek_3.1_max_tokens_limit",
  125. Description: "Limit max_tokens to 16000 for DeepSeek models",
  126. ConditionLogic: LogicOr,
  127. Conditions: []PatchCondition{
  128. {
  129. Key: "model",
  130. Operator: OperatorContains,
  131. Value: "deepseek-v3.1",
  132. },
  133. {
  134. Key: "model",
  135. Operator: OperatorContains,
  136. Value: "deepseek-chat",
  137. },
  138. },
  139. Operations: []PatchOperation{
  140. {
  141. Op: OpLimit,
  142. Key: "max_tokens",
  143. Value: 8192,
  144. },
  145. },
  146. },
  147. {
  148. Name: "gpt5_max_tokens_to_max_completion_tokens",
  149. Description: "Convert max_tokens to max_completion_tokens for GPT-5 models",
  150. Conditions: []PatchCondition{
  151. {
  152. Key: "model",
  153. Operator: OperatorContains,
  154. Value: "gpt-5",
  155. },
  156. {
  157. Key: "max_tokens",
  158. Operator: OperatorExists,
  159. },
  160. },
  161. Operations: []PatchOperation{
  162. {
  163. Op: OpSet,
  164. Key: "max_completion_tokens",
  165. Value: "{{max_tokens}}", // Special placeholder that will be replaced with actual max_tokens value
  166. },
  167. {
  168. Op: OpDelete,
  169. Key: "max_tokens",
  170. },
  171. },
  172. },
  173. {
  174. Name: "gpt5_remove_temperature",
  175. Description: "Remove temperature field for GPT-5 models",
  176. Conditions: []PatchCondition{
  177. {
  178. Key: "model",
  179. Operator: OperatorContains,
  180. Value: "gpt-5",
  181. },
  182. {
  183. Key: "temperature",
  184. Operator: OperatorExists,
  185. },
  186. },
  187. Operations: []PatchOperation{
  188. {
  189. Op: OpDelete,
  190. Key: "temperature",
  191. },
  192. },
  193. },
  194. {
  195. Name: "gpt5_remove_top_p",
  196. Description: "Remove top_p field for GPT-5 models",
  197. Conditions: []PatchCondition{
  198. {
  199. Key: "model",
  200. Operator: OperatorContains,
  201. Value: "gpt-5",
  202. },
  203. {
  204. Key: "top_p",
  205. Operator: OperatorExists,
  206. },
  207. },
  208. Operations: []PatchOperation{
  209. {
  210. Op: OpDelete,
  211. Key: "top_p",
  212. },
  213. },
  214. },
  215. {
  216. Name: "gemini_gpt5_remove_generation_config_top_p",
  217. Description: "Remove generationConfig.topP for GPT-5 models in Gemini format",
  218. Conditions: []PatchCondition{
  219. {
  220. Key: "model",
  221. Operator: OperatorContains,
  222. Value: "gpt-5",
  223. },
  224. {
  225. Key: "generationConfig.topP",
  226. Operator: OperatorExists,
  227. },
  228. },
  229. Operations: []PatchOperation{
  230. {
  231. Op: OpDelete,
  232. Key: "generationConfig.topP",
  233. },
  234. },
  235. },
  236. }