-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zhiyong Li
committed
Mar 29, 2024
1 parent
019b56d
commit a72d00a
Showing
18 changed files
with
166 additions
and
121 deletions.
There are no files selected for viewing
File renamed without changes.
47 changes: 47 additions & 0 deletions
47
...h-spring-boot-starter/src/main/java/dev/langchain4j/azure/aisearch/spring/AutoConfig.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,47 @@ | ||
package dev.langchain4j.azure.aisearch.spring; | ||
|
||
import com.azure.search.documents.indexes.models.SearchIndex; | ||
import dev.langchain4j.model.embedding.EmbeddingModel; | ||
import dev.langchain4j.rag.content.retriever.azure.search.AzureAiSearchContentRetriever; | ||
import dev.langchain4j.store.embedding.azure.search.AzureAiSearchEmbeddingStore; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.lang.Nullable; | ||
|
||
import static dev.langchain4j.azure.aisearch.spring.Properties.PREFIX; | ||
|
||
@AutoConfiguration | ||
@EnableConfigurationProperties(Properties.class) | ||
public class AutoConfig { | ||
@Bean | ||
@ConditionalOnProperty(PREFIX + ".content-retriver.api-key") | ||
public AzureAiSearchContentRetriever azureAiSearchContentRetriever(Properties properties, @Nullable EmbeddingModel embeddingModel, @Nullable SearchIndex index) { | ||
Properties.NestedProperties nestedProperties = properties.getContentRetriver(); | ||
return AzureAiSearchContentRetriever.builder() | ||
.endpoint(nestedProperties.getEndpoint()) | ||
.apiKey(nestedProperties.getApiKey()) | ||
.createOrUpdateIndex(nestedProperties.isCreateOrUpdateIndex()) | ||
.embeddingModel(embeddingModel) | ||
.dimensions(nestedProperties.getDimensions()) | ||
.index(index) | ||
.maxResults(nestedProperties.getMaxResults()) | ||
.minScore(nestedProperties.getMinScore()) | ||
.queryType(nestedProperties.getQueryType()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnProperty(PREFIX + ".embedding-store.api-key") | ||
public AzureAiSearchEmbeddingStore azureAiSearchEmbeddingStore(Properties properties, @Nullable EmbeddingModel embeddingModel, @Nullable SearchIndex index) { | ||
Properties.NestedProperties nestedProperties = properties.getEmbeddingStore(); | ||
return AzureAiSearchEmbeddingStore.builder() | ||
.endpoint(nestedProperties.getEndpoint()) | ||
.apiKey(nestedProperties.getApiKey()) | ||
.createOrUpdateIndex(nestedProperties.isCreateOrUpdateIndex()) | ||
.dimensions(nestedProperties.getDimensions()) | ||
.index(index) | ||
.build(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...h-spring-boot-starter/src/main/java/dev/langchain4j/azure/aisearch/spring/Properties.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,33 @@ | ||
package dev.langchain4j.azure.aisearch.spring; | ||
|
||
import dev.langchain4j.rag.content.retriever.azure.search.AzureAiSearchQueryType; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.context.properties.NestedConfigurationProperty; | ||
|
||
@Getter | ||
@Setter | ||
@ConfigurationProperties(prefix = Properties.PREFIX) | ||
public class Properties { | ||
|
||
static final String PREFIX = "langchain4j.azure.ai-search"; | ||
|
||
@NestedConfigurationProperty | ||
NestedProperties contentRetriver; | ||
|
||
@NestedConfigurationProperty | ||
NestedProperties embeddingStore; | ||
|
||
@Getter | ||
@Setter | ||
public static class NestedProperties { | ||
String endpoint; | ||
String apiKey; | ||
int dimensions; | ||
boolean createOrUpdateIndex; | ||
int maxResults = 3; | ||
double minScore; | ||
AzureAiSearchQueryType queryType; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
79 changes: 79 additions & 0 deletions
79
...spring-boot-starter/src/test/java/dev/langchain4j/azure/aisearch/spring/AutoConfigIT.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,79 @@ | ||
package dev.langchain4j.azure.aisearch.spring; | ||
|
||
import dev.langchain4j.model.embedding.AllMiniLmL6V2EmbeddingModel; | ||
import dev.langchain4j.model.embedding.EmbeddingModel; | ||
import dev.langchain4j.rag.content.retriever.ContentRetriever; | ||
import dev.langchain4j.rag.content.retriever.azure.search.AzureAiSearchContentRetriever; | ||
import dev.langchain4j.store.embedding.EmbeddingStore; | ||
import dev.langchain4j.store.embedding.azure.search.AzureAiSearchEmbeddingStore; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class AutoConfigIT { | ||
|
||
private static final String AZURE_AISEARCH_API_KEY = System.getenv("AZURE_AISEARCH_API_KEY"); | ||
private static final String AZURE_AISEARCH_ENDPOINT = System.getenv("AZURE_AISEARCH_ENDPOINT"); | ||
private static final String AZURE_AISEARCH_DIMENSIONS = System.getenv("AZURE_AISEARCH_DIMENSIONS"); | ||
private static final String AZURE_AISEARCH_MAX_RESULTS = System.getenv("AZURE_AISEARCH_MAX_RESULTS"); | ||
private static final String AZURE_AISEARCH_MIN_SCORE = System.getenv("AZURE_AISEARCH_MIN_SCORE"); | ||
|
||
ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(AutoConfig.class)); | ||
|
||
@Test | ||
void should_provide_ai_search_retriver_only_search() { | ||
contextRunner | ||
.withPropertyValues( | ||
Properties.PREFIX + ".content-retriver.api-key=" + AZURE_AISEARCH_API_KEY, | ||
Properties.PREFIX + ".content-retriver.endpoint=" + AZURE_AISEARCH_ENDPOINT, | ||
Properties.PREFIX + ".content-retriver.create-or-update-index=" + "false", | ||
Properties.PREFIX + ".content-retriver.query-type=" + "FULL_TEXT" | ||
).run(context -> { | ||
ContentRetriever contentRetriever = context.getBean(ContentRetriever.class); | ||
assertThat(contentRetriever).isInstanceOf(AzureAiSearchContentRetriever.class); | ||
|
||
assertThat(context.getBean(AzureAiSearchContentRetriever.class)).isSameAs(contentRetriever); | ||
}); | ||
} | ||
|
||
@Test | ||
void should_provide_ai_search_retrive_create_or_update_indexr() { | ||
contextRunner | ||
.withPropertyValues( | ||
Properties.PREFIX + ".content-retriver.api-key=" + AZURE_AISEARCH_API_KEY, | ||
Properties.PREFIX + ".content-retriver.endpoint=" + AZURE_AISEARCH_ENDPOINT, | ||
Properties.PREFIX + ".content-retriver.dimensions=" + AZURE_AISEARCH_DIMENSIONS, | ||
Properties.PREFIX + ".content-retriver.create-or-update-index=" + "true", | ||
Properties.PREFIX + ".content-retriver.max-results=" + AZURE_AISEARCH_MAX_RESULTS, | ||
Properties.PREFIX + ".content-retriver.min-score=" + AZURE_AISEARCH_MIN_SCORE, | ||
Properties.PREFIX + ".content-retriver.query-type=" + "VECTOR" | ||
).withBean(EmbeddingModel.class, AllMiniLmL6V2EmbeddingModel::new) | ||
.run(context -> { | ||
ContentRetriever contentRetriever = context.getBean(ContentRetriever.class); | ||
assertThat(contentRetriever).isInstanceOf(AzureAiSearchContentRetriever.class); | ||
|
||
assertThat(context.getBean(AzureAiSearchContentRetriever.class)).isSameAs(contentRetriever); | ||
}); | ||
} | ||
|
||
@Test | ||
void should_provide_ai_search_embedding_store() { | ||
contextRunner | ||
.withPropertyValues( | ||
Properties.PREFIX + ".embedding-store.api-key=" + AZURE_AISEARCH_API_KEY, | ||
Properties.PREFIX + ".embedding-store.endpoint=" + AZURE_AISEARCH_ENDPOINT, | ||
Properties.PREFIX + ".embedding-store.dimensions=" + AZURE_AISEARCH_DIMENSIONS, | ||
Properties.PREFIX + ".embedding-store.create-or-update-index=" + "true" | ||
).withBean(EmbeddingModel.class, AllMiniLmL6V2EmbeddingModel::new) | ||
.run(context -> { | ||
EmbeddingStore embeddingStore = context.getBean(EmbeddingStore.class); | ||
assertThat(embeddingStore).isInstanceOf(AzureAiSearchEmbeddingStore.class); | ||
|
||
assertThat(context.getBean(AzureAiSearchEmbeddingStore.class)).isSameAs(embeddingStore); | ||
}); | ||
} | ||
|
||
} |
30 changes: 0 additions & 30 deletions
30
...h-spring-boot-starter/src/main/java/dev/langchain4j/azure/aisearch/spring/AutoConfig.java
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
...h-spring-boot-starter/src/main/java/dev/langchain4j/azure/aisearch/spring/Properties.java
This file was deleted.
Oops, something went wrong.
65 changes: 0 additions & 65 deletions
65
...spring-boot-starter/src/test/java/dev/langchain4j/azure/aisearch/spring/AutoConfigIT.java
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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