Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #887: do not configure both ContentRetriever and RetrievalAugmentor #17

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
});
}
}
Loading