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..d9c275e3cdd7 --- /dev/null +++ b/changelog/unreleased/migration_of_ai_proxy_plugin.yml @@ -0,0 +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 already been enabled upon unsupported providers. +type: bugfix diff --git a/kong-3.7.0-0.rockspec b/kong-3.7.0-0.rockspec index 7414bd0ae452..cee0f129947f 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.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", 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..ee575080fbb5 --- /dev/null +++ b/kong/plugins/ai-proxy/migrations/001_360_to_370.lua @@ -0,0 +1,17 @@ +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" + and config.model.provider == "anthropic" then + config.logging.log_statistics = false + return true + end +end + +return { + postgres = { + teardown = function(connector) + ops.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..98eb0e968c72 --- /dev/null +++ b/spec/05-migration/plugins/ai-proxy/migrations/001_360_to_370_spec.lua @@ -0,0 +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" + +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 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", + }, + }, + logging = { + log_statistics = true, -- anthropic does not support statistics + }, + } + + 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(res) + end) + + 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 + + if plugin.name == 'ai-proxy' then + assert.falsy(plugin.config.logging.log_statistics) + end + end + end) + end) +end