Skip to content

Commit 78b560c

Browse files
committed
Initial commit
0 parents  commit 78b560c

File tree

9 files changed

+234
-0
lines changed

9 files changed

+234
-0
lines changed

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea
8+
*.iws
9+
*.iml
10+
*.ipr
11+
12+
### Eclipse ###
13+
.apt_generated
14+
.classpath
15+
.factorypath
16+
.project
17+
.settings
18+
.springBeans
19+
.sts4-cache
20+
21+
### NetBeans ###
22+
/nbproject/private/
23+
/nbbuild/
24+
/dist/
25+
/nbdist/
26+
/.nb-gradle/
27+
build/
28+
!**/src/main/**/build/
29+
!**/src/test/**/build/
30+
31+
### VS Code ###
32+
.vscode/
33+
34+
### Mac OS ###
35+
.DS_Store

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# PDF Q&A
2+
202 KB
Binary file not shown.

pom.xml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>org.springframework.boot</groupId>
9+
<artifactId>spring-boot-starter-parent</artifactId>
10+
<version>3.4.1</version>
11+
<relativePath/>
12+
</parent>
13+
14+
<groupId>com.javaaidev</groupId>
15+
<artifactId>pdf-qa</artifactId>
16+
<version>1.0.0-SNAPSHOT</version>
17+
18+
<properties>
19+
<maven.compiler.source>21</maven.compiler.source>
20+
<maven.compiler.target>21</maven.compiler.target>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
<spring-ai.version>1.0.0-M5</spring-ai.version>
23+
</properties>
24+
25+
<dependencyManagement>
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.ai</groupId>
29+
<artifactId>spring-ai-bom</artifactId>
30+
<version>${spring-ai.version}</version>
31+
<type>pom</type>
32+
<scope>import</scope>
33+
</dependency>
34+
</dependencies>
35+
</dependencyManagement>
36+
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.springframework.ai</groupId>
40+
<artifactId>spring-ai-chroma-store-spring-boot-starter</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.ai</groupId>
44+
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.springframework.ai</groupId>
48+
<artifactId>spring-ai-pdf-document-reader</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-web</artifactId>
53+
</dependency>
54+
</dependencies>
55+
56+
<repositories>
57+
<repository>
58+
<id>spring-milestones</id>
59+
<name>Spring Milestones</name>
60+
<url>https://repo.spring.io/milestone</url>
61+
<snapshots>
62+
<enabled>false</enabled>
63+
</snapshots>
64+
</repository>
65+
<repository>
66+
<id>spring-snapshots</id>
67+
<name>Spring Snapshots</name>
68+
<url>https://repo.spring.io/snapshot</url>
69+
<releases>
70+
<enabled>false</enabled>
71+
</releases>
72+
</repository>
73+
</repositories>
74+
75+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.javaaidev.pdfqa;
2+
3+
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
4+
import org.springframework.ai.vectorstore.SearchRequest;
5+
import org.springframework.ai.vectorstore.VectorStore;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
@Configuration
10+
public class AppConfiguration {
11+
12+
@Bean
13+
public QuestionAnswerAdvisor questionAnswerAdvisor(VectorStore vectorStore) {
14+
return new QuestionAnswerAdvisor(vectorStore, SearchRequest.builder().topK(3).build());
15+
}
16+
17+
@Bean
18+
public PDFContentLoader pdfContentLoader(VectorStore vectorStore) {
19+
return new PDFContentLoader(vectorStore);
20+
}
21+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.javaaidev.pdfqa;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Path;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.ai.reader.pdf.PagePdfDocumentReader;
8+
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
9+
import org.springframework.ai.vectorstore.VectorStore;
10+
import org.springframework.boot.CommandLineRunner;
11+
import org.springframework.core.io.FileSystemResource;
12+
13+
public class PDFContentLoader implements CommandLineRunner {
14+
15+
private static final Logger LOGGER = LoggerFactory.getLogger(PDFContentLoader.class);
16+
private final VectorStore vectorStore;
17+
18+
public PDFContentLoader(VectorStore vectorStore) {
19+
this.vectorStore = vectorStore;
20+
}
21+
22+
public void load(Path pdfFilePath) {
23+
LOGGER.info("Load PDF file {}", pdfFilePath);
24+
var reader = new PagePdfDocumentReader(new FileSystemResource(pdfFilePath));
25+
var splitter = new TokenTextSplitter();
26+
var docs = splitter.split(reader.read());
27+
vectorStore.add(docs);
28+
LOGGER.info("Loaded {} docs", docs.size());
29+
}
30+
31+
@Override
32+
public void run(String... args) throws Exception {
33+
var markerFile = Path.of(".", ".pdf-imported");
34+
if (Files.exists(markerFile)) {
35+
return;
36+
}
37+
load(Path.of(".", "content", "Understanding_Climate_Change.pdf"));
38+
Files.createFile(markerFile);
39+
}
40+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.javaaidev.pdfqa;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class PdfQaApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(PdfQaApplication.class, args);
11+
}
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.javaaidev.pdfqa;
2+
3+
import org.springframework.ai.chat.client.ChatClient;
4+
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
5+
import org.springframework.web.bind.annotation.PostMapping;
6+
import org.springframework.web.bind.annotation.RequestBody;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
public class QaController {
11+
12+
private final ChatClient chatClient;
13+
14+
public QaController(ChatClient.Builder builder, QuestionAnswerAdvisor questionAnswerAdvisor) {
15+
this.chatClient = builder.defaultAdvisors(questionAnswerAdvisor).build();
16+
}
17+
18+
@PostMapping("/qa")
19+
public QaResponse qa(@RequestBody QaRequest request) {
20+
return new QaResponse(chatClient.prompt().user(request.input()).call().content());
21+
}
22+
23+
public record QaRequest(String input) {
24+
25+
}
26+
27+
public record QaResponse(String output) {
28+
29+
}
30+
}

src/main/resources/application.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
spring:
2+
application:
3+
name: pdf-qa
4+
threads:
5+
virtual:
6+
enabled: true
7+
ai:
8+
ollama:
9+
chat:
10+
options:
11+
model: "phi3"
12+
temperature: 0
13+
embedding:
14+
options:
15+
model: "bge-large"
16+
vectorstore:
17+
chroma:
18+
collectionName: pdf-qa
19+
initializeSchema: true

0 commit comments

Comments
 (0)