Skip to content

Commit

Permalink
Merge pull request #17 from langchain4j/fix-887
Browse files Browse the repository at this point in the history
Fix #887: do not configure both ContentRetriever and RetrievalAugmentor
  • Loading branch information
langchain4j authored Apr 16, 2024
2 parents ecc5069 + af77ed0 commit 5203c8b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,10 @@ public Object getObject() {
builder.chatMemoryProvider(chatMemoryProvider);
}

if (contentRetriever != null) {
builder = builder.contentRetriever(contentRetriever);
}

if (retrievalAugmentor != null) {
builder = builder.retrievalAugmentor(retrievalAugmentor);
} else if (contentRetriever != null) {
builder = builder.contentRetriever(contentRetriever);
}

if (!isNullOrEmpty(tools)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.langchain4j.service.spring.mode.automatic.withContentRetrieverAndRetrievalAugmentor.withRetrievalAugmentor;

import dev.langchain4j.service.spring.AiService;

@AiService
interface AiServiceWithContentRetrieverAndRetrievalAugmentor {

String chat(String userMessage);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dev.langchain4j.service.spring.mode.automatic.withContentRetrieverAndRetrievalAugmentor.withRetrievalAugmentor;

import dev.langchain4j.rag.DefaultRetrievalAugmentor;
import dev.langchain4j.rag.RetrievalAugmentor;
import dev.langchain4j.rag.content.Content;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import static java.util.Collections.singletonList;

@SpringBootApplication
class AiServiceWithContentRetrieverAndRetrievalAugmentorApplication {

@Bean
ContentRetriever contentRetriever() {
return query -> singletonList(Content.from("My name is Klaus."));
}

@Bean
RetrievalAugmentor retrievalAugmentor(ContentRetriever contentRetriever) {
return DefaultRetrievalAugmentor.builder()
.contentRetriever(contentRetriever)
.build();
}

public static void main(String[] args) {
SpringApplication.run(AiServiceWithContentRetrieverAndRetrievalAugmentorApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.langchain4j.service.spring.mode.automatic.withContentRetrieverAndRetrievalAugmentor.withRetrievalAugmentor;

import dev.langchain4j.service.spring.AiServicesAutoConfig;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import static dev.langchain4j.service.spring.mode.ApiKeys.OPENAI_API_KEY;
import static org.assertj.core.api.Assertions.assertThat;

class AiServiceWithContentRetrieverAndRetrievalAugmentorIT {

ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class));

@Test
void should_create_AI_service_with_content_retriever_and_retrieval_augmentor() {
contextRunner
.withPropertyValues(
"langchain4j.open-ai.chat-model.api-key=" + OPENAI_API_KEY,
"langchain4j.open-ai.chat-model.max-tokens=20",
"langchain4j.open-ai.chat-model.temperature=0.0"
)
.withUserConfiguration(AiServiceWithContentRetrieverAndRetrievalAugmentorApplication.class)
.run(context -> {

// given
AiServiceWithContentRetrieverAndRetrievalAugmentor aiService = context.getBean(AiServiceWithContentRetrieverAndRetrievalAugmentor.class);

// when
String answer = aiService.chat("What is my name?");

// then
assertThat(answer).containsIgnoringCase("Klaus");
});
}
}

0 comments on commit 5203c8b

Please sign in to comment.