Crates.io | mini-openai |
lib.rs | mini-openai |
version | 0.1.2 |
source | src |
created_at | 2024-10-16 09:00:33.461268 |
updated_at | 2024-10-27 08:29:46.699686 |
description | An OpenAI API client with minimal dependencies |
homepage | https://github.com/Papierkorb/mini_openai |
repository | https://github.com/Papierkorb/mini_openai |
max_upload_size | |
id | 1411502 |
size | 77,111 |
A Minimal-Dependency Rust Crate for OpenAI-compatible LLM Servers
mini_openai
is a lightweight Rust crate that provides a simple and minimal-dependency way to interact with OpenAI-compatible LLM (Large Language Model) servers. With this crate, you can easily integrate OpenAI's language models into your Rust applications without adding unnecessary dependencies or complexity.
reqwest
and ureq
HTTP clients, allowing you to choose the one that best fits your needs.To get started with mini_openai, add the following dependency to your Cargo.toml
file:
[dependencies]
mini_openai = "*"
Then, import the crate in your Rust code and create a new client instance:
use mini_openai;
fn main() -> Result<(), mini_openai::Error> {
let client = mini_openai::Client::new(None, None)?;
// Create a new chat completion request
let mut request = mini_openai::ChatCompletions::default();
// Add a message to the chat history
request.messages.push(mini_openai::Message {
content: "Who are you?".to_string(),
role: mini_openai::ROLE_USER.to_string(),
});
// Send the request to the OpenAI API
let response = client.chat_completions(&request)?;
// Print the generated completion
println!("{}", response.choices[0].message.content);
Ok(())
}
You can also run the examples:
# Modify as you require:
export OPENAI_API_BASE=http://localhost:3000/v1
# export OPENAI_API_KEY=sk-...
# Synchronous API with ureq:
cargo run --features=ureq --example sync-chat-completion
# Asynchronous API with reqwest:
cargo run --features=reqwest --example async-chat-completion
Your users can set the following environment variables for configuration. You, the developer, can then use either Client::new()
or Client::new_from_environment()
to abide to these variables. If set, they'll take precedence over your parameters passed to Client::new()
!
OPENAI_API_KEY
: The API-Key. Optional if not using OpenAI services.OPENAI_API_BASE
: Base URL to the API (Example: http://localhost:3000/v1
- Note the /v1
!)If you don't intend for your user to take control, you can use Client::new_without_environment()
instead.
mini_openai is an open-source project, and we welcome contributions from the community. If you'd like to report a bug, suggest a new feature, or help with development, please open an issue or submit a pull request on our GitHub page.
mini_openai is licensed under the BSD 2-clause License. See the LICENSE
file for details.
A Thank You goes to the maintainers of reqwest
and ureq
for their excellent HTTP client libraries. I wrote this library because I wanted a small and easy-to-use library to interact with LLMs without pulling in dependencies for features I don't need.