mirror of
https://github.com/alibaba/higress.git
synced 2026-06-02 00:57:28 +08:00
feat(ai-proxy): add context cleanup command support (#3409)
This commit is contained in:
@@ -421,6 +421,9 @@ type ProviderConfig struct {
|
||||
// @Title zh-CN generic Provider 对应的Host
|
||||
// @Description zh-CN 仅适用于generic provider,用于覆盖请求转发的目标Host
|
||||
genericHost string `required:"false" yaml:"genericHost" json:"genericHost"`
|
||||
// @Title zh-CN 上下文清理命令
|
||||
// @Description zh-CN 配置清理命令文本列表,当请求的 messages 中存在完全匹配任意一个命令的 user 消息时,将该消息及之前所有非 system 消息清理掉,实现主动清理上下文的效果
|
||||
contextCleanupCommands []string `required:"false" yaml:"contextCleanupCommands" json:"contextCleanupCommands"`
|
||||
// @Title zh-CN 首包超时
|
||||
// @Description zh-CN 流式请求中收到上游服务第一个响应包的超时时间,单位为毫秒。默认值为 0,表示不开启首包超时
|
||||
firstByteTimeout uint32 `required:"false" yaml:"firstByteTimeout" json:"firstByteTimeout"`
|
||||
@@ -461,6 +464,10 @@ func (c *ProviderConfig) GetVllmServerHost() string {
|
||||
return c.vllmServerHost
|
||||
}
|
||||
|
||||
func (c *ProviderConfig) GetContextCleanupCommands() []string {
|
||||
return c.contextCleanupCommands
|
||||
}
|
||||
|
||||
func (c *ProviderConfig) IsOpenAIProtocol() bool {
|
||||
return c.protocol == protocolOpenAI
|
||||
}
|
||||
@@ -639,6 +646,12 @@ func (c *ProviderConfig) FromJson(json gjson.Result) {
|
||||
c.vllmServerHost = json.Get("vllmServerHost").String()
|
||||
c.vllmCustomUrl = json.Get("vllmCustomUrl").String()
|
||||
c.doubaoDomain = json.Get("doubaoDomain").String()
|
||||
c.contextCleanupCommands = make([]string, 0)
|
||||
for _, cmd := range json.Get("contextCleanupCommands").Array() {
|
||||
if cmd.String() != "" {
|
||||
c.contextCleanupCommands = append(c.contextCleanupCommands, cmd.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ProviderConfig) Validate() error {
|
||||
@@ -949,6 +962,16 @@ func (c *ProviderConfig) handleRequestBody(
|
||||
log.Debugf("[Auto Protocol] converted Claude request body to OpenAI format")
|
||||
}
|
||||
|
||||
// handle context cleanup command for chat completion requests
|
||||
if apiName == ApiNameChatCompletion && len(c.contextCleanupCommands) > 0 {
|
||||
body, err = cleanupContextMessages(body, c.contextCleanupCommands)
|
||||
if err != nil {
|
||||
log.Warnf("[contextCleanup] failed to cleanup context messages: %v", err)
|
||||
// Continue processing even if cleanup fails
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
|
||||
// use openai protocol (either original openai or converted from claude)
|
||||
if handler, ok := provider.(TransformRequestBodyHandler); ok {
|
||||
body, err = handler.TransformRequestBody(ctx, apiName, body)
|
||||
|
||||
Reference in New Issue
Block a user