Spring Boot Integration
Spring Boot Starters for Popular Integrations
LangChain4j provides Spring Boot starters for popular integrations.
To use one of the Spring Boot starters, first import the corresponding dependency:
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
<version>0.30.0</version>
</dependency>
Then, you can configure model parameters in the application.properties
file as follows:
langchain4j.open-ai.chat-model.api-key=${OPENAI_API_KEY}
...
The complete list of supported properties can be found here.
In this case, an instance of OpenAiChatModel
(an implementation of a ChatLanguageModel
) will be automatically created,
and you can autowire it where needed.
If you need an instance of a StreamingChatLanguageModel
,
use the streaming-chat-model
instead of the chat-model
properties:
langchain4j.open-ai.streaming-chat-model.api-key=${OPENAI_API_KEY}
...
LangChain4j Spring Boot Starter
LangChain4j provides a Spring Boot starter for auto-configuring AI Services, RAG, Tools etc.
Assuming you have already imported one of the integrations starters (see above),
import langchain4j-spring-boot-starter
:
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-spring-boot-starter</artifactId>
<version>0.30.0</version>
</dependency>
You can now define AI Service interface and annotate it with @AiService
:
@AiService
interface Assistant {
@SystemMessage("You are a polite assistant")
String chat(String userMessage);
}
Think of it as a standard Spring Boot @Service
, but with AI capabilities.
When the application starts, LangChain4j starter will scan the classpath
and find all interfaces annotated with @AiService
.
For each AI Service found, it will create an implementation of this interface
using all LangChain4j components available in the application context and will register it as a bean,
so you can auto-wire it where needed:
@RestController
class AssistantController {
@Autowired
Assistant assistant;
@GetMapping("/chat")
public String chat(String message) {
return assistant.chat(message);
}
}
More details here.
Supported Spring Boot Versions
Spring Boot 2 and 3 are supported.