Skip to content

Commit 349dfbe

Browse files
return http code when read chunk throw error
1 parent e0a8256 commit 349dfbe

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

apisix/plugins/ai-drivers/openai-base.lua

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ end
141141
function _M.read_response(ctx, res)
142142
local body_reader = res.body_reader
143143
if not body_reader then
144-
core.log.error("AI service sent no response body")
144+
core.log.warn("AI service sent no response body")
145145
return 500
146146
end
147147

@@ -152,11 +152,14 @@ function _M.read_response(ctx, res)
152152
while true do
153153
local chunk, err = body_reader() -- will read chunk by chunk
154154
if err then
155-
core.log.error("failed to read response chunk: ", err)
156-
break
155+
core.log.warn("failed to read response chunk: ", err)
156+
if core.string.find(err, "timeout") then
157+
return 504
158+
end
159+
return 500
157160
end
158161
if not chunk then
159-
break
162+
return
160163
end
161164

162165
ngx_print(chunk)
@@ -192,7 +195,8 @@ function _M.read_response(ctx, res)
192195

193196
-- usage field is null for non-last events, null is parsed as userdata type
194197
if data and data.usage and type(data.usage) ~= "userdata" then
195-
core.log.info("got token usage from ai service: ", core.json.delay_encode(data.usage))
198+
core.log.info("got token usage from ai service: ",
199+
core.json.delay_encode(data.usage))
196200
ctx.ai_token_usage = {
197201
prompt_tokens = data.usage.prompt_tokens or 0,
198202
completion_tokens = data.usage.completion_tokens or 0,
@@ -208,7 +212,7 @@ function _M.read_response(ctx, res)
208212

209213
local raw_res_body, err = res:read_body()
210214
if not raw_res_body then
211-
core.log.error("failed to read response body: ", err)
215+
core.log.warn("failed to read response body: ", err)
212216
if core.string.find(err, "timeout") then
213217
return 504
214218
end

apisix/plugins/ai-proxy/base.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function _M.before_proxy(conf, ctx)
4646

4747
local res, err = ai_driver:request(conf, request_body, extra_opts)
4848
if not res then
49-
core.log.error("failed to send request to AI service: ", err)
49+
core.log.warn("failed to send request to AI service: ", err)
5050
if core.string.find(err, "timeout") then
5151
return 504
5252
end

0 commit comments

Comments
 (0)