Skip to content

Commit

Permalink
Fix the review problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhiyong Li committed Mar 29, 2024
1 parent 019b56d commit a72d00a
Show file tree
Hide file tree
Showing 18 changed files with 166 additions and 121 deletions.
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();
}
}
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;
}
}
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);
});
}

}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
<version>0.29.0-SNAPSHOT</version>
<artifactId>langchain4j-core</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<type>test-jar</type>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import dev.langchain4j.azure.openai.spring.AutoConfig;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.model.ExampleTestTokenizer;
import dev.langchain4j.model.StreamingResponseHandler;
import dev.langchain4j.model.Tokenizer;
import dev.langchain4j.model.azure.AzureOpenAiChatModel;
import dev.langchain4j.model.azure.AzureOpenAiEmbeddingModel;
import dev.langchain4j.model.azure.AzureOpenAiImageModel;
Expand All @@ -11,7 +13,6 @@
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.image.ImageModel;
import dev.langchain4j.model.openai.OpenAiTokenizer;
import dev.langchain4j.model.output.Response;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
Expand Down Expand Up @@ -116,7 +117,7 @@ void should_provide_embedding_model() {
.withPropertyValues("langchain4j.azure.open-ai.embedding-model.api-key=" + AZURE_OPENAI_KEY,
"langchain4j.azure.open-ai.embedding-model.endpoint=" + AZURE_OPENAI_ENDPOINT,
"langchain4j.azure.open-ai.embedding-model.deployment-name=" + AZURE_EMBEDDING_DEPLOYMENT_NAME)
.withBean(OpenAiTokenizer.class, () -> new OpenAiTokenizer("gpt-3.5-turbo"))
.withBean(Tokenizer.class, ExampleTestTokenizer::new)
.run(context -> {

EmbeddingModel embeddingModel = context.getBean(EmbeddingModel.class);
Expand Down

0 comments on commit a72d00a

Please sign in to comment.