pub struct LlamaTokenDataArray {
pub data: Vec<LlamaTokenData>,
pub selected: Option<usize>,
pub sorted: bool,
}Expand description
a safe wrapper around llama_token_data_array.
Fields§
§data: Vec<LlamaTokenData>the underlying data
selected: Option<usize>the index of the selected token in data
sorted: boolis the data sorted?
Implementations§
Source§impl LlamaTokenDataArray
impl LlamaTokenDataArray
Sourcepub fn new(data: Vec<LlamaTokenData>, sorted: bool) -> Self
pub fn new(data: Vec<LlamaTokenData>, sorted: bool) -> Self
Create a new LlamaTokenDataArray from a vector and whether or not the data is sorted.
let array = LlamaTokenDataArray::new(vec![
LlamaTokenData::new(LlamaToken(0), 0.0, 0.0),
LlamaTokenData::new(LlamaToken(1), 0.1, 0.1)
], false);
assert_eq!(array.data.len(), 2);
assert_eq!(array.sorted, false);Sourcepub fn from_iter<TIterator>(
data: TIterator,
sorted: bool,
) -> LlamaTokenDataArraywhere
TIterator: IntoIterator<Item = LlamaTokenData>,
pub fn from_iter<TIterator>(
data: TIterator,
sorted: bool,
) -> LlamaTokenDataArraywhere
TIterator: IntoIterator<Item = LlamaTokenData>,
Create a new LlamaTokenDataArray from an iterator and whether or not the data is sorted.
let array = LlamaTokenDataArray::from_iter([
LlamaTokenData::new(LlamaToken(0), 0.0, 0.0),
LlamaTokenData::new(LlamaToken(1), 0.1, 0.1)
], false);
assert_eq!(array.data.len(), 2);
assert_eq!(array.sorted, false);Sourcepub fn selected_token(&self) -> Option<LlamaToken>
pub fn selected_token(&self) -> Option<LlamaToken>
Returns the current selected token, if one exists.
Source§impl LlamaTokenDataArray
impl LlamaTokenDataArray
Sourcepub unsafe fn modify_as_c_llama_token_data_array<TResult>(
&mut self,
modify: impl FnOnce(&mut llama_token_data_array) -> TResult,
) -> TResult
pub unsafe fn modify_as_c_llama_token_data_array<TResult>( &mut self, modify: impl FnOnce(&mut llama_token_data_array) -> TResult, ) -> TResult
Modify the underlying data as a llama_token_data_array. and reconstruct the LlamaTokenDataArray.
§Panics
Panics if some of the safety conditions are not met. (we cannot check all of them at runtime so breaking them is UB)
§Safety
The returned array formed by the data pointer and the length must entirely consist of initialized token data and the length must be less than the capacity of this array’s data buffer. If the data is not sorted, sorted must be false.
Sourcepub fn apply_sampler(&mut self, sampler: &LlamaSampler)
pub fn apply_sampler(&mut self, sampler: &LlamaSampler)
Modifies the data array by applying a sampler to it
Sourcepub fn with_sampler(self, sampler: &mut LlamaSampler) -> Self
pub fn with_sampler(self, sampler: &mut LlamaSampler) -> Self
Modifies the data array by applying a sampler to it
Sourcepub fn sample_token(&mut self, seed: u32) -> LlamaToken
pub fn sample_token(&mut self, seed: u32) -> LlamaToken
Randomly selects a token from the candidates based on their probabilities.
§Panics
If the internal llama.cpp sampler fails to select a token.
Sourcepub fn sample_token_greedy(&mut self) -> LlamaToken
pub fn sample_token_greedy(&mut self) -> LlamaToken
Selects the token with the highest probability.
§Panics
If the internal llama.cpp sampler fails to select a token.
Trait Implementations§
Source§impl Clone for LlamaTokenDataArray
impl Clone for LlamaTokenDataArray
Source§fn clone(&self) -> LlamaTokenDataArray
fn clone(&self) -> LlamaTokenDataArray
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more