Skip to content

Commit

Permalink
Merge pull request eugenp#17359 from hmdrzsharifi/BAEL-8207
Browse files Browse the repository at this point in the history
Bael 8207: Update Spring AI article and merge modules
  • Loading branch information
davidmartinezbarua authored Oct 4, 2024
2 parents 9ca4130 + 16e146f commit 734f8c2
Show file tree
Hide file tree
Showing 36 changed files with 912 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void givenMistralAiChatClient_whenAskChatAPIAboutPatientHealthStatusAndWhenThisS
logger.info(paymentStatusResponseContent);

Assertions.assertThat(paymentStatusResponseContent)
.containsIgnoringCase("healthy");
.containsIgnoringCase("healthy");

ChatResponse changeDateResponse = chatClient.call(
new Prompt(
Expand Down Expand Up @@ -106,4 +106,4 @@ void givenHttpClient_whenSendTheRequestToChatAPI_thenShouldBeExpectedWordInRespo
Assertions.assertThat(responseBody)
.containsIgnoringCase("healthy");
}
}
}
46 changes: 45 additions & 1 deletion spring-ai/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,54 @@
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mistral-ai-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-redis-store-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-transformers-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pdf-document-reader</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mongodb-atlas-store-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-redis-store-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-transformers-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pdf-document-reader</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mistral-ai-spring-boot-starter</artifactId>
</dependency>
</dependencies>

<build>
Expand All @@ -97,4 +141,4 @@
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@

@SpringBootApplication
public class SpringAIProjectApplication {
public static void main(String[] args) { SpringApplication.run(SpringAIProjectApplication.class, args);}
public static void main(String[] args) {
SpringApplication.run(SpringAIProjectApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung.airag;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringAiRagApplication {

public static void main(String[] args) {
SpringApplication.run(SpringAiRagApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.baeldung.airag.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baeldung.airag.service.ChatBotService;

@RestController
public class ChatBotController {
@Autowired
private ChatBotService chatBotService;

@GetMapping("/chat")
public Map chat(@RequestParam(name = "query") String query) {
return Map.of("answer", chatBotService.chat(query));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.baeldung.airag.service;

import java.util.List;

import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.document.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class ChatBotService {
@Autowired
@Qualifier("openAiChatModel")
private ChatModel chatClient;
@Autowired
private DataRetrievalService dataRetrievalService;

private final String PROMPT_BLUEPRINT = """
Answer the query strictly referring the provided context:
{context}
Query:
{query}
In case you don't have any answer from the context provided, just say:
I'm sorry I don't have the information you are looking for.
""";

public String chat(String query) {
return chatClient.call(createPrompt(query, dataRetrievalService.searchData(query)));
}

private String createPrompt(String query, List<Document> context) {
PromptTemplate promptTemplate = new PromptTemplate(PROMPT_BLUEPRINT);
promptTemplate.add("query", query);
promptTemplate.add("context", context);
return promptTemplate.render();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.baeldung.airag.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.reader.ExtractedTextFormatter;
import org.springframework.ai.reader.pdf.PagePdfDocumentReader;
import org.springframework.ai.reader.pdf.config.PdfDocumentReaderConfig;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.RedisVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.params.ScanParams;
import redis.clients.jedis.resps.ScanResult;

@Service
public class DataLoaderService {
private static final Logger logger = LoggerFactory.getLogger(DataLoaderService.class);

@Value("classpath:/data/Employee_Handbook.pdf")
private Resource pdfResource;

@Autowired
private VectorStore vectorStore;

public void load() {
PagePdfDocumentReader pdfReader = new PagePdfDocumentReader(this.pdfResource,
PdfDocumentReaderConfig.builder()
.withPageExtractedTextFormatter(ExtractedTextFormatter.builder()
.withNumberOfBottomTextLinesToDelete(3)
.withNumberOfTopPagesToSkipBeforeDelete(1)
.build())
.withPagesPerDocument(1)
.build());
var tokenTextSplitter = new TokenTextSplitter();
this.vectorStore.accept(tokenTextSplitter.apply(pdfReader.get()));
}


public void deleteAll(String keyPattern) {
JedisPooled jedisPooled = ((RedisVectorStore)vectorStore).getJedis();
ScanParams scanParams = new ScanParams().match(keyPattern);
String cursor = scanParams.SCAN_POINTER_START;
do {
ScanResult<String> scanResult = jedisPooled.scan(cursor, scanParams);
List<String> keys = scanResult.getResult();
cursor = scanResult.getCursor();

if (!keys.isEmpty()) {
jedisPooled.del(keys.toArray(new String[0]));
logger.info("Deleted keys: " + keys);
}
} while (!cursor.equals(ScanParams.SCAN_POINTER_START));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.airag.service;

import java.util.List;

import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DataRetrievalService {
@Autowired
private VectorStore vectorStore;

public List<Document> searchData(String query) {
return vectorStore.similaritySearch(query);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baeldung.ollamachatbot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ChatBotApplication {
public static void main(String[] args) {
SpringApplication.run(ChatBotApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.baeldung.ollamachatbot.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baeldung.ollamachatbot.model.HelpDeskResponse;
import com.baeldung.ollamachatbot.service.HelpDeskChatbotAgentService;
import com.baeldung.ollamachatbot.model.HelpDeskRequest;

@RestController
@RequestMapping("/helpdesk")
public class HelpDeskController {
private final HelpDeskChatbotAgentService helpDeskChatbotAgentService;

public HelpDeskController(HelpDeskChatbotAgentService helpDeskChatbotAgentService) {
this.helpDeskChatbotAgentService = helpDeskChatbotAgentService;
}

@PostMapping("/chat")
public ResponseEntity<HelpDeskResponse> chat(@RequestBody HelpDeskRequest helpDeskRequest) {
var chatResponse = helpDeskChatbotAgentService.call(helpDeskRequest.getPromptMessage(), helpDeskRequest.getHistoryId());

return new ResponseEntity<>(new HelpDeskResponse(chatResponse), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baeldung.ollamachatbot.model;

import com.fasterxml.jackson.annotation.JsonProperty;

public class HelpDeskRequest {
@JsonProperty("prompt_message")
String promptMessage;

@JsonProperty("history_id")
String historyId;

public HelpDeskRequest(String promptMessage, String historyId) {
this.promptMessage = promptMessage;
this.historyId = historyId;
}

public String getPromptMessage() {
return promptMessage;
}

public String getHistoryId() {
return historyId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.ollamachatbot.model;

public class HelpDeskResponse {
String result;
public HelpDeskResponse(String result) {
this.result = result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.baeldung.ollamachatbot.model;

import java.util.Objects;

public class HistoryEntry {

private String prompt;

private String response;

public String getPrompt() {
return prompt;
}

public HistoryEntry(String prompt, String response) {
this.prompt = prompt;
this.response = response;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
HistoryEntry that = (HistoryEntry) o;
return Objects.equals(prompt, that.prompt) && Objects.equals(response, that.response);
}

@Override
public int hashCode() {
return Objects.hash(prompt, response);
}

@Override
public String toString() {
return String.format("""
`history_entry`:
`prompt`: %s
`response`: %s
-----------------
""", prompt, response);
}
}
Loading

0 comments on commit 734f8c2

Please sign in to comment.