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
impl Vocabulary
Sourcepub fn from_pretrained(
model: &str,
parameters: Option<FromPretrainedParameters>,
) -> Result<Self>
pub fn from_pretrained( model: &str, parameters: Option<FromPretrainedParameters>, ) -> Result<Self>
Creates the vocabulary of pre-trained model from Hugging Face Hub.
Sourcepub fn tokens(&self) -> &HashMap<Token, Vec<TokenId>>
pub fn tokens(&self) -> &HashMap<Token, Vec<TokenId>>
Returns all tokens with their token ids in vocabulary.
Sourcepub fn token_ids(&self, token: impl AsRef<[u8]>) -> Option<&Vec<TokenId>>
pub fn token_ids(&self, token: impl AsRef<[u8]>) -> Option<&Vec<TokenId>>
Returns all token ids per provided token if available in the vocabulary.
Sourcepub fn eos_token_id(&self) -> TokenId
pub fn eos_token_id(&self) -> TokenId
Gets the identifier of the special end of the sentence token.
Trait Implementations§
Source§impl<'__de> BorrowDecode<'__de> for Vocabulary
impl<'__de> BorrowDecode<'__de> for Vocabulary
Source§fn borrow_decode<__D: BorrowDecoder<'__de>>(
decoder: &mut __D,
) -> Result<Self, DecodeError>
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
impl Clone for Vocabulary
Source§fn clone(&self) -> Vocabulary
fn clone(&self) -> Vocabulary
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for Vocabulary
impl Debug for Vocabulary
Source§impl Decode for Vocabulary
impl Decode for Vocabulary
Source§impl Default for Vocabulary
impl Default for Vocabulary
Source§fn default() -> Vocabulary
fn default() -> Vocabulary
Returns the “default value” for a type. Read more
Source§impl Display for Vocabulary
impl Display for Vocabulary
Source§impl Encode for Vocabulary
impl Encode for Vocabulary
Source§impl PartialEq for Vocabulary
impl PartialEq for Vocabulary
Source§impl TryFrom<(u32, HashMap<String, Vec<u32>, FxBuildHasher>)> for Vocabulary
impl TryFrom<(u32, HashMap<String, Vec<u32>, FxBuildHasher>)> for Vocabulary
impl StructuralPartialEq for Vocabulary
Auto Trait Implementations§
impl Freeze for Vocabulary
impl RefUnwindSafe for Vocabulary
impl Send for Vocabulary
impl Sync for Vocabulary
impl Unpin for Vocabulary
impl UnwindSafe for Vocabulary
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
Mutably borrows from an owned value. Read more
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>
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 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>
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