From 0446f9fd8e679d114acbb02ca8303b42b6af5682 Mon Sep 17 00:00:00 2001 From: Robin Xiang Date: Mon, 15 Apr 2024 15:06:03 +0800 Subject: [PATCH 1/4] This PR is a follow-up one for PR #12781. In this PR, a database migration is added for AI-proxy plugin, disabling the log_statistics feature of this plugin for providers not supporting this feature. Also, a changelog entry is added as well to describe it. --- .../migration_of_ai_proxy_plugin.yml | 3 + kong-3.7.0-0.rockspec | 2 + kong/llm/init.lua | 1 - .../ai-proxy/migrations/001_360_to_370.lua | 19 ++++++ kong/plugins/ai-proxy/migrations/init.lua | 3 + .../migrations/001_360_to_370_spec.lua | 59 +++++++++++++++++++ 6 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/migration_of_ai_proxy_plugin.yml create mode 100644 kong/plugins/ai-proxy/migrations/001_360_to_370.lua create mode 100644 kong/plugins/ai-proxy/migrations/init.lua create mode 100644 spec/05-migration/plugins/ai-proxy/migrations/001_360_to_370_spec.lua diff --git a/changelog/unreleased/migration_of_ai_proxy_plugin.yml b/changelog/unreleased/migration_of_ai_proxy_plugin.yml new file mode 100644 index 000000000000..5137297b27fe --- /dev/null +++ b/changelog/unreleased/migration_of_ai_proxy_plugin.yml @@ -0,0 +1,3 @@ +message: | + **AI-proxy**: A configuration validation is added to prevent from enabling `log_statistics` upon providers not supporting statistics. Accordingly, the default of `log_statistics` is changed from `true` to `false`, and a database migration is added as well for disabling `log_statistics` if it has bee enabling upon unsupported providers. +type: bugfix diff --git a/kong-3.7.0-0.rockspec b/kong-3.7.0-0.rockspec index 7414bd0ae452..3118916ef9da 100644 --- a/kong-3.7.0-0.rockspec +++ b/kong-3.7.0-0.rockspec @@ -568,6 +568,8 @@ build = { ["kong.plugins.ai-proxy.handler"] = "kong/plugins/ai-proxy/handler.lua", ["kong.plugins.ai-proxy.schema"] = "kong/plugins/ai-proxy/schema.lua", + ["kong.plugins.ai-proxy.migrations"] = "kong/plugins/ai-proxy/migrations/init.lua", + ["kong.plugins.ai-proxy.migrations.000_base_ai_proxy"] = "kong/plugins/ai-proxy/migrations/001_360_to_370.lua", ["kong.plugins.ai-request-transformer.handler"] = "kong/plugins/ai-request-transformer/handler.lua", ["kong.plugins.ai-request-transformer.schema"] = "kong/plugins/ai-request-transformer/schema.lua", diff --git a/kong/llm/init.lua b/kong/llm/init.lua index 1cef01b0ac49..5bc54531de56 100644 --- a/kong/llm/init.lua +++ b/kong/llm/init.lua @@ -219,7 +219,6 @@ _M.config_schema = { custom_entity_check = { field_sources = { "route_type", "model", "logging" }, fn = function(entity) - -- print(cjson.encode(entity)) if entity.logging.log_statistics and UNSUPPORTED_LOG_STATISTICS[entity.route_type] and UNSUPPORTED_LOG_STATISTICS[entity.route_type][entity.model.provider] then return nil, fmt("%s does not support statistics when route_type is %s", diff --git a/kong/plugins/ai-proxy/migrations/001_360_to_370.lua b/kong/plugins/ai-proxy/migrations/001_360_to_370.lua new file mode 100644 index 000000000000..f6f4b8cfc6d7 --- /dev/null +++ b/kong/plugins/ai-proxy/migrations/001_360_to_370.lua @@ -0,0 +1,19 @@ +local operations = require "kong.db.migrations.operations.200_to_210" + +local function update_logging_statistic(config) + if config.logging.log_statistics and config.route_type == "llm/v1/completions" + and config.model.provider == "anthropic" then + config.logging.log_statistics = false + return true + end +end + + +return { + postgres = { + up = "", + teardown = function(connector) + operations.postgres.teardown:fixup_plugin_config(connector, "ai-proxy", update_logging_statistic) + end + } +} \ No newline at end of file diff --git a/kong/plugins/ai-proxy/migrations/init.lua b/kong/plugins/ai-proxy/migrations/init.lua new file mode 100644 index 000000000000..1031313ffe1c --- /dev/null +++ b/kong/plugins/ai-proxy/migrations/init.lua @@ -0,0 +1,3 @@ +return { + "001_360_to_370", +} \ No newline at end of file diff --git a/spec/05-migration/plugins/ai-proxy/migrations/001_360_to_370_spec.lua b/spec/05-migration/plugins/ai-proxy/migrations/001_360_to_370_spec.lua new file mode 100644 index 000000000000..da45109e86c4 --- /dev/null +++ b/spec/05-migration/plugins/ai-proxy/migrations/001_360_to_370_spec.lua @@ -0,0 +1,59 @@ +local uh = require "spec.upgrade_helpers" +local helpers = require "spec.helpers" + +for _, strategy in helpers.each_strategy() do + describe("ai-proxy plugin migration", function() + local db, ai_proxy_plugin + uh.setup(function() + _, db = helpers.get_db_utils(strategy, {"plugins"}, { "ai-proxy" }) + local plugin, err, err_t = db.plugins:insert { + name = "ai-proxy", + config = { + route_type = "llm/v1/completions", + auth = { + header_name = "x-api-key", + header_value = "anthropic-key", + }, + model = { + name = "claude-2.1", + provider = "anthropic", + options = { + max_tokens = 256, + temperature = 1.0, + upstream_url = "http://example.com/llm/v1/completions/good", + anthropic_version = "2023-06-01", + }, + }, + logging = { + log_statistics = true, -- anthropic does not support statistics + }, + }, + } + assert.is_nil(err) + assert.is_nil(err_t) + assert.is_not_nil(plugin) + ai_proxy_plugin = plugin + end) + + uh.new_after_up("has updated ai-proxy plugin configuration", function () + -- local cache_key = db.plugins:cache_key("ai-proxy") + local plugin, err = db.plugins:select({ id = ai_proxy_plugin.id }) + assert.is_nil(err) + assert.is_not_nil(plugin) + -- assert.equal(1, #rows) + + assert.equal("ai-proxy", plugin.name) + local expected_config = { + logging = { + log_statistics = false + }, + route_type = "llm/v1/completions", + model = { + provider = "anthropic" + } + } + + assert.partial_match(expected_config, plugin.config) + end) + end) +end \ No newline at end of file From 1865a5c2a5cc67e285ff6164f85101fd02a60a51 Mon Sep 17 00:00:00 2001 From: Robin Xiang Date: Tue, 16 Apr 2024 16:40:13 +0800 Subject: [PATCH 2/4] update changlog --- .../migration_of_ai_proxy_plugin.yml | 5 +- .../ai-proxy/migrations/001_360_to_370.lua | 6 +- .../migrations/001_360_to_370_spec.lua | 103 ++++++++++-------- 3 files changed, 61 insertions(+), 53 deletions(-) diff --git a/changelog/unreleased/migration_of_ai_proxy_plugin.yml b/changelog/unreleased/migration_of_ai_proxy_plugin.yml index 5137297b27fe..d9c275e3cdd7 100644 --- a/changelog/unreleased/migration_of_ai_proxy_plugin.yml +++ b/changelog/unreleased/migration_of_ai_proxy_plugin.yml @@ -1,3 +1,6 @@ message: | - **AI-proxy**: A configuration validation is added to prevent from enabling `log_statistics` upon providers not supporting statistics. Accordingly, the default of `log_statistics` is changed from `true` to `false`, and a database migration is added as well for disabling `log_statistics` if it has bee enabling upon unsupported providers. + **AI-proxy**: A configuration validation is added to prevent from enabling `log_statistics` upon + providers not supporting statistics. Accordingly, the default of `log_statistics` is changed from + `true` to `false`, and a database migration is added as well for disabling `log_statistics` if it + has already been enabled upon unsupported providers. type: bugfix diff --git a/kong/plugins/ai-proxy/migrations/001_360_to_370.lua b/kong/plugins/ai-proxy/migrations/001_360_to_370.lua index f6f4b8cfc6d7..012caad11c20 100644 --- a/kong/plugins/ai-proxy/migrations/001_360_to_370.lua +++ b/kong/plugins/ai-proxy/migrations/001_360_to_370.lua @@ -1,4 +1,4 @@ -local operations = require "kong.db.migrations.operations.200_to_210" +local ops = require("kong.db.migrations.operations.200_to_210.lua") local function update_logging_statistic(config) if config.logging.log_statistics and config.route_type == "llm/v1/completions" @@ -8,12 +8,10 @@ local function update_logging_statistic(config) end end - return { postgres = { - up = "", teardown = function(connector) - operations.postgres.teardown:fixup_plugin_config(connector, "ai-proxy", update_logging_statistic) + ops.postgres.teardown:fixup_plugin_config(connector, "ai-proxy", update_logging_statistic) end } } \ No newline at end of file diff --git a/spec/05-migration/plugins/ai-proxy/migrations/001_360_to_370_spec.lua b/spec/05-migration/plugins/ai-proxy/migrations/001_360_to_370_spec.lua index da45109e86c4..98eb0e968c72 100644 --- a/spec/05-migration/plugins/ai-proxy/migrations/001_360_to_370_spec.lua +++ b/spec/05-migration/plugins/ai-proxy/migrations/001_360_to_370_spec.lua @@ -1,59 +1,66 @@ local uh = require "spec.upgrade_helpers" local helpers = require "spec.helpers" +local pgmoon_json = require("pgmoon.json") +local uuid = require "resty.jit-uuid" -for _, strategy in helpers.each_strategy() do +local strategy = "postgres" + +local function render(template, keys) + return (template:gsub("$%(([A-Z_]+)%)", keys)) +end + +if uh.database_type() == strategy then describe("ai-proxy plugin migration", function() - local db, ai_proxy_plugin - uh.setup(function() - _, db = helpers.get_db_utils(strategy, {"plugins"}, { "ai-proxy" }) - local plugin, err, err_t = db.plugins:insert { - name = "ai-proxy", - config = { - route_type = "llm/v1/completions", - auth = { - header_name = "x-api-key", - header_value = "anthropic-key", - }, - model = { - name = "claude-2.1", - provider = "anthropic", - options = { - max_tokens = 256, - temperature = 1.0, - upstream_url = "http://example.com/llm/v1/completions/good", - anthropic_version = "2023-06-01", - }, - }, - logging = { - log_statistics = true, -- anthropic does not support statistics - }, + local plugin_name = "ai-proxy" + local plugin_config = { + route_type = "llm/v1/completions", + auth = { + header_name = "x-api-key", + header_value = "anthropic-key", + }, + model = { + name = "claude-2.1", + provider = "anthropic", + options = { + max_tokens = 256, + temperature = 1.0, + upstream_url = "http://example.com/llm/v1/completions/good", + anthropic_version = "2023-06-01", }, - } - assert.is_nil(err) - assert.is_nil(err_t) - assert.is_not_nil(plugin) - ai_proxy_plugin = plugin - end) + }, + logging = { + log_statistics = true, -- anthropic does not support statistics + }, + } - uh.new_after_up("has updated ai-proxy plugin configuration", function () - -- local cache_key = db.plugins:cache_key("ai-proxy") - local plugin, err = db.plugins:select({ id = ai_proxy_plugin.id }) + uh.setup(function() + local _, db = helpers.get_db_utils(strategy, { "plugins" }) + local id = uuid.generate_v4() + local sql = render([[ + INSERT INTO plugins (id, name, config, enabled) VALUES + ('$(ID)', '$(PLUGIN_NAME)', $(CONFIG)::jsonb, TRUE); + COMMIT; + ]], { + ID = id, + PLUGIN_NAME = plugin_name, + CONFIG = pgmoon_json.encode_json(plugin_config), + }) + + local res, err = db.connector:query(sql) assert.is_nil(err) - assert.is_not_nil(plugin) - -- assert.equal(1, #rows) + assert.is_not_nil(res) + end) - assert.equal("ai-proxy", plugin.name) - local expected_config = { - logging = { - log_statistics = false - }, - route_type = "llm/v1/completions", - model = { - provider = "anthropic" - } - } + uh.new_after_finish("has updated ai-proxy plugin configuration", function () + for plugin, err in helpers.db.connector:iterate("SELECT id, name, config FROM plugins") do + if err then + return nil, err + end - assert.partial_match(expected_config, plugin.config) + if plugin.name == 'ai-proxy' then + assert.falsy(plugin.config.logging.log_statistics) + end + end end) end) -end \ No newline at end of file +end From 560ea4d0b178be5f6c3179e616406eb6f2455787 Mon Sep 17 00:00:00 2001 From: Robin Xiang Date: Fri, 19 Apr 2024 09:22:09 +0800 Subject: [PATCH 3/4] test case --- kong/plugins/ai-proxy/migrations/001_360_to_370.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kong/plugins/ai-proxy/migrations/001_360_to_370.lua b/kong/plugins/ai-proxy/migrations/001_360_to_370.lua index 012caad11c20..ee575080fbb5 100644 --- a/kong/plugins/ai-proxy/migrations/001_360_to_370.lua +++ b/kong/plugins/ai-proxy/migrations/001_360_to_370.lua @@ -1,4 +1,4 @@ -local ops = require("kong.db.migrations.operations.200_to_210.lua") +local ops = require("kong.db.migrations.operations.200_to_210") local function update_logging_statistic(config) if config.logging.log_statistics and config.route_type == "llm/v1/completions" From e6af7891c17e18c800c7b87c135315a5a84239ad Mon Sep 17 00:00:00 2001 From: Robin Xiang Date: Fri, 19 Apr 2024 13:59:18 +0800 Subject: [PATCH 4/4] update rockspec --- kong-3.7.0-0.rockspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kong-3.7.0-0.rockspec b/kong-3.7.0-0.rockspec index 3118916ef9da..cee0f129947f 100644 --- a/kong-3.7.0-0.rockspec +++ b/kong-3.7.0-0.rockspec @@ -569,7 +569,7 @@ build = { ["kong.plugins.ai-proxy.handler"] = "kong/plugins/ai-proxy/handler.lua", ["kong.plugins.ai-proxy.schema"] = "kong/plugins/ai-proxy/schema.lua", ["kong.plugins.ai-proxy.migrations"] = "kong/plugins/ai-proxy/migrations/init.lua", - ["kong.plugins.ai-proxy.migrations.000_base_ai_proxy"] = "kong/plugins/ai-proxy/migrations/001_360_to_370.lua", + ["kong.plugins.ai-proxy.migrations.001_360_to_370"] = "kong/plugins/ai-proxy/migrations/001_360_to_370.lua", ["kong.plugins.ai-request-transformer.handler"] = "kong/plugins/ai-request-transformer/handler.lua", ["kong.plugins.ai-request-transformer.schema"] = "kong/plugins/ai-request-transformer/schema.lua",