pub struct LlamaModel { /* private fields */ }
Expand description

A safe wrapper around llama_model.

Implementations§

source§

impl LlamaModel

source

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.

source

pub fn tokens( &self ) -> impl Iterator<Item = (LlamaToken, Result<String, TokenToStringError>)> + '_

Get all tokens in the model.

source

pub fn token_bos(&self) -> LlamaToken

Get the beginning of stream token.

source

pub fn token_eos(&self) -> LlamaToken

Get the end of stream token.

source

pub fn token_nl(&self) -> LlamaToken

Get the newline token.

source

pub fn token_to_str( &self, token: LlamaToken ) -> Result<String, TokenToStringError>

Convert single token to a string.

Errors

See TokenToStringError for more information.

source

pub fn tokens_to_str( &self, tokens: &[LlamaToken] ) -> Result<String, TokenToStringError>

Convert a vector of tokens to a single string.

Errors

See TokenToStringError for more information.

source

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
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)?;
source

pub fn token_type(&self, LlamaToken: LlamaToken) -> LlamaTokenType

Get the type of a token.

Panics

If the token type is not known to this library.

source

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
  • if [buffer_size] does not fit into a c_int.
  • if the returned size from llama-cpp does not fit into a usize. (this should never happen)
source

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.

source

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.

source

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.

source

pub fn load_from_file( _: &LlamaBackend, path: impl AsRef<Path>, params: &LlamaModelParams ) -> Result<Self, LlamaModelLoadError>

loads a model from a file.

Errors

See LlamaModelLoadError for more information.

source

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.

Trait Implementations§

source§

impl Debug for LlamaModel

source§

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

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

impl Drop for LlamaModel

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Send for LlamaModel

source§

impl Sync for LlamaModel

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more