forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#17359 from hmdrzsharifi/BAEL-8207
Bael 8207: Update Spring AI article and merge modules
- Loading branch information
Showing
36 changed files
with
912 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
spring-ai/src/main/java/com/baeldung/airag/SpringAiRagApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
spring-ai/src/main/java/com/baeldung/airag/controller/ChatBotController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
spring-ai/src/main/java/com/baeldung/airag/service/ChatBotService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
spring-ai/src/main/java/com/baeldung/airag/service/DataLoaderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
spring-ai/src/main/java/com/baeldung/airag/service/DataRetrievalService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
spring-ai/src/main/java/com/baeldung/ollamachatbot/ChatBotApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
spring-ai/src/main/java/com/baeldung/ollamachatbot/controller/HelpDeskController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
spring-ai/src/main/java/com/baeldung/ollamachatbot/model/HelpDeskRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
spring-ai/src/main/java/com/baeldung/ollamachatbot/model/HelpDeskResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
spring-ai/src/main/java/com/baeldung/ollamachatbot/model/HistoryEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.