From ee759094095e52bac50b06f17c267f1b8533c37c Mon Sep 17 00:00:00 2001 From: Peter Tripp Date: Fri, 27 Sep 2024 12:32:52 -0400 Subject: [PATCH] Fix Ollama timeouts broken on Preview (#18449) Supersedes: https://github.com/zed-industries/zed/pull/18310 See also: https://github.com/zed-industries/zed/issues/18304 This PR targets v0.155.x and is a hotfix for Preview. Signed-off-by: Vladimir Bulyga Co-authored-by: Vladimir Bulyga --- Cargo.lock | 1 + crates/ollama/Cargo.toml | 1 + crates/ollama/src/ollama.rs | 9 +++++++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f1bc684401cb9..641c57f8b0d38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7523,6 +7523,7 @@ dependencies = [ "anyhow", "futures 0.3.30", "http_client", + "isahc", "schemars", "serde", "serde_json", diff --git a/crates/ollama/Cargo.toml b/crates/ollama/Cargo.toml index 34d8802b977df..76a8b1a8c16cf 100644 --- a/crates/ollama/Cargo.toml +++ b/crates/ollama/Cargo.toml @@ -19,6 +19,7 @@ schemars = ["dep:schemars"] anyhow.workspace = true futures.workspace = true http_client.workspace = true +isahc.workspace = true schemars = { workspace = true, optional = true } serde.workspace = true serde_json.workspace = true diff --git a/crates/ollama/src/ollama.rs b/crates/ollama/src/ollama.rs index e592bfa17717d..6fb2a28a451f0 100644 --- a/crates/ollama/src/ollama.rs +++ b/crates/ollama/src/ollama.rs @@ -1,6 +1,7 @@ use anyhow::{anyhow, Context, Result}; use futures::{io::BufReader, stream::BoxStream, AsyncBufReadExt, AsyncReadExt, StreamExt}; use http_client::{http, AsyncBody, HttpClient, Method, Request as HttpRequest}; +use isahc::config::Configurable; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use serde_json::{value::RawValue, Value}; @@ -262,14 +263,18 @@ pub async fn stream_chat_completion( client: &dyn HttpClient, api_url: &str, request: ChatRequest, - _: Option, + low_speed_timeout: Option, ) -> Result>> { let uri = format!("{api_url}/api/chat"); - let request_builder = http::Request::builder() + let mut request_builder = http::Request::builder() .method(Method::POST) .uri(uri) .header("Content-Type", "application/json"); + if let Some(low_speed_timeout) = low_speed_timeout { + request_builder = request_builder.low_speed_timeout(100, low_speed_timeout); + }; + let request = request_builder.body(AsyncBody::from(serde_json::to_string(&request)?))?; let mut response = client.send(request).await?; if response.status().is_success() {