1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
pub extern crate futures_util;
use anyhow::{anyhow, Result};
use lazy_static::lazy_static;
use std::time::Duration;
lazy_static! {
static ref DEFAULT_BASE_URL: reqwest::Url =
reqwest::Url::parse("https://api.openai.com/v1/models").unwrap();
/// Shared HTTP client with optimized connection pooling for high-throughput LLM workloads.
///
/// Configuration rationale:
/// - pool_max_idle_per_host: 100 (handle burst traffic without connection churn)
/// - pool_idle_timeout: 90s (keep connections warm between requests)
/// - tcp_keepalive: 60s (detect dead connections proactively)
/// - connect_timeout: 10s (fail fast on connection issues)
/// - timeout: 300s (generous for large model responses)
/// - tcp_nodelay: true (reduce latency for small requests)
static ref SHARED_HTTP_CLIENT: reqwest::Client = {
reqwest::ClientBuilder::new()
.pool_idle_timeout(Some(Duration::from_secs(90)))
.pool_max_idle_per_host(100)
.tcp_keepalive(Some(Duration::from_secs(60)))
.timeout(Duration::from_secs(300))
.connect_timeout(Duration::from_secs(10))
.tcp_nodelay(true)
.build()
.expect("Failed to build shared HTTP client")
};
}
pub struct Client {
req_client: reqwest::Client,
key: String,
base_url: reqwest::Url,
}
pub mod chat;
pub mod completions;
pub mod edits;
pub mod embeddings;
pub mod images;
pub mod models;
impl Client {
/// Create a new client with the shared optimized HTTP client.
/// Uses connection pooling with keep-alive for high-throughput workloads.
pub fn new(api_key: &str) -> Client {
Client {
req_client: SHARED_HTTP_CLIENT.clone(),
key: api_key.to_owned(),
base_url: DEFAULT_BASE_URL.clone(),
}
}
/// Create a new client with a custom reqwest::Client.
/// Use this when you need custom TLS, proxy, or connection pool settings.
pub fn new_with_client(api_key: &str, req_client: reqwest::Client) -> Client {
Client {
req_client,
key: api_key.to_owned(),
base_url: DEFAULT_BASE_URL.clone(),
}
}
/// Create a new client with the shared optimized HTTP client and custom base URL.
pub fn new_with_base_url(api_key: &str, base_url: &str) -> Client {
let base_url = reqwest::Url::parse(base_url).unwrap();
Client {
req_client: SHARED_HTTP_CLIENT.clone(),
key: api_key.to_owned(),
base_url,
}
}
/// Create a new client with a custom reqwest::Client and custom base URL.
pub fn new_with_client_and_base_url(
api_key: &str,
req_client: reqwest::Client,
base_url: &str,
) -> Client {
Client {
req_client,
key: api_key.to_owned(),
base_url: reqwest::Url::parse(base_url).unwrap(),
}
}
/// Get a reference to the shared HTTP client for advanced usage.
pub fn shared_client() -> &'static reqwest::Client {
&SHARED_HTTP_CLIENT
}
/// Helper to send a POST request with JSON body and parse the response.
/// Returns the raw text for non-200 responses, or parses JSON for 200 responses.
async fn send_json<T: serde::de::DeserializeOwned>(
&self,
path: &str,
body: &impl serde::Serialize,
error_context: &str,
) -> Result<T, anyhow::Error> {
let mut url = self.base_url.clone();
url.set_path(path);
let res = self
.req_client
.post(url)
.bearer_auth(&self.key)
.json(body)
.send()
.await?;
let status = res.status();
let text = res.text().await?;
if status == 200 {
serde_json::from_str(&text)
.map_err(|e| anyhow!("{} failed to parse: {}. Raw: {}", error_context, e, text))
} else {
Err(anyhow!(
"{} API error ({}): {}",
error_context,
status,
text
))
}
}
/// Helper for GET requests
async fn send_get<T: serde::de::DeserializeOwned>(
&self,
path: &str,
error_context: &str,
) -> Result<T, anyhow::Error> {
let mut url = self.base_url.clone();
url.set_path(path);
let res = self
.req_client
.get(url)
.bearer_auth(&self.key)
.send()
.await?;
let status = res.status();
let text = res.text().await?;
if status == 200 {
serde_json::from_str(&text)
.map_err(|e| anyhow!("{} failed to parse: {}. Raw: {}", error_context, e, text))
} else {
Err(anyhow!(
"{} API error ({}): {}",
error_context,
status,
text
))
}
}
pub async fn list_models(
&self,
opt_url_path: Option<String>,
) -> Result<Vec<models::Model>, anyhow::Error> {
let path = opt_url_path.unwrap_or_else(|| String::from("/v1/models"));
#[derive(serde::Deserialize)]
struct ListModelsResponse {
data: Vec<models::Model>,
}
let response: ListModelsResponse = self.send_get(&path, "list_models").await?;
Ok(response.data)
}
pub async fn create_chat(
&self,
args: chat::ChatArguments,
opt_url_path: Option<String>,
) -> Result<chat::ChatCompletion, anyhow::Error> {
let path = opt_url_path.unwrap_or_else(|| String::from("/v1/chat/completions"));
self.send_json(&path, &args, "create_chat").await
}
pub async fn create_chat_stream(
&self,
args: chat::ChatArguments,
opt_url_path: Option<String>,
) -> Result<chat::stream::ChatCompletionChunkStream> {
let mut url = self.base_url.clone();
url.set_path(&opt_url_path.unwrap_or_else(|| String::from("/v1/chat/completions")));
let mut args = args;
args.stream = Some(true);
let res = self
.req_client
.post(url)
.bearer_auth(&self.key)
.json(&args)
.send()
.await?;
if res.status() == 200 {
Ok(chat::stream::ChatCompletionChunkStream::new(Box::pin(
res.bytes_stream(),
)))
} else {
Err(anyhow!(res.text().await?))
}
}
pub async fn create_completion(
&self,
args: completions::CompletionArguments,
opt_url_path: Option<String>,
) -> Result<completions::CompletionResponse> {
let path = opt_url_path.unwrap_or_else(|| String::from("/v1/completions"));
self.send_json(&path, &args, "create_completion").await
}
pub async fn create_embeddings(
&self,
args: embeddings::EmbeddingsArguments,
opt_url_path: Option<String>,
) -> Result<embeddings::EmbeddingsResponse> {
let path = opt_url_path.unwrap_or_else(|| String::from("/v1/embeddings"));
self.send_json(&path, &args, "create_embeddings").await
}
pub async fn create_image_old(
&self,
args: images::ImageArguments,
opt_url_path: Option<String>,
) -> Result<Vec<String>> {
let path = opt_url_path.unwrap_or_else(|| String::from("/v1/images/generations"));
let response: images::ImageResponse =
self.send_json(&path, &args, "create_image_old").await?;
Ok(response
.data
.iter()
.map(|o| match o {
images::ImageObject::Url(s) => s.to_string(),
images::ImageObject::Base64JSON(s) => s.to_string(),
})
.collect())
}
pub async fn create_image(
&self,
args: images::ImageArguments,
opt_url_path: Option<String>,
) -> Result<Vec<String>> {
let path = opt_url_path.unwrap_or_else(|| String::from("/v1/images/generations"));
let image_args = images::ImageArguments {
prompt: args.prompt,
model: Some("gpt-image-1".to_string()),
n: Some(1),
size: Some("1024x1024".to_string()),
quality: Some("auto".to_string()),
user: None,
};
let response: images::ImageResponse =
self.send_json(&path, &image_args, "create_image").await?;
Ok(response
.data
.iter()
.map(|o| match o {
images::ImageObject::Url(s) => s.to_string(),
images::ImageObject::Base64JSON(s) => s.to_string(),
})
.collect())
}
/// Create a response using xAI's Responses API with agentic tool calling.
///
/// This method calls the `/v1/responses` endpoint which supports server-side
/// tools like web_search, x_search, code_execution, and more.
///
/// # Arguments
/// * `args` - The ResponsesArguments containing model, input messages, and tools
/// * `opt_url_path` - Optional URL path override (defaults to `/v1/responses`)
///
/// # Example
/// ```rust,no_run
/// use openai_rust2::chat::{ResponsesArguments, ResponsesMessage, GrokTool};
/// use openai_rust2::Client;
///
/// async fn example() -> anyhow::Result<()> {
/// let client = Client::new_with_base_url("your-api-key", "https://api.x.ai/v1");
/// let args = ResponsesArguments::new(
/// "grok-4-1-fast-reasoning",
/// vec![ResponsesMessage {
/// role: "user".to_string(),
/// content: "What is the current Bitcoin price?".to_string(),
/// }],
/// ).with_tools(vec![GrokTool::web_search()]);
///
/// let response = client.create_responses(args, None).await?;
/// println!("{}", response.get_text_content());
/// Ok(())
/// }
/// ```
pub async fn create_responses(
&self,
args: chat::ResponsesArguments,
opt_url_path: Option<String>,
) -> Result<chat::ResponsesCompletion, anyhow::Error> {
let path = opt_url_path.unwrap_or_else(|| String::from("/v1/responses"));
self.send_json(&path, &args, "create_responses").await
}
/// Create a response using OpenAI's Responses API with agentic tool calling.
///
/// This method calls the `/v1/responses` endpoint which supports server-side
/// tools like web_search, file_search, and code_interpreter.
///
/// Supported models: gpt-5, gpt-4o, and other models with tool support.
///
/// # Arguments
/// * `args` - The OpenAIResponsesArguments containing model, input messages, and tools
/// * `opt_url_path` - Optional URL path override (defaults to `/v1/responses`)
///
/// # Example
/// ```rust,no_run
/// use openai_rust2::chat::{OpenAIResponsesArguments, ResponsesMessage, OpenAITool};
/// use openai_rust2::Client;
///
/// async fn example() -> anyhow::Result<()> {
/// let client = Client::new("your-openai-api-key");
/// let args = OpenAIResponsesArguments::new(
/// "gpt-5",
/// vec![ResponsesMessage {
/// role: "user".to_string(),
/// content: "What are the latest developments in AI?".to_string(),
/// }],
/// ).with_tools(vec![OpenAITool::web_search()]);
///
/// let response = client.create_openai_responses(args, None).await?;
/// println!("{}", response.get_text_content());
/// Ok(())
/// }
/// ```
pub async fn create_openai_responses(
&self,
args: chat::OpenAIResponsesArguments,
opt_url_path: Option<String>,
) -> Result<chat::ResponsesCompletion, anyhow::Error> {
let path = opt_url_path.unwrap_or_else(|| String::from("/v1/responses"));
self.send_json(&path, &args, "create_openai_responses")
.await
}
}