@@ -67,7 +67,7 @@ for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
6767 local client
6868
6969 lazy_setup (function ()
70- local bp = helpers .get_db_utils (strategy == " off" and " postgres" or strategy , nil , { PLUGIN_NAME })
70+ local bp = helpers .get_db_utils (strategy == " off" and " postgres" or strategy , nil , { PLUGIN_NAME , " ctx-checker-last " , " ctx-checker " })
7171
7272 -- set up openai mock fixtures
7373 local fixtures = {
@@ -274,6 +274,15 @@ for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
274274 path = FILE_LOG_PATH_STATS_ONLY ,
275275 },
276276 }
277+ bp .plugins :insert {
278+ name = " ctx-checker-last" ,
279+ route = { id = chat_good .id },
280+ config = {
281+ ctx_kind = " kong.ctx.shared" ,
282+ ctx_check_field = " llm_model_requested" ,
283+ ctx_check_value = " gpt-3.5-turbo" ,
284+ }
285+ }
277286
278287 -- 200 chat good with one option
279288 local chat_good_no_allow_override = assert (bp .routes :insert {
@@ -544,16 +553,16 @@ for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
544553 }
545554 --
546555
547- -- 200 chat good but no model set
548- local chat_good = assert (bp .routes :insert {
556+ -- 200 chat good but no model set in plugin config
557+ local chat_good_no_model = assert (bp .routes :insert {
549558 service = empty_service ,
550559 protocols = { " http" },
551560 strip_path = true ,
552561 paths = { " /openai/llm/v1/chat/good-no-model-param" }
553562 })
554563 bp .plugins :insert {
555564 name = PLUGIN_NAME ,
556- route = { id = chat_good .id },
565+ route = { id = chat_good_no_model .id },
557566 config = {
558567 route_type = " llm/v1/chat" ,
559568 auth = {
@@ -572,11 +581,20 @@ for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
572581 }
573582 bp .plugins :insert {
574583 name = " file-log" ,
575- route = { id = chat_good .id },
584+ route = { id = chat_good_no_model .id },
576585 config = {
577586 path = " /dev/stdout" ,
578587 },
579588 }
589+ bp .plugins :insert {
590+ name = " ctx-checker-last" ,
591+ route = { id = chat_good_no_model .id },
592+ config = {
593+ ctx_kind = " kong.ctx.shared" ,
594+ ctx_check_field = " llm_model_requested" ,
595+ ctx_check_value = " try-to-override-the-model" ,
596+ }
597+ }
580598 --
581599
582600 -- 200 completions good using post body key
@@ -755,7 +773,7 @@ for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
755773 },
756774 },
757775 }
758- --
776+
759777
760778 -- start kong
761779 assert (helpers .start_kong ({
@@ -764,7 +782,7 @@ for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
764782 -- use the custom test template to create a local mock server
765783 nginx_conf = " spec/fixtures/custom_nginx.template" ,
766784 -- make sure our plugin gets loaded
767- plugins = " bundled," .. PLUGIN_NAME ,
785+ plugins = " bundled,ctx-checker-last,ctx-checker, " .. PLUGIN_NAME ,
768786 -- write & load declarative config, only if 'strategy=off'
769787 declarative_config = strategy == " off" and helpers .make_yaml_file () or nil ,
770788 }, nil , nil , fixtures ))
@@ -835,6 +853,7 @@ for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
835853 assert .same (first_expected , first_got )
836854 assert .is_true (actual_llm_latency >= 0 )
837855 assert .same (actual_time_per_token , time_per_token )
856+ assert .same (first_got .meta .request_model , " gpt-3.5-turbo" )
838857 end )
839858
840859 it (" does not log statistics" , function ()
@@ -1030,6 +1049,9 @@ for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
10301049 content = " The sum of 1 + 1 is 2." ,
10311050 role = " assistant" ,
10321051 }, json .choices [1 ].message )
1052+
1053+ -- from ctx-checker-last plugin
1054+ assert .equals (r .headers [" ctx-checker-last-llm-model-requested" ], " gpt-3.5-turbo" )
10331055 end )
10341056
10351057 it (" good request, parses model of cjson.null" , function ()
@@ -1110,6 +1132,38 @@ for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
11101132 assert .is_truthy (json .error )
11111133 assert .equals (json .error .message , " request format not recognised" )
11121134 end )
1135+
1136+ -- check that kong.ctx.shared.llm_model_requested is set
1137+ it (" good request setting model from client body" , function ()
1138+ local r = client :get (" /openai/llm/v1/chat/good-no-model-param" , {
1139+ headers = {
1140+ [" content-type" ] = " application/json" ,
1141+ [" accept" ] = " application/json" ,
1142+ },
1143+ body = pl_file .read (" spec/fixtures/ai-proxy/openai/llm-v1-chat/requests/good_own_model.json" ),
1144+ })
1145+
1146+ -- validate that the request succeeded, response status 200
1147+ local body = assert .res_status (200 , r )
1148+ local json = cjson .decode (body )
1149+
1150+ -- check this is in the 'kong' response format
1151+ assert .equals (json .id , " chatcmpl-8T6YwgvjQVVnGbJ2w8hpOA17SeNy2" )
1152+ assert .equals (json .model , " gpt-3.5-turbo-0613" )
1153+ assert .equals (json .object , " chat.completion" )
1154+ assert .equals (r .headers [" X-Kong-LLM-Model" ], " openai/try-to-override-the-model" )
1155+
1156+ assert .is_table (json .choices )
1157+ assert .is_table (json .choices [1 ].message )
1158+ assert .same ({
1159+ content = " The sum of 1 + 1 is 2." ,
1160+ role = " assistant" ,
1161+ }, json .choices [1 ].message )
1162+
1163+ -- from ctx-checker-last plugin
1164+ assert .equals (r .headers [" ctx-checker-last-llm-model-requested" ], " try-to-override-the-model" )
1165+ end )
1166+
11131167 end )
11141168
11151169 describe (" openai llm/v1/completions" , function ()
0 commit comments