Crates.io | simple-llm-client |
lib.rs | simple-llm-client |
version | 0.2.8 |
created_at | 2025-06-20 15:52:32.092853+00 |
updated_at | 2025-08-07 20:58:46.224412+00 |
description | A Rust crate for interacting with Large Language Model APIs |
homepage | |
repository | https://gitlab.com/dshamany/simple-llm-client |
max_upload_size | |
id | 1719767 |
size | 84,655 |
A Rust crate for interacting with Large Language Model APIs to streamline content creation, research, and information synthesis for use with RAG applications.
Add this to your Cargo.toml
:
[dependencies]
simple-llm-client = "^0.2"
The crate is also available via its directory name for local development:
[dependencies]
simple_llm_client = { path = "path/to/llm_client" }
use simple_llm_client::perplexity::{chat_completion, models::ChatMessage};
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let messages = vec![
ChatMessage {
role: "system".to_string(),
content: "Be precise and concise.".to_string(),
},
ChatMessage {
role: "user".to_string(),
content: "How many stars are there in our galaxy?".to_string(),
},
];
// Stream the response to stdout
chat_completion("sonar-pro", messages).await?;
Ok(())
}
use simple_llm_client::perplexity::{chat_completion_markdown, models::ChatMessage};
use std::{error::Error, path::Path};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let messages = vec![
ChatMessage {
role: "system".to_string(),
content: "Be precise and concise. Return the response as markdown.".to_string(),
},
ChatMessage {
role: "user".to_string(),
content: "Explain the difference between fusion and fission.".to_string(),
},
];
// Save the formatted response to a file
chat_completion_markdown("sonar-pro", messages, Some(Path::new("./output")), "research_result.md").await?;
Ok(())
}
The crate requires a Perplexity API key to be set in your environment or in a .env
file:
PERPLEXITY_API_KEY=your_api_key_here
OPENAI_API_KEY=your_api_key_here
For the examples to work correctly, create a .env
file in the project root with your API key.
When using the file output functionality, make sure the output directories exist:
mkdir -p output # Create the output directory if it doesn't exist
The example code creates this directory automatically.
This project is licensed under the MIT License - see the LICENSE file for details.
The crate includes several examples to help you get started:
To run any example, use the cargo run --example
command:
# Test the Perplexity implementation
cargo run --example perplexity --features perplexity
# Test the OpenAI implementation
cargo run --example openai --features openai
Contributions are welcome! Please feel free to submit a Pull Request.