-
Notifications
You must be signed in to change notification settings - Fork 2k
Add flexible API key management for OpenAI #2164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
35 changes: 35 additions & 0 deletions
35
models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiApiKey.java
This file contains hidden or 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,35 @@ | ||
| /* | ||
| * Copyright 2023-2024 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.ai.openai.api; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
|
|
||
| import java.lang.annotation.*; | ||
|
|
||
| /** | ||
| * Qualifier annotation for OpenAI API key beans. Used to distinguish OpenAI API keys from | ||
| * other provider API keys. | ||
| * | ||
| * @author Mark Pollack | ||
| */ | ||
| @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE }) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Documented | ||
| @Qualifier | ||
| public @interface OpenAiApiKey { | ||
|
|
||
| } | ||
148 changes: 148 additions & 0 deletions
148
...ring-ai-openai/src/test/java/org/springframework/ai/openai/api/OpenAiApiBuilderTests.java
This file contains hidden or 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,148 @@ | ||
| /* | ||
| * Copyright 2023-2025 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.ai.openai.api; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import org.springframework.ai.model.ApiKey; | ||
| import org.springframework.ai.model.SimpleApiKey; | ||
| import org.springframework.util.LinkedMultiValueMap; | ||
| import org.springframework.util.MultiValueMap; | ||
| import org.springframework.web.client.ResponseErrorHandler; | ||
| import org.springframework.web.client.RestClient; | ||
| import org.springframework.web.reactive.function.client.WebClient; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| public class OpenAiApiBuilderTests { | ||
|
|
||
| private static final ApiKey TEST_API_KEY = new SimpleApiKey("test-api-key"); | ||
|
|
||
| private static final String TEST_BASE_URL = "https://test.openai.com"; | ||
|
|
||
| private static final String TEST_COMPLETIONS_PATH = "/test/completions"; | ||
|
|
||
| private static final String TEST_EMBEDDINGS_PATH = "/test/embeddings"; | ||
|
|
||
| @Test | ||
| void testMinimalBuilder() { | ||
| OpenAiApi api = OpenAiApi.builder().apiKey(TEST_API_KEY).build(); | ||
|
|
||
| assertThat(api).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void testFullBuilder() { | ||
| MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); | ||
| headers.add("Custom-Header", "test-value"); | ||
| RestClient.Builder restClientBuilder = RestClient.builder(); | ||
| WebClient.Builder webClientBuilder = WebClient.builder(); | ||
| ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class); | ||
|
|
||
| OpenAiApi api = OpenAiApi.builder() | ||
| .apiKey(TEST_API_KEY) | ||
| .baseUrl(TEST_BASE_URL) | ||
| .headers(headers) | ||
| .completionsPath(TEST_COMPLETIONS_PATH) | ||
| .embeddingsPath(TEST_EMBEDDINGS_PATH) | ||
| .restClientBuilder(restClientBuilder) | ||
| .webClientBuilder(webClientBuilder) | ||
| .responseErrorHandler(errorHandler) | ||
| .build(); | ||
|
|
||
| assertThat(api).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void testDefaultValues() { | ||
| OpenAiApi api = OpenAiApi.builder().apiKey(TEST_API_KEY).build(); | ||
|
|
||
| assertThat(api).isNotNull(); | ||
| // We can't directly test the default values as they're private fields, | ||
| // but we know the builder succeeded with defaults | ||
| } | ||
|
|
||
| @Test | ||
| void testMissingApiKey() { | ||
| assertThatThrownBy(() -> { | ||
| OpenAiApi.builder().build(); | ||
| }).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("apiKey must be set"); | ||
| } | ||
|
|
||
| @Test | ||
| void testInvalidBaseUrl() { | ||
| assertThatThrownBy(() -> { | ||
| OpenAiApi.builder().baseUrl("").build(); | ||
| }).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("baseUrl cannot be null or empty"); | ||
|
|
||
| assertThatThrownBy(() -> { | ||
| OpenAiApi.builder().baseUrl(null).build(); | ||
| }).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("baseUrl cannot be null or empty"); | ||
| } | ||
|
|
||
| @Test | ||
| void testInvalidHeaders() { | ||
| assertThatThrownBy(() -> { | ||
| OpenAiApi.builder().headers(null).build(); | ||
| }).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("headers cannot be null"); | ||
| } | ||
|
|
||
| @Test | ||
| void testInvalidCompletionsPath() { | ||
| assertThatThrownBy(() -> { | ||
| OpenAiApi.builder().completionsPath("").build(); | ||
| }).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("completionsPath cannot be null or empty"); | ||
|
|
||
| assertThatThrownBy(() -> { | ||
| OpenAiApi.builder().completionsPath(null).build(); | ||
| }).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("completionsPath cannot be null or empty"); | ||
| } | ||
|
|
||
| @Test | ||
| void testInvalidEmbeddingsPath() { | ||
| assertThatThrownBy(() -> { | ||
| OpenAiApi.builder().embeddingsPath("").build(); | ||
| }).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("embeddingsPath cannot be null or empty"); | ||
|
|
||
| assertThatThrownBy(() -> { | ||
| OpenAiApi.builder().embeddingsPath(null).build(); | ||
| }).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("embeddingsPath cannot be null or empty"); | ||
| } | ||
|
|
||
| @Test | ||
| void testInvalidRestClientBuilder() { | ||
| assertThatThrownBy(() -> { | ||
| OpenAiApi.builder().restClientBuilder(null).build(); | ||
| }).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("restClientBuilder cannot be null"); | ||
| } | ||
|
|
||
| @Test | ||
| void testInvalidWebClientBuilder() { | ||
| assertThatThrownBy(() -> { | ||
| OpenAiApi.builder().webClientBuilder(null).build(); | ||
| }).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("webClientBuilder cannot be null"); | ||
| } | ||
|
|
||
| @Test | ||
| void testInvalidResponseErrorHandler() { | ||
| assertThatThrownBy(() -> { | ||
| OpenAiApi.builder().responseErrorHandler(null).build(); | ||
| }).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("responseErrorHandler cannot be null"); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change to
OpenAiChatApiKeyso it is specific to chat models, there will beOpenAiEmbeddingApiKeydifferent models can have different endpoints.