Skip to content

Commit

Permalink
Visibility and formatting improvements (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
kszapsza authored Jan 7, 2025
1 parent a6a3786 commit f09389a
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@SpringBootApplication
public class SpringAiRagApplication {
public static void main(String[] args) {
SpringApplication.run(SpringAiRagApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(SpringAiRagApplication.class, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

@Configuration
@EnableConfigurationProperties(ChatProperties.class)
public class ApplicationConfiguration {
class ApplicationConfiguration {
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package io.github.kszapsza.springairag.adapter.application;

import io.github.kszapsza.springairag.adapter.db.realestate.RealEstateRepositoryFeeder;
import io.github.kszapsza.springairag.domain.embedding.EmbeddingsFeeder;
import io.github.kszapsza.springairag.domain.realestate.RealEstateFeeder;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
@Profile("local")
public class ApplicationStartupListener {
class ApplicationStartupListener {

private final EmbeddingsFeeder embeddingsFeeder;
private final RealEstateRepositoryFeeder realEstateRepositoryFeeder;
private final RealEstateFeeder realEstateFeeder;

public ApplicationStartupListener(
ApplicationStartupListener(
EmbeddingsFeeder embeddingsFeeder,
RealEstateRepositoryFeeder realEstateRepositoryFeeder) {
RealEstateFeeder realEstateFeeder) {
this.embeddingsFeeder = embeddingsFeeder;
this.realEstateRepositoryFeeder = realEstateRepositoryFeeder;
this.realEstateFeeder = realEstateFeeder;
}

@EventListener(ApplicationReadyEvent.class)
public void onApplicationReady() {
void onApplicationReady() {
embeddingsFeeder.importEmbeddings();
realEstateRepositoryFeeder.feedDummyData();
realEstateFeeder.feedDummyData();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
import java.util.List;

@Component
public class RealEstateRepositoryFeeder implements RealEstateFeeder {
class RealEstateRepositoryFeeder implements RealEstateFeeder {

private static final Logger logger = LoggerFactory.getLogger(RealEstateRepositoryFeeder.class);

private final RealEstateRepository realEstateRepository;
private final ResourceLoader resourceLoader;
private final ObjectMapper objectMapper;

public RealEstateRepositoryFeeder(
RealEstateRepositoryFeeder(
RealEstateRepository realEstateRepository,
ResourceLoader resourceLoader,
ObjectMapper objectMapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

import java.util.List;

public class ClasspathEmbeddingDocumentsProvider implements EmbeddingDocumentsProvider {
class ClasspathEmbeddingDocumentsProvider implements EmbeddingDocumentsProvider {

private static final Logger logger = LoggerFactory.getLogger(ClasspathEmbeddingDocumentsProvider.class);

private final List<Document> data;

public ClasspathEmbeddingDocumentsProvider(Resource documentsResource) {
ClasspathEmbeddingDocumentsProvider(Resource documentsResource) {
if (documentsResource == null || !documentsResource.exists() || !documentsResource.isReadable()) {
throw new IllegalArgumentException("RAG input data is missing or not readable");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.core.io.Resource;

public class ClasspathSystemPromptTemplateProvider implements SystemPromptTemplateProvider {
class ClasspathSystemPromptTemplateProvider implements SystemPromptTemplateProvider {

private static final Logger logger = LoggerFactory.getLogger(ClasspathSystemPromptTemplateProvider.class);

private final SystemPromptTemplate systemPromptTemplate;

public ClasspathSystemPromptTemplateProvider(Resource systemPromptResource) {
ClasspathSystemPromptTemplateProvider(Resource systemPromptResource) {
if (systemPromptResource == null || !systemPromptResource.exists() || !systemPromptResource.isReadable()) {
throw new IllegalArgumentException("System prompt resource is missing or not readable");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.kszapsza.springairag.adapter.llm.function;

import io.github.kszapsza.springairag.adapter.db.realestate.RealEstateRepository;
import io.github.kszapsza.springairag.adapter.llm.function.realestate.RealEstateSearchFunction;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.kszapsza.springairag.adapter.llm.function.realestate;
package io.github.kszapsza.springairag.adapter.llm.function;

import io.github.kszapsza.springairag.adapter.db.realestate.RealEstateEntity;
import io.github.kszapsza.springairag.adapter.db.realestate.RealEstateRepository;
Expand All @@ -9,14 +9,14 @@
import java.util.List;
import java.util.function.Function;

public class RealEstateSearchFunction
class RealEstateSearchFunction
implements Function<RealEstateSearchFunction.Request, RealEstateSearchFunction.Response> {

private static final Logger logger = LoggerFactory.getLogger(RealEstateSearchFunction.class);

private final RealEstateRepository realEstateRepository;

public RealEstateSearchFunction(RealEstateRepository realEstateRepository) {
RealEstateSearchFunction(RealEstateRepository realEstateRepository) {
this.realEstateRepository = realEstateRepository;
}

Expand All @@ -37,14 +37,14 @@ public Response apply(Request request) {
}
}

public record Request(
record Request(
String countryCode,
String location,
BigDecimal minPrice,
BigDecimal maxPrice,
Integer minBedrooms) {
}

public record Response(List<RealEstateEntity> results) {
record Response(List<RealEstateEntity> results) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import java.util.List;

@Component
public class ChatMemoryProvider implements ChatMemoryPort {
class ChatMemoryProvider implements ChatMemoryPort {

private static final Logger logger = LoggerFactory.getLogger(ChatMemoryProvider.class);

private final ChatMemory chatMemory;

public ChatMemoryProvider(ChatMemory chatMemory) {
ChatMemoryProvider(ChatMemory chatMemory) {
this.chatMemory = chatMemory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

@Component
public class OpenAiChatAdapter implements ChatPort {
class OpenAiChatAdapter implements ChatPort {

private static final Logger logger = LoggerFactory.getLogger(OpenAiChatAdapter.class);

Expand All @@ -27,7 +27,7 @@ public class OpenAiChatAdapter implements ChatPort {
private final List<Advisor> advisors;
private final Message systemMessage;

public OpenAiChatAdapter(
OpenAiChatAdapter(
ChatClient chatClient,
ChatOptions chatOptions,
List<Advisor> advisors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import org.springframework.stereotype.Component;

@Component
public class OpenAiEmbeddingsFeeder implements EmbeddingsFeeder {
class OpenAiEmbeddingsFeeder implements EmbeddingsFeeder {

private static final Logger logger = LoggerFactory.getLogger(OpenAiEmbeddingsFeeder.class);

private final EmbeddingDocumentsProvider dataProvider;
private final VectorStore vectorStore;

public OpenAiEmbeddingsFeeder(
OpenAiEmbeddingsFeeder(
VectorStore vectorStore,
EmbeddingDocumentsProvider dataProvider) {
this.vectorStore = vectorStore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@

@RestController
@RequestMapping("/api/chat")
public class ChatController {
class ChatController {

private static final Logger logger = LoggerFactory.getLogger(ChatController.class);

private final ChatPort chatPort;

public ChatController(ChatPort chatPort) {
ChatController(ChatPort chatPort) {
this.chatPort = chatPort;
}

@PostMapping(produces = { MediaType.APPLICATION_JSON_VALUE }, consumes = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<ChatResponseDto> chat(@Valid @RequestBody ChatRequestDto request) {
ResponseEntity<ChatResponseDto> chat(@Valid @RequestBody ChatRequestDto request) {
var chatResponse = chatPort.chat(request.toDomain());

return switch (chatResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@

@RestController
@RequestMapping("/api/conversation")
public class ConversationController {
class ConversationController {

private final ChatMemoryPort chatMemoryPort;
private final int maxLastN;

public ConversationController(
ConversationController(
ChatMemoryPort chatMemoryPort,
@Value("${app.conversation.max-last-n}") int maxLastN) {
this.chatMemoryPort = chatMemoryPort;
this.maxLastN = maxLastN;
}

@GetMapping(path = "{conversationId}", produces = { APPLICATION_JSON_VALUE }, consumes = { APPLICATION_JSON_VALUE })
public ResponseEntity<ConversationHistoryDto> getConversationHistory(
@GetMapping(path = "{conversationId}", produces = {APPLICATION_JSON_VALUE}, consumes = {APPLICATION_JSON_VALUE})
ResponseEntity<ConversationHistoryDto> getConversationHistory(
@PathVariable(name = "conversationId") @Valid @NotBlank String conversationId,
@RequestParam(name = "lastN", defaultValue = "10") int lastN) {
if (lastN > maxLastN) {
Expand All @@ -47,21 +47,11 @@ public ResponseEntity<ConversationHistoryDto> getConversationHistory(
new ConversationHistoryDto(
conversationId,
messages.stream()
.map((message) -> new ConversationHistoryDto.ChatMessageDto(
message.content(),
ConversationHistoryDto.ChatMessageTypeDto.valueOf(message.type().name())))
.map(ConversationHistoryDto.ChatMessageDto::fromDomain)
.toList()));
}

public record ConversationHistoryDto(String conversationId, List<ChatMessageDto> messages) {
record ChatMessageDto(String content, ChatMessageTypeDto type) {
static ChatMessageDto fromDomain(ChatMessage message) {
return new ChatMessageDto(
message.content(),
ChatMessageTypeDto.fromDomain(message.type()));
}
}

record ConversationHistoryDto(String conversationId, List<ChatMessageDto> messages) {
enum ChatMessageTypeDto {
USER,
ASSISTANT;
Expand All @@ -73,5 +63,13 @@ static ChatMessageTypeDto fromDomain(ChatMessageType type) {
};
}
}

record ChatMessageDto(String content, ChatMessageTypeDto type) {
static ChatMessageDto fromDomain(ChatMessage message) {
return new ChatMessageDto(
message.content(),
ChatMessageTypeDto.fromDomain(message.type()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

class SampleUnitTest {
@Test
void dummyUnitTestCase() {
assertEquals(42, 42, "42 should equal 42");
}
@Test
void dummyUnitTestCase() {
assertEquals(42, 42, "42 should equal 42");
}
}

0 comments on commit f09389a

Please sign in to comment.