Interface LLMProvider
- All Known Implementing Classes:
LangChain4JLLMProvider,SpringAILLMProvider
Use the from(...) factory methods to create a provider from a
vendor-specific model or client object:
// Spring AI LLMProvider provider = LLMProvider.from(chatModel); // LangChain4j LLMProvider provider = LLMProvider.from(streamingChatModel);
- Author:
- Vaadin Ltd.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceRepresents a request to the LLM containing all necessary context, configuration, and tools. -
Method Summary
Modifier and TypeMethodDescriptionstatic LangChain4JLLMProviderfrom(dev.langchain4j.model.chat.ChatModel chatModel) Creates anLLMProviderfrom a LangChain4jChatModel.static LangChain4JLLMProviderfrom(dev.langchain4j.model.chat.StreamingChatModel streamingChatModel) Creates anLLMProviderfrom a LangChain4jStreamingChatModel.static SpringAILLMProviderfrom(org.springframework.ai.chat.client.ChatClient chatClient) Creates anLLMProviderfrom a Spring AIChatClient.static SpringAILLMProviderfrom(org.springframework.ai.chat.model.ChatModel chatModel) Creates anLLMProviderfrom a Spring AIChatModel.default 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
-
from
Creates anLLMProviderfrom a Spring AIChatModel.The provider manages conversation memory internally. Streaming is enabled by default and can be toggled via
SpringAILLMProvider.setStreaming(boolean).- Parameters:
chatModel- the Spring AI chat model, notnull- Returns:
- a new provider instance, never
null - Throws:
NullPointerException- if chatModel isnull
-
from
Creates anLLMProviderfrom a Spring AIChatClient.Use this when the
ChatClientis pre-configured with custom advisors or externally managed memory. Note that providers created from aChatClientdo not support history restoration viasetHistory(List, Map)because the memory is managed externally.- Parameters:
chatClient- the Spring AI chat client, notnull- Returns:
- a new provider instance, never
null - Throws:
NullPointerException- if chatClient isnull
-
from
static LangChain4JLLMProvider from(dev.langchain4j.model.chat.StreamingChatModel streamingChatModel) Creates anLLMProviderfrom a LangChain4jStreamingChatModel.The provider manages conversation memory internally. Responses are streamed token-by-token.
- Parameters:
streamingChatModel- the LangChain4j streaming chat model, notnull- Returns:
- a new provider instance, never
null - Throws:
NullPointerException- if streamingChatModel isnull
-
from
Creates anLLMProviderfrom a LangChain4jChatModel.The provider manages conversation memory internally. Responses are returned as a single block (non-streaming).
- Parameters:
chatModel- the LangChain4j chat model, notnull- Returns:
- a new provider instance, never
null - Throws:
NullPointerException- if chatModel isnull
-
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.- 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
-