Skip to content

Commit

Permalink
GenAiService basis
Browse files Browse the repository at this point in the history
  • Loading branch information
goulven authored and goulven committed Dec 12, 2024
1 parent 25c354a commit 42efd0d
Show file tree
Hide file tree
Showing 18 changed files with 440 additions and 59 deletions.
54 changes: 44 additions & 10 deletions api/src/main/java/org/open4goods/api/config/ApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.open4goods.api.services.completion.IcecatCompletionService;
import org.open4goods.api.services.completion.ResourceCompletionService;
import org.open4goods.api.services.store.DataFragmentStoreService;
import org.open4goods.commons.config.yml.attributes.PromptConfig;
import org.open4goods.commons.config.yml.attributes.LegacyPromptConfig;
import org.open4goods.commons.dao.ProductRepository;
import org.open4goods.commons.exceptions.TechnicalException;
import org.open4goods.commons.exceptions.ValidationException;
Expand All @@ -42,7 +42,8 @@
import org.open4goods.commons.services.SerialisationService;
import org.open4goods.commons.services.StandardiserService;
import org.open4goods.commons.services.VerticalsConfigService;
import org.open4goods.commons.services.ai.AiService;
import org.open4goods.commons.services.ai.GenAiService;
import org.open4goods.commons.services.ai.LegacyAiService;
import org.open4goods.commons.services.textgen.BlablaService;
import org.open4goods.commons.store.repository.elastic.BrandScoresRepository;
import org.open4goods.crawler.config.yml.FetcherProperties;
Expand All @@ -60,7 +61,10 @@
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCache;
Expand All @@ -72,6 +76,8 @@
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.client.RestClient;
import org.springframework.web.reactive.function.client.WebClient;
import org.xml.sax.SAXException;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
Expand Down Expand Up @@ -99,6 +105,9 @@ public ApiConfig(ApiProperties apiProperties, Environment env) {
this.apiProperties = apiProperties;
}




// @Bean
// RedisTemplate<String, Product> redisTemplate(RedisConnectionFactory connectionFactory) {
// RedisTemplate<String, Product> template = new RedisTemplate<>();
Expand All @@ -117,7 +126,7 @@ SerialisationService serialisationService() {
}

@Bean
VerticalsGenerationService verticalsGenerationService(ProductRepository pRepo, SerialisationService serialisationService, AiService aiService, GoogleTaxonomyService gTaxoService, VerticalsConfigService verticalsConfigService, ResourcePatternResolver resourceResolver,
VerticalsGenerationService verticalsGenerationService(ProductRepository pRepo, SerialisationService serialisationService, LegacyAiService aiService, GoogleTaxonomyService gTaxoService, VerticalsConfigService verticalsConfigService, ResourcePatternResolver resourceResolver,
EvaluationService evaluationService, IcecatService icecatService) throws SAXException {
return new VerticalsGenerationService(apiProperties.getVerticalsGenerationConfig(), pRepo, serialisationService, aiService, gTaxoService, verticalsConfigService, resourceResolver, evaluationService, icecatService);
}
Expand All @@ -130,18 +139,41 @@ IcecatService icecatFeatureService(RemoteFileCachingService fileCachingService,
return new IcecatService(new XmlMapper(), apiProperties.getIcecatFeatureConfig(), fileCachingService, apiProperties.remoteCachingFolder(), brandService, verticalConfigService);
}


@Bean

GenAiCompletionService aiCompletionService(AiService aiService, ProductRepository productRepository, VerticalsConfigService verticalConfigService) {
@Qualifier("perplexityChatModel")
OpenAiApi perplexityApi(ApiProperties apiConfig) {
return new OpenAiApi(apiConfig.getGenAiConfig().getPerplexityBaseUrl(),
apiConfig.getGenAiConfig().getPerplexityApiKey(),
apiConfig.getGenAiConfig().getPerplexityCompletionsPath(),
"/v1/embeddings", RestClient.builder(), WebClient.builder(),
RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
}

@Bean
@Qualifier("openAiCustomApi")
OpenAiApi openAiCustomApi(ApiProperties apiConfig) {
return new OpenAiApi(apiConfig.getGenAiConfig().getOpenaiApiKey());
}

@Bean
GenAiService genAiService (@Autowired @Qualifier("perplexityChatModel") OpenAiApi perplexityApi,
@Autowired @Qualifier("openAiCustomApi") OpenAiApi openAiCustomApi,
ApiProperties apiConfig, EvaluationService spelEvaluationService, SerialisationService serialisationService) {
return new GenAiService(apiProperties.getGenAiConfig(), perplexityApi, openAiCustomApi, serialisationService, spelEvaluationService);
}

@Bean
GenAiCompletionService aiCompletionService(LegacyAiService aiService, ProductRepository productRepository, VerticalsConfigService verticalConfigService) {
return new GenAiCompletionService(aiService, productRepository, verticalConfigService, apiProperties);
}

@Bean

AiService aiService(OpenAiChatModel chatModel, EvaluationService spelEvaluationService, SerialisationService serialisationService) {
return new AiService(chatModel, spelEvaluationService, serialisationService);
LegacyAiService aiService(OpenAiChatModel chatModel, EvaluationService spelEvaluationService, SerialisationService serialisationService) {
return new LegacyAiService(chatModel, spelEvaluationService, serialisationService);
}


@Bean

IcecatCompletionService icecatCompletionService(ProductRepository productRepository, VerticalsConfigService verticalConfigService, DataSourceConfigService dataSourceConfigService, AggregationFacadeService aggregationFacade) throws TechnicalException {
Expand Down Expand Up @@ -199,6 +231,8 @@ BrandService brandService(@Autowired RemoteFileCachingService rfc, @Autowired Ap
return new BrandService(rfc, properties.logsFolder(), serialisationService );
}



@Bean
BrandScoreService brandScoreService( @Autowired ApiProperties properties, @Autowired BrandScoresRepository brandScoreRepository) throws Exception {
return new BrandScoreService(brandScoreRepository, properties.logsFolder());
Expand All @@ -207,8 +241,8 @@ BrandScoreService brandScoreService( @Autowired ApiProperties properties, @Autow


@Bean
PromptConfig aiConfig() {
return new PromptConfig();
LegacyPromptConfig aiConfig() {
return new LegacyPromptConfig();
}

@Bean
Expand Down
18 changes: 18 additions & 0 deletions api/src/main/java/org/open4goods/api/config/yml/ApiProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.open4goods.commons.config.yml.IcecatConfiguration;
import org.open4goods.commons.config.yml.IndexationConfig;
import org.open4goods.commons.config.yml.ui.DescriptionsAggregationConfig;
import org.open4goods.commons.config.yml.ui.GenAiConfig;
import org.open4goods.commons.config.yml.ui.ImageGenerationConfig;
import org.open4goods.crawler.config.yml.FetcherProperties;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -189,6 +190,11 @@ public class ApiProperties {
private IndexationConfig indexationConfig = new IndexationConfig();


/**
* Configuration for gen ai
*/
private GenAiConfig genAiConfig = new GenAiConfig();

/**
* Duration of the pause to apply beetween 2 subsequent GenAI generation
*/
Expand Down Expand Up @@ -628,6 +634,18 @@ public void setVerticalsGenerationConfig(VerticalsGenerationConfig verticalsGene



public GenAiConfig getGenAiConfig() {
return genAiConfig;
}



public void setGenAiConfig(GenAiConfig genAiConfig) {
this.genAiConfig = genAiConfig;
}






Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,21 @@
package org.open4goods.api.controller.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.open4goods.api.services.AggregationFacadeService;
import org.open4goods.api.services.BatchService;
import org.open4goods.api.services.completion.GenAiCompletionService;
import org.open4goods.api.services.completion.ResourceCompletionService;
import org.open4goods.commons.dao.ProductRepository;
import org.open4goods.commons.exceptions.AggregationSkipException;
import org.open4goods.commons.exceptions.InvalidParameterException;
import org.open4goods.commons.exceptions.ResourceNotFoundException;
import org.open4goods.commons.model.constants.RolesConstants;
import org.open4goods.commons.services.SerialisationService;
import org.open4goods.commons.services.VerticalsConfigService;
import org.open4goods.commons.services.ai.AiService;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.open4goods.commons.services.ai.GenAiService;
import org.open4goods.commons.services.ai.SamplePromptEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.NotBlank;

/**
* This controller allows informations and communications about DatasourceConfigurations
Expand All @@ -45,22 +30,32 @@
public class GenAiController {


private AiService aiService;
private GenAiService aiService;



public GenAiController(AiService aiService) {
public GenAiController(GenAiService aiService) {
this.aiService = aiService;
}


@GetMapping("/prompt/")
@Operation(summary="Launch prompt")
public String sanitizeOne(@RequestParam(defaultValue = "Agis en tant qu'agent IA offrant de l'information factuelle, précise. Réponds au format JSON") String systemMessage,
@RequestParam String userMessage) throws InvalidParameterException, IOException, ResourceNotFoundException, AggregationSkipException {
String response = aiService.prompt(systemMessage, userMessage);
public String prompt(@RequestParam(defaultValue = "test") String key,
@RequestParam Map<String,Object> context) throws InvalidParameterException, IOException, ResourceNotFoundException, AggregationSkipException {

String response = aiService.prompt(key, context).content();
return response;
}


@GetMapping("/prompt/json")
@Operation(summary="Launch prompt")
public SamplePromptEntity promptJson(@RequestParam(defaultValue = "test") String key,
@RequestParam Map<String,Object> context) throws InvalidParameterException, IOException, ResourceNotFoundException, AggregationSkipException {

SamplePromptEntity response = aiService.entityPrompt(key, context);
return response;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.open4goods.commons.model.data.AiDescriptions;
import org.open4goods.commons.model.product.Product;
import org.open4goods.commons.services.VerticalsConfigService;
import org.open4goods.commons.services.ai.AiService;
import org.open4goods.commons.services.ai.LegacyAiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
Expand All @@ -37,7 +37,7 @@ public class ProductController {
private VerticalsConfigService configService;

@Autowired
private AiService aiService;
private LegacyAiService aiService;



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.open4goods.commons.services.IcecatService;
import org.open4goods.commons.services.SerialisationService;
import org.open4goods.commons.services.VerticalsConfigService;
import org.open4goods.commons.services.ai.AiService;
import org.open4goods.commons.services.ai.LegacyAiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
Expand All @@ -58,11 +58,11 @@ public class VerticalsGenerationService {


private Map<String, VerticalCategoryMapping> sortedMappings = new LinkedHashMap<String, VerticalCategoryMapping>();
private AiService aiService;
private LegacyAiService aiService;
private GoogleTaxonomyService googleTaxonomyService;
private EvaluationService evalService;

public VerticalsGenerationService(VerticalsGenerationConfig config, ProductRepository repository, SerialisationService serialisationService, AiService aiService, GoogleTaxonomyService googleTaxonomyService, VerticalsConfigService verticalsConfigService, ResourcePatternResolver resourceResolver, EvaluationService evaluationService, IcecatService icecatService) {
public VerticalsGenerationService(VerticalsGenerationConfig config, ProductRepository repository, SerialisationService serialisationService, LegacyAiService aiService, GoogleTaxonomyService googleTaxonomyService, VerticalsConfigService verticalsConfigService, ResourcePatternResolver resourceResolver, EvaluationService evaluationService, IcecatService icecatService) {
super();
this.config = config;
this.repository = repository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
import org.open4goods.commons.dao.ProductRepository;
import org.open4goods.commons.model.product.Product;
import org.open4goods.commons.services.VerticalsConfigService;
import org.open4goods.commons.services.ai.AiService;
import org.open4goods.commons.services.ai.LegacyAiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GenAiCompletionService extends AbstractCompletionService{

protected static final Logger logger = LoggerFactory.getLogger(GenAiCompletionService.class);

private AiService aiService;
private LegacyAiService aiService;

private ApiProperties apiProperties;


public GenAiCompletionService( AiService aiService, ProductRepository dataRepository, VerticalsConfigService verticalConfigService, ApiProperties apiProperties) {
public GenAiCompletionService( LegacyAiService aiService, ProductRepository dataRepository, VerticalsConfigService verticalConfigService, ApiProperties apiProperties) {
// TODO(p3,design) : Should set a specific log level here (not "aggregation)" one)
super(dataRepository, verticalConfigService, apiProperties.logsFolder(), apiProperties.aggLogLevel());
this.aiService = aiService;
Expand Down
2 changes: 1 addition & 1 deletion commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.0-M4</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.open4goods.commons.config.yml;

public enum GenAiServiceType {
OPEN_AI,
PERPLEXITY

}
Loading

0 comments on commit 42efd0d

Please sign in to comment.