From 7ed84d8ec1ba4cbcd983bf8f48ab94b77bd73c59 Mon Sep 17 00:00:00 2001 From: Diego Ramp Date: Mon, 13 Jan 2025 08:11:13 +0100 Subject: [PATCH] Draft: refactor ModelAuthProvider to async --- .../langchain4j/auth/ModelAuthProvider.java | 5 +++++ .../openai/common/OpenAiRestApi.java | 20 ++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/core/runtime/src/main/java/io/quarkiverse/langchain4j/auth/ModelAuthProvider.java b/core/runtime/src/main/java/io/quarkiverse/langchain4j/auth/ModelAuthProvider.java index 8b2e56f93..df802703c 100644 --- a/core/runtime/src/main/java/io/quarkiverse/langchain4j/auth/ModelAuthProvider.java +++ b/core/runtime/src/main/java/io/quarkiverse/langchain4j/auth/ModelAuthProvider.java @@ -9,6 +9,7 @@ import jakarta.enterprise.inject.spi.CDI; import io.quarkiverse.langchain4j.ModelName; +import io.smallrye.mutiny.Uni; /** * Model authentication providers can be used to supply credentials such as access tokens, API keys, and other type of @@ -27,6 +28,10 @@ public interface ModelAuthProvider { */ String getAuthorization(Input input); + default Uni getAuthorizationAsync(Input input) { + return Uni.createFrom().item(getAuthorization(input)); + } + /* * Representation of an HTTP request to the model provider */ diff --git a/model-providers/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/common/OpenAiRestApi.java b/model-providers/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/common/OpenAiRestApi.java index c86d4ceda..52bcc46b1 100644 --- a/model-providers/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/common/OpenAiRestApi.java +++ b/model-providers/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/common/OpenAiRestApi.java @@ -185,6 +185,7 @@ public boolean test(SseEvent event) { class OpenAIRestAPIFilter implements ResteasyReactiveClientRequestFilter { ModelAuthProvider authorizer; + Logger log = Logger.getLogger(OpenAIRestAPIFilter.class); public OpenAIRestAPIFilter(ModelAuthProvider authorizer) { this.authorizer = authorizer; @@ -192,11 +193,20 @@ public OpenAIRestAPIFilter(ModelAuthProvider authorizer) { @Override public void filter(ResteasyReactiveClientRequestContext requestContext) { - String authValue = authorizer.getAuthorization(new AuthInputImpl(requestContext.getMethod(), - requestContext.getUri(), requestContext.getHeaders())); - if (authValue != null) { - requestContext.getHeaders().putSingle("Authorization", authValue); - } + requestContext.suspend(); + authorizer + .getAuthorizationAsync( + new AuthInputImpl( + requestContext.getMethod(), requestContext.getUri(), requestContext.getHeaders())) + .subscribe().with(authValue -> { + if (authValue != null) { + requestContext.getHeaders().putSingle("Authorization", authValue); + } + requestContext.resume(); + }, t -> { + log.warnf(t, "failed on aquiring authorization, proceeding without it"); + requestContext.resume(t); + }); } private record AuthInputImpl(