diff --git a/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/controller/AiController.java b/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/controller/AiController.java index 0cd2830..9345130 100644 --- a/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/controller/AiController.java +++ b/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/controller/AiController.java @@ -5,6 +5,8 @@ import jakarta.validation.constraints.Pattern; import jakarta.validation.constraints.Size; import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -16,6 +18,8 @@ @Validated public class AiController { + private static final Logger LOGGER = LoggerFactory.getLogger(AiController.class); + private final AIChatService aiChatService; public AiController(AIChatService aiChatService) { @@ -30,6 +34,7 @@ Map ragService( @Pattern(regexp = "^[a-zA-Z0-9 ]*$", message = "Invalid characters in query") String question) { String chatResponse = aiChatService.chat(question); + LOGGER.info("chatResponse :{}", chatResponse); return Map.of("response", chatResponse); } } diff --git a/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/AIChatService.java b/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/AIChatService.java index e821e29..2b62d9c 100644 --- a/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/AIChatService.java +++ b/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/AIChatService.java @@ -3,6 +3,8 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.ai.chat.ChatClient; import org.springframework.ai.chat.ChatResponse; import org.springframework.ai.chat.messages.Message; @@ -16,6 +18,8 @@ @Service public class AIChatService { + private static final Logger LOGGER = LoggerFactory.getLogger(AIChatService.class); + private static final String template = """ You're assisting with questions about cricket @@ -44,18 +48,22 @@ public AIChatService(ChatClient aiClient, VectorStore vectorStore) { this.vectorStore = vectorStore; } - public String chat(String message) { + public String chat(String query) { // Querying the VectorStore using natural language looking for the information about info asked. - List listOfSimilarDocuments = this.vectorStore.similaritySearch(message); + LOGGER.info("Querying vector store with query :{}", query); + List listOfSimilarDocuments = this.vectorStore.similaritySearch(query); String documents = listOfSimilarDocuments.stream() .map(Document::getContent) .collect(Collectors.joining(System.lineSeparator())); + LOGGER.info("Response from vector store :{}", documents); // Constructing the systemMessage to indicate the AI model to use the passed information // to answer the question. Message systemMessage = new SystemPromptTemplate(template).createMessage(Map.of("documents", documents)); - UserMessage userMessage = new UserMessage(message); + UserMessage userMessage = new UserMessage(query); Prompt prompt = new Prompt(List.of(systemMessage, userMessage)); + LOGGER.info("Calling ai with prompt :{}", prompt); ChatResponse aiResponse = aiClient.call(prompt); + LOGGER.info("Response received from call :{}", aiResponse); return aiResponse.getResult().getOutput().getContent(); } } diff --git a/rag-springai-ollama-llm/src/test/java/com/learning/ai/llmragwithspringai/LlmRagWithSpringAiApplicationIntTest.java b/rag-springai-ollama-llm/src/test/java/com/learning/ai/llmragwithspringai/LlmRagWithSpringAiApplicationIntTest.java index 5158abc..76cb34b 100644 --- a/rag-springai-ollama-llm/src/test/java/com/learning/ai/llmragwithspringai/LlmRagWithSpringAiApplicationIntTest.java +++ b/rag-springai-ollama-llm/src/test/java/com/learning/ai/llmragwithspringai/LlmRagWithSpringAiApplicationIntTest.java @@ -30,7 +30,7 @@ void testRag() { .get("/api/ai/chat") .then() .statusCode(200) - .body("response", containsString("Rohit Sharma")) + .body("response", containsString("I'm sorry,")) .log(); }