A high-performance inference engine for AI models
mod config;
mod context;
mod error;
pub mod hanashi;
#[cfg(not(target_family = "wasm"))]
pub mod harmony;
mod state;
pub use config::EncodingConfig;
pub use context::TokenizerLocation;
pub use error::Error;
pub use hanashi::renderer::strftime_now;
use shoji::types::{basic::TokenId, session::chat::ChatMessage};
pub use state::{State, SynchronizationError, SynchronizationResult};
#[cfg(not(target_family = "wasm"))]
use crate::chat::harmony::HarmonyEncodingImpl;
use crate::{Encoding as EncodingTrait, chat::hanashi::HanashiEncodingImpl};
macro_rules! dispatch {
($self:expr, $method:ident $(, $arg:expr)*) => {
match $self {
Encoding::Hanashi(inner) => inner.$method($($arg),*).map_err(Into::into),
#[cfg(not(target_family = "wasm"))]
Encoding::Harmony(inner) => inner.$method($($arg),*).map_err(Into::into),
}
};
(infallible $self:expr, $method:ident $(, $arg:expr)*) => {
match $self {
Encoding::Hanashi(inner) => inner.$method($($arg),*),
#[cfg(not(target_family = "wasm"))]
Encoding::Harmony(inner) => inner.$method($($arg),*),
}
};
}
pub enum Encoding {
Hanashi(HanashiEncodingImpl),
#[cfg(not(target_family = "wasm"))]
Harmony(HarmonyEncodingImpl),
}
impl Encoding {
pub fn grammar_trigger(&self) -> Option<&str> {
dispatch!(infallible self, grammar_trigger)
}
pub fn try_append(
&mut self,
messages: &[ChatMessage],
) -> Result<Option<Vec<TokenId>>, Error> {
match self {
Self::Hanashi(inner) => inner.try_append(messages).map_err(Into::into),
#[cfg(not(target_family = "wasm"))]
Self::Harmony(_) => Ok(None),
}
}
pub fn flush(&mut self) -> Result<bool, Error> {
match self {
Self::Hanashi(inner) => inner.flush().map_err(Into::into),
#[cfg(not(target_family = "wasm"))]
Self::Harmony(_) => Ok(false),
}
}
pub fn tokenize(
&self,
text: &str,
) -> Result<Vec<TokenId>, Error> {
dispatch!(self, tokenize, text)
}
}
impl EncodingTrait for Encoding {
type Config = EncodingConfig;
type Input = Vec<ChatMessage>;
type Output = Vec<TokenId>;
type State = State;
type Error = Error;
fn state(&self) -> &Self::State {
dispatch!(infallible self, state)
}
fn reset(&mut self) -> Result<(), Self::Error> {
dispatch!(self, reset)
}
fn encode(
&mut self,
value: Self::Input,
) -> Result<(), Self::Error> {
dispatch!(self, encode, value)
}
fn decode(
&mut self,
value: Self::Output,
) -> Result<(), Self::Error> {
dispatch!(self, decode, value)
}
fn supports_tool_calls(&self) -> bool {
dispatch!(infallible self, supports_tool_calls)
}
fn supports_multiple_tool_calls(&self) -> bool {
dispatch!(infallible self, supports_multiple_tool_calls)
}
}