pub mod definition;
pub mod log_probabilities;
pub mod text_completion;
use crate::core::TextSynth;
use crate::engine::log_probabilities::{LogProbabilities, LogProbabilitiesRequest, NonEmptyString};
use crate::engine::text_completion::TextCompletionBuilder;
use definition::EngineDefinition;
#[derive(Debug, Clone)]
pub struct Engine<'ts> {
pub text_synth: &'ts TextSynth,
pub definition: EngineDefinition,
}
impl<'ts> Engine<'ts> {
pub const fn new(text_synth: &'ts TextSynth, definition: EngineDefinition) -> Self {
Self {
text_synth,
definition,
}
}
pub async fn log_probabilities(
&self,
context: String,
continuation: NonEmptyString,
) -> reqwest::Result<crate::Result<LogProbabilities>> {
let url = format!(
"https://api.textsynth.com/v1/engines/{}/logprob",
self.definition.id()
);
self.text_synth
.post(url)
.json(&LogProbabilitiesRequest {
context,
continuation,
})
.send()
.await?
.json::<crate::UntaggedResult<_>>()
.await
.map(Into::into)
}
pub fn text_completion(&self, prompt: String) -> TextCompletionBuilder {
TextCompletionBuilder::new(self, prompt)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils;
use once_cell::sync::Lazy;
#[test]
fn test_engine_new() {
let textsynth = test_utils::text_synth::get();
let _ = Engine::new(textsynth, EngineDefinition::GptJ6B);
}
#[tokio::test]
async fn test_engine_log_probabilities() {
let _ = Lazy::force(&test_utils::cache::LOG_PROBABILITIES);
}
#[test]
fn test_engine_text_completion() {
let textsynth = test_utils::text_synth::engine();
let _ = textsynth.text_completion("The quick brown fox jumps over the lazy ".into());
}
}