@@ -496,3 +496,90 @@ max_tokens=555
496496 }
497497--- response_body
498498max_tokens=555
499+
500+
501+
502+ === TEST 10: openai chat - deprecated max_tokens in body is respected in default mode and cleared in force mode
503+ --- config
504+ location /t {
505+ content_by_lua_block {
506+ local t = require("lib.test_admin").test
507+
508+ -- Route with default mode (no force)
509+ local code = t('/apisix/admin/routes/1',
510+ ngx.HTTP_PUT,
511+ [[{
512+ "uri": "/chat",
513+ "plugins": {
514+ "ai-proxy": {
515+ "provider": "openai",
516+ "model": { "name": "gpt-4" },
517+ "auth": { "header": { "Authorization": "Bearer t" } },
518+ "override": {
519+ "endpoint": "http://localhost:6732",
520+ "request_body": {
521+ "max_tokens": 999
522+ }
523+ },
524+ "ssl_verify": false
525+ }
526+ }
527+ }]]
528+ )
529+ if code >= 300 then ngx.status = code; return end
530+
531+ local http = require("resty.http").new()
532+ local cjson = require("cjson.safe")
533+
534+ -- Client sends deprecated max_tokens=200; default mode should NOT override
535+ local res = assert(http:request_uri("http://127.0.0.1:" .. ngx.var.server_port .. "/chat", {
536+ method = "POST",
537+ body = '{"messages":[{"role":"user","content":"hi"}],"max_tokens":200}',
538+ headers = { ["Content-Type"] = "application/json" },
539+ }))
540+ local body = cjson.decode(res.body)
541+ local echoed = cjson.decode(body.choices[1].message.content)
542+ ngx.say("default: max_tokens=", echoed.max_tokens,
543+ " max_completion_tokens=", echoed.max_completion_tokens)
544+
545+ -- Switch to force mode
546+ code = t('/apisix/admin/routes/1',
547+ ngx.HTTP_PUT,
548+ [[{
549+ "uri": "/chat",
550+ "plugins": {
551+ "ai-proxy": {
552+ "provider": "openai",
553+ "model": { "name": "gpt-4" },
554+ "auth": { "header": { "Authorization": "Bearer t" } },
555+ "override": {
556+ "endpoint": "http://localhost:6732",
557+ "request_body": {
558+ "max_tokens": 999
559+ },
560+ "request_body_force_override": true
561+ },
562+ "ssl_verify": false
563+ }
564+ }
565+ }]]
566+ )
567+ if code >= 300 then ngx.status = code; return end
568+
569+ ngx.sleep(0.5)
570+
571+ -- Client sends deprecated max_tokens=200; force mode should clear it and set max_completion_tokens
572+ res = assert(http:request_uri("http://127.0.0.1:" .. ngx.var.server_port .. "/chat", {
573+ method = "POST",
574+ body = '{"messages":[{"role":"user","content":"hi"}],"max_tokens":200}',
575+ headers = { ["Content-Type"] = "application/json" },
576+ }))
577+ body = cjson.decode(res.body)
578+ echoed = cjson.decode(body.choices[1].message.content)
579+ ngx.say("force: max_tokens=", echoed.max_tokens,
580+ " max_completion_tokens=", echoed.max_completion_tokens)
581+ }
582+ }
583+ --- response_body
584+ default: max_tokens=200 max_completion_tokens=nil
585+ force: max_tokens=nil max_completion_tokens=999
0 commit comments