openai_client_rs/lib.rs
1// Copyright (c) 2025 xiefujin <490021684@qq.com>
2// Licensed under Apache-2.0, see LICENSE file for full license terms.
3
4//! # openai-client-rs
5//!
6//! A Rust client for the OpenAI API — chat completions, embeddings, images,
7//! audio, files, and streaming SSE. Compatible with OpenAI and any
8//! OpenAI-compatible provider (DeepSeek, Kimi, MiniMax, Ollama, etc).
9//!
10//! ## Features
11//!
12//! - **Sync** (default) — lightweight `ureq`
13//! - **Async** — optional `reqwest` + `tokio` (`features = ["async"]`)
14//! - **Chat Completions** — full API with streaming SSE
15//! - **Models** / **Embeddings** / **Images** / **Audio** / **Files**
16//! - **Tool calling** — function calling with fragmented argument accumulation
17//! - **Reasoning** — thinking extraction from `reasoning_content` /
18//! `reasoning` / `thinking` fields and inline `<think>...</think>` tags
19//! (DeepSeek / Kimi / Qwen and other providers)
20//! - **Vision** — image URL and base64 inputs
21//! - **Auto-retry** — exponential backoff with jitter and `Retry-After` support
22//! - **Token counting** — approximate message token estimation
23//! - **Multi-provider** — OpenAI, DeepSeek, Kimi, MiniMax, Ollama
24//!
25//! ## Quick start
26//!
27//! ```rust,no_run
28//! use openai_client_rs::{OpenAiClient, ChatMessage};
29//!
30//! let client = OpenAiClient::new("sk-xxx", "gpt-4o");
31//! let resp = client.chat_create(
32//! &[ChatMessage::user("Hello!")],
33//! None,
34//! ).unwrap();
35//! println!("{}", resp.text);
36//! ```
37
38pub mod api_common;
39pub mod audio;
40pub mod error;
41pub mod files;
42pub mod images;
43pub mod models;
44pub mod request;
45pub mod retry;
46pub mod sse;
47pub mod thinking;
48pub mod tokens;
49pub mod types;
50
51mod chat;
52mod client;
53mod embeddings;
54
55#[cfg(feature = "async")]
56pub mod async_chat;
57#[cfg(feature = "async")]
58mod async_client;
59#[cfg(feature = "async")]
60mod async_embeddings;
61#[cfg(feature = "async")]
62mod async_models;
63#[cfg(feature = "async")]
64pub mod async_sse;
65
66#[cfg(feature = "async")]
67pub use async_client::OpenAiAsyncClient;
68pub use audio::{SpeechRequest, TranscriptionRequest, TranscriptionResponse};
69pub use chat::{parse_openai_stream, parse_openai_stream_rich, ChatChunkStream, StreamHandler};
70pub use client::OpenAiClient;
71pub use error::{ApiError, OpenAiError, Result};
72pub use files::{FileList, FileObject};
73pub use images::{ImageData, ImageRequest, ImageResponse};
74pub use models::ModelList;
75pub use request::{ChatCompletionRequest, JsonSchemaObject, ResponseFormat, StreamOptions};
76pub use retry::RetryConfig;
77pub use types::{
78 ChatCompletion, ChatCompletionChoice, ChatCompletionChunk, ChatCompletionTokenLogprob,
79 ChatMessage, ChatMessageContent, ChoiceLogprobs, ContentPart, EmbeddingResponse, FunctionCall,
80 ImageUrl, LlmResponse, ModelInfo, SimplifiedToolCall, Tool, ToolCall, ToolChoice, ToolFunction,
81 TopLogprob, Usage,
82};