-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : expose endpoint to upload multiple documents (#46)
* feat : expose endpoint to upload multiple documents * feat : adds more test cases * fix : add more assertions * feat : fix spotless
- Loading branch information
1 parent
d6f4e17
commit fefbb7f
Showing
7 changed files
with
233 additions
and
42 deletions.
There are no files selected for viewing
30 changes: 0 additions & 30 deletions
30
...pringai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/config/AppConfig.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 |
---|---|---|
@@ -1,44 +1,14 @@ | ||
package com.learning.ai.llmragwithspringai.config; | ||
|
||
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.VectorStore; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.Resource; | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
public class AppConfig { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(AppConfig.class); | ||
|
||
@Value("classpath:Rohit_Gurunath_Sharma.pdf") | ||
private Resource resource; | ||
|
||
@Bean | ||
TokenTextSplitter tokenTextSplitter() { | ||
return new TokenTextSplitter(); | ||
} | ||
|
||
@Bean | ||
ApplicationRunner runner(VectorStore vectorStore, TokenTextSplitter tokenTextSplitter) { | ||
return args -> { | ||
LOGGER.info("Loading file(s) as Documents"); | ||
PdfDocumentReaderConfig config = PdfDocumentReaderConfig.builder() | ||
.withPageExtractedTextFormatter(new ExtractedTextFormatter.Builder() | ||
.withNumberOfBottomTextLinesToDelete(3) | ||
.withNumberOfTopPagesToSkipBeforeDelete(1) | ||
.build()) | ||
.withPagesPerDocument(1) | ||
.build(); | ||
PagePdfDocumentReader pagePdfDocumentReader = new PagePdfDocumentReader(resource, config); | ||
vectorStore.accept(tokenTextSplitter.apply(pagePdfDocumentReader.get())); | ||
LOGGER.info("Loaded document to database."); | ||
}; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...-llm/src/main/java/com/learning/ai/llmragwithspringai/controller/DataIndexController.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,36 @@ | ||
package com.learning.ai.llmragwithspringai.controller; | ||
|
||
import com.learning.ai.llmragwithspringai.service.DataIndexerService; | ||
import java.util.Map; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@RestController | ||
@RequestMapping("/api/data/v1/") | ||
public class DataIndexController { | ||
|
||
private final DataIndexerService dataIndexerService; | ||
|
||
public DataIndexController(DataIndexerService dataIndexerService) { | ||
this.dataIndexerService = dataIndexerService; | ||
} | ||
|
||
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) | ||
public ResponseEntity<String> load(@RequestPart("file") MultipartFile multipartFile) { | ||
try { | ||
this.dataIndexerService.loadData(multipartFile.getResource()); | ||
return ResponseEntity.ok("Data indexed successfully!"); | ||
} catch (Exception e) { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.body("An error occurred while indexing data: " + e.getMessage()); | ||
} | ||
} | ||
|
||
@GetMapping("count") | ||
public Map<String, Long> count() { | ||
return Map.of("count", dataIndexerService.count()); | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
...lama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/DataIndexerService.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,59 @@ | ||
package com.learning.ai.llmragwithspringai.service; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.ai.document.DocumentReader; | ||
import org.springframework.ai.reader.ExtractedTextFormatter; | ||
import org.springframework.ai.reader.JsonReader; | ||
import org.springframework.ai.reader.TextReader; | ||
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.VectorStore; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class DataIndexerService { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(DataIndexerService.class); | ||
|
||
private final TokenTextSplitter tokenTextSplitter; | ||
private final VectorStore vectorStore; | ||
|
||
public DataIndexerService(TokenTextSplitter tokenTextSplitter, VectorStore vectorStore) { | ||
this.tokenTextSplitter = tokenTextSplitter; | ||
this.vectorStore = vectorStore; | ||
} | ||
|
||
public void loadData(Resource documentResource) { | ||
DocumentReader documentReader = null; | ||
if (documentResource.getFilename() != null | ||
&& documentResource.getFilename().endsWith(".pdf")) { | ||
LOGGER.info("Loading PDF document"); | ||
PdfDocumentReaderConfig pdfDocumentReaderConfig = PdfDocumentReaderConfig.builder() | ||
.withPageExtractedTextFormatter(ExtractedTextFormatter.builder() | ||
.withNumberOfBottomTextLinesToDelete(3) | ||
.withNumberOfTopPagesToSkipBeforeDelete(1) | ||
.build()) | ||
.withPagesPerDocument(1) | ||
.build(); | ||
documentReader = new PagePdfDocumentReader(documentResource, pdfDocumentReaderConfig); | ||
} else if (documentResource.getFilename() != null | ||
&& documentResource.getFilename().endsWith(".txt")) { | ||
documentReader = new TextReader(documentResource); | ||
} else if (documentResource.getFilename() != null | ||
&& documentResource.getFilename().endsWith(".json")) { | ||
documentReader = new JsonReader(documentResource); | ||
} | ||
if (documentReader != null) { | ||
LOGGER.info("Loading text document to redis vector database"); | ||
vectorStore.accept(tokenTextSplitter.apply(documentReader.get())); | ||
LOGGER.info("Loaded document to redis vector database."); | ||
} | ||
} | ||
|
||
public long count() { | ||
return this.vectorStore.similaritySearch("*").size(); | ||
} | ||
} |
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