update docs, remove extra test annotation

This commit is contained in:
Mark Pollack
2025-02-05 14:55:30 -05:00
parent d126499d7b
commit c499e24919
2 changed files with 15 additions and 50 deletions

View File

@@ -566,24 +566,25 @@ spring.ai.openai.api-key=your-api-key-here
=== Custom API Key Configuration
You can provide your own `ApiKey` implementation by creating a bean with the `@OpenAiApiKey` qualifier:
You can create a custom instance of `OpenAiApi` with your own `ApiKey` implementation using the builder pattern:
[source,java]
----
@Configuration
class OpenAiConfig {
@Bean
@OpenAiApiKey
public ApiKey customOpenAiKey() {
return new ApiKey() {
@Override
public String getValue() {
// Custom logic to retrieve API key
return "your-api-key-here";
}
};
ApiKey customApiKey = new ApiKey() {
@Override
public String getValue() {
// Custom logic to retrieve API key
return "your-api-key-here";
}
}
};
OpenAiApi openAiApi = OpenAiApi.builder()
.apiKey(customApiKey)
.build();
// Create a chat client with the custom OpenAiApi instance
OpenAiChatClient chatClient = new OpenAiChatClient(openAiApi);
----
This is useful when you need to:
@@ -592,4 +593,3 @@ This is useful when you need to:
* Rotate API keys dynamically
* Implement custom API key selection logic
NOTE: If you provide multiple `ApiKey` beans, make sure to use the `@OpenAiApiKey` qualifier to specify which one should be used for OpenAI operations. Without proper qualification, Spring will fail to start due to ambiguous bean definitions.

View File

@@ -1,35 +0,0 @@
/*
* 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.autoconfigure.openai;
import org.springframework.beans.factory.annotation.Qualifier;
import java.lang.annotation.*;
/**
* Test qualifier annotation for other API key beans. Used to test multiple API key
* configurations.
*
* @author Mark Pollack
*/
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Qualifier
public @interface OtherApiKey {
}