Skip to content

Commit

Permalink
feat : adds loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Mar 28, 2024
1 parent 3a752bc commit 6df48cf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -30,6 +34,7 @@ Map<String, String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<Document> listOfSimilarDocuments = this.vectorStore.similaritySearch(message);
LOGGER.info("Querying vector store with query :{}", query);
List<Document> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 6df48cf

Please sign in to comment.