-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose functions
llama_load_session_file
and llama_save_session_file
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//! utilities for working with session files | ||
use std::ffi::{CString, NulError}; | ||
use std::path::{Path, PathBuf}; | ||
use crate::context::LlamaContext; | ||
use crate::token::LlamaToken; | ||
|
||
#[derive(Debug, Eq, PartialEq, thiserror::Error)] | ||
pub enum SaveSessionError { | ||
#[error("Failed to save session file")] | ||
FailedToSave, | ||
|
||
#[error("null byte in string {0}")] | ||
NullError(#[from] NulError), | ||
|
||
#[error("failed to convert path {0} to str")] | ||
PathToStrError(PathBuf), | ||
} | ||
|
||
#[derive(Debug, Eq, PartialEq, thiserror::Error)] | ||
pub enum LoadSessionError { | ||
#[error("Failed to load session file")] | ||
FailedToLoad, | ||
|
||
#[error("null byte in string {0}")] | ||
NullError(#[from] NulError), | ||
|
||
#[error("failed to convert path {0} to str")] | ||
PathToStrError(PathBuf), | ||
} | ||
|
||
impl LlamaContext<'_> { | ||
pub fn save_session_file(&self, path_session: impl AsRef<Path>, tokens: &[LlamaToken]) -> Result<(), SaveSessionError> { | ||
let path = path_session.as_ref(); | ||
let path = path | ||
.to_str() | ||
.ok_or(SaveSessionError::PathToStrError(path.to_path_buf()))?; | ||
|
||
let cstr = CString::new(path)?; | ||
|
||
if unsafe { | ||
llama_cpp_sys_2::llama_save_session_file( | ||
self.context.as_ptr(), | ||
cstr.as_ptr(), | ||
tokens.as_ptr() as *const i32, | ||
tokens.len()) | ||
} { | ||
Ok(()) | ||
} else { | ||
Err(SaveSessionError::FailedToSave) | ||
} | ||
} | ||
pub fn load_session_file(&mut self, path_session: impl AsRef<Path>, max_tokens: usize) -> Result<Vec<LlamaToken>, LoadSessionError> { | ||
let path = path_session.as_ref(); | ||
let path = path | ||
.to_str() | ||
.ok_or(LoadSessionError::PathToStrError(path.to_path_buf()))?; | ||
|
||
let cstr = CString::new(path)?; | ||
let mut tokens = Vec::with_capacity(max_tokens); | ||
let mut n_out = 0; | ||
|
||
unsafe { | ||
if llama_cpp_sys_2::llama_load_session_file( | ||
self.context.as_ptr(), | ||
cstr.as_ptr(), | ||
tokens.as_mut_ptr() as *mut i32, | ||
max_tokens, | ||
&mut n_out) { | ||
tokens.set_len(n_out); | ||
Ok(tokens) | ||
} else { | ||
Err(LoadSessionError::FailedToLoad) | ||
} | ||
} | ||
} | ||
} |