ExternalApiReader
public class ExternalApiReader extends AbstractChunkingReader
io/agentscope/core/rag/reader/ExternalApiReader.java:98–448外部 API 读取器,把私有文档转化为可检索的知识。位于 io.agentscope.core.rag.reader 包,继承自 AbstractChunkingReader。核心成员包括 read、getSupportedFormats、builder。
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
| 类Class | 类型Kind | 说明Description |
|---|---|---|
RequestBuilder | 接口interface | 定义请求构建器契约的接口。 Request builder functional interface. |
ResponseParser | 接口interface | 定义响应解析器契约的接口。 Response parser functional interface. |
Builder | 类class | 构建器,用于构建并配置目标实例。 Builder class for constructing ExternalApiReader instances. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public Mono<List<Document>> read(ReaderInput input) | 设置读取。 Performs the read operation. |
public List<String> getSupportedFormats() | 获取 `getSupportedFormats`。 Returns the supported formats. |
public static Builder builder()static | 创建构建器实例。 Creates Builder instance. |
方法详情Method Detail
read
public Mono<List<Document>> read(ReaderInput input)
设置读取。
Performs the read operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
input | ReaderInput | 输入消息(类型 `ReaderInput`)。The input. |
Mono<List<Document>>io/agentscope/core/rag/reader/ExternalApiReader.java:136–166getSupportedFormats
public List<String> getSupportedFormats()
获取 getSupportedFormats。
Returns the supported formats.
List<String>io/agentscope/core/rag/reader/ExternalApiReader.java:168–173builder
public static Builder builder()
创建构建器实例。
Creates Builder instance.
Builderio/agentscope/core/rag/reader/ExternalApiReader.java:261–263嵌套类详情Nested Class Detail
RequestBuilder
@FunctionalInterface public interface RequestBuilder定义请求构建器契约的接口。位于 io.agentscope.core.rag.reader 包。核心成员包括 buildRequest。
Request builder functional interface. Allows users to customize how to build HTTP requests.
ResponseParser
@FunctionalInterface public interface ResponseParser定义响应解析器契约的接口。位于 io.agentscope.core.rag.reader 包。核心成员包括 parseResponse。
Response parser functional interface. Allows users to customize how to extract markdown from API response.
Builder
public static class Builder构建器,用于构建并配置目标实例。位于 io.agentscope.core.rag.reader 包。核心成员包括 requestBuilder、responseParser、chunkSize、splitStrategy、overlapSize、connectTimeout。
Builder class for constructing ExternalApiReader instances.