From 0380cb03d3e57ea85135cb4cb45cfb545de7fa2c Mon Sep 17 00:00:00 2001 From: rinfx <893383980@qq.com> Date: Mon, 17 Jun 2024 15:33:05 +0800 Subject: [PATCH] add plugin: ai-prompt-template (#1019) --- .../extensions/ai-prompt-template/.gitignore | 3 + .../extensions/ai-prompt-template/README.md | 48 ++++++++++++++++ .../extensions/ai-prompt-template/go.mod | 19 +++++++ .../extensions/ai-prompt-template/go.sum | 25 +++++++++ .../extensions/ai-prompt-template/main.go | 55 +++++++++++++++++++ 5 files changed, 150 insertions(+) create mode 100644 plugins/wasm-go/extensions/ai-prompt-template/.gitignore create mode 100644 plugins/wasm-go/extensions/ai-prompt-template/README.md create mode 100644 plugins/wasm-go/extensions/ai-prompt-template/go.mod create mode 100644 plugins/wasm-go/extensions/ai-prompt-template/go.sum create mode 100644 plugins/wasm-go/extensions/ai-prompt-template/main.go diff --git a/plugins/wasm-go/extensions/ai-prompt-template/.gitignore b/plugins/wasm-go/extensions/ai-prompt-template/.gitignore new file mode 100644 index 000000000..c9f9dc52b --- /dev/null +++ b/plugins/wasm-go/extensions/ai-prompt-template/.gitignore @@ -0,0 +1,3 @@ +config.yaml +main.wasm +tmp/ \ No newline at end of file diff --git a/plugins/wasm-go/extensions/ai-prompt-template/README.md b/plugins/wasm-go/extensions/ai-prompt-template/README.md new file mode 100644 index 000000000..6fe6437e8 --- /dev/null +++ b/plugins/wasm-go/extensions/ai-prompt-template/README.md @@ -0,0 +1,48 @@ +# 简介 +AI提示词模板,用于快速构建同类型的AI请求。 + +# 配置说明 +| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 | +|----------------|-----------------|------|-----|----------------------------------| +| `templates` | array of object | 必填 | - | 模板设置 | + +template object 配置说明: + +| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 | +|----------------|-----------------|------|-----|----------------------------------| +| `name` | string | 必填 | - | 模板名称 | +| `template.model` | string | 必填 | - | 模型名称 | +| `template.messages` | array of object | 必填 | - | 大模型输入 | + +message object 配置说明: + +| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 | +|----------------|-----------------|------|-----|----------------------------------| +| `role` | string | 必填 | - | 角色 | +| `content` | string | 必填 | - | 消息 | + +配置示例如下: + +```yaml +templates: +- name: "developer-chat" + template: + model: gpt-3.5-turbo + messages: + - role: system + content: "You are a {{program}} expert, in {{language}} programming language." + - role: user + content: "Write me a {{program}} program." +``` + +使用以上配置的请求body示例: + +```json +{ + "template": "developer-chat", + "properties": { + "program": "quick sort", + "language": "python" + } +} +``` \ No newline at end of file diff --git a/plugins/wasm-go/extensions/ai-prompt-template/go.mod b/plugins/wasm-go/extensions/ai-prompt-template/go.mod new file mode 100644 index 000000000..9fb3b06f6 --- /dev/null +++ b/plugins/wasm-go/extensions/ai-prompt-template/go.mod @@ -0,0 +1,19 @@ +module ai-prompt-template + +go 1.18 + +require ( + github.com/alibaba/higress/plugins/wasm-go v1.3.5 + github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240226064518-b3dc4646a35a + github.com/tidwall/gjson v1.14.3 +) + +require ( + github.com/google/uuid v1.3.0 // indirect + github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520 // indirect + github.com/magefile/mage v1.14.0 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect + github.com/tidwall/resp v0.1.1 // indirect + github.com/tidwall/sjson v1.2.5 +) diff --git a/plugins/wasm-go/extensions/ai-prompt-template/go.sum b/plugins/wasm-go/extensions/ai-prompt-template/go.sum new file mode 100644 index 000000000..94fcae90c --- /dev/null +++ b/plugins/wasm-go/extensions/ai-prompt-template/go.sum @@ -0,0 +1,25 @@ +github.com/alibaba/higress/plugins/wasm-go v1.3.5 h1:VOLL3m442IHCSu8mR5AZ4sc6LVT9X0w1hdqDI7oB9jY= +github.com/alibaba/higress/plugins/wasm-go v1.3.5/go.mod h1:kr3V9Ntbspj1eSrX8rgjBsdMXkGupYEf+LM72caGPQc= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520 h1:IHDghbGQ2DTIXHBHxWfqCYQW1fKjyJ/I7W1pMyUDeEA= +github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520/go.mod h1:Nz8ORLaFiLWotg6GeKlJMhv8cci8mM43uEnLA5t8iew= +github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240226064518-b3dc4646a35a h1:luYRvxLTE1xYxrXYj7nmjd1U0HHh8pUPiKfdZ0MhCGE= +github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240226064518-b3dc4646a35a/go.mod h1:hNFjhrLUIq+kJ9bOcs8QtiplSQ61GZXtd2xHKx4BYRo= +github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo= +github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw= +github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/resp v0.1.1 h1:Ly20wkhqKTmDUPlyM1S7pWo5kk0tDu8OoC/vFArXmwE= +github.com/tidwall/resp v0.1.1/go.mod h1:3/FrruOBAxPTPtundW0VXgmsQ4ZBA0Aw714lVYgwFa0= +github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= +github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/plugins/wasm-go/extensions/ai-prompt-template/main.go b/plugins/wasm-go/extensions/ai-prompt-template/main.go new file mode 100644 index 000000000..9a806c76c --- /dev/null +++ b/plugins/wasm-go/extensions/ai-prompt-template/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + "strings" + + "github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper" + "github.com/higress-group/proxy-wasm-go-sdk/proxywasm" + "github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types" + "github.com/tidwall/gjson" +) + +func main() { + wrapper.SetCtx( + "ai-prompt-template", + wrapper.ParseConfigBy(parseConfig), + wrapper.ProcessRequestHeadersBy(onHttpRequestHeaders), + wrapper.ProcessRequestBodyBy(onHttpRequestBody), + ) +} + +type AIPromptTemplateConfig struct { + templates map[string]string +} + +func parseConfig(json gjson.Result, config *AIPromptTemplateConfig, log wrapper.Log) error { + config.templates = make(map[string]string) + for _, v := range json.Get("templates").Array() { + config.templates[v.Get("name").String()] = v.Get("template").Raw + log.Info(v.Get("template").Raw) + } + return nil +} + +func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIPromptTemplateConfig, log wrapper.Log) types.Action { + templateEnable, _ := proxywasm.GetHttpRequestHeader("template-enable") + if templateEnable != "true" { + ctx.DontReadRequestBody() + return types.ActionContinue + } + proxywasm.RemoveHttpRequestHeader("content-length") + return types.ActionContinue +} + +func onHttpRequestBody(ctx wrapper.HttpContext, config AIPromptTemplateConfig, body []byte, log wrapper.Log) types.Action { + if gjson.GetBytes(body, "template").Exists() && gjson.GetBytes(body, "properties").Exists() { + name := gjson.GetBytes(body, "template").String() + template := config.templates[name] + for key, value := range gjson.GetBytes(body, "properties").Map() { + template = strings.ReplaceAll(template, fmt.Sprintf("{{%s}}", key), value.String()) + } + proxywasm.ReplaceHttpRequestBody([]byte(template)) + } + return types.ActionContinue +}