Skip to content

Commit fd9457c

Browse files
committed
update readme for new stream format
1 parent aec6edc commit fd9457c

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,12 @@ client:create_chat_completion({
146146
}, {
147147
stream = true
148148
}, function(chunk)
149-
io.stdout:write(chunk.content)
150-
io.stdout:flush()
149+
-- Raw event object from API: access content via choices[1].delta.content
150+
local delta = chunk.choices and chunk.choices[1] and chunk.choices[1].delta
151+
if delta and delta.content then
152+
io.stdout:write(delta.content)
153+
io.stdout:flush()
154+
end
151155
end)
152156

153157
print() -- print a newline
@@ -203,15 +207,19 @@ server-side via `previous_response_id`.
203207

204208
##### `client:create_chat_completion(messages, opts, chunk_callback)`
205209

206-
Sends a request to the `/chat/completions` endpoint. Also available as `client:chat(...)` for backward compatibility.
210+
Sends a request to the `/chat/completions` endpoint.
207211

208212
- `messages`: An array of message objects.
209213
- `opts`: Additional options for the chat, passed directly to the API (eg. model, temperature, etc.) https://platform.openai.com/docs/api-reference/chat
210-
- `chunk_callback`: A function to be called for parsed streaming output when `stream = true` is passed to `opts`.
214+
- `chunk_callback`: A function to be called for each raw event object when `stream = true` is passed to `opts`. Each chunk is the parsed API response (eg. `{object = "chat.completion.chunk", choices = {{delta = {content = "..."}, index = 0}}}`).
211215

212216
Returns HTTP status, response object, and output headers. The response object
213217
will be decoded from JSON if possible, otherwise the raw string is returned.
214218

219+
##### `client:chat(messages, opts, chunk_callback)`
220+
221+
Legacy alias for `create_chat_completion` with filtered streaming chunks. When streaming, the callback receives parsed chunks in the format `{content = "...", index = ...}` instead of raw event objects.
222+
215223
##### `client:completion(prompt, opts)`
216224

217225
Sends a request to the `/completions` endpoint.

0 commit comments

Comments
 (0)