pub struct Index { /* private fields */ }Expand description
Index efficiently maps vocabulary tokens to state transitions.
Implementations§
Source§impl Index
The Index structure is designed to efficiently map tokens from a given vocabulary
to state transitions within a finite-state automaton.
impl Index
The Index structure is designed to efficiently map tokens from a given vocabulary
to state transitions within a finite-state automaton.
§Usage:
The Index is typically constructed by combining a vocabulary and regular expressions.
Once built, it can be used to efficiently evaluate token sequences or to validate input data.
§Example:
use outlines_core::prelude::*;
let regex = "0|[1-9][0-9]*";
let vocabulary = Vocabulary::from_pretrained("openai-community/gpt2", None)?;
let index = Index::new(regex, &vocabulary)?;
let initial_state = index.initial_state();
println!("Initial state is {}", initial_state);
println!("Is initial state a final state? {}", index.is_final_state(&initial_state));
let allowed_tokens = index.allowed_tokens(&initial_state).expect("Some allowed tokens");
println!("Allowed tokens at initial state are {:?}", allowed_tokens);
let token_id = allowed_tokens.first().expect("First token");
println!("Next state for the token_id {} is {:?}", token_id, index.next_state(&initial_state, token_id));
println!("Final states are {:?}", index.final_states());
println!("Index has exactly {} transitions", index.transitions().len());
§Performance:
- Complexity:
The
Indexcan accommodate large vocabularies and complex regular expressions. However, its size may grow significantly with the complexity of the input. - Construction Cost:
Building the
Indexinvolves processing the vocabulary and regular expressions, which may require a considerable amount of time and computational resources.
Sourcepub fn new(regex: &str, vocabulary: &Vocabulary) -> Result<Self>
pub fn new(regex: &str, vocabulary: &Vocabulary) -> Result<Self>
Builds an Index from regular expression and vocabulary tokens.
Sourcepub fn initial_state(&self) -> StateId
pub fn initial_state(&self) -> StateId
Returns the ID of the initial state in the automaton.
Sourcepub fn final_states(&self) -> &HashSet<StateId>
pub fn final_states(&self) -> &HashSet<StateId>
Returns set of final states.
Sourcepub fn transitions(&self) -> &HashMap<StateId, HashMap<TokenId, StateId>>
pub fn transitions(&self) -> &HashMap<StateId, HashMap<TokenId, StateId>>
Returns state transitions map of tokens ids and their corresponding transition states.
Sourcepub fn is_final_state(&self, state: &StateId) -> bool
pub fn is_final_state(&self, state: &StateId) -> bool
Checks if state is in final states set or not.
Sourcepub fn allowed_tokens(&self, state: &StateId) -> Option<Vec<TokenId>>
pub fn allowed_tokens(&self, state: &StateId) -> Option<Vec<TokenId>>
Lists allowed tokens for a give state ID or None if it is not found in Index.
pub fn allowed_tokens_iter( &self, state: &StateId, ) -> Option<impl Iterator<Item = &TokenId>>
Sourcepub fn next_state(&self, state: &StateId, token_id: &TokenId) -> Option<StateId>
pub fn next_state(&self, state: &StateId, token_id: &TokenId) -> Option<StateId>
Returns transition state for a given state and token id or None otherwise.
pub fn vocab_size(&self) -> usize
Trait Implementations§
Source§impl<'__de, __Context> BorrowDecode<'__de, __Context> for Index
impl<'__de, __Context> BorrowDecode<'__de, __Context> for Index
Source§fn borrow_decode<__D: BorrowDecoder<'__de, Context = __Context>>(
decoder: &mut __D,
) -> Result<Self, DecodeError>
fn borrow_decode<__D: BorrowDecoder<'__de, Context = __Context>>( decoder: &mut __D, ) -> Result<Self, DecodeError>
impl StructuralPartialEq for Index
Auto Trait Implementations§
impl Freeze for Index
impl RefUnwindSafe for Index
impl Send for Index
impl Sync for Index
impl Unpin for Index
impl UnwindSafe for Index
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more