Skip to content

Commit 5bd4de0

Browse files
committed
store last response on chat completion, and return the whole object as second return value
1 parent f2dd072 commit 5bd4de0

4 files changed

Lines changed: 37 additions & 9 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,11 @@ Appends a message to the chat history and triggers a completion with
445445
`generate_response` and returns the response as a string. On failure, returns
446446
`nil`, an error message, and the raw request response.
447447

448+
On success, a second return value contains the raw API response. For
449+
non-streaming requests this is the decoded response object (including fields
450+
like `usage`); for streaming requests it is the raw SSE payload string. The
451+
latest raw response is also stored on `chat.last_response`.
452+
448453
If the response includes `tool_calls` or a `function_call`, the entire message
449454
object is returned instead of a string. You can send the result back by passing
450455
a `role = "tool"` message (with `tool_call_id`) or a `role = "function"` message
@@ -488,6 +493,18 @@ Calls the OpenAI API to generate the next response for the stored chat history.
488493
Returns the response as a string. On failure, returns `nil`, an error message,
489494
and the raw request response.
490495

496+
On success, a second return value contains the raw API response. For
497+
non-streaming requests this is the decoded response object, so you can inspect
498+
fields like `usage`:
499+
500+
```lua
501+
local text, raw = chat:generate_response()
502+
print(text)
503+
print(raw.usage.total_tokens)
504+
```
505+
506+
The latest raw response is also stored on `chat.last_response`.
507+
491508
- `append_response`: Whether the response should be appended to the chat history (default: true).
492509
- `opts`: (optional) A table of per-request overrides. For backward compatibility, a function can be passed and will be treated as `{stream_callback = fn}`.
493510

openai/chat_completions.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ do
184184
end
185185
end
186186
local status, response = self.client:chat(self.messages, params, stream_callback)
187+
self.last_response = response
187188
if status ~= 200 then
188189
local err_msg = "Bad status: " .. tostring(status)
189190
do
@@ -280,9 +281,9 @@ do
280281
self:append_message(message)
281282
end
282283
if message.tool_calls then
283-
return message
284+
return message, response
284285
end
285-
return message.content or message
286+
return message.content or message, response
286287
end
287288
local out, err = parse_chat_response(response)
288289
if not (out) then
@@ -302,7 +303,7 @@ do
302303
end
303304
self:append_message(message)
304305
end
305-
return out.response or out.message
306+
return out.response or out.message, response
306307
end
307308
}
308309
_base_0.__index = _base_0
@@ -313,6 +314,7 @@ do
313314
end
314315
self.client, self.opts = client, opts
315316
self.messages = { }
317+
self.last_response = nil
316318
if type(self.opts.messages) == "table" then
317319
self:append_message(unpack(self.opts.messages))
318320
end

openai/chat_completions.moon

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ import create_stream_filter from require "openai.sse"
153153
class ChatSession
154154
new: (@client, @opts={}) =>
155155
@messages = {}
156+
@last_response = nil
156157

157158
if type(@opts.messages) == "table"
158159
@append_message unpack @opts.messages
@@ -218,6 +219,7 @@ class ChatSession
218219
params[k] = v
219220

220221
status, response = @client\chat @messages, params, stream_callback
222+
@last_response = response
221223

222224
if status != 200
223225
err_msg = "Bad status: #{status}"
@@ -295,9 +297,9 @@ class ChatSession
295297
@append_message message
296298

297299
if message.tool_calls
298-
return message
300+
return message, response
299301

300-
return message.content or message
302+
return message.content or message, response
301303

302304
out, err = parse_chat_response response
303305

@@ -318,7 +320,7 @@ class ChatSession
318320
@append_message message
319321

320322
-- response is missing for function_calls, so we return the entire message object
321-
out.response or out.message
323+
out.response or out.message, response
322324

323325
{
324326
:ChatSession

spec/openai_spec.moon

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ describe "OpenAI API Client", ->
117117
}
118118
}
119119

120-
res = assert chat\send "Who are you?"
120+
res, raw = assert chat\send "Who are you?"
121121
assert.same "I am you", res
122+
assert.same {}, raw.usage
123+
assert.same raw, chat.last_response
122124

123125
-- verify that all the messages are stored
124126
assert.same {
@@ -163,7 +165,10 @@ describe "OpenAI API Client", ->
163165
}
164166
}
165167

166-
assert.same "You're welcome", chat\send "Thank you"
168+
follow_up, raw = chat\send "Thank you"
169+
assert.same "You're welcome", follow_up
170+
assert.same {}, raw.usage
171+
assert.same raw, chat.last_response
167172

168173
it "handles error responses", ->
169174
client = OpenAI "test-api-key"
@@ -571,7 +576,7 @@ describe "OpenAI API Client", ->
571576
stream_callback = (chunk, raw) ->
572577
table.insert chunks_received, {chunk, raw}
573578

574-
response = assert chat\send("Why did the chicken cross the road?", stream_callback)
579+
response, raw = assert chat\send("Why did the chicken cross the road?", stream_callback)
575580

576581
-- Callback receives parsed chunk {content, index} and raw event object
577582
assert.same {
@@ -584,6 +589,8 @@ describe "OpenAI API Client", ->
584589
assert.same "This is ", tostring(chunks_received[1][1])
585590

586591
assert.same "This is a chat response.", response
592+
assert.same "string", type(raw)
593+
assert.same raw, chat.last_response
587594

588595
it "processes streaming chunks with create_chat_completion (raw)", ->
589596
client = OpenAI "test-api-key"

0 commit comments

Comments
 (0)