|
| 1 | +package ee.carlrobert.llm.client.watsonx; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import ee.carlrobert.llm.PropertiesLoader; |
| 5 | +import ee.carlrobert.llm.client.DeserializationUtil; |
| 6 | +import ee.carlrobert.llm.client.openai.completion.ErrorDetails; |
| 7 | +import ee.carlrobert.llm.client.watsonx.completion.WatsonxCompletionRequest; |
| 8 | +import ee.carlrobert.llm.client.watsonx.completion.WatsonxCompletionResponse; |
| 9 | +import ee.carlrobert.llm.client.watsonx.completion.WatsonxCompletionResponseError; |
| 10 | +import ee.carlrobert.llm.completion.CompletionEventListener; |
| 11 | +import ee.carlrobert.llm.completion.CompletionEventSourceListener; |
| 12 | +import okhttp3.*; |
| 13 | +import okhttp3.sse.EventSource; |
| 14 | +import okhttp3.sse.EventSources; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import static ee.carlrobert.llm.client.DeserializationUtil.OBJECT_MAPPER; |
| 21 | + |
| 22 | +public class WatsonxClient { |
| 23 | + |
| 24 | + private static final MediaType APPLICATION_JSON = MediaType.parse("application/json"); |
| 25 | + private final OkHttpClient httpClient; |
| 26 | + private final String host; |
| 27 | + private final String apiVersion; |
| 28 | + private final WatsonxAuthenticator authenticator; |
| 29 | + |
| 30 | + private WatsonxClient(Builder builder, OkHttpClient.Builder httpClientBuilder) { |
| 31 | + this.httpClient = httpClientBuilder.build(); |
| 32 | + this.apiVersion = builder.apiVersion; |
| 33 | + this.host = builder.host; |
| 34 | + if (builder.isOnPrem) { |
| 35 | + if (builder.isZenApiKey) |
| 36 | + this.authenticator = new WatsonxAuthenticator(builder.username, builder.apiKey); |
| 37 | + else |
| 38 | + this.authenticator = new WatsonxAuthenticator(builder.username, builder.apiKey, builder.host); |
| 39 | + } else { |
| 40 | + this.authenticator = new WatsonxAuthenticator(builder.apiKey); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public EventSource getCompletionAsync( |
| 45 | + WatsonxCompletionRequest request, |
| 46 | + CompletionEventListener<String> eventListener) { |
| 47 | + return EventSources.createFactory(httpClient).newEventSource( |
| 48 | + buildCompletionRequest(request), |
| 49 | + getCompletionEventSourceListener(eventListener)); |
| 50 | + } |
| 51 | + |
| 52 | + public WatsonxCompletionResponse getCompletion(WatsonxCompletionRequest request) { |
| 53 | + try (var response = httpClient.newCall(buildCompletionRequest(request)).execute()) { |
| 54 | + return DeserializationUtil.mapResponse(response, WatsonxCompletionResponse.class); |
| 55 | + } catch (IOException e) { |
| 56 | + throw new RuntimeException(e); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + protected Request buildCompletionRequest(WatsonxCompletionRequest request) { |
| 61 | + var headers = new HashMap<>(getRequiredHeaders()); |
| 62 | + if (request.getStream()) { |
| 63 | + headers.put("Accept", "text/event-stream"); |
| 64 | + } |
| 65 | + try { |
| 66 | + return new Request.Builder() |
| 67 | + .url(host + "/ml/v1/text/" + (request.getStream() ? "generation_stream" : "generation") + "?version=" + apiVersion) |
| 68 | + .headers(Headers.of(headers)) |
| 69 | + .post(RequestBody.create(OBJECT_MAPPER.writeValueAsString(request), APPLICATION_JSON)) |
| 70 | + .build(); |
| 71 | + } catch (JsonProcessingException e) { |
| 72 | + throw new RuntimeException("Unable to process request", e); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private Map<String, String> getRequiredHeaders() { |
| 77 | + return new HashMap<>(Map.of("Authorization", "Bearer " + authenticator.getBearerTokenValue())); |
| 78 | + } |
| 79 | + |
| 80 | + private CompletionEventSourceListener<String> getCompletionEventSourceListener( |
| 81 | + CompletionEventListener<String> eventListener) { |
| 82 | + return new CompletionEventSourceListener<>(eventListener) { |
| 83 | + @Override |
| 84 | + protected String getMessage(String data) { |
| 85 | + try { |
| 86 | + return OBJECT_MAPPER.readValue(data, WatsonxCompletionResponse.class) |
| 87 | + .getResults().get(0).getGeneratedText(); |
| 88 | + } catch (Exception e) { |
| 89 | + try { |
| 90 | + String message = OBJECT_MAPPER.readValue(data, WatsonxCompletionResponseError.class) |
| 91 | + .getError() |
| 92 | + .getMessage(); |
| 93 | + if (message != null) return message; |
| 94 | + return ""; |
| 95 | + } catch (Exception ex) { |
| 96 | + return ""; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + protected ErrorDetails getErrorDetails(String error) { |
| 103 | + try { |
| 104 | + return OBJECT_MAPPER.readValue(error, WatsonxCompletionResponseError.class).getError(); |
| 105 | + } catch (JsonProcessingException e) { |
| 106 | + throw new RuntimeException(e); |
| 107 | + } |
| 108 | + } |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + public static class Builder { |
| 113 | + |
| 114 | + private final String apiKey; |
| 115 | + private String host = PropertiesLoader.getValue("watsonx.baseUrl"); |
| 116 | + private String apiVersion = "2024-03-14"; |
| 117 | + private Boolean isOnPrem; |
| 118 | + private Boolean isZenApiKey; |
| 119 | + private String username; |
| 120 | + |
| 121 | + public Builder(String apiKey){ |
| 122 | + this.apiKey = apiKey; |
| 123 | + } |
| 124 | + public Builder setApiVersion(String apiVersion) { |
| 125 | + this.apiVersion = apiVersion; |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + public Builder setHost(String host) { |
| 130 | + this.host = host; |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + public Builder setIsZenApiKey(Boolean isZenApiKey) { |
| 135 | + this.isZenApiKey = isZenApiKey; |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + public Builder setIsOnPrem(Boolean isOnPrem) { |
| 140 | + this.isOnPrem = isOnPrem; |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + public Builder setUsername(String username) { |
| 145 | + this.username = username; |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + public WatsonxClient build(OkHttpClient.Builder builder) { |
| 150 | + return new WatsonxClient(this, builder); |
| 151 | + } |
| 152 | + |
| 153 | + public WatsonxClient build() { |
| 154 | + return build(new OkHttpClient.Builder()); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
0 commit comments