From c499e24919173184787400957cac1649c4a42dea Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Wed, 5 Feb 2025 14:55:30 -0500 Subject: [PATCH] update docs, remove extra test annotation --- .../ROOT/pages/api/chat/openai-chat.adoc | 30 ++++++++-------- .../ai/autoconfigure/openai/OtherApiKey.java | 35 ------------------- 2 files changed, 15 insertions(+), 50 deletions(-) delete mode 100644 spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/openai/OtherApiKey.java diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-chat.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-chat.adoc index e39176731..0942a22e2 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-chat.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-chat.adoc @@ -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. diff --git a/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/openai/OtherApiKey.java b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/openai/OtherApiKey.java deleted file mode 100644 index e9e313d07..000000000 --- a/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/openai/OtherApiKey.java +++ /dev/null @@ -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 { - -}