Skip to content

Commit bb01fb3

Browse files
committed
Fix deprecated methods
1 parent 40db87e commit bb01fb3

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

src/main/scala/alpakka/sse_to_elasticsearch/NerRequestOpenAI.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package alpakka.sse_to_elasticsearch;
22

3-
import org.apache.commons.io.IOUtils;
43
import org.apache.hc.client5.http.classic.methods.HttpPost;
5-
import org.apache.hc.client5.http.config.RequestConfig;
4+
import org.apache.hc.client5.http.config.ConnectionConfig;
65
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
76
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
87
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
8+
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
99
import org.apache.hc.core5.http.ContentType;
10+
import org.apache.hc.core5.http.HttpEntity;
11+
import org.apache.hc.core5.http.io.entity.EntityUtils;
1012
import org.apache.hc.core5.http.io.entity.StringEntity;
1113
import org.apache.hc.core5.util.TimeValue;
1214
import org.apache.hc.core5.util.Timeout;
@@ -15,7 +17,6 @@
1517
import org.slf4j.LoggerFactory;
1618

1719
import java.io.IOException;
18-
import java.nio.charset.StandardCharsets;
1920
import java.util.concurrent.TimeUnit;
2021

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

50-
HttpPost request = new HttpPost("https://api.openai.com/v1/completions");
51+
String endpointURL = "https://api.openai.com/v1/completions";
52+
HttpPost request = new HttpPost(endpointURL);
5153
request.setHeader("Authorization", "Bearer " + API_KEY);
5254
StringEntity requestEntity = new StringEntity(
5355
requestParams.toString(),
5456
ContentType.APPLICATION_JSON);
5557
request.setEntity(requestEntity);
5658

57-
RequestConfig timeoutsConfig = RequestConfig.custom()
58-
.setConnectTimeout(Timeout.of(DELAY_TO_RETRY_SECONDS, TimeUnit.SECONDS)).build();
59+
PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create();
60+
connectionManagerBuilder.setDefaultConnectionConfig(ConnectionConfig.custom()
61+
.setSocketTimeout(Timeout.of(DELAY_TO_RETRY_SECONDS, TimeUnit.SECONDS))
62+
.build());
5963

6064
try (CloseableHttpClient httpClient = HttpClientBuilder.create()
61-
.setDefaultRequestConfig(timeoutsConfig)
65+
.setConnectionManager(connectionManagerBuilder.build())
6266
.setRetryStrategy(new DefaultHttpRequestRetryStrategy(3, TimeValue.ofMinutes(1L)))
6367
.build()) {
64-
return IOUtils.toString(httpClient.execute(request).getEntity().getContent(), StandardCharsets.UTF_8);
68+
return httpClient.execute(request, response -> {
69+
HttpEntity entity = response.getEntity();
70+
return entity != null ? EntityUtils.toString(entity) : "N/A";
71+
});
6572
} catch (IOException e) {
66-
LOGGER.warn("Unable to get result from openai completions endpoint. Cause: ", e);
67-
return "N/A";
73+
LOGGER.warn("Connection issue while accessing openai API endpoint: {}. Cause: ", endpointURL, e);
74+
throw new RuntimeException(e);
6875
}
6976
}
7077
}

src/main/scala/sample/stream_shared_state/DownloaderRetry.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
66
import org.apache.hc.client5.http.HttpResponseException;
77
import org.apache.hc.client5.http.classic.methods.HttpGet;
8-
import org.apache.hc.client5.http.config.RequestConfig;
8+
import org.apache.hc.client5.http.config.ConnectionConfig;
99
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
1010
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
11+
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
1112
import org.apache.hc.core5.http.ClassicHttpResponse;
1213
import org.apache.hc.core5.http.HttpRequest;
1314
import org.apache.hc.core5.http.HttpResponse;
@@ -46,11 +47,13 @@ public static void main(String[] args) throws Exception {
4647

4748
public Path download(int traceID, URI url, Path destinationFile) {
4849
LOGGER.info("TRACE_ID: {} about to download...", traceID);
49-
RequestConfig timeoutsConfig = RequestConfig.custom()
50-
.setConnectTimeout(Timeout.of(DELAY_TO_RETRY_SECONDS, TimeUnit.SECONDS)).build();
50+
PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create();
51+
connectionManagerBuilder.setDefaultConnectionConfig(ConnectionConfig.custom()
52+
.setSocketTimeout(Timeout.of(DELAY_TO_RETRY_SECONDS, TimeUnit.SECONDS))
53+
.build());
5154

5255
try (CloseableHttpClient httpClient = HttpClientBuilder.create()
53-
.setDefaultRequestConfig(timeoutsConfig)
56+
.setConnectionManager(connectionManagerBuilder.build())
5457
.setRetryStrategy(new CustomHttpRequestRetryStrategy())
5558
.build()) {
5659
Path localPath = httpClient.execute(new HttpGet(url), new HttpResponseHandler(destinationFile));

src/main/scala/tools/OpenAICompletions.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package tools;
22

3-
import org.apache.commons.io.IOUtils;
43
import org.apache.commons.lang3.tuple.ImmutablePair;
54
import org.apache.hc.client5.http.classic.methods.HttpPost;
65
import org.apache.hc.client5.http.config.RequestConfig;
76
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
87
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
98
import org.apache.hc.core5.http.ContentType;
9+
import org.apache.hc.core5.http.HttpEntity;
10+
import org.apache.hc.core5.http.io.entity.EntityUtils;
1011
import org.apache.hc.core5.http.io.entity.StringEntity;
1112
import org.apache.hc.core5.util.Timeout;
1213
import org.json.JSONArray;
@@ -15,7 +16,6 @@
1516
import org.slf4j.LoggerFactory;
1617

1718
import java.io.IOException;
18-
import java.nio.charset.StandardCharsets;
1919
import java.util.concurrent.TimeUnit;
2020

2121
/**
@@ -100,11 +100,14 @@ public String postRequest(JSONObject requestParams, String endpointURL) {
100100
.setDefaultRequestConfig(timeoutsConfig)
101101
.setRetryStrategy(new HttpRequestRetryStrategyOpenAI())
102102
.build()) {
103-
return IOUtils.toString(httpClient.execute(request).getEntity().getContent(), StandardCharsets.UTF_8);
103+
return httpClient.execute(request, response -> {
104+
HttpEntity entity = response.getEntity();
105+
return entity != null ? EntityUtils.toString(entity) : "N/A";
106+
});
104107
} catch (IOException e) {
105-
LOGGER.warn("Connection issue while accessing openai endpoint. Cause: ", e);
108+
LOGGER.warn("Connection issue while accessing openai API endpoint: {}. Cause: ", endpointURL, e);
109+
throw new RuntimeException(e);
106110
}
107-
return "N/A";
108111
}
109112

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

0 commit comments

Comments
 (0)