openai_sdk_rs/lib.rs
1//! # openai-sdk-rs
2//!
3//! **Unofficial**, minimal, async OpenAI API client for Rust.
4//!
5//! > ⚠️ **Disclaimer**: This is an **unofficial** implementation and is not affiliated with OpenAI. Use at your own discretion.
6//!
7//! ## Features
8//!
9//! - ✅ **Chat Completions**: Support for GPT-4, GPT-3.5-turbo with streaming
10//! - ✅ **Embeddings**: Generate embeddings for text
11//! - ✅ **Images**: Generate images with DALL-E models
12//! - ✅ **Responses**: Beta responses API with advanced reasoning
13//! - ✅ **Tool Calling**: Function calling with structured outputs
14//! - ✅ **Streaming**: Real-time SSE streaming for chat and responses
15//! - ✅ **Async/Await**: Built on tokio for high performance
16//! - ✅ **Type Safety**: Comprehensive Rust types for all API endpoints
17//!
18//! ## Quick Start
19//!
20//! ```no_run
21//! use openai_sdk_rs::{OpenAI, types::chat::{ChatMessage, ChatCompletionRequest}};
22//!
23//! # #[tokio::main]
24//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
25//! let client = OpenAI::from_env()?; // reads OPENAI_API_KEY
26//!
27//! let req = ChatCompletionRequest {
28//! model: "gpt-4o-mini".to_string(),
29//! messages: vec![
30//! ChatMessage::system("You are a helpful assistant."),
31//! ChatMessage::user("Say hello in 5 words."),
32//! ],
33//! ..Default::default()
34//! };
35//!
36//! let resp = client.chat_completion(req).await?;
37//! println!("{}", resp.first_choice_text().unwrap_or_default());
38//! # Ok(())
39//! # }
40//! ```
41//!
42//! ## Tool Calling Example
43//!
44//! ```no_run
45//! use openai_sdk_rs::{OpenAI, types::responses::{ResponsesRequest, ToolSpec}};
46//! use serde_json::json;
47//!
48//! # #[tokio::main]
49//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
50//! let client = OpenAI::from_env()?;
51//!
52//! let tools = vec![ToolSpec {
53//! type_: "function".to_string(),
54//! name: "get_weather".to_string(),
55//! description: Some("Get current weather".to_string()),
56//! parameters: Some(json!({
57//! "type": "object",
58//! "properties": {
59//! "location": {"type": "string", "description": "City name"}
60//! },
61//! "required": ["location"]
62//! })),
63//! }];
64//!
65//! let req = ResponsesRequest {
66//! model: "gpt-4o-2024-12-17".to_string(),
67//! input: Some(json!("What's the weather in Tokyo?")),
68//! tools: Some(tools),
69//! ..Default::default()
70//! };
71//!
72//! let resp = client.responses(req).await?;
73//! println!("{:?}", resp);
74//! # Ok(())
75//! # }
76//! ```
77//!
78//! ## Environment Setup
79//!
80//! Set your OpenAI API key:
81//!
82//! ```bash
83//! export OPENAI_API_KEY="your-api-key-here"
84//! ```
85
86mod client;
87mod error;
88pub mod sse;
89pub mod types;
90
91pub use crate::client::OpenAI;
92pub use crate::error::{ApiError, Error};