|
|
4 maanden geleden | |
|---|---|---|
| .. | ||
| README.md | 4 maanden geleden | |
| README.zh.md | 4 maanden geleden | |
| config.go | 4 maanden geleden | |
| example.json | 4 maanden geleden | |
| patch.go | 4 maanden geleden | |
| patch_test.go | 4 maanden geleden | |
The Patch Plugin provides powerful JSON request modification capabilities using sonic for high-performance JSON processing. It allows you to automatically modify API requests based on model types, field values, or custom conditions.
{{field_name}} syntaxThe plugin comes with several built-in patches:
max_tokens to 16000 for DeepSeek modelsmax_tokens field to maximum 16000max_tokens to max_completion_tokens for GPT-5 modelsmax_tokens field existsmax_completion_tokens to the value of max_tokensmax_tokens fieldmax_tokens to max_completion_tokens for o1 modelsmax_tokens to 8192 for Claude modelsmax_tokens field to maximum 8192stream_options for older GPT models that don't support itstream_options existsstream_options fieldimport (
"github.com/labring/aiproxy/core/relay/plugin/patch"
)
// Create plugin - configuration is loaded from model config
plugin := patch.New()
The patch plugin loads configuration from the model's plugin configuration in the database. The configuration should be stored in the model config's plugin field under the key "patch".
Example model config plugin configuration:
{
"patch": {
"enable": true,
"user_patches": [
{
"name": "custom_temperature_limit",
"description": "Limit temperature for specific models",
"conditions": [
{
"key": "model",
"operator": "contains",
"value": "gpt-4"
}
],
"operations": [
{
"op": "limit",
"key": "temperature",
"value": 1.0
}
]
}
]
}
}
The plugin comes with built-in predefined patches that are always enabled:
max_tokens to 16000 for DeepSeek modelsmax_tokens to max_completion_tokens for GPT-5 modelsmax_tokens to 8192 for Claude modelsstream_options for older GPT modelsThese predefined patches run automatically and cannot be disabled.
equals: Exact string matchnot_equals: Not equal to stringcontains: String contains substringnot_contains: String does not contain substringregex: Regular expression matchexists: Field exists (non-nil)not_exists: Field does not exist (nil)set: Set field to a specific valuedelete: Remove field from JSONadd: Add field only if it doesn't existlimit: Limit numeric field to maximum valuemodel: References the actual model name from metaoriginal_model: References the original model name from metaUse {{field_name}} to reference values from the JSON data:
{
Op: patch.OpSet,
Key: "max_completion_tokens",
Value: "{{max_tokens}}", // Will be replaced with actual max_tokens value
}
Use dot notation to access nested fields:
{
Key: "parameters.max_tokens", // Accesses parameters.max_tokens
// ...
}
import (
"github.com/labring/aiproxy/core/relay/plugin"
"github.com/labring/aiproxy/core/relay/plugin/patch"
)
// Create patch plugin
patchPlugin := patch.New()
// Wrap adaptor with plugin
adaptor = plugin.WrapperAdaptor(adaptor, patchPlugin)
{
"name": "anthropic_max_tokens",
"description": "Set appropriate max_tokens for Anthropic models",
"conditions": [
{
"key": "model",
"operator": "contains",
"value": "claude"
}
],
"operations": [
{
"op": "limit",
"key": "max_tokens",
"value": 4096
}
]
}
{
"name": "add_default_temperature",
"description": "Add default temperature if not specified",
"conditions": [
{
"key": "temperature",
"operator": "not_exists",
"value": ""
}
],
"operations": [
{
"op": "add",
"key": "temperature",
"value": 0.7
}
]
}
{
"name": "streaming_optimization",
"description": "Optimize streaming for specific models",
"conditions": [
{
"key": "stream",
"operator": "equals",
"value": "true"
},
{
"key": "model",
"operator": "regex",
"value": "^gpt-4"
}
],
"operations": [
{
"op": "set",
"key": "stream_options.include_usage",
"value": true
}
]
}