算的 LLM 并發(fā)上限(max_concurrency))
后端AI 應(yīng)用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 調(diào)用單獨設(shè)置并發(fā)上限并與concurrency.max_threads線程預(yù)算實現(xiàn)“雙通道”獨立調(diào)優(yōu)。讀完本文你將掌握LlmConfig.max_concurrency的語義邊界、源碼層的解析優(yōu)先級覆蓋與回退規(guī)則、在 C/Rust 等語言綁定中的寫法以及哪些功能會消費該字段、哪些不會。這個契約測試在驗證什么xberg 的契約測試體系用同一份 fixture 驅(qū)動 15 種語言綁定保證各語言行為一致。本文對應(yīng)的測試描述只有一句話“Tests that LLM max_concurrency is accepted independently of the extraction thread budget”——LLM 的max_concurrency被獨立接受不受提取線程預(yù)算的約束。完整定義見 fixtures/contract/config_llm_max_concurrency.json它向 mock 服務(wù)器發(fā)起一次 URI 提取text/plain同時下發(fā)一份同時包含兩套并發(fā)控制的配置{ concurrency: { max_threads: 8 }, captioning: { llm: { model: openai/gpt-4o, max_concurrency: 2 }, min_image_area: 1000 } }concurrency.max_threads 8這是提取本身的線程預(yù)算約束 Rayon 全局線程池、ONNX Runtime intra-op 線程和批量提取的任務(wù)扇出。captioning.llm.max_concurrency 2這是LLM 在途請求數(shù)上限與上面的 8 沒有任何換算關(guān)系。斷言只檢查結(jié)果本身results[0].mime_type為text/plain、正文長度不少于 5也就是說測試關(guān)注的是“配置能被接受并完成提取”而不是兩者之間的相互鉗制——這正是“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.關(guān)鍵結(jié)論均有源碼依據(jù)它是真實、全局的 provider 并發(fā)上限而非單次提取的配額。xberg::llm::client::create_client對每個解析結(jié)果不同的配置共享一個進(jìn)程級客戶端實例因此所有并發(fā)提取只要解析到同一份配置就共享這一個在途請求上限而不是每次提取各自新造一個見源碼注釋中引用的 GH#1465。None不設(shè)置表示不限制。PDF / 圖片 OCR 的批量大小不從該字段推導(dǎo)——那些調(diào)用點混合了 CPU 密集的柵格化/OCR 工作與偶發(fā)的遠(yuǎn)端請求始終按通用線程預(yù)算ConcurrencyConfig::max_threads來定批GH#1465。Captioning圖像描述是唯一“額外”消費該值的特性它在全局 provider 級限制之上還用這個值限制自己每次提取內(nèi)的異步請求扇出因為 captioning 只發(fā) VLM 請求沒有 CPU 批處理需要保護(hù)。源碼層的優(yōu)先級顯式值優(yōu)先否則回退線程預(yù)算實際解析邏輯在 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) }邏輯可以歸納為三條規(guī)則顯式值優(yōu)先llm_config.max_concurrency一旦設(shè)置直接作為 LLM 請求并發(fā)數(shù)concurrency.max_threads完全不影響它這就是契約測試驗證的“獨立接受”。未設(shè)置時回退未設(shè)置則回退到resolve_thread_budget(concurrency)——即線程預(yù)算max_threads顯式值優(yōu)先否則min(檢測到的 CPU 核數(shù), 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結(jié)果為3證明顯式值贏過更大的線程預(yù)算llm_concurrency_falls_back_to_general_thread_budgetmax_concurrency None、max_threads Some(5)時結(jié)果為5證明未設(shè)置時回退線程預(yù)算。邊界行為0、None與鉗制差異值得注意的是max_concurrency有兩個鉗制點行為并不相同Captioning 的每次提取扇出值會被.max(1)鉗制到至少 1因此Some(0)在此路徑上表現(xiàn)為“并發(fā) 1”串行。全局 provider 級限制不做鉗制——Some(0)會原樣傳給 liter-llm由 liter-llm 在建客戶端時拒絕0 個允許的在途請求沒有意義從而以create_client錯誤的形式暴露而不是被靜默地改成 1。因此如果看到create_client報錯先檢查是否把max_concurrency設(shè)成了0。與LlmConfig其他字段的關(guān)系max_concurrency位于LlmConfig內(nèi)部因此它天然跟隨每個 LLM 特性的獨立配置VLM OCR、結(jié)構(gòu)化提取、captioning 各自攜帶自己的LlmConfig可對同一份提取使用不同 provider 與不同并發(fā)。同模塊中還提供model如openai/gpt-4o、anthropic/...、groq/...的 liter-llm 路由格式、api_key、base_url、timeout_secs、max_retries默認(rèn) 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僅當(dāng) 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后經(jīng) JSON 序列化再反序列化保持一致。captioning 配置容器CaptioningConfigmax_concurrency在契約測試中掛在captioning.llm下所屬容器是 CaptioningConfig約 L15-L26字段llm: LlmConfigVLM 調(diào)用使用的 LLM 配置必需prompt: OptionString自定義描述提示詞None時使用默認(rèn)的RegionKind::Caption提示min_image_area: u32跳過width * height低于該閾值的圖片單位像素默認(rèn)1000用于過濾圖標(biāo)與裝飾性小圖。當(dāng)ExtractionConfig::captioning為Some時captioning 后處理器在 Middle 階段運(yùn)行遍歷ExtractedDocument::images為像素面積超過min_image_area的每張圖填充ExtractedImage::caption。公開 API 位于 captioning/mod.rscaption_image、caption_image_file、caption_images批量版按順序逐張?zhí)幚砭蓚魅胍粋€LlmConfig結(jié)構(gòu)體——Rust 內(nèi)嵌時可直接用結(jié)構(gòu)體更新語法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解析為配置句柄——本示例的關(guān)鍵同時攜帶兩套并發(fā)控制xberg_extract(input, config)執(zhí)行提取三個*_free釋放句柄避免泄漏。同樣的語義在 15 種綁定中都有對應(yīng) e2e 測試?yán)?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 內(nèi)嵌調(diào)用等價于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() };結(jié)構(gòu)體字段名以當(dāng)前倉庫源碼為準(zhǔn)。驗證方式與回歸保障契約 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釘死解析優(yōu)先級llm.rs 的test_llm_config_max_concurrency_round_trip保證配置跨 TOML/JSON 無損往返??缯Z言 e2e上述 15 份 e2e 契約測試在各自語言中重復(fù)“以 JSON 配置執(zhí)行提取”的驗證防止綁定層丟字段或改寫語義。小結(jié)在 xberg 中“提取線程預(yù)算”與“LLM 請求并發(fā)”是兩條互相獨立的控制線控制維度配置鍵作用范圍提取線程預(yù)算concurrency.max_threadsRayon / ONNX Runtime / 批量提取任務(wù)扇出未設(shè)置時默認(rèn)min(核數(shù), 8)LLM 全局在途請求上限captioning.llm.max_concurrency進(jìn)程內(nèi)共享的 provider 客戶端并發(fā)None為不限Captioning 每次提取扇出同一字段在全局限制之上再加一層.max(1)鉗制的每提取扇出需要同時控制文檔解析的 CPU 開銷與遠(yuǎn)端 LLM 調(diào)用壓力時就在一份配置里同時設(shè)置兩者需要保護(hù)第三方 LLM 服務(wù)的限流配額時優(yōu)先使用max_concurrency而非壓低整個提取的線程預(yù)算——這正是契約測試所驗證的“獨立接受”帶來的配置自由度。贊分享后端AI 應(yīng)用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點擊查看免費下載相關(guān)推薦Go-spew并發(fā)配置如何創(chuàng)建獨立的ConfigState實例Go spew并發(fā)配置如何創(chuàng)建獨立的ConfigState實例 Go spew是Go語言中一個強(qiáng)大的深度數(shù)據(jù)打印工具專門用于調(diào)試復(fù)雜的數(shù)據(jù)結(jié)構(gòu)。在并發(fā)環(huán)境中開發(fā)工具調(diào)試器xberg C FFI 實戰(zhàn)用 extract 完成獨立 PDF 文本提取從 ExtractInput 到結(jié)果校驗xberg C FFI 實戰(zhàn)用 extract 完成獨立 PDF 文本提取從 ExtractInput 到結(jié)果校驗 本文基于 xberg 倉庫中自動生成的后端AI 應(yīng)用NLPxberg C 綁定實戰(zhàn)用 VLM 視覺大模型liter-llm配置 OCR 文本提取xberg C 綁定實戰(zhàn)用 VLM 視覺大模型liter llm配置 OCR 文本提取 本文以 xberg 的 C FFI 接口為主線講解如何配置 VL后端AI 應(yīng)用NLP上一篇CSS-Only Chat常見問題解答從安裝到調(diào)試的完整解決方案下一篇Chrome DevTools Console API終極指南從基礎(chǔ)調(diào)試到高級技巧創(chuàng)作聲明:本文部分內(nèi)容由AI輔助生成(AIGC),僅供參考