From 952a53d425a9020a75375eb041f3116e955edcb9 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Sat, 16 Dec 2023 13:42:18 +0200 Subject: [PATCH] Limit scope of OpenAiRestApi providers The previous way they were declared meant that they would affect other REST Clients too. --- .../langchain4j/openai/OpenAiRestApi.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/OpenAiRestApi.java b/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/OpenAiRestApi.java index ffdad9b53..93be0eaa8 100644 --- a/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/OpenAiRestApi.java +++ b/openai/openai-common/runtime/src/main/java/io/quarkiverse/langchain4j/openai/OpenAiRestApi.java @@ -15,26 +15,24 @@ import java.util.regex.Pattern; import jakarta.annotation.Priority; -import jakarta.ws.rs.ConstrainedTo; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.POST; import jakarta.ws.rs.Path; import jakarta.ws.rs.Priorities; import jakarta.ws.rs.Produces; -import jakarta.ws.rs.RuntimeType; import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.MultivaluedMap; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.MessageBodyWriter; -import jakarta.ws.rs.ext.Provider; import jakarta.ws.rs.ext.ReaderInterceptor; import jakarta.ws.rs.ext.ReaderInterceptorContext; import jakarta.ws.rs.ext.WriterInterceptor; import jakarta.ws.rs.ext.WriterInterceptorContext; import org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam; +import org.eclipse.microprofile.rest.client.annotation.RegisterProvider; import org.jboss.logging.Logger; import org.jboss.resteasy.reactive.RestQuery; import org.jboss.resteasy.reactive.RestStreamElementType; @@ -76,6 +74,9 @@ @ClientHeaderParam(name = "api-key", value = "{apiKey}") // used by AzureAI @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) +@RegisterProvider(OpenAiRestApi.OpenAiRestApiJacksonProvider.class) +@RegisterProvider(OpenAiRestApi.OpenAiRestApiReaderInterceptor.class) +@RegisterProvider(OpenAiRestApi.OpenAiRestApiWriterInterceptor.class) public interface OpenAiRestApi { /** @@ -185,8 +186,6 @@ public boolean test(SseEvent event) { * We need a custom version of the Jackson provider because reading SSE values does not work properly with * {@code @ClientObjectMapper} due to the lack of a complete context in those requests */ - @Provider - @ConstrainedTo(RuntimeType.CLIENT) @Priority(Priorities.USER + 100) class OpenAiRestApiJacksonProvider extends AbstractJsonMessageBodyReader implements MessageBodyWriter { @@ -230,8 +229,6 @@ public static class ObjectMapperHolder { /** * This method validates that the response is not empty, which happens when the API returns an error object */ - @Provider - @ConstrainedTo(RuntimeType.CLIENT) class OpenAiRestApiReaderInterceptor implements ReaderInterceptor { @Override @@ -272,8 +269,6 @@ private Object validateResponse(Object result) { * The point of this is to properly set the {@code stream} value of the request * so users don't have to remember to set it manually */ - @Provider - @ConstrainedTo(RuntimeType.CLIENT) class OpenAiRestApiWriterInterceptor implements WriterInterceptor { @Override public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {