Structured Text Generation in Rust
Maintained. Last release 11 months ago, commits in 1 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install outlines-coreOutlines-core
This package provides the core functionality for structured generation, formerly implemented in Outlines, with a focus on performance and portability, it offers a convenient way to:
-
build regular expressions from JSON schemas
-
construct an
Indexobject by combining aVocabularyand regular expression to efficiently map tokens from a given vocabulary to state transitions in a finite-state automation
Example
Basic example of how it all fits together.
use outlines_core::prelude::*;
// Define a JSON schema
let schema = r#"{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" }
},
"required": ["name", "age"]
}"#;
// Generate a regular expression from it
let regex = json_schema::regex_from_str(&schema, None)?;
// Create `Vocabulary` from pretrained large language model (but manually is also possible)
let vocabulary = Vocabulary::from_pretrained("openai-community/gpt2", None)?;
// Create new `Index` from regex and a given `Vocabulary`
let index = Index::new(®ex, &vocabulary)?;
let initial_state = index.initial_state();
let allowed_tokens = index.allowed_tokens(&initial_state).expect("Some allowed token ids");
let token_id = allowed_tokens.first().expect("First token id");
let next_state = index.next_state(&initial_state, token_id);
let final_states = index.final_states();
Vocabulary
You can create a Vocabulary in three ways:
-
Vocabulary::from_pretrained(model, parameters)- Loads from a pretrained model (as in the example above) -
Manual creation - You can create a vocabulary from token mappings:
