Skip to content

Commit a1796ad

Browse files
committed
Add Perplexity chat client integration with API updates
Integrated Perplexity chat client into the application by implementing its configuration and exposing endpoints in PdfController. Updated related properties and existing test setup to align with the new integration. Simplified prompts in PodcastServiceImpl and enabled configuration properties for Perplexity.
1 parent dd25221 commit a1796ad

File tree

6 files changed

+71
-25
lines changed

6 files changed

+71
-25
lines changed

src/main/java/app/quantun/summary/config/ai/AiConfig.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44
import org.springframework.ai.anthropic.AnthropicChatModel;
55
import org.springframework.ai.chat.client.ChatClient;
66
import org.springframework.ai.openai.OpenAiChatModel;
7+
import org.springframework.ai.openai.OpenAiChatOptions;
8+
import org.springframework.ai.openai.api.OpenAiApi;
79
import org.springframework.ai.vertexai.gemini.VertexAiGeminiChatModel;
10+
import org.springframework.beans.factory.annotation.Qualifier;
11+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
812
import org.springframework.context.annotation.Bean;
913
import org.springframework.context.annotation.Configuration;
14+
import org.springframework.context.annotation.Primary;
1015

1116
/**
1217
* Configuration class for AI chat clients.
1318
* This class provides beans for OpenAI and Gemini chat clients.
1419
*/
1520
@Configuration
16-
//@EnableConfigurationProperties(PerplexityProperties.class)
21+
@EnableConfigurationProperties(PerplexityProperties.class)
1722
public class AiConfig {
1823

1924
/**
@@ -24,6 +29,7 @@ public class AiConfig {
2429
*/
2530
//@Primary
2631
@Bean
32+
@Primary
2733
ChatClient openAiChatClient(OpenAiChatModel chatModel) {
2834
return ChatClient.create(chatModel);
2935
}
@@ -41,4 +47,17 @@ ChatClient anthropicChatClient(AnthropicChatModel chatModel) {
4147
}
4248

4349

50+
@Bean
51+
public ChatClient perplexityChatClient(@Qualifier("perplexityProperties")
52+
PerplexityProperties perplexityProperties) {
53+
OpenAiApi openAiApi = new OpenAiApi(perplexityProperties.getBaseUrl(), perplexityProperties.getApiKey());
54+
OpenAiChatOptions chatOptions = OpenAiChatOptions.builder()
55+
.model(perplexityProperties.getChat().getModel())
56+
.temperature(perplexityProperties.getChat().getTemperature())
57+
.build();
58+
OpenAiChatModel chatModel = new OpenAiChatModel(openAiApi, chatOptions);
59+
return ChatClient.create(chatModel);
60+
}
61+
62+
4463
}

src/main/java/app/quantun/summary/config/ai/PerplexityProperties.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package app.quantun.summary.config.ai;
22

33
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.context.annotation.Configuration;
46

5-
// Configuration Properties Class
6-
//@ConfigurationProperties(prefix = "spring.ai.perplexity")
7-
//@Data
7+
@Configuration
8+
@ConfigurationProperties(prefix = "spring.ai.perplexity")
9+
10+
11+
@Data
812
public class PerplexityProperties {
913
private String apiKey;
1014
private String baseUrl;

src/main/java/app/quantun/summary/controller/PdfController.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
import io.swagger.v3.oas.annotations.responses.ApiResponse;
99
import io.swagger.v3.oas.annotations.tags.Tag;
1010
import lombok.RequiredArgsConstructor;
11+
import org.springframework.ai.chat.client.ChatClient;
12+
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.beans.factory.annotation.Qualifier;
1115
import org.springframework.http.MediaType;
1216
import org.springframework.http.ResponseEntity;
17+
import org.springframework.web.bind.annotation.GetMapping;
1318
import org.springframework.web.bind.annotation.PostMapping;
1419
import org.springframework.web.bind.annotation.RequestParam;
1520
import org.springframework.web.bind.annotation.RestController;
@@ -21,6 +26,15 @@
2126
public class PdfController {
2227

2328
private final PdfServices pdfServices;
29+
@Autowired
30+
@Qualifier("anthropicChatClient")
31+
private ChatClient anthropicChatClient;
32+
@Autowired
33+
@Qualifier("openAiChatClient")
34+
private ChatClient openAiChatClient;
35+
@Autowired
36+
@Qualifier("perplexityChatClient")
37+
private ChatClient perplexityChatClient;
2438

2539
/**
2640
* Handles the upload of a PDF file.
@@ -59,5 +73,20 @@ public TableIndexContent getTableOfContent(String message) {
5973
return this.pdfServices.getBookTableOfContentPages(message);
6074
}
6175

76+
@GetMapping("/anthropic")
77+
public String getAnthropicChatClient() {
78+
return anthropicChatClient.prompt().user("GIVE A NUMBER BETWEEN ONE TO FIVE").call().content();
79+
}
80+
81+
@GetMapping("/openai")
82+
public String getOpenAiChatClient() {
83+
return openAiChatClient.prompt().user("GIVE A NUMBER BETWEEN ONE TO FIVE").call().content();
84+
}
85+
86+
@GetMapping("/perplexity")
87+
public String getPerplexityChatClient() {
88+
return perplexityChatClient.prompt().advisors(new SimpleLoggerAdvisor()).user("GIVE A NUMBER BETWEEN ONE TO FIVE").call().content();
89+
}
90+
6291

6392
}

src/main/java/app/quantun/summary/service/impl/PodcastServiceImpl.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ public PodCast createScript(Book book) {
5757

5858
BeanOutputConverter<PodCast> format = new BeanOutputConverter<>(PodCast.class);
5959

60-
Prompt systemPrompt = systemPromptTemplate.create(Map.of("summary", bookSummaryJson, "format", format.getFormat()));
60+
Prompt systemPrompt = systemPromptTemplate.create();
6161

6262
Prompt userPrompt = userPromptTemplate.create(Map.of("summary", bookSummaryJson, "format", format.getFormat()));
6363

6464

65-
6665
String aiResponse = chatClient.prompt(systemPrompt).advisors(new SimpleLoggerAdvisor()).user(userPrompt.getContents()).call().content();
6766

6867

@@ -77,7 +76,5 @@ public PodCast createScript(Book book) {
7776
}
7877

7978

80-
81-
8279
}
8380
}

src/main/resources/application.properties

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ spring.ai.anthropic.chat.options.max-tokens=8192
7575
spring.ai.perplexity.api-key=${PERPLEXITY_API_KEY}
7676
spring.ai.perplexity.base-url=https://api.perplexity.ai
7777
spring.ai.perplexity.chat.completions-path=/chat/completions
78-
spring.ai.perplexity.chat.model=llama-3.1-sonar-small-128k-online
79-
spring.ai.perplexity.chat.temperature=0.7
78+
spring.ai.perplexity.chat.model=sonar-pro
79+
spring.ai.perplexity.chat.temperature=0.9
8080
##############################################################################################
8181
# File Upload Configuration
8282
############################################################
@@ -91,16 +91,16 @@ azure.tts.server-url=${AZURE_TTS_SERVER_URL}
9191
# Application-Specific File Paths
9292
############################################################
9393
# Path for PDF uploads
94-
app.config.pdf.upload.path=c:/tmp/summary-ai-pdf/
94+
app.config.pdf.upload.path=${UPLOAD_CONTENT_PATH}
9595
# Path where TTS files are saved
96-
app.config.tts.save.path=c:/tmp/summary-ai-tts/
96+
app.config.tts.save.path=${UPLOAD_TTS_PATH}
9797
############################################################
9898
# Logging Configuration
9999
############################################################
100100
logging.level.root=INFO
101101
logging.level.org.springframework.web.reactive.function.client=INFO
102-
logging.level.org.springframework.http.codec=DEBUG
103-
logging.level.org.springframework.web.service=DEBUG
102+
logging.level.org.springframework.http.codec=INFO
103+
logging.level.org.springframework.web.service=INFO
104104
# Spring AI advisor logging
105105
logging.level.org.springframework.ai.chat.client.advisor=DEBUG
106106
# Optional: Full request/response logging

src/test/java/app/quantun/summary/message/consumer/BookConsumerTest.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package app.quantun.summary.message.consumer;
22

33
import app.quantun.summary.repository.BookRepository;
4+
import app.quantun.summary.service.PodcastService;
5+
import app.quantun.summary.service.SummaryServices;
46
import org.junit.jupiter.api.BeforeEach;
57
import org.junit.jupiter.api.Test;
68
import org.mockito.Mockito;
@@ -26,23 +28,18 @@ class BookConsumerTest {
2628

2729
@BeforeEach
2830
public void setUp(EmbeddedKafkaBroker embeddedKafkaBroker) {
29-
/*
30-
// Create a mock SummaryBookRepository to satisfy the dependency of SummaryBookConsumerImpl.
31-
SummaryBookRepository repository = mock(SummaryBookRepository.class);
32-
summaryBookConsumer = Mockito.spy(new SummaryBookConsumerImpl(repository));
33-
3431
Map<String, Object> producerProps = KafkaTestUtils.producerProps(embeddedKafkaBroker);
35-
ProducerFactory<String, Map<String, String>> producerFactory = new DefaultKafkaProducerFactory<>(producerProps);
36-
kafkaTemplate = new KafkaTemplate<>(producerFactory);
37-
*/
3832

39-
// Create a mock SummaryBookRepository to satisfy the dependency of SummaryBookConsumerImpl.
4033
BookRepository repository = Mockito.mock(BookRepository.class);
41-
summaryBookConsumer = Mockito.spy(new SummaryBookConsumerImpl(repository));
34+
PodcastService podcastService = Mockito.mock(PodcastService.class);
35+
36+
SummaryServices summaryServices = Mockito.mock(SummaryServices.class);
37+
38+
summaryBookConsumer = Mockito.spy(new SummaryBookConsumerImpl(repository, summaryServices, podcastService));
4239

43-
Map<String, Object> producerProps = KafkaTestUtils.producerProps(embeddedKafkaBroker);
4440
ProducerFactory<String, Map<String, String>> producerFactory = new DefaultKafkaProducerFactory<>(producerProps);
4541
KafkaTemplate<String, Map<String, String>> kafkaTemplate = new KafkaTemplate<>(producerFactory);
42+
4643
}
4744

4845
@Test

0 commit comments

Comments
 (0)