openai-interface 0.14.0

A low-level Rust interface for the OpenAI API
Documentation
//! Non-streaming chat completion.
//!
//! ```sh
//! cargo run --example chat_non_streaming
//! ```
//!
//! Requires `DEEPSEEK_API_KEY` in the environment. TLS: run with the
//! `ferritls` feature (`cargo run --features ferritls --example
//! chat_non_streaming`) or install your own rustls crypto provider — see
//! the crate docs ("TLS Crypto Provider").

use openai_interface::chat::create::request::{Message, RequestBody};
use openai_interface::chat::create::response::no_streaming::ChatCompletion;
use openai_interface::rest::post::PostNoStream;
use openai_interface::rest::{RequestOptions, default_client};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Needs the `ferritls` cargo feature; drop this line if you install
    // your own rustls crypto provider.
    #[cfg(feature = "ferritls")]
    openai_interface::rest::install_crypto_provider().ok();

    let api_key = std::env::var("DEEPSEEK_API_KEY")?;

    let request = RequestBody {
        messages: vec![
            Message::system("You are a helpful assistant."),
            Message::user("Hello, how are you?"),
        ],
        model: "deepseek-chat".to_string(),
        stream: Some(false),
        ..Default::default()
    };

    let options = RequestOptions::bearer(api_key);
    let completion: ChatCompletion = request
        .get_response(&default_client(), "https://api.deepseek.com", &options)
        .await?;

    println!(
        "{}",
        completion.choices[0]
            .message
            .content
            .as_deref()
            .unwrap_or("(no content)")
    );
    Ok(())
}