Skip to content

Commit 24b6f59

Browse files
committed
Reuse reqwest client for chat streaming requests
1 parent c985ee4 commit 24b6f59

File tree

2 files changed

+10
-27
lines changed

2 files changed

+10
-27
lines changed

src/chats.rs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ impl ChatWorkspaceSettings {
141141
self
142142
}
143143

144-
pub fn set_prompts(&mut self, prompts: ChatPrompts) -> &mut Self {
145-
self.prompts = Some(prompts);
144+
pub fn set_prompts(&mut self, prompts: impl Into<ChatPrompts>) -> &mut Self {
145+
self.prompts = Some(prompts.into());
146146
self
147147
}
148148
}
@@ -275,34 +275,13 @@ impl Client<crate::reqwest::ReqwestClient> {
275275
uid: impl AsRef<str>,
276276
body: &S,
277277
) -> Result<reqwest::Response, Error> {
278-
use reqwest::{
279-
header::{self, HeaderValue, ACCEPT, CONTENT_TYPE},
280-
Client as HttpClient,
281-
};
278+
use reqwest::header::{HeaderValue, ACCEPT, CONTENT_TYPE};
282279

283280
let payload = to_vec(body).map_err(Error::ParseError)?;
284281

285-
let mut headers = header::HeaderMap::new();
286-
#[cfg(not(target_arch = "wasm32"))]
287-
headers.insert(
288-
header::USER_AGENT,
289-
header::HeaderValue::from_str(&crate::reqwest::qualified_version()).unwrap(),
290-
);
291-
#[cfg(target_arch = "wasm32")]
292-
headers.insert(
293-
header::HeaderName::from_static("x-meilisearch-client"),
294-
header::HeaderValue::from_str(&crate::reqwest::qualified_version()).unwrap(),
295-
);
296-
if let Some(api_key) = self.api_key.as_deref() {
297-
headers.insert(
298-
header::AUTHORIZATION,
299-
header::HeaderValue::from_str(&format!("Bearer {api_key}")).unwrap(),
300-
);
301-
}
302-
303-
let http = HttpClient::builder().default_headers(headers).build()?;
304-
305-
let response = http
282+
let response = self
283+
.http_client
284+
.inner()
306285
.post(format!(
307286
"{}/chats/{}/chat/completions",
308287
self.host,

src/reqwest.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ impl ReqwestClient {
4949

5050
Ok(ReqwestClient { client })
5151
}
52+
53+
pub(crate) fn inner(&self) -> &reqwest::Client {
54+
&self.client
55+
}
5256
}
5357

5458
#[cfg_attr(feature = "futures-unsend", async_trait(?Send))]

0 commit comments

Comments
 (0)