Package dev.langchain4j.service
Class AiServiceTokenStream
java.lang.Object
dev.langchain4j.service.AiServiceTokenStream
- All Implemented Interfaces:
TokenStream
-
Constructor Summary
ConstructorsConstructorDescriptionAiServiceTokenStream
(AiServiceTokenStreamParameters parameters) Creates a new instance ofAiServiceTokenStream
with the given parameters. -
Method Summary
Modifier and TypeMethodDescriptionbeforeToolExecution
(Consumer<BeforeToolExecution> beforeToolExecutionHandler) The provided consumer will be invoked right before a tool is executed.All errors during streaming will be ignored (but will be logged with a WARN log level).onCompleteResponse
(Consumer<ChatResponse> completionHandler) The provided consumer will be invoked when a language model finishes streaming the final chat response, as opposed to the intermediate response (seeTokenStream.onIntermediateResponse(Consumer)
).The provided consumer will be invoked when an error occurs during streaming.onIntermediateResponse
(Consumer<ChatResponse> intermediateResponseHandler) The provided consumer will be invoked when a language model finishes streaming the intermediate chat response, as opposed to the final response (seeTokenStream.onCompleteResponse(Consumer)
).onPartialResponse
(Consumer<String> partialResponseHandler) The provided consumer will be invoked every time a new partial textual response (usually a single token) from a language model is available.onPartialThinking
(Consumer<PartialThinking> partialThinkingHandler) The provided consumer will be invoked every time a new partial thinking/reasoning text (usually a single token) from a language model is available.onRetrieved
(Consumer<List<Content>> contentsHandler) The provided consumer will be invoked if anyContent
s are retrieved usingRetrievalAugmentor
.onToolExecuted
(Consumer<ToolExecution> toolExecutionHandler) The provided consumer will be invoked right after a tool is executed.void
start()
Completes the current token stream building and starts processing.
-
Constructor Details
-
AiServiceTokenStream
Creates a new instance ofAiServiceTokenStream
with the given parameters.- Parameters:
parameters
- the parameters for creating the token stream
-
-
Method Details
-
onPartialResponse
Description copied from interface:TokenStream
The provided consumer will be invoked every time a new partial textual response (usually a single token) from a language model is available.- Specified by:
onPartialResponse
in interfaceTokenStream
- Parameters:
partialResponseHandler
- lambda that will be invoked when a model generates a new partial textual response- Returns:
- token stream instance used to configure or start stream processing
-
onPartialThinking
Description copied from interface:TokenStream
The provided consumer will be invoked every time a new partial thinking/reasoning text (usually a single token) from a language model is available.- Specified by:
onPartialThinking
in interfaceTokenStream
- Parameters:
partialThinkingHandler
- lambda that will be invoked when a model generates a new partial thinking/reasoning text- Returns:
- token stream instance used to configure or start stream processing
-
onRetrieved
Description copied from interface:TokenStream
The provided consumer will be invoked if anyContent
s are retrieved usingRetrievalAugmentor
.The invocation happens before any call is made to the language model.
- Specified by:
onRetrieved
in interfaceTokenStream
- Parameters:
contentsHandler
- lambda that consumes all retrieved contents- Returns:
- token stream instance used to configure or start stream processing
-
onIntermediateResponse
Description copied from interface:TokenStream
The provided consumer will be invoked when a language model finishes streaming the intermediate chat response, as opposed to the final response (seeTokenStream.onCompleteResponse(Consumer)
). Intermediate chat responses containToolExecutionRequest
s, AI service will execute them after returning from this consumer.- Specified by:
onIntermediateResponse
in interfaceTokenStream
- Parameters:
intermediateResponseHandler
- lambda that consumes intermediate chat responses- Returns:
- token stream instance used to configure or start stream processing
- See Also:
-
beforeToolExecution
Description copied from interface:TokenStream
The provided consumer will be invoked right before a tool is executed.- Specified by:
beforeToolExecution
in interfaceTokenStream
- Parameters:
beforeToolExecutionHandler
- lambda that consumesBeforeToolExecution
- Returns:
- token stream instance used to configure or start stream processing
-
onToolExecuted
Description copied from interface:TokenStream
The provided consumer will be invoked right after a tool is executed.The invocation happens after the tool method has finished and before any other tool is executed.
- Specified by:
onToolExecuted
in interfaceTokenStream
- Parameters:
toolExecutionHandler
- lambda that consumesToolExecution
- Returns:
- token stream instance used to configure or start stream processing
-
onCompleteResponse
Description copied from interface:TokenStream
The provided consumer will be invoked when a language model finishes streaming the final chat response, as opposed to the intermediate response (seeTokenStream.onIntermediateResponse(Consumer)
).Please note that
ChatResponse.tokenUsage()
contains aggregate token usage across all calls to the LLM. It is a sum ofChatResponse.tokenUsage()
s of all intermediate responses (TokenStream.onIntermediateResponse(Consumer)
).- Specified by:
onCompleteResponse
in interfaceTokenStream
- Parameters:
completionHandler
- lambda that will be invoked when language model finishes streaming- Returns:
- token stream instance used to configure or start stream processing
- See Also:
-
onError
Description copied from interface:TokenStream
The provided consumer will be invoked when an error occurs during streaming.- Specified by:
onError
in interfaceTokenStream
- Parameters:
errorHandler
- lambda that will be invoked when an error occurs- Returns:
- token stream instance used to configure or start stream processing
-
ignoreErrors
Description copied from interface:TokenStream
All errors during streaming will be ignored (but will be logged with a WARN log level).- Specified by:
ignoreErrors
in interfaceTokenStream
- Returns:
- token stream instance used to configure or start stream processing
-
start
public void start()Description copied from interface:TokenStream
Completes the current token stream building and starts processing.Will send a request to LLM and start response streaming.
- Specified by:
start
in interfaceTokenStream
-