Interface LLMProvider
- All Known Implementing Classes:
LangChain4JLLMProvider,SpringAILLMProvider
Create an instance by constructing the appropriate implementation directly:
// Spring AI LLMProvider provider = new SpringAILLMProvider(chatModel); // LangChain4j LLMProvider provider = new LangChain4JLLMProvider(streamingChatModel);
- Since:
- 25.1
- Author:
- Vaadin Ltd.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceRepresents a request to the LLM containing all necessary context, configuration, and tools.static interfaceA framework-agnostic tool definition that the LLM can invoke. -
Method Summary
Modifier and TypeMethodDescriptiondefault voidsetHistory(List<ChatMessage> history, Map<String, List<AIAttachment>> attachmentsByMessageId) Restores the provider's conversation memory from a list of chat messages with their associated attachments.reactor.core.publisher.Flux<String> stream(LLMProvider.LLMRequest request) Streams a response from the LLM based on the provided request.
-
Method Details
-
stream
Streams a response from the LLM based on the provided request. This method returns a reactive stream that emits response tokens as they become available from the LLM. The provider manages conversation history internally, so each call to this method adds to the ongoing conversation context.Threading: this method is called on the thread that triggers the prompt, and the returned stream is subscribed to on that same thread. An implementation whose LLM call blocks must therefore schedule that call itself — for example with
subscribeOn(Schedulers.boundedElastic())— otherwise it occupies the UI thread and holds the session lock for the whole turn, and nothing the turn produces reaches the browser until it ends. The built-in providers expose this as asetBackgroundExecution(boolean)setting, since running the turn on the request thread is the simpler default when a turn is short.Callers only consume the stream and do not schedule it, so whether a turn runs in the background is decided entirely by the implementation.
- Parameters:
request- the LLM request containing user message, system prompt, attachments, and tools, notnull- Returns:
- a Flux stream that emits response tokens as strings, never
null - Throws:
NullPointerException- if request isnull
-
setHistory
default void setHistory(List<ChatMessage> history, Map<String, List<AIAttachment>> attachmentsByMessageId) Restores the provider's conversation memory from a list of chat messages with their associated attachments. Any existing memory is cleared before the new history is applied.Providers that support setting chat history should override this method.
This method must not be called while a streaming response is in progress.
- Parameters:
history- the list of chat messages to restore, notnullattachmentsByMessageId- a map fromChatMessage.messageId()to the list of attachments for that message, notnull- Throws:
NullPointerException- if any argument isnullUnsupportedOperationException- if this provider does not support chat history restoration
-