Skip to content

Instantly share code, notes, and snippets.

@marttp
Created February 10, 2025 06:43
Show Gist options
  • Save marttp/c397be25eade88a39df3e32e1669caa5 to your computer and use it in GitHub Desktop.
Save marttp/c397be25eade88a39df3e32e1669caa5 to your computer and use it in GitHub Desktop.
Setup Agent by Spring AI
package dev.tpcoder.application.agent;
import dev.tpcoder.application.dto.GetNewsByPreferenceRequest;
import dev.tpcoder.application.dto.GetNewsByPreferenceResponse;
import dev.tpcoder.application.dto.UserPreferenceRequest;
import dev.tpcoder.application.dto.UserPreferenceResponse;
import dev.tpcoder.application.repository.UserPreferencesRepository;
import dev.tpcoder.application.service.NewsService;
import dev.tpcoder.application.service.agent.GetNewsByUserPreferences;
import dev.tpcoder.application.service.agent.UserPreferencesService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import java.util.function.Function;
@Configuration
public class AiAgentConfig {
public static final String GET_USER_PREFERENCES_FUNCTION_NAME = "getUserPreferences";
public static final String GET_LATEST_NEWS_BY_TOPIC_FUNCTION_NAME = "getLatestNewsByTopic";
@Bean(name = GET_USER_PREFERENCES_FUNCTION_NAME)
@Description("Get topic by userId")
public Function<UserPreferenceRequest, UserPreferenceResponse> getUserPreferencesInfo(
UserPreferencesRepository userPreferencesRepository) {
return new UserPreferencesService(userPreferencesRepository);
}
@Bean(name = GET_LATEST_NEWS_BY_TOPIC_FUNCTION_NAME)
@Description("Get latest news from user topic")
public Function<GetNewsByPreferenceRequest, GetNewsByPreferenceResponse> getNewsFromPreference(NewsService newsService) {
return new GetNewsByUserPreferences(newsService);
}
}
package dev.tpcoder.application.service.agent;
import dev.tpcoder.application.dto.GetNewsByPreferenceRequest;
import dev.tpcoder.application.dto.GetNewsByPreferenceResponse;
import dev.tpcoder.application.dto.GetNewsRequest;
import dev.tpcoder.application.service.NewsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Function;
public class GetNewsByUserPreferences implements Function<GetNewsByPreferenceRequest, GetNewsByPreferenceResponse> {
private static final Logger logger = LoggerFactory.getLogger(GetNewsByUserPreferences.class);
private final NewsService newsService;
public GetNewsByUserPreferences(NewsService newsService) {
this.newsService = newsService;
}
@Override
public GetNewsByPreferenceResponse apply(GetNewsByPreferenceRequest getNewsByPreferenceRequest) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime last2Days = now.minusDays(2);
LocalDateTime yesterday = now.minusDays(1);
GetNewsRequest request = new GetNewsRequest(
List.of("th", "jp", "us", "gb"),
getNewsByPreferenceRequest.topic(),
last2Days.toLocalDate(),
yesterday.toLocalDate()
);
logger.info("Request: {}", request);
var newsResponse = newsService.getNews(request);
return newsService.extractNewsAndGiveOpinion(newsResponse);
}
}
package dev.tpcoder.application.service.agent;
import dev.tpcoder.application.dto.UserPreferenceRequest;
import dev.tpcoder.application.dto.UserPreferenceResponse;
import dev.tpcoder.application.repository.UserPreferencesRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.function.Function;
public class UserPreferencesService implements Function<UserPreferenceRequest, UserPreferenceResponse> {
private static final Logger logger = LoggerFactory.getLogger(UserPreferencesService.class);
private final UserPreferencesRepository userPreferencesRepository;
public UserPreferencesService(UserPreferencesRepository userPreferencesRepository) {
this.userPreferencesRepository = userPreferencesRepository;
}
@Override
public UserPreferenceResponse apply(UserPreferenceRequest userPreferenceRequest) {
logger.info("UserPreference: {}", userPreferenceRequest);
var preference = userPreferencesRepository.findById(userPreferenceRequest.userId())
.orElseThrow(() -> new IllegalArgumentException("User preferences not found"));
logger.info("User preferences: {}", preference);
var topic = preference.category();
return new UserPreferenceResponse(topic);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment