Skip to content

Commit 65cb6ae

Browse files
committed
feat: move Spring AI init to own config
1 parent ce56d88 commit 65cb6ae

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.learnhub.config;
2+
3+
import java.util.List;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.ai.chat.client.ChatClient;
7+
import org.springframework.ai.chat.model.ChatModel;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
11+
/**
12+
* Configuration for Spring AI chat clients.
13+
* Manually configures the ChatClient bean from available ChatModel instances.
14+
*/
15+
@Configuration
16+
public class SpringAIConfiguration {
17+
18+
private static final Logger log = LoggerFactory.getLogger(SpringAIConfiguration.class);
19+
20+
/**
21+
* Creates a ChatClient from the available ChatModel beans.
22+
*
23+
* @param chatModels list of available chat models (may be empty if none configured)
24+
* @return a configured ChatClient, or null if no model is available
25+
*/
26+
@Bean
27+
public ChatClient chatClient(List<ChatModel> chatModels) {
28+
if (chatModels == null || chatModels.isEmpty()) {
29+
log.warn("No ChatModel found. LLM features will be disabled.");
30+
return null;
31+
}
32+
for (ChatModel model : chatModels) {
33+
log.info("Found Chat Model: {} with temperature: {}",
34+
model.getDefaultOptions().getModel(),
35+
model.getDefaultOptions().getTemperature());
36+
}
37+
ChatModel chatModel = chatModels.getFirst();
38+
return ChatClient.builder(chatModel).build();
39+
}
40+
}

server/src/main/java/com/learnhub/documentmanagement/service/LLMService.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.slf4j.LoggerFactory;
1111
import org.springframework.ai.chat.client.ChatClient;
1212
import org.springframework.ai.chat.prompt.PromptTemplate;
13-
import org.springframework.beans.factory.ObjectProvider;
1413
import org.springframework.beans.factory.annotation.Value;
1514
import org.springframework.core.io.Resource;
1615
import org.springframework.stereotype.Service;
@@ -37,15 +36,14 @@ public class LLMService {
3736
private final ChatClient chatClient;
3837
private final ObjectMapper objectMapper = new ObjectMapper();
3938

40-
public LLMService(ObjectProvider<ChatClient.Builder> chatClientBuilderProvider) {
41-
ChatClient.Builder builder = chatClientBuilderProvider.getIfAvailable();
42-
this.chatClient = builder != null ? builder.build() : null;
43-
}
44-
45-
public Map<String, Object> extractActivityData(String pdfText) {
39+
public LLMService(ChatClient chatClient) {
4640
if (chatClient == null) {
4741
throw new IllegalStateException("ChatClient is not available. Please configure a ChatModel.");
4842
}
43+
this.chatClient = chatClient;
44+
}
45+
46+
public Map<String, Object> extractActivityData(String pdfText) {
4947

5048
String promptText = new PromptTemplate(extractionPromptResource).render(Map.of("pdfText", pdfText));
5149

@@ -77,10 +75,6 @@ public Map<String, Object> extractActivityData(String pdfText) {
7775
* user-adjusted activity metadata to inform the schema
7876
*/
7977
public String generateArtikulationsschema(String pdfText, Map<String, Object> metadata) {
80-
if (chatClient == null) {
81-
throw new IllegalStateException("ChatClient is not available. Please configure a ChatModel.");
82-
}
83-
8478
String metadataSection = buildMetadataSection(metadata);
8579
String promptText = new PromptTemplate(artikulationsschemaPromptResource)
8680
.render(Map.of("metadataSection", metadataSection, "pdfText", pdfText));
@@ -105,10 +99,6 @@ public String generateArtikulationsschema(String pdfText, Map<String, Object> me
10599
* user-adjusted activity metadata to inform the generation
106100
*/
107101
public String generateDeckblatt(String pdfText, Map<String, Object> metadata) {
108-
if (chatClient == null) {
109-
throw new IllegalStateException("ChatClient is not available. Please configure a ChatModel.");
110-
}
111-
112102
String metadataSection = buildMetadataSection(metadata);
113103
String promptText = new PromptTemplate(deckblattPromptResource)
114104
.render(Map.of("metadataSection", metadataSection, "pdfText", pdfText));
@@ -134,10 +124,6 @@ public String generateDeckblatt(String pdfText, Map<String, Object> metadata) {
134124
* user-adjusted activity metadata to inform the generation
135125
*/
136126
public String generateHintergrundwissen(String pdfText, Map<String, Object> metadata) {
137-
if (chatClient == null) {
138-
throw new IllegalStateException("ChatClient is not available. Please configure a ChatModel.");
139-
}
140-
141127
String metadataSection = buildMetadataSection(metadata);
142128
String promptText = new PromptTemplate(hintergrundwissenPromptResource)
143129
.render(Map.of("metadataSection", metadataSection, "pdfText", pdfText));

0 commit comments

Comments
 (0)