Skip to content

Commit fcf1a76

Browse files
committed
UT config for openai
Signed-off-by: lambochen <[email protected]>
1 parent 8e1f251 commit fcf1a76

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/OpenAiTestConfiguration.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,48 @@ public class OpenAiTestConfiguration {
3232

3333
@Bean
3434
public OpenAiApi openAiApi() {
35-
return OpenAiApi.builder().apiKey(getApiKey()).build();
35+
var builder = OpenAiApi.builder().apiKey(getApiKey());
36+
37+
String baseUrl = getBaseUrl();
38+
if (StringUtils.hasText(baseUrl)) {
39+
builder.baseUrl(baseUrl);
40+
}
41+
String completionsPath = getCompletionsPath();
42+
if (StringUtils.hasText(completionsPath)) {
43+
builder.completionsPath(completionsPath);
44+
}
45+
46+
return builder.build();
3647
}
3748

3849
@Bean
3950
public OpenAiImageApi openAiImageApi() {
40-
return OpenAiImageApi.builder().apiKey(getApiKey()).build();
51+
var builder = OpenAiImageApi.builder().apiKey(getApiKey());
52+
String baseUrl = getBaseUrl();
53+
if (StringUtils.hasText(baseUrl)) {
54+
builder.baseUrl(baseUrl);
55+
}
56+
return builder.build();
4157
}
4258

4359
@Bean
4460
public OpenAiAudioApi openAiAudioApi() {
45-
return OpenAiAudioApi.builder().apiKey(getApiKey()).build();
61+
var builder = OpenAiAudioApi.builder().apiKey(getApiKey());
62+
String baseUrl = getBaseUrl();
63+
if (StringUtils.hasText(baseUrl)) {
64+
builder.baseUrl(baseUrl);
65+
}
66+
return builder.build();
4667
}
4768

4869
@Bean
4970
public OpenAiModerationApi openAiModerationApi() {
50-
return OpenAiModerationApi.builder().apiKey(getApiKey()).build();
71+
var builder = OpenAiModerationApi.builder().apiKey(getApiKey());
72+
String baseUrl = getBaseUrl();
73+
if (StringUtils.hasText(baseUrl)) {
74+
builder.baseUrl(baseUrl);
75+
}
76+
return builder.build();
5177
}
5278

5379
private ApiKey getApiKey() {
@@ -59,6 +85,22 @@ private ApiKey getApiKey() {
5985
return new SimpleApiKey(apiKey);
6086
}
6187

88+
private String getBaseUrl() {
89+
String baseUrl = System.getenv("OPENAI_BASE_URL");
90+
if (StringUtils.hasText(baseUrl)) {
91+
return baseUrl;
92+
}
93+
return null;
94+
}
95+
96+
private String getCompletionsPath() {
97+
String path = System.getenv("OPENAI_COMPLETIONS_PATH");
98+
if (StringUtils.hasText(path)) {
99+
return path;
100+
}
101+
return null;
102+
}
103+
62104
@Bean
63105
public OpenAiChatModel openAiChatModel(OpenAiApi api) {
64106
return OpenAiChatModel.builder()

spring-ai-model/src/main/java/org/springframework/ai/model/tool/ToolExecutionEligibilityPredicate.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ default boolean isToolExecutionRequired(ChatOptions promptOptions, ChatResponse
4444
}
4545

4646
/**
47-
* Determines if tool execution should be performed based on the prompt options and chat response and the number of attempts.
47+
* Determines if tool execution should be performed based on the prompt options and
48+
* chat response and the number of attempts.
4849
* @param promptOptions The options from the prompt
4950
* @param chatResponse The response from the chat model
5051
* @param attempts The number of attempts

spring-ai-model/src/test/java/org/springframework/ai/model/tool/ToolExecutionEligibilityCheckerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,7 @@ class TestToolExecutionEligibilityChecker implements ToolExecutionEligibilityChe
4949
public Boolean apply(ChatResponse chatResponse) {
5050
return true;
5151
}
52+
5253
}
54+
5355
}

0 commit comments

Comments
 (0)