lmql-rs
An typesafe high-level LLM API for Rust, inspired by the Python library of the same name.
Features
Usage
use futures::StreamExt;
use lmql::{PromptOptions, Chunk, Message, LLM};
#[tokio::main]
async fn main() {
let claude = lmql::llms::anthropic::Claude::new_from_env(
lmql::llms::anthropic::ClaudeModel::Claude_3_5_Haiku_20241022,
);
let mut stream = claude
.prompt(
&[Message::User("Please provide a poem about the moon.".into())],
&PromptOptions::default(),
)
.unwrap();
while let Some(t) = stream.next().await {
if let Ok(Chunk::Token(t)) = t {
print!("{}", t)
} else {
panic!("Unexpected chunk: {t:#?}")
}
}
let mut stream = claude
.prompt(
&[Message::User("What is bitcoin?".into())],
&PromptOptions::default(),
)
.unwrap();
use lmql::TokenStreamExt;
let response = stream.all_tokens().await.unwrap();
assert_eq!(response.len(), 1);
let Chunk::Token(t) = &response[0] else {
panic!("Expected only text in response")
};
println!("{t}");
}