From 455d39e7cd9a1dd27a7bd6ac4b62d8cad747b4e6 Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Fri, 5 Jul 2024 12:18:39 -0400 Subject: [PATCH] Improve error messages in OpenAI Autoconfiguration --- .../openai/OpenAiAutoConfiguration.java | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/openai/OpenAiAutoConfiguration.java b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/openai/OpenAiAutoConfiguration.java index b86294646..6c3340404 100644 --- a/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/openai/OpenAiAutoConfiguration.java +++ b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/openai/OpenAiAutoConfiguration.java @@ -72,7 +72,7 @@ public class OpenAiAutoConfiguration { var openAiApi = openAiApi(chatProperties.getBaseUrl(), commonProperties.getBaseUrl(), chatProperties.getApiKey(), commonProperties.getApiKey(), restClientBuilder, webClientBuilder, - responseErrorHandler); + responseErrorHandler, "chat"); if (!CollectionUtils.isEmpty(toolFunctionCallbacks)) { chatProperties.getOptions().getFunctionCallbacks().addAll(toolFunctionCallbacks); @@ -92,7 +92,7 @@ public class OpenAiAutoConfiguration { var openAiApi = openAiApi(embeddingProperties.getBaseUrl(), commonProperties.getBaseUrl(), embeddingProperties.getApiKey(), commonProperties.getApiKey(), restClientBuilder, webClientBuilder, - responseErrorHandler); + responseErrorHandler, "embedding"); return new OpenAiEmbeddingModel(openAiApi, embeddingProperties.getMetadataMode(), embeddingProperties.getOptions(), retryTemplate); @@ -100,13 +100,17 @@ public class OpenAiAutoConfiguration { private OpenAiApi openAiApi(String baseUrl, String commonBaseUrl, String apiKey, String commonApiKey, RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder, - ResponseErrorHandler responseErrorHandler) { + ResponseErrorHandler responseErrorHandler, String modelType) { String resolvedBaseUrl = StringUtils.hasText(baseUrl) ? baseUrl : commonBaseUrl; - Assert.hasText(resolvedBaseUrl, "OpenAI base URL must be set"); + Assert.hasText(resolvedBaseUrl, + "OpenAI base URL must be set. Use the connection property: spring.ai.openai.base-url or spring.ai.openai." + + modelType + ".base-url property."); String resolvedApiKey = StringUtils.hasText(apiKey) ? apiKey : commonApiKey; - Assert.hasText(resolvedApiKey, "OpenAI API key must be set"); + Assert.hasText(resolvedApiKey, + "OpenAI API key must be set. Use the connection property: spring.ai.openai.api-key or spring.ai.openai." + + modelType + ".api-key property."); return new OpenAiApi(resolvedBaseUrl, resolvedApiKey, restClientBuilder, webClientBuilder, responseErrorHandler); @@ -126,8 +130,10 @@ public class OpenAiAutoConfiguration { String baseUrl = StringUtils.hasText(imageProperties.getBaseUrl()) ? imageProperties.getBaseUrl() : commonProperties.getBaseUrl(); - Assert.hasText(apiKey, "OpenAI API key must be set. Use the property: spring.ai.openai.base-url"); - Assert.hasText(baseUrl, "OpenAI base URL must be set. Use the property: spring.ai.openai.api-key"); + Assert.hasText(apiKey, + "OpenAI API key must be set. Use the property: spring.ai.openai.api-key or spring.ai.openai.image.api-key property."); + Assert.hasText(baseUrl, + "OpenAI base URL must be set. Use the property: spring.ai.openai.base-url or spring.ai.openai.image.base-url property."); var openAiImageApi = new OpenAiImageApi(baseUrl, apiKey, restClientBuilder, responseErrorHandler); @@ -149,8 +155,10 @@ public class OpenAiAutoConfiguration { String baseUrl = StringUtils.hasText(transcriptionProperties.getBaseUrl()) ? transcriptionProperties.getBaseUrl() : commonProperties.getBaseUrl(); - Assert.hasText(apiKey, "OpenAI API key must be set"); - Assert.hasText(baseUrl, "OpenAI base URL must be set"); + Assert.hasText(apiKey, + "OpenAI API key must be set. Use the property: spring.ai.openai.api-key or spring.ai.openai.audio.transcription.api-key property."); + Assert.hasText(baseUrl, + "OpenAI base URL must be set. Use the property: spring.ai.openai.base-url or spring.ai.openai.audio.transcription.base-url property."); var openAiAudioApi = new OpenAiAudioApi(baseUrl, apiKey, restClientBuilder, webClientBuilder, responseErrorHandler); @@ -175,8 +183,10 @@ public class OpenAiAutoConfiguration { String baseUrl = StringUtils.hasText(speechProperties.getBaseUrl()) ? speechProperties.getBaseUrl() : commonProperties.getBaseUrl(); - Assert.hasText(apiKey, "OpenAI API key must be set"); - Assert.hasText(baseUrl, "OpenAI base URL must be set"); + Assert.hasText(apiKey, + "OpenAI API key must be set. Use the property: spring.ai.openai.api-key or spring.ai.openai.audio.speech.api-key property."); + Assert.hasText(baseUrl, + "OpenAI base URL must be set. Use the property: spring.ai.openai.base-url or spring.ai.openai.audio.speech.base-url property."); var openAiAudioApi = new OpenAiAudioApi(baseUrl, apiKey, restClientBuilder, webClientBuilder, responseErrorHandler);