Struct Vocabulary

Source
pub struct Vocabulary { /* private fields */ }
Expand description

Vocabulary of large language model.

§Examples

§Create a vocabulary from a pretrained model.

use outlines_core::prelude::*;

let vocabulary = Vocabulary::from_pretrained("openai-community/gpt2", None);

§Create a vocabulary from a pretrained model with some additional parameters.

use outlines_core::prelude::*;

let params = FromPretrainedParameters {
    revision: "607a30d783dfa663caf39e06633721c8d4cfcd7e".to_string(),
    ..Default::default()
};
let vocabulary = Vocabulary::from_pretrained("openai-community/gpt2", Some(params));

§Create an empty vocabulary and manually insert some tokens.

use outlines_core::prelude::*;

let eos_token_id = 1;
let mut vocabulary = Vocabulary::new(eos_token_id);

vocabulary.try_insert("token", 0).expect("New token inserted");
assert_eq!(vocabulary.token_ids("token"), Some(&vec![0]));
assert_eq!(vocabulary.tokens().len(), 1);
assert_eq!(vocabulary.eos_token_id(), eos_token_id);

vocabulary.remove("token");
assert_eq!(vocabulary.token_ids("token"), None);

Implementations§

Source§

impl Vocabulary

Source

pub fn new(eos_token_id: TokenId) -> Self

Creates an empty vocabulary.

Source

pub fn from_pretrained( model: &str, parameters: Option<FromPretrainedParameters>, ) -> Result<Self>

Creates the vocabulary of pre-trained model from Hugging Face Hub.

Source

pub fn tokens(&self) -> &HashMap<Token, Vec<TokenId>>

Returns all tokens with their token ids in vocabulary.

Source

pub fn token_ids(&self, token: impl AsRef<[u8]>) -> Option<&Vec<TokenId>>

Returns all token ids per provided token if available in the vocabulary.

Source

pub fn eos_token_id(&self) -> TokenId

Gets the identifier of the special end of the sentence token.

Source

pub fn try_insert( &mut self, token: impl Into<Token>, id: TokenId, ) -> Result<(), Error>

Inserts a token to the vocabulary with the specified identifier.

Source

pub fn remove(&mut self, token: impl Into<Token>)

Removes a given token from the vocabulary.

Trait Implementations§

Source§

impl<'__de> BorrowDecode<'__de> for Vocabulary

Source§

fn borrow_decode<__D: BorrowDecoder<'__de>>( decoder: &mut __D, ) -> Result<Self, DecodeError>

Attempt to decode this type with the given BorrowDecode.
Source§

impl Clone for Vocabulary

Source§

fn clone(&self) -> Vocabulary

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Vocabulary

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Decode for Vocabulary

Source§

fn decode<__D: Decoder>(decoder: &mut __D) -> Result<Self, DecodeError>

Attempt to decode this type with the given Decode.
Source§

impl Default for Vocabulary

Source§

fn default() -> Vocabulary

Returns the “default value” for a type. Read more
Source§

impl Display for Vocabulary

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Encode for Vocabulary

Source§

fn encode<__E: Encoder>(&self, encoder: &mut __E) -> Result<(), EncodeError>

Encode a given type.
Source§

impl PartialEq for Vocabulary

Source§

fn eq(&self, other: &Vocabulary) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<(u32, HashMap<String, Vec<u32>, FxBuildHasher>)> for Vocabulary

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from( values: (TokenId, HashMap<String, Vec<TokenId>>), ) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<(u32, HashMap<Vec<u8>, Vec<u32>, FxBuildHasher>)> for Vocabulary

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from( values: (TokenId, HashMap<Token, Vec<TokenId>>), ) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl StructuralPartialEq for Vocabulary

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T