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
use std::{future::Future, str::FromStr};
use eventsource_stream::Eventsource;
use futures_util::{Stream, StreamExt, TryStreamExt};
use serde::{Serialize, de::DeserializeOwned};
use crate::errors::OapiError;
pub trait Post {
fn is_streaming(&self) -> bool;
/// Builds the URL for the request.
///
/// `base_url` should be like <https://api.openai.com/v1>
fn build_url(&self, base_url: &str) -> Result<String, OapiError>;
}
pub trait PostNoStream: Post + Serialize + Sync + Send {
type Response: DeserializeOwned + FromStr<Err = OapiError> + Send + Sync;
/// Sends a POST request and returns the raw response body.
///
/// The `client` is supplied by the caller; see [`crate::rest::default_client`]
/// for a sensible default.
fn get_response_string(
&self,
client: &reqwest::Client,
base_url: &str,
key: &str,
) -> impl Future<Output = Result<String, OapiError>> + Send + Sync {
async move {
if self.is_streaming() {
return Err(OapiError::NonStreamingViolation);
}
let response = client
.post(self.build_url(base_url)?)
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.bearer_auth(key)
.json(self)
.send()
.await?;
crate::rest::response_text_checked(response).await
}
}
/// Sends a POST request and deserializes the response.
fn get_response(
&self,
client: &reqwest::Client,
url: &str,
key: &str,
) -> impl Future<Output = Result<Self::Response, OapiError>> + Send + Sync {
async move {
let text = self.get_response_string(client, url, key).await?;
let result = Self::Response::from_str(&text)?;
Ok(result)
}
}
}
pub trait PostStream: Post + Serialize + Sync + Send {
type Response: DeserializeOwned + FromStr<Err = OapiError> + Send + Sync + 'static;
/// Sends a streaming POST request and returns the raw SSE data items as
/// strings.
///
/// The returned stream stops after the `data: [DONE]` sentinel. Errors
/// terminate the stream.
///
/// The `client` is supplied by the caller; see [`crate::rest::default_client`]
/// for a sensible default.
///
/// # Example
///
/// ```rust,no_run
/// use std::sync::LazyLock;
/// use futures_util::StreamExt;
/// use openai_interface::chat::create::request::{Message, RequestBody};
/// use openai_interface::rest::{default_client, post::PostStream};
///
/// const DEEPSEEK_API_KEY: &str = "YOUR_API_KEY";
/// const DEEPSEEK_CHAT_URL: &'static str = "https://api.deepseek.com";
/// const DEEPSEEK_MODEL: &'static str = "deepseek-chat";
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// // Needs the `ferritls` cargo feature; drop this line if you install
/// // your own rustls crypto provider (see `openai_interface::rest`).
/// # #[cfg(feature = "ferritls")]
/// openai_interface::rest::install_crypto_provider().ok();
///
/// let request = RequestBody {
/// messages: vec![
/// Message::System {
/// content: "This is a request of test purpose. Reply briefly".to_string(),
/// name: None,
/// },
/// Message::User {
/// content: "What's your name?".into(),
/// name: None,
/// },
/// ],
/// model: DEEPSEEK_MODEL.to_string(),
/// stream: Some(true),
/// ..Default::default()
/// };
///
/// let mut response = request
/// .get_stream_response_string(&default_client(), DEEPSEEK_CHAT_URL, DEEPSEEK_API_KEY)
/// .await?;
///
/// while let Some(chunk) = response.next().await {
/// println!("{}", chunk?);
/// }
/// # Ok(())
/// # }
/// ```
fn get_stream_response_string(
&self,
client: &reqwest::Client,
base_url: &str,
api_key: &str,
) -> impl Future<
Output = Result<
impl Stream<Item = Result<String, OapiError>> + Send + Unpin + 'static,
OapiError,
>,
> + Send
+ Sync {
async move {
if !self.is_streaming() {
return Err(OapiError::StreamingViolation);
}
let response = client
.post(self.build_url(base_url)?)
.header("Content-Type", "application/json")
.header("Accept", "text/event-stream")
.bearer_auth(api_key)
.json(self)
.send()
.await?;
// Parse the body as an SSE stream. Errors end the stream.
let stream = crate::rest::check_status(response)
.await?
.bytes_stream()
.eventsource()
.map(|event| match event {
Ok(event) => Ok(event.data),
Err(e) => Err(OapiError::SseParseError(format!("SSE parse error: {e}"))),
});
Ok(stream)
}
}
/// Sends a streaming POST request and deserializes every SSE data item
/// into [`Self::Response`].
///
/// The stream ends after the `data: [DONE]` sentinel or on the first
/// error, whichever comes first.
fn get_stream_response(
&self,
client: &reqwest::Client,
base_url: &str,
api_key: &str,
) -> impl Future<
Output = Result<
impl Stream<Item = Result<Self::Response, OapiError>> + Send + Unpin + 'static,
OapiError,
>,
> + Send
+ Sync {
async move {
let stream = self
.get_stream_response_string(client, base_url, api_key)
.await?;
let parsed_stream = stream
// The `data: [DONE]` sentinel is not JSON; end the stream there.
// `future::ready` keeps the combinators `Unpin` (async blocks
// are not).
.take_while(|result| {
std::future::ready(matches!(result, Ok(data) if data != "[DONE]"))
})
.and_then(|data| std::future::ready(Self::Response::from_str(&data)));
Ok(parsed_stream)
}
}
}
/// Trait for POST requests whose response body is binary rather than JSON.
///
/// Endpoints such as `POST /audio/speech` return the raw audio bytes instead
/// of a JSON object, so the body is returned as raw bytes.
pub trait PostBinary: Post + Serialize + Sync + Send {
/// Sends a POST request with a JSON body and returns the raw response
/// body bytes.
///
/// The `client` is supplied by the caller; see [`crate::rest::default_client`]
/// for a sensible default.
fn get_response_bytes(
&self,
client: &reqwest::Client,
base_url: &str,
api_key: &str,
) -> impl Future<Output = Result<Vec<u8>, OapiError>> + Send + Sync {
async move {
let response = client
.post(self.build_url(base_url)?)
.header("Content-Type", "application/json")
.header("Accept", "application/octet-stream")
.bearer_auth(api_key)
.json(self)
.send()
.await?;
crate::rest::response_bytes_checked(response).await
}
}
/// Sends a POST request with a JSON body and streams the raw response
/// body as byte chunks.
///
/// Unlike [`Self::get_response_bytes`], the body is not buffered: each
/// item is one transport-level chunk. This matches the official SDK's
/// streaming binary responses (e.g. `POST /audio/speech` with
/// `stream_format`, where the chunks carry either raw audio or SSE
/// frames). Parsing any framing inside the byte stream is up to the
/// caller.
///
/// The `client` is supplied by the caller; see [`crate::rest::default_client`]
/// for a sensible default.
fn get_stream_response_bytes(
&self,
client: &reqwest::Client,
base_url: &str,
api_key: &str,
) -> impl Future<
Output = Result<
impl Stream<Item = Result<Vec<u8>, OapiError>> + Send + Unpin + 'static,
OapiError,
>,
> + Send
+ Sync {
async move {
let response = client
.post(self.build_url(base_url)?)
.header("Content-Type", "application/json")
.header("Accept", "application/octet-stream")
.bearer_auth(api_key)
.json(self)
.send()
.await?;
let response = crate::rest::check_status(response).await?;
Ok(response.bytes_stream().map(|chunk| {
chunk
.map(|bytes| bytes.to_vec())
.map_err(OapiError::SendError)
}))
}
}
}