Class: BraintrustStream
A Braintrust stream. This is a wrapper around a ReadableStream of BraintrustStreamChunk
,
with some utility methods to make them easy to log and convert into various formats.
Constructors
constructor
• new BraintrustStream(baseStream
): BraintrustStream
Parameters
Name | Type |
---|---|
baseStream | ReadableStream <Uint8Array > |
Returns
• new BraintrustStream(stream
): BraintrustStream
Parameters
Name | Type |
---|---|
stream | ReadableStream <string > |
Returns
• new BraintrustStream(stream
): BraintrustStream
Parameters
Name | Type |
---|---|
stream | ReadableStream <{ data : string ; type : "text_delta" } | { data : string ; type : "json_delta" }> |
Returns
Methods
[asyncIterator]
▸ [asyncIterator](): AsyncIterator
<{ data
: string
; type
: "text_delta"
} | { data
: string
; type
: "json_delta"
}, any
, undefined
>
Returns an async iterator for the BraintrustStream. This allows for easy consumption of the stream using a for-await...of loop.
Returns
AsyncIterator
<{ data
: string
; type
: "text_delta"
} | { data
: string
; type
: "json_delta"
}, any
, undefined
>
An async iterator that yields BraintrustStreamChunk objects.
copy
▸ copy(): BraintrustStream
Copy the stream. This returns a new stream that shares the same underlying
stream (via tee
). Since streams are consumed in Javascript, use copy()
if you
need to use the stream multiple times.
Returns
A new stream that you can independently consume.
finalValue
▸ finalValue(): Promise
<unknown
>
Get the final value of the stream. The final value is the concatenation of all the chunks in the stream, deserialized into a string or JSON object, depending on the value's type.
This function returns a promise that resolves when the stream is closed, and
contains the final value. Multiple calls to finalValue()
will return the same
promise, so it is safe to call this multiple times.
This function consumes the stream, so if you need to use the stream multiple
times, you should call copy()
first.
Returns
Promise
<unknown
>
A promise that resolves with the final value of the stream or undefined
if the stream is empty.
toReadableStream
▸ toReadableStream(): ReadableStream
<{ data
: string
; type
: "text_delta"
} | { data
: string
; type
: "json_delta"
}>
Get the underlying ReadableStream.
Returns
ReadableStream
<{ data
: string
; type
: "text_delta"
} | { data
: string
; type
: "json_delta"
}>
The underlying ReadableStream.