From 217064dbefde74ac3a15e818e0de0928c6f30c4d Mon Sep 17 00:00:00 2001 From: vburlachenko Date: Thu, 8 Feb 2024 20:03:36 +0200 Subject: [PATCH 1/3] 1596 - MVP for genAI service support --- gradle/libs.versions.toml | 5 ++- .../config/WebClientConfiguration.java | 28 ++++++++++++++ .../controller/GenAIController.java | 24 ++++++++++++ .../oddplatform/exception/GenAIException.java | 8 ++++ .../service/genai/GenAIService.java | 9 +++++ .../service/genai/GenAIServiceImpl.java | 38 +++++++++++++++++++ .../src/main/resources/application.yml | 3 ++ odd-platform-specification/components.yaml | 12 ++++++ odd-platform-specification/openapi.yaml | 24 +++++++++++- 9 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/config/WebClientConfiguration.java create mode 100644 odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/controller/GenAIController.java create mode 100644 odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/exception/GenAIException.java create mode 100644 odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIService.java create mode 100644 odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIServiceImpl.java diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index b69b9483a..e4681d38b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -7,6 +7,7 @@ oddrn-generator-java = '0.1.21' odd-integration-manifests = '0.0.6' apache-collections = '4.4' apache-lang = '3.12.0' +apache-text = '1.9' r2dbc-spi = '1.0.0.RELEASE' r2dbc-postgresql = '1.0.1.RELEASE' r2dbc-pool = '1.0.0.RELEASE' @@ -67,6 +68,7 @@ oddrn-generator-java = { module = 'org.opendatadiscovery:oddrn-generator-java', odd-integration-manifests = { module = 'org.opendatadiscovery:integration-manifests', version.ref = 'odd-integration-manifests' } apache-collections = { module = 'org.apache.commons:commons-collections4', version.ref = 'apache-collections' } apache-lang = { module = 'org.apache.commons:commons-lang3', version.ref = 'apache-lang' } +apache-text = { module = 'org.apache.commons:commons-text', version.ref = 'apache-text' } r2dbc-spi = { module = 'io.r2dbc:r2dbc-spi', version.ref = 'r2dbc-spi' } r2dbc-postgresql = { module = 'org.postgresql:r2dbc-postgresql', version.ref = 'r2dbc-postgresql' } r2dbc-pool = { module = 'io.r2dbc:r2dbc-pool', version.ref = 'r2dbc-pool' } @@ -142,7 +144,8 @@ internal = [ ] apache-commons = [ 'apache-collections', - 'apache-lang' + 'apache-lang', + 'apache-text' ] r2dbc = [ 'r2dbc-spi', diff --git a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/config/WebClientConfiguration.java b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/config/WebClientConfiguration.java new file mode 100644 index 000000000..72eabaafc --- /dev/null +++ b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/config/WebClientConfiguration.java @@ -0,0 +1,28 @@ +package org.opendatadiscovery.oddplatform.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.http.client.reactive.ClientHttpConnector; +import org.springframework.http.client.reactive.ReactorClientHttpConnector; +import reactor.netty.http.client.HttpClient; +import java.time.Duration; + +@Configuration +public class WebClientConfiguration { + + @Value("${genai.url:}") + private String genAIUrl; + + @Bean + public WebClient webClient() { + final HttpClient httpClient = HttpClient.create().responseTimeout(Duration.ofMinutes(2)); + final ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient); + + return WebClient.builder() + .clientConnector(connector) + .baseUrl(genAIUrl) + .build(); + } +} diff --git a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/controller/GenAIController.java b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/controller/GenAIController.java new file mode 100644 index 000000000..194b0e9e7 --- /dev/null +++ b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/controller/GenAIController.java @@ -0,0 +1,24 @@ +package org.opendatadiscovery.oddplatform.controller; + +import lombok.RequiredArgsConstructor; +import org.opendatadiscovery.oddplatform.api.contract.api.GenaiApi; +import org.opendatadiscovery.oddplatform.api.contract.model.GenAIQuestionResult; +import org.opendatadiscovery.oddplatform.api.contract.model.GenAIRequest; +import org.opendatadiscovery.oddplatform.service.genai.GenAIService; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +@RestController +@RequiredArgsConstructor +public class GenAIController implements GenaiApi { + private final GenAIService genAIService; + + @Override + public Mono> genAiQuestion(final Mono genAIRequest, + final ServerWebExchange exchange) { + return genAIRequest.flatMap(genAIService::getResponseFromGenAI) + .map(ResponseEntity::ok); + } +} diff --git a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/exception/GenAIException.java b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/exception/GenAIException.java new file mode 100644 index 000000000..97835cec1 --- /dev/null +++ b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/exception/GenAIException.java @@ -0,0 +1,8 @@ +package org.opendatadiscovery.oddplatform.exception; + +public class GenAIException extends RuntimeException { + + public GenAIException(final Throwable e) { + super(e); + } +} diff --git a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIService.java b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIService.java new file mode 100644 index 000000000..04e7d2fb5 --- /dev/null +++ b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIService.java @@ -0,0 +1,9 @@ +package org.opendatadiscovery.oddplatform.service.genai; + +import org.opendatadiscovery.oddplatform.api.contract.model.GenAIQuestionResult; +import org.opendatadiscovery.oddplatform.api.contract.model.GenAIRequest; +import reactor.core.publisher.Mono; + +public interface GenAIService { + Mono getResponseFromGenAI(final GenAIRequest item); +} diff --git a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIServiceImpl.java b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIServiceImpl.java new file mode 100644 index 000000000..5bd966661 --- /dev/null +++ b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIServiceImpl.java @@ -0,0 +1,38 @@ +package org.opendatadiscovery.oddplatform.service.genai; + +import io.netty.handler.timeout.ReadTimeoutException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.text.StringEscapeUtils; +import org.opendatadiscovery.oddplatform.api.contract.model.GenAIQuestionResult; +import org.opendatadiscovery.oddplatform.api.contract.model.GenAIRequest; +import org.opendatadiscovery.oddplatform.exception.GenAIException; +import org.springframework.stereotype.Service; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; +import java.util.Map; +import java.util.concurrent.TimeoutException; + +@Service +@RequiredArgsConstructor +@Slf4j +public class GenAIServiceImpl implements GenAIService { + private final WebClient webClient; + + @Override + public Mono getResponseFromGenAI(GenAIRequest request) { + return webClient.post() + .uri("/query_data") + .bodyValue(Map.of("question", request.getQuestion())) + .retrieve() + .bodyToMono(String.class) + .onErrorMap(e -> { + if (e.getCause() instanceof ReadTimeoutException) { + return new TimeoutException(); + } else { + return new GenAIException(e); + } + }) + .map(item -> new GenAIQuestionResult().questionResponse(StringEscapeUtils.unescapeJava(item.replaceAll("^\"|\"$", "")))); + } +} diff --git a/odd-platform-api/src/main/resources/application.yml b/odd-platform-api/src/main/resources/application.yml index f1f386e9d..45b26ca05 100644 --- a/odd-platform-api/src/main/resources/application.yml +++ b/odd-platform-api/src/main/resources/application.yml @@ -14,6 +14,9 @@ spring: codec: max-in-memory-size: 20MB +genai: + url: http://localhost:5000 + springdoc: api-docs: path: /api/v3/swagger-ui.html diff --git a/odd-platform-specification/components.yaml b/odd-platform-specification/components.yaml index 93321f9d7..75b58804a 100644 --- a/odd-platform-specification/components.yaml +++ b/odd-platform-specification/components.yaml @@ -4063,6 +4063,18 @@ components: - GRAPH - ALL + GenAIRequest: + type: object + properties: + question: + type: string + + GenAIQuestionResult: + type: object + properties: + question_response: + type: string + parameters: PageParam: name: page diff --git a/odd-platform-specification/openapi.yaml b/odd-platform-specification/openapi.yaml index 31b88aebc..fb0391f0e 100644 --- a/odd-platform-specification/openapi.yaml +++ b/odd-platform-specification/openapi.yaml @@ -44,6 +44,7 @@ tags: - name: dataQualityRuns - name: queryExample - name: referenceData + - name: genai paths: /api/integrations: @@ -3883,4 +3884,25 @@ paths: schema: $ref: './components.yaml/#/components/schemas/DataEntityGraphRelationshipDetails' tags: - - relationship \ No newline at end of file + - relationship + + /api/genai/question: + post: + summary: Request to genai service + description: create request to genai service with specific question + operationId: genAiQuestion + requestBody: + required: true + content: + application/json: + schema: + $ref: './components.yaml/#/components/schemas/GenAIRequest' + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: './components.yaml/#/components/schemas/GenAIQuestionResult' + tags: + - genai \ No newline at end of file From ee08842135ab91d89ce770730fb03356d8a25a23 Mon Sep 17 00:00:00 2001 From: vburlachenko Date: Fri, 9 Feb 2024 19:06:13 +0200 Subject: [PATCH 2/3] 1596 - implementation of genAI service support --- .../config/WebClientConfiguration.java | 11 ++-- .../controller/GenAIController.java | 6 +-- .../exception/ControllerAdvice.java | 7 +++ .../oddplatform/exception/GenAIException.java | 8 ++- .../service/genai/GenAIService.java | 4 +- .../service/genai/GenAIServiceImpl.java | 51 ++++++++++++------- .../src/main/resources/application.yml | 1 + odd-platform-specification/components.yaml | 6 +-- odd-platform-specification/openapi.yaml | 4 +- 9 files changed, 63 insertions(+), 35 deletions(-) diff --git a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/config/WebClientConfiguration.java b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/config/WebClientConfiguration.java index 72eabaafc..13c88413c 100644 --- a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/config/WebClientConfiguration.java +++ b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/config/WebClientConfiguration.java @@ -1,23 +1,24 @@ package org.opendatadiscovery.oddplatform.config; +import java.time.Duration; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.web.reactive.function.client.WebClient; import org.springframework.http.client.reactive.ClientHttpConnector; import org.springframework.http.client.reactive.ReactorClientHttpConnector; +import org.springframework.web.reactive.function.client.WebClient; import reactor.netty.http.client.HttpClient; -import java.time.Duration; @Configuration public class WebClientConfiguration { - @Value("${genai.url:}") private String genAIUrl; + @Value("${genai.request_timeout:2}") + private Integer getAiRequestTimeout; - @Bean + @Bean("genAiWebClient") public WebClient webClient() { - final HttpClient httpClient = HttpClient.create().responseTimeout(Duration.ofMinutes(2)); + final HttpClient httpClient = HttpClient.create().responseTimeout(Duration.ofMinutes(getAiRequestTimeout)); final ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient); return WebClient.builder() diff --git a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/controller/GenAIController.java b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/controller/GenAIController.java index 194b0e9e7..c22143670 100644 --- a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/controller/GenAIController.java +++ b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/controller/GenAIController.java @@ -2,8 +2,8 @@ import lombok.RequiredArgsConstructor; import org.opendatadiscovery.oddplatform.api.contract.api.GenaiApi; -import org.opendatadiscovery.oddplatform.api.contract.model.GenAIQuestionResult; import org.opendatadiscovery.oddplatform.api.contract.model.GenAIRequest; +import org.opendatadiscovery.oddplatform.api.contract.model.GenAIResponse; import org.opendatadiscovery.oddplatform.service.genai.GenAIService; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RestController; @@ -16,8 +16,8 @@ public class GenAIController implements GenaiApi { private final GenAIService genAIService; @Override - public Mono> genAiQuestion(final Mono genAIRequest, - final ServerWebExchange exchange) { + public Mono> genAiQuestion(final Mono genAIRequest, + final ServerWebExchange exchange) { return genAIRequest.flatMap(genAIService::getResponseFromGenAI) .map(ResponseEntity::ok); } diff --git a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/controller/exception/ControllerAdvice.java b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/controller/exception/ControllerAdvice.java index 57caaf817..f5e79d712 100644 --- a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/controller/exception/ControllerAdvice.java +++ b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/controller/exception/ControllerAdvice.java @@ -8,6 +8,7 @@ import org.opendatadiscovery.oddplatform.exception.CascadeDeleteException; import org.opendatadiscovery.oddplatform.exception.ErrorCode; import org.opendatadiscovery.oddplatform.exception.ExceptionWithErrorCode; +import org.opendatadiscovery.oddplatform.exception.GenAIException; import org.opendatadiscovery.oddplatform.exception.NotFoundException; import org.opendatadiscovery.oddplatform.exception.UniqueConstraintException; import org.springframework.http.HttpStatus; @@ -51,6 +52,12 @@ public ErrorResponse handleException(final WebExchangeBindException e) { return buildResponse(e); } + @ExceptionHandler(GenAIException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public ErrorResponse handleGenAIException(final GenAIException e) { + return buildResponse(e); + } + @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ErrorResponse handleServerException(final Exception e) { diff --git a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/exception/GenAIException.java b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/exception/GenAIException.java index 97835cec1..d6b1799ae 100644 --- a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/exception/GenAIException.java +++ b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/exception/GenAIException.java @@ -1,8 +1,12 @@ package org.opendatadiscovery.oddplatform.exception; -public class GenAIException extends RuntimeException { +public class GenAIException extends ExceptionWithErrorCode { + + public GenAIException(final String message) { + super(ErrorCode.SERVER_EXCEPTION, message); + } public GenAIException(final Throwable e) { - super(e); + super(ErrorCode.SERVER_EXCEPTION, e.getMessage()); } } diff --git a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIService.java b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIService.java index 04e7d2fb5..e1aa2182f 100644 --- a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIService.java +++ b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIService.java @@ -1,9 +1,9 @@ package org.opendatadiscovery.oddplatform.service.genai; -import org.opendatadiscovery.oddplatform.api.contract.model.GenAIQuestionResult; import org.opendatadiscovery.oddplatform.api.contract.model.GenAIRequest; +import org.opendatadiscovery.oddplatform.api.contract.model.GenAIResponse; import reactor.core.publisher.Mono; public interface GenAIService { - Mono getResponseFromGenAI(final GenAIRequest item); + Mono getResponseFromGenAI(final GenAIRequest item); } diff --git a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIServiceImpl.java b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIServiceImpl.java index 5bd966661..56baba84d 100644 --- a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIServiceImpl.java +++ b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIServiceImpl.java @@ -1,38 +1,53 @@ package org.opendatadiscovery.oddplatform.service.genai; import io.netty.handler.timeout.ReadTimeoutException; -import lombok.RequiredArgsConstructor; +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.GenAIQuestionResult; import org.opendatadiscovery.oddplatform.api.contract.model.GenAIRequest; +import org.opendatadiscovery.oddplatform.api.contract.model.GenAIResponse; +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; -import java.util.Map; -import java.util.concurrent.TimeoutException; -@Service -@RequiredArgsConstructor @Slf4j +@Service public class GenAIServiceImpl implements GenAIService { - private final WebClient webClient; + public static final String QUERY_DATA = "/query_data"; + public static final String QUESTION_FIELD = "question"; + + @Value("${genai.url:}") + private String genAIUrl; + + @Value("${genai.request_timeout:2}") + private Integer getAiRequestTimeout; + + @Autowired + @Qualifier("genAiWebClient") + private WebClient webClient; @Override - public Mono getResponseFromGenAI(GenAIRequest request) { + public Mono getResponseFromGenAI(final GenAIRequest request) { + if (StringUtils.isBlank(genAIUrl)) { + return Mono.error(new BadUserRequestException("Gen AI is disabled")); + } + return webClient.post() - .uri("/query_data") - .bodyValue(Map.of("question", request.getQuestion())) + .uri(QUERY_DATA) + .bodyValue(Map.of(QUESTION_FIELD, request.getBody())) .retrieve() .bodyToMono(String.class) - .onErrorMap(e -> { - if (e.getCause() instanceof ReadTimeoutException) { - return new TimeoutException(); - } else { - return new GenAIException(e); - } - }) - .map(item -> new GenAIQuestionResult().questionResponse(StringEscapeUtils.unescapeJava(item.replaceAll("^\"|\"$", "")))); + .map(item -> new GenAIResponse() + .body(StringEscapeUtils.unescapeJava(item.replaceAll("^\"|\"$", "")))) + .onErrorResume(e -> e.getCause() instanceof ReadTimeoutException + ? Mono.error(new GenAIException( + "Gen AI request take longer that %s min".formatted(getAiRequestTimeout))) + : Mono.error(new GenAIException(e))); } } diff --git a/odd-platform-api/src/main/resources/application.yml b/odd-platform-api/src/main/resources/application.yml index 45b26ca05..da7c31bbb 100644 --- a/odd-platform-api/src/main/resources/application.yml +++ b/odd-platform-api/src/main/resources/application.yml @@ -16,6 +16,7 @@ spring: genai: url: http://localhost:5000 + request_timeout: 2 springdoc: api-docs: diff --git a/odd-platform-specification/components.yaml b/odd-platform-specification/components.yaml index 75b58804a..804c773e9 100644 --- a/odd-platform-specification/components.yaml +++ b/odd-platform-specification/components.yaml @@ -4066,13 +4066,13 @@ components: GenAIRequest: type: object properties: - question: + body: type: string - GenAIQuestionResult: + GenAIResponse: type: object properties: - question_response: + body: type: string parameters: diff --git a/odd-platform-specification/openapi.yaml b/odd-platform-specification/openapi.yaml index fb0391f0e..fc1659db3 100644 --- a/odd-platform-specification/openapi.yaml +++ b/odd-platform-specification/openapi.yaml @@ -3886,7 +3886,7 @@ paths: tags: - relationship - /api/genai/question: + /api/genai/ask: post: summary: Request to genai service description: create request to genai service with specific question @@ -3903,6 +3903,6 @@ paths: content: application/json: schema: - $ref: './components.yaml/#/components/schemas/GenAIQuestionResult' + $ref: './components.yaml/#/components/schemas/GenAIResponse' tags: - genai \ No newline at end of file From 9fced87436ea168743d63417fa270b61cfa686b8 Mon Sep 17 00:00:00 2001 From: vburlachenko Date: Mon, 12 Feb 2024 15:29:25 +0200 Subject: [PATCH 3/3] 1596 - fix sonar issues --- .../service/genai/GenAIServiceImpl.java | 20 +++++++++++-------- .../src/main/resources/application.yml | 6 +++--- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIServiceImpl.java b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIServiceImpl.java index 56baba84d..37b384089 100644 --- a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIServiceImpl.java +++ b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIServiceImpl.java @@ -1,5 +1,6 @@ package org.opendatadiscovery.oddplatform.service.genai; +import com.google.common.base.CharMatcher; import io.netty.handler.timeout.ReadTimeoutException; import java.util.Map; import lombok.extern.slf4j.Slf4j; @@ -22,15 +23,18 @@ public class GenAIServiceImpl implements GenAIService { public static final String QUERY_DATA = "/query_data"; public static final String QUESTION_FIELD = "question"; - @Value("${genai.url:}") - private String genAIUrl; - - @Value("${genai.request_timeout:2}") - private Integer getAiRequestTimeout; + private final String genAIUrl; + private final Integer getAiRequestTimeout; + private final WebClient webClient; @Autowired - @Qualifier("genAiWebClient") - private WebClient webClient; + public GenAIServiceImpl(@Value("${genai.url:}") final String genAIUrl, + @Value("${genai.request_timeout:2}") final Integer getAiRequestTimeout, + @Qualifier("genAiWebClient") final WebClient webClient) { + this.genAIUrl = genAIUrl; + this.getAiRequestTimeout = getAiRequestTimeout; + this.webClient = webClient; + } @Override public Mono getResponseFromGenAI(final GenAIRequest request) { @@ -44,7 +48,7 @@ public Mono getResponseFromGenAI(final GenAIRequest request) { .retrieve() .bodyToMono(String.class) .map(item -> new GenAIResponse() - .body(StringEscapeUtils.unescapeJava(item.replaceAll("^\"|\"$", "")))) + .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))) diff --git a/odd-platform-api/src/main/resources/application.yml b/odd-platform-api/src/main/resources/application.yml index da7c31bbb..c6bdd25bb 100644 --- a/odd-platform-api/src/main/resources/application.yml +++ b/odd-platform-api/src/main/resources/application.yml @@ -14,9 +14,9 @@ spring: codec: max-in-memory-size: 20MB -genai: - url: http://localhost:5000 - request_timeout: 2 +#genai: +# url: http://localhost:5000 +# request_timeout: 2 springdoc: api-docs: