I would like to use a LangChain4j service with memory and streaming.
Code without streaming:
Assistant assistant = AiServices.builder(Assistant.class)
.chatLanguageModel(model)
.chatMemory(chatMemory)
.build();
This code does not work if the type of the model is StreamingChatLanguageModel. How can I do in this case?
You can declare the return type of the AiService method as a
TokenStream
:Then create the Assistant:
Then invoke it with streaming:
Here is an example, but without a memory: https://github.com/langchain4j/langchain4j-examples/blob/main/other-examples/src/main/java/ServiceWithStreamingExample.java