Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable timeout to API config #24

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/config/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ pub struct ApiConfig {
pub default_model: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timeout_seconds: Option<u16>,
}

impl Default for ApiConfig {
// default to openai
// default to ollama
fn default() -> Self {
ApiConfig::ollama()
}
Expand Down Expand Up @@ -101,6 +103,7 @@ impl ApiConfig {
url: String::from("http://localhost:11434/api/chat"),
default_model: Some(String::from("phi3")),
version: None,
timeout_seconds: Some(30),
}
}

Expand All @@ -111,6 +114,7 @@ impl ApiConfig {
url: String::from("https://api.openai.com/v1/chat/completions"),
default_model: Some(String::from("gpt-4")),
version: None,
timeout_seconds: Some(30),
}
}

Expand All @@ -121,6 +125,7 @@ impl ApiConfig {
url: String::from("https://api.mistral.ai/v1/chat/completions"),
default_model: Some(String::from("mistral-medium")),
version: None,
timeout_seconds: Some(30),
}
}

Expand All @@ -131,6 +136,7 @@ impl ApiConfig {
url: String::from("https://api.groq.com/openai/v1/chat/completions"),
default_model: Some(String::from("llama3-70b-8192")),
version: None,
timeout_seconds: Some(30),
}
}

Expand All @@ -141,6 +147,7 @@ impl ApiConfig {
url: String::from("https://api.anthropic.com/v1/messages"),
default_model: Some(String::from("claude-3-opus-20240229")),
version: Some(String::from("2023-06-01")),
timeout_seconds: Some(30),
}
}
}
Expand All @@ -151,7 +158,7 @@ pub(super) fn api_keys_path() -> PathBuf {

pub(super) fn generate_api_keys_file() -> std::io::Result<()> {
let mut api_config = HashMap::new();
api_config.insert(Api::Ollama.to_string(), ApiConfig::openai());
api_config.insert(Api::Ollama.to_string(), ApiConfig::ollama());
efugier marked this conversation as resolved.
Show resolved Hide resolved
api_config.insert(Api::Openai.to_string(), ApiConfig::openai());
api_config.insert(Api::Mistral.to_string(), ApiConfig::mistral());
api_config.insert(Api::Groq.to_string(), ApiConfig::groq());
Expand Down
11 changes: 10 additions & 1 deletion src/text/api_call.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use super::request_schemas::{AnthropicPrompt, OpenAiPrompt};
use super::response_schemas::{AnthropicResponse, OllamaResponse, OpenAiResponse};

Expand Down Expand Up @@ -37,7 +39,14 @@ pub fn post_prompt_and_get_answer(
// currently not compatible with streams
prompt.stream = Some(false);

let client = reqwest::blocking::Client::new();
let client = reqwest::blocking::Client::builder()
.timeout(
api_config
.timeout_seconds
.map(|t| Duration::from_secs(t.into())),
)
.build()
.expect("Unable to initialize HTTP client");

let prompt_format = match prompt.api {
Api::Ollama | Api::Openai | Api::Mistral | Api::Groq => {
Expand Down