diff options
Diffstat (limited to '')
| -rw-r--r-- | src/provider.rs | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/src/provider.rs b/src/provider.rs index b45282e..1b9e220 100644 --- a/src/provider.rs +++ b/src/provider.rs | |||
| @@ -196,6 +196,18 @@ fn explain(status: u16, message: &str) -> String { | |||
| 196 | message.to_string() | 196 | message.to_string() |
| 197 | } | 197 | } |
| 198 | 198 | ||
| 199 | /// One client per turn: rebuilding it per step pays a TLS handshake on every | ||
| 200 | /// request, which is noticeable on an i686 core. | ||
| 201 | pub fn agent() -> ureq::Agent { | ||
| 202 | ureq::Agent::config_builder() | ||
| 203 | .http_status_as_error(false) | ||
| 204 | .timeout_connect(Some(Duration::from_secs(10))) | ||
| 205 | .timeout_recv_response(Some(Duration::from_secs(180))) | ||
| 206 | .timeout_per_read(Some(Duration::from_secs(180))) | ||
| 207 | .build() | ||
| 208 | .into() | ||
| 209 | } | ||
| 210 | |||
| 199 | pub fn chat( | 211 | pub fn chat( |
| 200 | cfg: &Config, | 212 | cfg: &Config, |
| 201 | messages: &[Value], | 213 | messages: &[Value], |
| @@ -203,6 +215,17 @@ pub fn chat( | |||
| 203 | cancel: &AtomicBool, | 215 | cancel: &AtomicBool, |
| 204 | sink: &mut dyn Sink, | 216 | sink: &mut dyn Sink, |
| 205 | ) -> Result<Outcome, Error> { | 217 | ) -> Result<Outcome, Error> { |
| 218 | chat_with(&agent(), cfg, messages, tools, cancel, sink) | ||
| 219 | } | ||
| 220 | |||
| 221 | pub fn chat_with( | ||
| 222 | agent: &ureq::Agent, | ||
| 223 | cfg: &Config, | ||
| 224 | messages: &[Value], | ||
| 225 | tools: &[Value], | ||
| 226 | cancel: &AtomicBool, | ||
| 227 | sink: &mut dyn Sink, | ||
| 228 | ) -> Result<Outcome, Error> { | ||
| 206 | // serde_json::Value orders objects by key, so the serialised bytes are | 229 | // serde_json::Value orders objects by key, so the serialised bytes are |
| 207 | // stable for the same input. That is what keeps the provider's prompt cache | 230 | // stable for the same input. That is what keeps the provider's prompt cache |
| 208 | // warm. Do not add anything session-varying here. | 231 | // warm. Do not add anything session-varying here. |
| @@ -217,14 +240,6 @@ pub fn chat( | |||
| 217 | } | 240 | } |
| 218 | let bytes = serde_json::to_vec(&body).map_err(|e| Error::Transport(e.to_string()))?; | 241 | let bytes = serde_json::to_vec(&body).map_err(|e| Error::Transport(e.to_string()))?; |
| 219 | 242 | ||
| 220 | let agent: ureq::Agent = ureq::Agent::config_builder() | ||
| 221 | .http_status_as_error(false) | ||
| 222 | .timeout_connect(Some(Duration::from_secs(10))) | ||
| 223 | .timeout_recv_response(Some(Duration::from_secs(180))) | ||
| 224 | .timeout_per_read(Some(Duration::from_secs(180))) | ||
| 225 | .build() | ||
| 226 | .into(); | ||
| 227 | |||
| 228 | let started = Instant::now(); | 243 | let started = Instant::now(); |
| 229 | let mut resp = { | 244 | let mut resp = { |
| 230 | let mut attempt = 0usize; | 245 | let mut attempt = 0usize; |
