Skip to content

Add keep-alive #173

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

Merged
merged 2 commits into from
Jun 16, 2023
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
17 changes: 15 additions & 2 deletions src/llms/openai.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::time::Duration;

use anyhow::{anyhow, bail, Ok, Result};

use async_trait::async_trait;

use reqwest::{tls, Proxy};
use tiktoken_rs::{async_openai::get_chat_completion_max_tokens, get_completion_max_tokens};

use crate::settings::OpenAISettings;
use crate::{settings::OpenAISettings, util::HTTP_USER_AGENT};
use async_openai::{
types::{
ChatCompletionRequestMessageArgs, CreateChatCompletionRequestArgs,
Expand Down Expand Up @@ -37,10 +39,21 @@ impl OpenAIClient {
let mut openai_client = Client::new().with_api_key(&api_key);

let api_base = settings.api_base.unwrap_or_default();
let mut http_client = reqwest::Client::builder().gzip(true).brotli(true);
// TODO make configurable
let mut http_client = reqwest::Client::builder()
.gzip(true)
.brotli(true)
.timeout(Duration::from_secs(60))
.user_agent(HTTP_USER_AGENT);

if api_base.is_empty() {
http_client = http_client
.http2_prior_knowledge()
.https_only(true)
.http2_adaptive_window(true)
.tcp_keepalive(Duration::from_secs(60))
.http2_keep_alive_interval(Duration::from_secs(60))
.http2_keep_alive_while_idle(true)
.min_tls_version(tls::Version::TLS_1_2);
} else {
openai_client = openai_client.with_api_base(&api_base);
Expand Down
3 changes: 3 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub(crate) static HTTP_USER_AGENT: &str =
concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);

pub(crate) trait SplitPrefixInclusive {
fn split_prefix_inclusive<'a>(&'a self, prefix: &str) -> Vec<&'a str>;
}
Expand Down