Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugins/AI-proxy): db migrations for AI-proxy plugin #12860

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/unreleased/migration_of_ai_proxy_plugin.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions kong-3.7.0-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion kong/llm/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions kong/plugins/ai-proxy/migrations/001_360_to_370.lua
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 3 additions & 0 deletions kong/plugins/ai-proxy/migrations/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
return {
"001_360_to_370",
}
Original file line number Diff line number Diff line change
@@ -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
Loading