Skip to content

Commit 58a32fb

Browse files
committed
addressed reviews, using boot autoconfig
1 parent 9e8766c commit 58a32fb

File tree

5 files changed

+42
-270
lines changed

5 files changed

+42
-270
lines changed

examples/rag-spring-article/pom.xml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,29 @@
3939
<dependency>
4040
<groupId>org.springframework.ai</groupId>
4141
<artifactId>spring-ai-bom</artifactId>
42-
<version>1.0.0-SNAPSHOT</version>
42+
<version>1.0.0-M3</version>
4343
<type>pom</type>
4444
<scope>import</scope>
4545
</dependency>
4646
</dependencies>
4747
</dependencyManagement>
4848
<dependencies>
49+
<!-- autoconfiguration of beans-->
4950
<dependency>
5051
<groupId>org.springframework.ai</groupId>
51-
<artifactId>spring-ai-elasticsearch-store</artifactId>
52+
<artifactId>spring-ai-spring-boot-autoconfigure</artifactId>
5253
<version>1.0.0-SNAPSHOT</version>
5354
</dependency>
55+
5456
<dependency>
55-
<groupId>org.apache.tika</groupId>
56-
<artifactId>tika-core</artifactId>
57-
<version>2.9.2</version>
57+
<groupId>org.springframework.ai</groupId>
58+
<artifactId>spring-ai-elasticsearch-store</artifactId>
59+
<version>1.0.0-SNAPSHOT</version>
5860
</dependency>
5961
<dependency>
60-
<groupId>org.apache.tika</groupId>
61-
<artifactId>tika-parser-pdf-module</artifactId>
62-
<version>2.9.2</version>
62+
<groupId>org.springframework.ai</groupId>
63+
<artifactId>spring-ai-pdf-document-reader</artifactId>
64+
<version>1.0.0-SNAPSHOT</version>
6365
</dependency>
6466

6567
<dependency>

examples/rag-spring-article/src/main/java/co/elastic/clients/rag/article/Config.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

examples/rag-spring-article/src/main/java/co/elastic/clients/rag/article/PageContentHandler.java

Lines changed: 0 additions & 128 deletions
This file was deleted.

examples/rag-spring-article/src/main/java/co/elastic/clients/rag/article/RagService.java

Lines changed: 24 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,79 +18,34 @@
1818
*/
1919
package co.elastic.clients.rag.article;
2020

21-
import org.apache.tika.exception.TikaException;
22-
import org.apache.tika.metadata.Metadata;
23-
import org.apache.tika.parser.AutoDetectParser;
24-
import org.apache.tika.parser.ParseContext;
25-
import org.apache.tika.parser.Parser;
26-
import org.apache.tika.parser.pdf.PDFParserConfig;
27-
import org.springframework.ai.chat.messages.Message;
28-
import org.springframework.ai.chat.messages.UserMessage;
29-
import org.springframework.ai.chat.model.ChatModel;
30-
import org.springframework.ai.chat.model.ChatResponse;
31-
import org.springframework.ai.chat.prompt.Prompt;
32-
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
21+
import org.springframework.ai.chat.client.ChatClient;
3322
import org.springframework.ai.document.Document;
23+
import org.springframework.ai.reader.pdf.PagePdfDocumentReader;
3424
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
3525
import org.springframework.ai.vectorstore.ElasticsearchVectorStore;
3626
import org.springframework.ai.vectorstore.SearchRequest;
37-
import org.springframework.beans.factory.annotation.Autowired;
3827
import org.springframework.stereotype.Service;
39-
import org.xml.sax.SAXException;
4028

41-
import java.io.FileInputStream;
42-
import java.io.IOException;
43-
import java.util.ArrayList;
44-
import java.util.HashMap;
4529
import java.util.List;
46-
import java.util.Map;
4730
import java.util.stream.Collectors;
4831

4932
@Service
5033
public class RagService {
5134

35+
// Both beans autowired from default configuration
5236
private ElasticsearchVectorStore vectorStore;
53-
private ChatModel chatModel;
37+
private ChatClient chatClient;
5438

55-
@Autowired
56-
public RagService(ElasticsearchVectorStore vectorStore, ChatModel model) {
39+
public RagService(ElasticsearchVectorStore vectorStore, ChatClient.Builder clientBuilder) {
5740
this.vectorStore = vectorStore;
58-
this.chatModel = model;
41+
this.chatClient = clientBuilder.build();
5942
}
6043

61-
public void ingestPDF(String path) throws IOException, TikaException, SAXException {
62-
// Initializing the PDF parser
63-
// Keep in mind that AutoDetectParser is not thread safe
64-
Parser parser = new AutoDetectParser();
65-
// Using our custom single page handler class
66-
PageContentHandler handler = new PageContentHandler();
44+
public void ingestPDF(String path) {
6745

68-
// No need for any other specific PDF configuration
69-
ParseContext parseContext = new ParseContext();
70-
parseContext.set(PDFParserConfig.class, new PDFParserConfig());
71-
72-
// The metadata contain information such as creation date, creation tool used, etc... which we
73-
// don't need
74-
Metadata metadata = new Metadata();
75-
76-
// Reading the file
77-
try (FileInputStream stream = new FileInputStream(path)) {
78-
parser.parse(stream, handler, metadata, parseContext);
79-
}
80-
81-
// Getting the result as a list of Strings with the content of the pages
82-
List<String> allPages = handler.getPages();
83-
List<Document> docbatch = new ArrayList<>();
84-
85-
// Converting pages to Documents
86-
for (int i = 0; i < allPages.size(); i++) {
87-
Map<String, Object> docMetadata = new HashMap<>();
88-
// The page number will be used in the response
89-
docMetadata.put("page", i + 1);
90-
91-
Document doc = new Document(allPages.get(i), docMetadata);
92-
docbatch.add(doc);
93-
}
46+
// Spring AI utility class to read a PDF file page by page
47+
PagePdfDocumentReader pdfReader = new PagePdfDocumentReader(path);
48+
List<Document> docbatch = pdfReader.read();
9449

9550
// Sending batch of documents to vector store
9651
// applying tokenizer
@@ -109,31 +64,31 @@ public String queryLLM(String question) {
10964
.map(Document::getContent)
11065
.collect(Collectors.joining(System.lineSeparator()));
11166

112-
// Setting the prompt
113-
String basePrompt = """
67+
// Setting the prompt with the context
68+
String prompt = """
11469
You're assisting with providing the rules of the tabletop game Runewars.
115-
Use the information from the DOCUMENTS section to provide accurate answers.
70+
Use the information from the DOCUMENTS section to provide accurate answers to the
71+
question in the QUESTION section.
11672
If unsure, simply state that you don't know.
11773
11874
DOCUMENTS:
119-
{documents}
120-
""";
121-
122-
// Preparing the question for the LLM
123-
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(basePrompt);
124-
Message systemMessage = systemPromptTemplate.createMessage(Map.of("documents", documents));
75+
""" + documents
76+
+ """
77+
QUESTION:
78+
""" + question;
12579

126-
UserMessage userMessage = new UserMessage(question);
12780

128-
Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
12981
// Calling the chat model with the question
130-
ChatResponse response = chatModel.call(prompt);
82+
String response = chatClient.prompt()
83+
.user(prompt)
84+
.call()
85+
.content();
13186

132-
return response.getResult().getOutput().getContent() +
87+
return response +
13388
System.lineSeparator() +
13489
"Found at page: " +
13590
// Retrieving the first ranked page number from the document metadata
136-
vectorStoreResult.get(0).getMetadata().get("page") +
91+
vectorStoreResult.get(0).getMetadata().get(PagePdfDocumentReader.METADATA_START_PAGE_NUMBER) +
13792
" of the manual";
13893
}
13994
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
spring.application.name=rag
2+
3+
spring.ai.openai.api-key=${OPENAI_API_KEY}
4+
spring.ai.chat.client.enabled=true
5+
6+
spring.elasticsearch.uris=${ES_SERVER_URL}
7+
spring.elasticsearch.username=${ES_USERNAME}
8+
spring.elasticsearch.password=${ES_PASSWORD}
9+
spring.ai.vectorstore.elasticsearch.initialize-schema=true

0 commit comments

Comments
 (0)