Struct llama_cpp_2::model::LlamaModel
source · pub struct LlamaModel { /* private fields */ }
Expand description
A safe wrapper around llama_model
.
Implementations§
source§impl LlamaModel
impl LlamaModel
sourcepub fn n_ctx_train(&self) -> u16
pub fn n_ctx_train(&self) -> u16
get the number of tokens the model was trained on
Panics
If the number of tokens the model was trained on does not fit into an u16
. This should be impossible on most
platforms due to llama.cpp returning a c_int
(i32 on most platforms) which is almost certainly positive.
sourcepub fn tokens(
&self
) -> impl Iterator<Item = (LlamaToken, Result<String, TokenToStringError>)> + '_
pub fn tokens( &self ) -> impl Iterator<Item = (LlamaToken, Result<String, TokenToStringError>)> + '_
Get all tokens in the model.
sourcepub fn token_bos(&self) -> LlamaToken
pub fn token_bos(&self) -> LlamaToken
Get the beginning of stream token.
sourcepub fn token_eos(&self) -> LlamaToken
pub fn token_eos(&self) -> LlamaToken
Get the end of stream token.
sourcepub fn token_nl(&self) -> LlamaToken
pub fn token_nl(&self) -> LlamaToken
Get the newline token.
sourcepub fn token_to_str(
&self,
token: LlamaToken
) -> Result<String, TokenToStringError>
pub fn token_to_str( &self, token: LlamaToken ) -> Result<String, TokenToStringError>
sourcepub fn tokens_to_str(
&self,
tokens: &[LlamaToken]
) -> Result<String, TokenToStringError>
pub fn tokens_to_str( &self, tokens: &[LlamaToken] ) -> Result<String, TokenToStringError>
sourcepub fn str_to_token(
&self,
str: &str,
add_bos: bool
) -> Result<Vec<LlamaToken>, StringToTokenError>
pub fn str_to_token( &self, str: &str, add_bos: bool ) -> Result<Vec<LlamaToken>, StringToTokenError>
Convert a string to a Vector of tokens.
Errors
- if
str
contains a null byte.
Panics
- if there is more than
usize::MAX
LlamaToken
s instr
.
use llama_cpp_2::model::LlamaModel;
use std::path::Path;
let backend = llama_cpp_2::llama_backend::LlamaBackend::init()?;
let model = LlamaModel::load_from_file(&backend, Path::new("path/to/model"), &Default::default())?;
let tokens = model.str_to_token("Hello, World!", true)?;
sourcepub fn token_type(&self, LlamaToken: LlamaToken) -> LlamaTokenType
pub fn token_type(&self, LlamaToken: LlamaToken) -> LlamaTokenType
sourcepub fn token_to_str_with_size(
&self,
token: LlamaToken,
buffer_size: usize
) -> Result<String, TokenToStringError>
pub fn token_to_str_with_size( &self, token: LlamaToken, buffer_size: usize ) -> Result<String, TokenToStringError>
Convert a token to a string with a specified buffer size.
Generally you should use LlamaModel::token_to_str
instead as 8 bytes is enough for most words and
the extra bytes do not really matter.
Errors
- if the token type is unknown
- the resultant token is larger than
buffer_size
. - the string returend by llama-cpp is not valid utf8.
Panics
sourcepub fn n_vocab(&self) -> i32
pub fn n_vocab(&self) -> i32
The number of tokens the model was trained on.
This returns a c_int
for maximum compatibility. Most of the time it can be cast to an i32
without issue.
sourcepub fn vocab_type(&self) -> VocabType
pub fn vocab_type(&self) -> VocabType
The type of vocab the model was trained on.
Panics
If llama-cpp emits a vocab type that is not known to this library.
sourcepub fn n_embd(&self) -> c_int
pub fn n_embd(&self) -> c_int
This returns a c_int
for maximum compatibility. Most of the time it can be cast to an i32
without issue.
sourcepub fn load_from_file(
_: &LlamaBackend,
path: impl AsRef<Path>,
params: &LlamaModelParams
) -> Result<Self, LlamaModelLoadError>
pub fn load_from_file( _: &LlamaBackend, path: impl AsRef<Path>, params: &LlamaModelParams ) -> Result<Self, LlamaModelLoadError>
sourcepub fn new_context<'a>(
&'a self,
_: &LlamaBackend,
params: &LlamaContextParams
) -> Result<LlamaContext<'a>, LlamaContextLoadError>
pub fn new_context<'a>( &'a self, _: &LlamaBackend, params: &LlamaContextParams ) -> Result<LlamaContext<'a>, LlamaContextLoadError>
Create a new context from this model.
Errors
There is many ways this can fail. See LlamaContextLoadError
for more information.