AI-search plugin supports controlling through the web_search_options parameter. (#1893)

This commit is contained in:
澄潭
2025-03-14 21:52:33 +08:00
committed by GitHub
parent 26bfdd45ff
commit 5e7e20ff7e
3 changed files with 51 additions and 1 deletions

View File

@@ -11,12 +11,13 @@ description: higress 支持通过集成搜索引擎Google/Bing/Arxiv/Elastics
## 运行属性
插件执行阶段:`默认阶段`
插件执行优先级:`440`
插件执行优先级:`460`
## 配置字段
| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 |
|------|----------|----------|--------|------|
| defaultEnable | bool | 选填 | true | 插件功能默认是否开启。设置为false时仅当请求中包含web_search_options字段时才启用插件功能 |
| needReference | bool | 选填 | false | 是否在回答中添加引用来源 |
| referenceFormat | string | 选填 | `"**References:**\n%s"` | 引用内容格式,必须包含%s占位符 |
| defaultLang | string | 选填 | - | 默认搜索语言代码如zh-CN/en-US |
@@ -242,6 +243,22 @@ searchRewrite:
timeoutMillisecond: 15000
```
### 按需启用插件配置
配置插件仅在请求中包含`web_search_options`字段时才启用:
```yaml
defaultEnable: false
searchFrom:
- type: google
apiKey: "your-google-api-key"
cx: "search-engine-id"
serviceName: "google-svc.dns"
servicePort: 443
```
这种配置适用于支持web_search选项的模型例如OpenAI的gpt-4o-search-preview模型。当请求中包含`web_search_options`字段时,即使是空对象(`"web_search_options": {}`),插件也会被激活。
## 注意事项
1. 提示词模版必须包含`{search_results}``{question}`占位符,可选使用`{cur_date}`插入当前日期格式2006年1月2日

View File

@@ -17,6 +17,7 @@ Plugin execution priority: `440`
| Name | Data Type | Requirement | Default Value | Description |
|------|-----------|-------------|---------------|-------------|
| defaultEnable | bool | Optional | true | Whether the plugin functionality is enabled by default. When set to false, the plugin will only be activated when the request contains a web_search_options field |
| needReference | bool | Optional | false | Whether to add reference sources in the response |
| referenceFormat | string | Optional | `"**References:**\n%s"` | Reference content format, must include %s placeholder |
| defaultLang | string | Optional | - | Default search language code (e.g. zh-CN/en-US) |
@@ -241,6 +242,22 @@ searchRewrite:
timeoutMillisecond: 15000
```
### On-Demand Plugin Activation Configuration
Configure the plugin to only be enabled when the request contains a `web_search_options` field:
```yaml
defaultEnable: false
searchFrom:
- type: google
apiKey: "your-google-api-key"
cx: "search-engine-id"
serviceName: "google-svc.dns"
servicePort: 443
```
This configuration is suitable for models that support web search options, such as OpenAI's gpt-4o-search-preview model. When the request contains a `web_search_options` field, even if it's an empty object (`"web_search_options": {}`), the plugin will be activated.
## Notes
1. The prompt template must include `{search_results}` and `{question}` placeholders, optionally use `{cur_date}` to insert current date (format: January 2, 2006)

View File

@@ -55,6 +55,7 @@ type Config struct {
defaultLanguage string
needReference bool
searchRewrite *SearchRewrite
defaultEnable bool
}
const (
@@ -86,6 +87,10 @@ func main() {
}
func parseConfig(json gjson.Result, config *Config, log wrapper.Log) error {
config.defaultEnable = true // Default to true if not specified
if json.Get("defaultEnable").Exists() {
config.defaultEnable = json.Get("defaultEnable").Bool()
}
config.needReference = json.Get("needReference").Bool()
if config.needReference {
config.referenceFormat = json.Get("referenceFormat").String()
@@ -254,6 +259,17 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log wrapper.Lo
}
func onHttpRequestBody(ctx wrapper.HttpContext, config Config, body []byte, log wrapper.Log) types.Action {
// Check if plugin should be enabled based on config and request
if !config.defaultEnable {
// When defaultEnable is false, we need to check if web_search_options exists in the request
webSearchOptions := gjson.GetBytes(body, "web_search_options")
if !webSearchOptions.Exists() {
log.Debugf("Plugin disabled by config and no web_search_options in request")
return types.ActionContinue
}
log.Debugf("Plugin enabled by web_search_options in request")
}
var queryIndex int
var query string
messages := gjson.GetBytes(body, "messages").Array()