ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

xberg 配置详解:独立于提取线程预算的 LLM 并发上限(max_concurrency)

xberg 配置详解:独立于提取线程预算的 LLM 并发上限(max_concurrency) 后端AI 应用NLP【免费下载链接】xbergPolyglot document intelligence with a Rust core: extract text, metadata, images, tables, and structured data from 106 formats across 140 file extensions, plus code intelligence for 371 languages. Fifteen bindings, with CLI, REST API, and MCP server.项目地址https://gitcode.com/gh_mirrors/kr/xberg点击查看免费下载本文围绕 xberg 的契约contract测试config_llm_max_concurrency讲解如何通过提取配置中的captioning.llm.max_concurrency为 LLM 调用单独设置并发上限并与concurrency.max_threads线程预算实现“双通道”独立调优。读完本文你将掌握LlmConfig.max_concurrency的语义边界、源码层的解析优先级覆盖与回退规则、在 C/Rust 等语言绑定中的写法以及哪些功能会消费该字段、哪些不会。这个契约测试在验证什么xberg 的契约测试体系用同一份 fixture 驱动 15 种语言绑定保证各语言行为一致。本文对应的测试描述只有一句话“Tests that LLM max_concurrency is accepted independently of the extraction thread budget”——LLM 的max_concurrency被独立接受不受提取线程预算的约束。完整定义见 fixtures/contract/config_llm_max_concurrency.json它向 mock 服务器发起一次 URI 提取text/plain同时下发一份同时包含两套并发控制的配置{ concurrency: { max_threads: 8 }, captioning: { llm: { model: openai/gpt-4o, max_concurrency: 2 }, min_image_area: 1000 } }concurrency.max_threads 8这是提取本身的线程预算约束 Rayon 全局线程池、ONNX Runtime intra-op 线程和批量提取的任务扇出。captioning.llm.max_concurrency 2这是LLM 在途请求数上限与上面的 8 没有任何换算关系。断言只检查结果本身results[0].mime_type为text/plain、正文长度不少于 5也就是说测试关注的是“配置能被接受并完成提取”而不是两者之间的相互钳制——这正是“independent”的含义。配置落点LlmConfig.max_concurrencymax_concurrency是 LlmConfig 的字段之一字段定义在 llm.rs 中约 L282-L308源码注释给出了精确语义Maximum number of simultaneously in-flight requests to the LLM provider this config resolves to.关键结论均有源码依据它是真实、全局的 provider 并发上限而非单次提取的配额。xberg::llm::client::create_client对每个解析结果不同的配置共享一个进程级客户端实例因此所有并发提取只要解析到同一份配置就共享这一个在途请求上限而不是每次提取各自新造一个见源码注释中引用的 GH#1465。None不设置表示不限制。PDF / 图片 OCR 的批量大小不从该字段推导——那些调用点混合了 CPU 密集的栅格化/OCR 工作与偶发的远端请求始终按通用线程预算ConcurrencyConfig::max_threads来定批GH#1465。Captioning图像描述是唯一“额外”消费该值的特性它在全局 provider 级限制之上还用这个值限制自己每次提取内的异步请求扇出因为 captioning 只发 VLM 请求没有 CPU 批处理需要保护。源码层的优先级显式值优先否则回退线程预算实际解析逻辑在 concurrency.rs 的resolve_llm_concurrency约 L459-L468feature captioning下编译pub(crate) fn resolve_llm_concurrency( llm_config: crate::core::config::LlmConfig, concurrency: OptionConcurrencyConfig, ) - usize { llm_config .max_concurrency .unwrap_or_else(|| resolve_thread_budget(concurrency)) .max(1) }逻辑可以归纳为三条规则显式值优先llm_config.max_concurrency一旦设置直接作为 LLM 请求并发数concurrency.max_threads完全不影响它这就是契约测试验证的“独立接受”。未设置时回退未设置则回退到resolve_thread_budget(concurrency)——即线程预算max_threads显式值优先否则min(检测到的 CPU 核数, 8)Linux 下有 cgroup CPU 配额时以配额为上限见 concurrency.rs 中resolve_thread_budget与DEFAULT_THREAD_CAP。下限钳制max(1)保证至少为 1。单元测试直接钉死了这两条分支见 concurrency.rs 测试模块llm_concurrency_overrides_general_thread_budgetmax_concurrency Some(3)、max_threads Some(12)时resolve_llm_concurrency结果为3证明显式值赢过更大的线程预算llm_concurrency_falls_back_to_general_thread_budgetmax_concurrency None、max_threads Some(5)时结果为5证明未设置时回退线程预算。边界行为0、None与钳制差异值得注意的是max_concurrency有两个钳制点行为并不相同Captioning 的每次提取扇出值会被.max(1)钳制到至少 1因此Some(0)在此路径上表现为“并发 1”串行。全局 provider 级限制不做钳制——Some(0)会原样传给 liter-llm由 liter-llm 在建客户端时拒绝0 个允许的在途请求没有意义从而以create_client错误的形式暴露而不是被静默地改成 1。因此如果看到create_client报错先检查是否把max_concurrency设成了0。与LlmConfig其他字段的关系max_concurrency位于LlmConfig内部因此它天然跟随每个 LLM 特性的独立配置VLM OCR、结构化提取、captioning 各自携带自己的LlmConfig可对同一份提取使用不同 provider 与不同并发。同模块中还提供model如openai/gpt-4o、anthropic/...、groq/...的 liter-llm 路由格式、api_key、base_url、timeout_secs、max_retries默认 3、temperature、max_tokens、top_p校验范围[0.0, 1.0]、presence_penalty/frequency_penalty[-2.0, 2.0]、stop、seed、reasoning_effort、headers、providers、cache/budget/rate_limit仅当 liter-llm 编入tower特性时生效、bedrock、credential_provider、max_response_bytes等。api_key与headers等敏感字段在Debug输出中一律以[redacted]掩码配置解析使用deny_unknown_fields写错字段名会直接报错。序列化往返由单元测试覆盖test_llm_config_max_concurrency_round_trip验证 TOML 写入max_concurrency 3后经 JSON 序列化再反序列化保持一致。captioning 配置容器CaptioningConfigmax_concurrency在契约测试中挂在captioning.llm下所属容器是 CaptioningConfig约 L15-L26字段llm: LlmConfigVLM 调用使用的 LLM 配置必需prompt: OptionString自定义描述提示词None时使用默认的RegionKind::Caption提示min_image_area: u32跳过width * height低于该阈值的图片单位像素默认1000用于过滤图标与装饰性小图。当ExtractionConfig::captioning为Some时captioning 后处理器在 Middle 阶段运行遍历ExtractedDocument::images为像素面积超过min_image_area的每张图填充ExtractedImage::caption。公开 API 位于 captioning/mod.rscaption_image、caption_image_file、caption_images批量版按顺序逐张处理均可传入一个LlmConfig结构体——Rust 内嵌时可直接用结构体更新语法LlmConfig { model: openai/gpt-4o-mini.to_string(), max_concurrency: Some(2), ..Default::default() }。C 语言绑定中的完整示例C 语言验证片段见 docs-site/src/snippets-generated/c/contract/config_llm_max_concurrency.md完整代码如下#include assert.h #include stdint.h #include stdio.h #include stdlib.h #include string.h #include xberg.h int main(void) { XBERGAlefHandle input_handle xberg_extract_input_from_json({\kind\:\uri\,\mime_type\:\text/plain\,\uri\:\https://example.com/text/report.txt\}); XBERGAlefHandle config_handle xberg_extraction_config_from_json({\captioning\:{\llm\:{\max_concurrency\:2,\model\:\openai/gpt-4o\},\min_image_area\:1000},\concurrency\:{\max_threads\:8}}); XBERGAlefHandle result xberg_extract(input_handle, config_handle); xberg_extract_input_free(input_handle); xberg_extraction_config_free(config_handle); xberg_extraction_result_free(result); return EXIT_SUCCESS; }流程要点API 均由 xberg 的 C FFI 层提供头文件定义见 xberg-ffi/include/xberg.hxberg_extract_input_from_json把 JSON 形式的ExtractInputkind、mime_type、uri/bytes解析为输入句柄xberg_extraction_config_from_json把 JSON 形式的ExtractionConfig解析为配置句柄——本示例的关键同时携带两套并发控制xberg_extract(input, config)执行提取三个*_free释放句柄避免泄漏。同样的语义在 15 种绑定中都有对应 e2e 测试例如 e2e/rust/tests/contract_test.rs由 alef 自动生成、e2e/go/contract_test.go、e2e/python/tests/test_contract.py、e2e/node/tests/contract.test.ts、e2e/java/src/test/java/io/xberg/e2e/ContractTest.java 等。Rust 内嵌调用等价于let config xberg::ExtractionConfig { captioning: Some(CaptioningConfig { llm: LlmConfig { model: openai/gpt-4o.into(), max_concurrency: Some(2), ..Default::default() }, min_image_area: 1000, ..Default::default() }), concurrency: ConcurrencyConfig { max_threads: Some(8), ..Default::default() }, ..Default::default() };结构体字段名以当前仓库源码为准。验证方式与回归保障契约 fixturefixtures/contract/config_llm_max_concurrency.json 中call为extractmock_responses把/text/report.txt路由到测试文档assertions校验results[0].mime_type text/plain与results[0].content长度下限。单元测试concurrency.rs 的llm_concurrency_overrides_general_thread_budget/llm_concurrency_falls_back_to_general_thread_budget钉死解析优先级llm.rs 的test_llm_config_max_concurrency_round_trip保证配置跨 TOML/JSON 无损往返。跨语言 e2e上述 15 份 e2e 契约测试在各自语言中重复“以 JSON 配置执行提取”的验证防止绑定层丢字段或改写语义。小结在 xberg 中“提取线程预算”与“LLM 请求并发”是两条互相独立的控制线控制维度配置键作用范围提取线程预算concurrency.max_threadsRayon / ONNX Runtime / 批量提取任务扇出未设置时默认min(核数, 8)LLM 全局在途请求上限captioning.llm.max_concurrency进程内共享的 provider 客户端并发None为不限Captioning 每次提取扇出同一字段在全局限制之上再加一层.max(1)钳制的每提取扇出需要同时控制文档解析的 CPU 开销与远端 LLM 调用压力时就在一份配置里同时设置两者需要保护第三方 LLM 服务的限流配额时优先使用max_concurrency而非压低整个提取的线程预算——这正是契约测试所验证的“独立接受”带来的配置自由度。赞分享后端AI 应用NLP【免费下载链接】xbergPolyglot document intelligence with a Rust core: extract text, metadata, images, tables, and structured data from 106 formats across 140 file extensions, plus code intelligence for 371 languages. Fifteen bindings, with CLI, REST API, and MCP server.项目地址https://gitcode.com/gh_mirrors/kr/xberg点击查看免费下载相关推荐Go-spew并发配置如何创建独立的ConfigState实例Go spew并发配置如何创建独立的ConfigState实例 Go spew是Go语言中一个强大的深度数据打印工具专门用于调试复杂的数据结构。在并发环境中开发工具调试器xberg C FFI 实战用 extract 完成独立 PDF 文本提取从 ExtractInput 到结果校验xberg C FFI 实战用 extract 完成独立 PDF 文本提取从 ExtractInput 到结果校验 本文基于 xberg 仓库中自动生成的后端AI 应用NLPxberg C 绑定实战用 VLM 视觉大模型liter-llm配置 OCR 文本提取xberg C 绑定实战用 VLM 视觉大模型liter llm配置 OCR 文本提取 本文以 xberg 的 C FFI 接口为主线讲解如何配置 VL后端AI 应用NLP上一篇CSS-Only Chat常见问题解答从安装到调试的完整解决方案下一篇Chrome DevTools Console API终极指南从基础调试到高级技巧创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表