feat(docreader): speed up scanned-PDF parsing, stream image results, isolate heavy async queues
Large scanned PDFs (hundreds of pages) were slow and fragile end-to-end.
This change addresses the parse, transport, and task-scheduling layers:
docreader (parse + transport):
- Parallelize per-page scanned rendering across processes (forkserver/fork),
with serial fallback. ~4-7x faster on large scanned PDFs; pdfium is not
thread-safe so we fan out across processes. Configurable via
DOCREADER_PDF_RENDER_PARALLELISM.
- Add server-streaming ReadStream RPC: emit one meta frame then one frame per
image, so documents with many page images are no longer capped by the unary
gRPC message-size limit (a 874-page PDF produced ~193MiB of images, far over
the 50MB cap) and memory is bounded on both ends. Unary Read is kept for
backward compatibility; the Go production reader switches to ReadStream.
VLM:
- Make the VLM HTTP timeout configurable (VLM_HTTP_TIMEOUT_SECONDS) and raise
the default 90s -> 180s so dense scanned-page OCR does not time out with
"context deadline exceeded".
Async task queues:
- Isolate high-volume, model-heavy fan-out tasks into dedicated asynq queues so
a single large document cannot saturate the shared worker pool and block
user-facing document parsing:
image:multimodal -> "multimodal"
chunk:extract -> "graph"
question:generation -> "question"
- Register the new queues in the server weight map and the cancel inspector's
scanned-queue set (so cancelling a knowledge still purges its pending tasks).
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -15,11 +17,27 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
defaultTimeout = 90 * time.Second
|
||||
// defaultTimeout is the fallback HTTP timeout for a single VLM request.
|
||||
// Dense scanned-PDF OCR (full-page text + layout extraction) can take well
|
||||
// over a minute on slow endpoints, so this is intentionally generous and
|
||||
// can be raised further via VLM_HTTP_TIMEOUT_SECONDS.
|
||||
defaultTimeout = 180 * time.Second
|
||||
defaultMaxToks = 5000
|
||||
defaultTemp = float32(0.1)
|
||||
)
|
||||
|
||||
// vlmHTTPTimeout returns the HTTP client timeout for VLM requests, read from
|
||||
// the VLM_HTTP_TIMEOUT_SECONDS env var when set (and positive), falling back to
|
||||
// defaultTimeout otherwise. Shared by all OpenAI-compatible VLM backends.
|
||||
func vlmHTTPTimeout() time.Duration {
|
||||
if v := strings.TrimSpace(os.Getenv("VLM_HTTP_TIMEOUT_SECONDS")); v != "" {
|
||||
if secs, err := strconv.Atoi(v); err == nil && secs > 0 {
|
||||
return time.Duration(secs) * time.Second
|
||||
}
|
||||
}
|
||||
return defaultTimeout
|
||||
}
|
||||
|
||||
// RemoteAPIVLM implements VLM via an OpenAI-compatible chat completions API.
|
||||
type RemoteAPIVLM struct {
|
||||
modelName string
|
||||
@@ -54,7 +72,7 @@ func NewRemoteAPIVLM(config *Config) (*RemoteAPIVLM, error) {
|
||||
apiCfg.BaseURL = config.BaseURL
|
||||
}
|
||||
}
|
||||
httpClient := &http.Client{Timeout: defaultTimeout}
|
||||
httpClient := &http.Client{Timeout: vlmHTTPTimeout()}
|
||||
|
||||
// 注入用户自定义 HTTP header(类似 OpenAI Python SDK 的 extra_headers)
|
||||
if len(config.CustomHeaders) > 0 {
|
||||
|
||||
@@ -51,7 +51,7 @@ func NewWeKnoraCloudVLM(config *Config) (*WeKnoraCloudVLM, error) {
|
||||
appID: config.AppID,
|
||||
apiKey: config.AppSecret,
|
||||
baseURL: strings.TrimRight(config.BaseURL, "/"),
|
||||
client: &http.Client{Timeout: defaultTimeout},
|
||||
client: &http.Client{Timeout: vlmHTTPTimeout()},
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user