-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes langchain4j/langchain4j#1430 Support `@AiService` with `@Profile`.
- Loading branch information
Showing
6 changed files
with
105 additions
and
19 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
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
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
11 changes: 11 additions & 0 deletions
11
...ava/dev/langchain4j/service/spring/mode/automatic/withProfiles/AiServiceWithProfiles.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,11 @@ | ||
package dev.langchain4j.service.spring.mode.automatic.withProfiles; | ||
|
||
import dev.langchain4j.service.spring.AiService; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
@AiService | ||
@Profile("!test") | ||
public interface AiServiceWithProfiles { | ||
|
||
String chat(String userMessage); | ||
} |
20 changes: 20 additions & 0 deletions
20
...gchain4j/service/spring/mode/automatic/withProfiles/AiServiceWithProfilesApplication.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,20 @@ | ||
package dev.langchain4j.service.spring.mode.automatic.withProfiles; | ||
|
||
import dev.langchain4j.model.chat.ChatLanguageModel; | ||
import dev.langchain4j.model.openai.OpenAiChatModel; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
@SpringBootApplication | ||
public class AiServiceWithProfilesApplication { | ||
|
||
@Bean | ||
ChatLanguageModel chatLanguageModel() { | ||
return OpenAiChatModel.withApiKey(System.getenv("OPENAI_API_KEY")); | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(AiServiceWithProfilesApplication.class, args); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...a/dev/langchain4j/service/spring/mode/automatic/withProfiles/AiServiceWithProfilesIT.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.withProfiles; | ||
|
||
import dev.langchain4j.service.spring.AiServicesAutoConfig; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class AiServiceWithProfilesIT { | ||
|
||
ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class)); | ||
|
||
@Test | ||
void should_not_create_ai_service() { | ||
contextRunner | ||
.withPropertyValues( | ||
"spring.profiles.active=test" | ||
) | ||
.withUserConfiguration(AiServiceWithProfilesApplication.class) | ||
.run(context -> assertThatThrownBy(() -> context.getBean(AiServiceWithProfiles.class)) | ||
.isInstanceOf(BeansException.class)); | ||
} | ||
|
||
@Test | ||
void should_create_ai_service() { | ||
contextRunner | ||
.withPropertyValues( | ||
"spring.profiles.active=dev" | ||
) | ||
.withUserConfiguration(AiServiceWithProfilesApplication.class) | ||
.run(context -> assertThat(context.getBean(AiServiceWithProfiles.class)).isNotNull()); | ||
} | ||
} |