-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from langchain4j/fix-887
Fix #887: do not configure both ContentRetriever and RetrievalAugmentor
- Loading branch information
Showing
4 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...lAugmentor/withRetrievalAugmentor/AiServiceWithContentRetrieverAndRetrievalAugmentor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
31 changes: 31 additions & 0 deletions
31
...withRetrievalAugmentor/AiServiceWithContentRetrieverAndRetrievalAugmentorApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ugmentor/withRetrievalAugmentor/AiServiceWithContentRetrieverAndRetrievalAugmentorIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
} | ||
} |