-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1596 - implementation of genAI service support (#1613)
- Loading branch information
Showing
10 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...rm-api/src/main/java/org/opendatadiscovery/oddplatform/config/WebClientConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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.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; | ||
|
||
@Configuration | ||
public class WebClientConfiguration { | ||
@Value("${genai.url:}") | ||
private String genAIUrl; | ||
@Value("${genai.request_timeout:2}") | ||
private Integer getAiRequestTimeout; | ||
|
||
@Bean("genAiWebClient") | ||
public WebClient webClient() { | ||
final HttpClient httpClient = HttpClient.create().responseTimeout(Duration.ofMinutes(getAiRequestTimeout)); | ||
final ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient); | ||
|
||
return WebClient.builder() | ||
.clientConnector(connector) | ||
.baseUrl(genAIUrl) | ||
.build(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...tform-api/src/main/java/org/opendatadiscovery/oddplatform/controller/GenAIController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.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; | ||
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<ResponseEntity<GenAIResponse>> genAiQuestion(final Mono<GenAIRequest> genAIRequest, | ||
final ServerWebExchange exchange) { | ||
return genAIRequest.flatMap(genAIService::getResponseFromGenAI) | ||
.map(ResponseEntity::ok); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...latform-api/src/main/java/org/opendatadiscovery/oddplatform/exception/GenAIException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.opendatadiscovery.oddplatform.exception; | ||
|
||
public class GenAIException extends ExceptionWithErrorCode { | ||
|
||
public GenAIException(final String message) { | ||
super(ErrorCode.SERVER_EXCEPTION, message); | ||
} | ||
|
||
public GenAIException(final Throwable e) { | ||
super(ErrorCode.SERVER_EXCEPTION, e.getMessage()); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...tform-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.opendatadiscovery.oddplatform.service.genai; | ||
|
||
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<GenAIResponse> getResponseFromGenAI(final GenAIRequest item); | ||
} |
57 changes: 57 additions & 0 deletions
57
...m-api/src/main/java/org/opendatadiscovery/oddplatform/service/genai/GenAIServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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; | ||
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.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; | ||
|
||
@Slf4j | ||
@Service | ||
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 WebClient webClient; | ||
|
||
@Autowired | ||
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<GenAIResponse> 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_FIELD, request.getBody())) | ||
.retrieve() | ||
.bodyToMono(String.class) | ||
.map(item -> new GenAIResponse() | ||
.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))) | ||
: Mono.error(new GenAIException(e))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters