Skip to content

Commit 5203c8b

Browse files
author
LangChain4j
authored
Merge pull request #17 from langchain4j/fix-887
Fix #887: do not configure both ContentRetriever and RetrievalAugmentor
2 parents ecc5069 + af77ed0 commit 5203c8b

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

langchain4j-spring-boot-starter/src/main/java/dev/langchain4j/service/spring/AiServiceFactory.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,10 @@ public Object getObject() {
7777
builder.chatMemoryProvider(chatMemoryProvider);
7878
}
7979

80-
if (contentRetriever != null) {
81-
builder = builder.contentRetriever(contentRetriever);
82-
}
83-
8480
if (retrievalAugmentor != null) {
8581
builder = builder.retrievalAugmentor(retrievalAugmentor);
82+
} else if (contentRetriever != null) {
83+
builder = builder.contentRetriever(contentRetriever);
8684
}
8785

8886
if (!isNullOrEmpty(tools)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dev.langchain4j.service.spring.mode.automatic.withContentRetrieverAndRetrievalAugmentor.withRetrievalAugmentor;
2+
3+
import dev.langchain4j.service.spring.AiService;
4+
5+
@AiService
6+
interface AiServiceWithContentRetrieverAndRetrievalAugmentor {
7+
8+
String chat(String userMessage);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package dev.langchain4j.service.spring.mode.automatic.withContentRetrieverAndRetrievalAugmentor.withRetrievalAugmentor;
2+
3+
import dev.langchain4j.rag.DefaultRetrievalAugmentor;
4+
import dev.langchain4j.rag.RetrievalAugmentor;
5+
import dev.langchain4j.rag.content.Content;
6+
import dev.langchain4j.rag.content.retriever.ContentRetriever;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.annotation.Bean;
10+
11+
import static java.util.Collections.singletonList;
12+
13+
@SpringBootApplication
14+
class AiServiceWithContentRetrieverAndRetrievalAugmentorApplication {
15+
16+
@Bean
17+
ContentRetriever contentRetriever() {
18+
return query -> singletonList(Content.from("My name is Klaus."));
19+
}
20+
21+
@Bean
22+
RetrievalAugmentor retrievalAugmentor(ContentRetriever contentRetriever) {
23+
return DefaultRetrievalAugmentor.builder()
24+
.contentRetriever(contentRetriever)
25+
.build();
26+
}
27+
28+
public static void main(String[] args) {
29+
SpringApplication.run(AiServiceWithContentRetrieverAndRetrievalAugmentorApplication.class, args);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package dev.langchain4j.service.spring.mode.automatic.withContentRetrieverAndRetrievalAugmentor.withRetrievalAugmentor;
2+
3+
import dev.langchain4j.service.spring.AiServicesAutoConfig;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.boot.autoconfigure.AutoConfigurations;
6+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
7+
8+
import static dev.langchain4j.service.spring.mode.ApiKeys.OPENAI_API_KEY;
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
class AiServiceWithContentRetrieverAndRetrievalAugmentorIT {
12+
13+
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
14+
.withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class));
15+
16+
@Test
17+
void should_create_AI_service_with_content_retriever_and_retrieval_augmentor() {
18+
contextRunner
19+
.withPropertyValues(
20+
"langchain4j.open-ai.chat-model.api-key=" + OPENAI_API_KEY,
21+
"langchain4j.open-ai.chat-model.max-tokens=20",
22+
"langchain4j.open-ai.chat-model.temperature=0.0"
23+
)
24+
.withUserConfiguration(AiServiceWithContentRetrieverAndRetrievalAugmentorApplication.class)
25+
.run(context -> {
26+
27+
// given
28+
AiServiceWithContentRetrieverAndRetrievalAugmentor aiService = context.getBean(AiServiceWithContentRetrieverAndRetrievalAugmentor.class);
29+
30+
// when
31+
String answer = aiService.chat("What is my name?");
32+
33+
// then
34+
assertThat(answer).containsIgnoringCase("Klaus");
35+
});
36+
}
37+
}

0 commit comments

Comments
 (0)