@@ -23,7 +23,40 @@ import { ToToolCall } from './tool-call';
23
23
import { ToToolResult } from './tool-result' ;
24
24
25
25
/**
26
- * Stream text generated by a language model.
26
+ Generate a text and call tools for a given prompt using a language model.
27
+
28
+ This function streams the output. If you do not want to stream the output, use `experimental_generateText` instead.
29
+
30
+ @param model - The language model to use.
31
+ @param tools - The tools that the model can call. The model needs to support calling tools.
32
+
33
+ @param system - A system message that will be part of the prompt.
34
+ @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
35
+ @param messages - A list of messages. You can either use `prompt` or `messages` but not both.
36
+
37
+ @param maxTokens - Maximum number of tokens to generate.
38
+ @param temperature - Temperature setting.
39
+ This is a number between 0 (almost no randomness) and 1 (very random).
40
+ It is recommended to set either `temperature` or `topP`, but not both.
41
+ @param topP - Nucleus sampling. This is a number between 0 and 1.
42
+ E.g. 0.1 would mean that only tokens with the top 10% probability mass are considered.
43
+ It is recommended to set either `temperature` or `topP`, but not both.
44
+ @param presencePenalty - Presence penalty setting.
45
+ It affects the likelihood of the model to repeat information that is already in the prompt.
46
+ The presence penalty is a number between -1 (increase repetition) and 1 (maximum penalty, decrease repetition).
47
+ 0 means no penalty.
48
+ @param frequencyPenalty - Frequency penalty setting.
49
+ It affects the likelihood of the model to repeatedly use the same words or phrases.
50
+ The frequency penalty is a number between -1 (increase repetition) and 1 (maximum penalty, decrease repetition).
51
+ 0 means no penalty.
52
+ @param seed - The seed (integer) to use for random sampling.
53
+ If set and supported by the model, calls will generate deterministic results.
54
+
55
+ @param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
56
+ @param abortSignal - An optional abort signal that can be used to cancel the call.
57
+
58
+ @return
59
+ A result object for accessing different stream types and additional information.
27
60
*/
28
61
export async function experimental_streamText <
29
62
TOOLS extends Record < string , ExperimentalTool > ,
@@ -38,7 +71,14 @@ export async function experimental_streamText<
38
71
...settings
39
72
} : CallSettings &
40
73
Prompt & {
74
+ /**
75
+ The language model to use.
76
+ */
41
77
model : LanguageModelV1 ;
78
+
79
+ /**
80
+ The tools that the model can call. The model needs to support calling tools.
81
+ */
42
82
tools ?: TOOLS ;
43
83
} ) : Promise < StreamTextResult < TOOLS > > {
44
84
const retry = retryWithExponentialBackoff ( { maxRetries } ) ;
@@ -101,9 +141,15 @@ export type TextStreamPart<TOOLS extends Record<string, ExperimentalTool>> =
101
141
} ;
102
142
} ;
103
143
144
+ /**
145
+ A result object for accessing different stream types and additional information.
146
+ */
104
147
export class StreamTextResult < TOOLS extends Record < string , ExperimentalTool > > {
105
148
private readonly originalStream : ReadableStream < TextStreamPart < TOOLS > > ;
106
149
150
+ /**
151
+ Warnings from the model provider (e.g. unsupported settings)
152
+ */
107
153
readonly warnings : LanguageModelV1CallWarning [ ] | undefined ;
108
154
109
155
constructor ( {
@@ -117,6 +163,11 @@ export class StreamTextResult<TOOLS extends Record<string, ExperimentalTool>> {
117
163
this . warnings = warnings ;
118
164
}
119
165
166
+ /**
167
+ A text stream that returns only the generated text deltas. You can use it
168
+ as either an AsyncIterable or a ReadableStream. When an error occurs, the
169
+ stream will throw the error.
170
+ */
120
171
get textStream ( ) : AsyncIterableStream < string > {
121
172
return createAsyncIterableStream ( this . originalStream , {
122
173
transform ( chunk , controller ) {
@@ -132,6 +183,12 @@ export class StreamTextResult<TOOLS extends Record<string, ExperimentalTool>> {
132
183
} ) ;
133
184
}
134
185
186
+ /**
187
+ A stream with all events, including text deltas, tool calls, tool results, and
188
+ errors.
189
+ You can use it as either an AsyncIterable or a ReadableStream. When an error occurs, the
190
+ stream will throw the error.
191
+ */
135
192
get fullStream ( ) : AsyncIterableStream < TextStreamPart < TOOLS > > {
136
193
return createAsyncIterableStream ( this . originalStream , {
137
194
transform ( chunk , controller ) {
@@ -147,6 +204,15 @@ export class StreamTextResult<TOOLS extends Record<string, ExperimentalTool>> {
147
204
} ) ;
148
205
}
149
206
207
+ /**
208
+ Converts the result to an `AIStream` object that is compatible with `StreamingTextResponse`.
209
+ It can be used with the `useChat` and `useCompletion` hooks.
210
+
211
+ @param callbacks
212
+ Stream callbacks that will be called when the stream emits events.
213
+
214
+ @returns an `AIStream` object.
215
+ */
150
216
toAIStream ( callbacks ?: AIStreamCallbacksAndOptions ) {
151
217
// TODO add support for tool calls
152
218
return readableFromAsyncIterable ( this . textStream )
0 commit comments