Skip to content

Commit

Permalink
Fix deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pbernet committed May 3, 2024
1 parent 40db87e commit bb01fb3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
27 changes: 17 additions & 10 deletions src/main/scala/alpakka/sse_to_elasticsearch/NerRequestOpenAI.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package alpakka.sse_to_elasticsearch;

import org.apache.commons.io.IOUtils;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
Expand All @@ -15,7 +17,6 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -47,24 +48,30 @@ public String run(String text) {
// For NER this means we get not just 'Person' but also 'Organisation', 'Location'
requestParams.put("temperature", 0.2);

HttpPost request = new HttpPost("https://api.openai.com/v1/completions");
String endpointURL = "https://api.openai.com/v1/completions";
HttpPost request = new HttpPost(endpointURL);
request.setHeader("Authorization", "Bearer " + API_KEY);
StringEntity requestEntity = new StringEntity(
requestParams.toString(),
ContentType.APPLICATION_JSON);
request.setEntity(requestEntity);

RequestConfig timeoutsConfig = RequestConfig.custom()
.setConnectTimeout(Timeout.of(DELAY_TO_RETRY_SECONDS, TimeUnit.SECONDS)).build();
PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create();
connectionManagerBuilder.setDefaultConnectionConfig(ConnectionConfig.custom()
.setSocketTimeout(Timeout.of(DELAY_TO_RETRY_SECONDS, TimeUnit.SECONDS))
.build());

try (CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(timeoutsConfig)
.setConnectionManager(connectionManagerBuilder.build())
.setRetryStrategy(new DefaultHttpRequestRetryStrategy(3, TimeValue.ofMinutes(1L)))
.build()) {
return IOUtils.toString(httpClient.execute(request).getEntity().getContent(), StandardCharsets.UTF_8);
return httpClient.execute(request, response -> {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : "N/A";
});
} catch (IOException e) {
LOGGER.warn("Unable to get result from openai completions endpoint. Cause: ", e);
return "N/A";
LOGGER.warn("Connection issue while accessing openai API endpoint: {}. Cause: ", endpointURL, e);
throw new RuntimeException(e);
}
}
}
11 changes: 7 additions & 4 deletions src/main/scala/sample/stream_shared_state/DownloaderRetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.HttpResponseException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
Expand Down Expand Up @@ -46,11 +47,13 @@ public static void main(String[] args) throws Exception {

public Path download(int traceID, URI url, Path destinationFile) {
LOGGER.info("TRACE_ID: {} about to download...", traceID);
RequestConfig timeoutsConfig = RequestConfig.custom()
.setConnectTimeout(Timeout.of(DELAY_TO_RETRY_SECONDS, TimeUnit.SECONDS)).build();
PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create();
connectionManagerBuilder.setDefaultConnectionConfig(ConnectionConfig.custom()
.setSocketTimeout(Timeout.of(DELAY_TO_RETRY_SECONDS, TimeUnit.SECONDS))
.build());

try (CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(timeoutsConfig)
.setConnectionManager(connectionManagerBuilder.build())
.setRetryStrategy(new CustomHttpRequestRetryStrategy())
.build()) {
Path localPath = httpClient.execute(new HttpGet(url), new HttpResponseHandler(destinationFile));
Expand Down
13 changes: 8 additions & 5 deletions src/main/scala/tools/OpenAICompletions.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package tools;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.util.Timeout;
import org.json.JSONArray;
Expand All @@ -15,7 +16,6 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -100,11 +100,14 @@ public String postRequest(JSONObject requestParams, String endpointURL) {
.setDefaultRequestConfig(timeoutsConfig)
.setRetryStrategy(new HttpRequestRetryStrategyOpenAI())
.build()) {
return IOUtils.toString(httpClient.execute(request).getEntity().getContent(), StandardCharsets.UTF_8);
return httpClient.execute(request, response -> {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : "N/A";
});
} catch (IOException e) {
LOGGER.warn("Connection issue while accessing openai endpoint. Cause: ", e);
LOGGER.warn("Connection issue while accessing openai API endpoint: {}. Cause: ", endpointURL, e);
throw new RuntimeException(e);
}
return "N/A";
}

private static ImmutablePair<String, Integer> extractPayloadChatCompletions(String jsonResponseChatCompletions) {
Expand Down

0 comments on commit bb01fb3

Please sign in to comment.