Skip to content

Commit

Permalink
1639 - fix sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladysl committed Mar 1, 2024
1 parent 785eb0a commit 0be7f6d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.opendatadiscovery.oddplatform.config;

import java.time.Duration;
import org.springframework.beans.factory.annotation.Value;
import lombok.RequiredArgsConstructor;
import org.opendatadiscovery.oddplatform.config.properties.GenAIProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.reactive.ClientHttpConnector;
Expand All @@ -10,20 +12,20 @@
import reactor.netty.http.client.HttpClient;

@Configuration
@EnableConfigurationProperties(GenAIProperties.class)
@RequiredArgsConstructor
public class WebClientConfiguration {
@Value("${genai.url:}")
private String genAIUrl;
@Value("${genai.request_timeout:2}")
private Integer getAiRequestTimeout;
private final GenAIProperties genAIProperties;

@Bean("genAiWebClient")
public WebClient webClient() {
final HttpClient httpClient = HttpClient.create().responseTimeout(Duration.ofMinutes(getAiRequestTimeout));
final HttpClient httpClient = HttpClient.create()
.responseTimeout(Duration.ofMinutes(genAIProperties.getRequestTimeout()));
final ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);

return WebClient.builder()
.clientConnector(connector)
.baseUrl(genAIUrl)
.baseUrl(genAIProperties.getUrl())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.opendatadiscovery.oddplatform.config.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("genai")
@Data
public class GenAIProperties {
private boolean enabled;
private String url;
private int requestTimeout;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import io.netty.handler.timeout.ReadTimeoutException;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.opendatadiscovery.oddplatform.api.contract.model.GenAIRequest;
import org.opendatadiscovery.oddplatform.api.contract.model.GenAIResponse;
import org.opendatadiscovery.oddplatform.config.properties.GenAIProperties;
import org.opendatadiscovery.oddplatform.exception.BadUserRequestException;
import org.opendatadiscovery.oddplatform.exception.GenAIException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
Expand All @@ -23,22 +22,19 @@ public class GenAIServiceImpl implements GenAIService {
public static final String QUERY_DATA = "/query_data";
public static final String QUESTION_FIELD = "question";

private final String genAIUrl;
private final Integer getAiRequestTimeout;
private final GenAIProperties genAIProperties;
private final WebClient webClient;

@Autowired
public GenAIServiceImpl(@Value("${genai.url:}") final String genAIUrl,
@Value("${genai.request_timeout:2}") final Integer getAiRequestTimeout,
public GenAIServiceImpl(final GenAIProperties genAIProperties,
@Qualifier("genAiWebClient") final WebClient webClient) {
this.genAIUrl = genAIUrl;
this.getAiRequestTimeout = getAiRequestTimeout;
this.genAIProperties = genAIProperties;
this.webClient = webClient;
}

@Override
public Mono<GenAIResponse> getResponseFromGenAI(final GenAIRequest request) {
if (StringUtils.isBlank(genAIUrl)) {
if (!genAIProperties.isEnabled()) {
return Mono.error(new BadUserRequestException("Gen AI is disabled"));
}

Expand All @@ -51,7 +47,7 @@ public Mono<GenAIResponse> getResponseFromGenAI(final GenAIRequest request) {
.body(StringEscapeUtils.unescapeJava(CharMatcher.is('\"').trimFrom(item))))
.onErrorResume(e -> e.getCause() instanceof ReadTimeoutException
? Mono.error(new GenAIException(
"Gen AI request take longer that %s min".formatted(getAiRequestTimeout)))
"Gen AI request take longer that %s min".formatted(genAIProperties.getRequestTimeout())))
: Mono.error(new GenAIException(e)));
}
}
5 changes: 3 additions & 2 deletions odd-platform-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ spring:
codec:
max-in-memory-size: 20MB

#genai:
genai:
enabled: false
# url: http://localhost:5000
# request_timeout: 2

Expand All @@ -37,7 +38,7 @@ auth:

# Server-To-Server token. Header 'X-API-Key: token'
s2s:
enabled: flase
enabled: false
token:

# For dev purposes only -- successful auth redirect URI
Expand Down

0 comments on commit 0be7f6d

Please sign in to comment.