Package io.agentscope.core.rag.reader
Class ExternalApiReader
java.lang.Object
io.agentscope.core.rag.reader.AbstractChunkingReader
io.agentscope.core.rag.reader.ExternalApiReader
- All Implemented Interfaces:
Reader
Generic external API document parsing reader that supports integration with any third-party
document parsing service.
This reader is designed to be highly abstract and configurable, adapting to different APIs through functional interfaces:
- Supports custom HTTP request construction (headers, body, method, etc.)
- Supports custom response parsing (extracting markdown from JSON/XML/other formats)
- Supports polling for asynchronous task completion
- Supports file upload or URL passing
Example usage - Integrating with MinerU Tianshu:
ExternalApiReader reader = ExternalApiReader.builder()
.requestBuilder((filePath, client) -> {
// Build task submission request
String json = String.format(
"{\"file_path\":\"%s\",\"engine\":\"pipeline\"}",
filePath
);
return new Request.Builder()
.url("http://localhost:8000/api/v1/tasks/submit")
.header("Content-Type", "application/json")
.post(RequestBody.create(json, MediaType.get("application/json")))
.build();
})
.responseParser((response, client) -> {
// Extract task_id from response, then poll for results
String responseBody = response.body().string();
// Parse JSON and poll...
return markdown;
})
.chunkSize(512)
.splitStrategy(SplitStrategy.PARAGRAPH)
.overlapSize(50)
.supportedFormats("pdf", "docx", "pptx", "xlsx", "txt") // Specify supported formats
.build();
List<Document> docs = reader.read(ReaderInput.fromString("path/to/file.pdf")).block();
Example usage - Integrating with other APIs:
ExternalApiReader reader = ExternalApiReader.builder()
.requestBuilder((filePath, client) -> {
// Custom request building logic
byte[] fileBytes = Files.readAllBytes(Paths.get(filePath));
RequestBody body = RequestBody.create(fileBytes, MediaType.get("application/octet-stream"));
return new Request.Builder()
.url("https://api.example.com/parse")
.header("Authorization", "Bearer YOUR_TOKEN")
.post(body)
.build();
})
.responseParser((response, client) -> {
// Custom response parsing logic
return response.body().string();
})
.build();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class for constructing ExternalApiReader instances.static interfaceRequest builder functional interface.static interfaceResponse parser functional interface. -
Field Summary
Fields inherited from class io.agentscope.core.rag.reader.AbstractChunkingReader
chunkSize, overlapSize, splitStrategy -
Method Summary
Modifier and TypeMethodDescriptionstatic ExternalApiReader.Builderbuilder()Creates Builder instance.Gets the list of file formats supported by this reader.reactor.core.publisher.Mono<List<io.agentscope.core.rag.model.Document>>read(ReaderInput input) Reads a document and returns a list of chunked Document objects.Methods inherited from class io.agentscope.core.rag.reader.AbstractChunkingReader
getChunkSize, getOverlapSize, getSplitStrategy, validateChunkingParameters
-
Method Details
-
read
public reactor.core.publisher.Mono<List<io.agentscope.core.rag.model.Document>> read(ReaderInput input) Description copied from interface:ReaderReads a document and returns a list of chunked Document objects.Errors during reading are propagated through the Mono via
Mono.error().- Parameters:
input- the input to read (text, file path, URL, etc.)- Returns:
- a Mono that emits a list of Document objects, or an error signal on failure
-
getSupportedFormats
Description copied from interface:ReaderGets the list of file formats supported by this reader.- Returns:
- a list of supported file extensions (e.g., "txt", "md", "pdf")
-
builder
Creates Builder instance.- Returns:
- A new
ExternalApiReader.Builderinstance for constructingExternalApiReader
-