I am successfully streaming a response from this endpoint using the 'gpt-3.5-turbo'
model, so I feel the implementation is right, but I am consistently missing the very first token. Is anybody else seeing this?
Here is my stream reader implementation (JavaScript):
const reader = response.body.getReader()
reader.read().then(function processingText({ done, value }) {
if (done) return
const decoded = new TextDecoder().decode(value)
const json = decoded.split('data: ')[1] // this data needs some manipulation in order to be parsed, a separate concern
const aiResponse = JSON.parse(json)
const aiResponseText = aiResponse.choices[0].delta?.content
result += aiResponseText || ""
return reader.read().then(processingText)
}).catch(console.error)
And here is what a sample response looks like:
data: {"id":"chatcmpl-7BWXDk8d3xI1ycw2fzoudrXCCKniY","object":"chat.completion.chunk","created":1682981027,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":" Mr"},"index":0,"finish_reason":null}]}
and after parsing:
[
{
"delta": {
"content": " Mr"
},
"index": 0,
"finish_reason": null
}
]
Notice the empty space before “Mr”. This is because there is supposed to be a “Dear” in front of that token. I am asking the model to compose a letter. I know it should contain the “Dear” because every other test I’ve run without stream has included this token first.
That is technically the second token I receive in the stream, the first looks like this:
[
{
"delta": {
"role": "assistant"
},
"index": 0,
"finish_reason": null
}
]
Notice there is no content
property on this object, as it seems to just be a metadata chunk. No issue there, but there is also no initial token between this chunk and the next chunk above (" Mr").
So, where is this missing token? A bug perhaps? I have combed the API response for this initial token, but it is absent.