Class ChatCompletionAccumulator
-
- All Implemented Interfaces:
public final class ChatCompletionAccumulator
An accumulator that constructs a ChatCompletion from a sequence of streamed chunks. Pass all chunks to accumulate and then call chatCompletion to get the final accumulated chat completion. The final ChatCompletion will be similar to what would have been received had the non-streaming API been used.
A ChatCompletionAccumulator may only be used to accumulate one chat completion. To accumulate another chat completion, create another instance of ChatCompletionAccumulator.
-
-
Method Summary
Modifier and Type Method Description final ChatCompletion
chatCompletion()
Gets the final accumulated chat completion. final ChatCompletionChunk
accumulate(ChatCompletionChunk chunk)
Accumulates a streamed chunk and uses it to construct a ChatCompletion. final static ChatCompletionAccumulator
create()
-
-
Method Detail
-
chatCompletion
final ChatCompletion chatCompletion()
Gets the final accumulated chat completion. Until the last chunk has been accumulated, a ChatCompletion will not be available. Wait until all chunks have been handled by accumulate before calling this method. See that method for more details on how the last chunk is detected.
-
accumulate
final ChatCompletionChunk accumulate(ChatCompletionChunk chunk)
Accumulates a streamed chunk and uses it to construct a ChatCompletion. When all chunks have been accumulated, the chat completion can be retrieved by calling chatCompletion.
The last chunk is identified as that which provides the
finishReason
. At that point, the ChatCompletion is created and available. However, if the request was configured to include the usage details, one additional chunk may be accumulated which provides those usage details and the ChatCompletion will be recreated to reflect them. After that, no more chunks of any kind may be accumulated.- Returns:
The given chunk for convenience, such as when chaining method calls.
-
create
final static ChatCompletionAccumulator create()
-
-
-
-