dottxt-ai/outlines-core

Faster structured generation

outlines‑core – Rust 中的結構化生成

是什麼 – 一個 Rust 函式庫 (附帶 Python 綁定) ,它讓您能將 JSON schema 轉換為決定性有限狀態自動機 (DFA) ,從而引導語言模型詞彙表的 token 生成。它是原始 Outlines 專案的效能導向核心。

關鍵能力

  • Schema → regex – 將 JSON schema 轉換為正規表示式 (json_schema::regex_from_str)。
  • 詞彙表處理 – 從預訓練模型 (例如 openai-community/gpt2) 載入 tokenizer,或手動建立一個,將 token 映射到整數 ID。
  • 索引物件 – 結合 regex 與 Vocabulary 以產生一個 Index ,用於高效地將 token ID 映射到 DFA 狀態轉換。
  • Guide 抽象 (Python) – 一個薄層封裝 (Guide) ,用於追蹤當前 DFA 狀態、回報允許的 token,並在 token 被消耗時推進狀態。
  • 跨語言 – 核心是用 Rust 撰寫的,以確保速度與安全性;一個基於 pyo3 的 Python 套件 (outlines_core) 向 Python 使用者提供相同的 API。

典型工作流程

  1. 撰寫一個描述所需輸出形狀的 JSON schema。
  2. 從 schema 產生 regex。
  3. 載入或建構一個與您將使用的 LLM tokenizer 匹配的 Vocabulary
  4. 使用 regex 與詞彙表建立 Index
  5. 在 Python (或 Rust) 中從 index 建立 Guide 並重複執行以下操作:
    • 查詢 guide.get_tokens() 以獲取模型被允許接下來發出的 token ID 集。
    • 透過 guide.advance(token_id) 將選擇的 token 傳回。
    • guide.is_finished() 時停止。

為什麼重要 – 透過將生成限制在能使輸出符合 schema 的 token 上,您可以獲得 結構化 的 LLM 輸出 (例如 JSON 物件、日期、ID) ,而無需進行事後解析或昂貴的束搜索 (beam search)。

入門指南

  • Rust: cargo add outlines-core 然後遵循 README 中的程式碼片段。
  • Python: Clone repo,建立 virtualenv,然後執行 pip install -e .[test]。按照 Python 範例中使用 outlines_core 套件。
  • 使用 make build-extension-debug 編譯原生擴充功能以進行除錯。

開發與貢獻

  • Fork → clone → 設定 Python venv。
  • 執行 make test (同時執行 Rust cargo test 與 Python pytest)。
  • 透過 make pcc (pre‑commit) 進行樣式檢查。使用 make pybench 進行基準測試。
  • 在 Discord 上加入社群,或在 GitHub 上開啟 issue/PR。

授權 – MIT (請參閱 repository 的 LICENSE 檔案)。


Outlines‑core 是為需要在使用大型語言模型時進行快速、具 schema 意識的 token 篩選的 AI 開發者提供的真實、低層級構建區塊。",

相關

  • Dispatch
  • 專案
  • 專案
  • 專案
  • 專案