Skip to content

Commit

Permalink
Make How field public and delete builder-methods
Browse files Browse the repository at this point in the history
  • Loading branch information
volkerstampa committed Jul 26, 2023
1 parent a2909a6 commit 02425a0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
39 changes: 12 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,44 +209,29 @@ impl Client {
/// Controls of how to execute a task
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct How {
be_nice: bool,
client_timeout: Duration,
}

impl Default for How {
fn default() -> Self {
// the aleph-alpha-api cancels request after 5 minute
let api_timeout = Duration::from_secs(300);
Self {
be_nice: Default::default(),
// on the client side a request can take longer in case of network errors
// therefore by default we wait slightly longer
client_timeout: api_timeout + Duration::from_secs(5),
}
}
}

impl How {
/// The be-nice flag is used to reduce load for the models you intend to use.
/// This is commonly used if you are conducting experiments
/// or trying things out that create a large load on the aleph-alpha-api
/// and you do not want to increase queue time for other users too much.
///
/// (!) This increases how often you get a `Busy` response.
pub fn be_nice(self) -> Self {
Self {
be_nice: true,
..self
}
}
pub be_nice: bool,

/// The maximum duration of a request before the client cancels the request. This is not passed on
/// to the server but only handled by the client locally, i.e. the client will not wait longer than
/// this duration for a response.
pub fn with_client_timeout(self, client_timeout: Duration) -> Self {
pub client_timeout: Duration,
}

impl Default for How {
fn default() -> Self {
// the aleph-alpha-api cancels request after 5 minute
let api_timeout = Duration::from_secs(300);
Self {
client_timeout,
..self
be_nice: Default::default(),
// on the client side a request can take longer in case of network errors
// therefore by default we wait slightly longer
client_timeout: api_timeout + Duration::from_secs(5),
}
}
}
Expand Down
27 changes: 24 additions & 3 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@ async fn explain_request() {

// When
let response = client
.explanation(&task, "luminous-base", &How::default().be_nice())
.explanation(
&task,
"luminous-base",
&How {
be_nice: true,
..Default::default()
},
)
.await
.unwrap();

Expand All @@ -163,7 +170,14 @@ async fn explain_request_with_auto_granularity() {

// When
let response = client
.explanation(&task, "luminous-base", &How::default().be_nice())
.explanation(
&task,
"luminous-base",
&How {
be_nice: true,
..Default::default()
},
)
.await
.unwrap();

Expand All @@ -188,7 +202,14 @@ async fn explain_request_with_image_modality() {

// When
let response = client
.explanation(&task, "luminous-base", &How::default().be_nice())
.explanation(
&task,
"luminous-base",
&How {
be_nice: true,
..Default::default()
},
)
.await
.unwrap();

Expand Down
13 changes: 11 additions & 2 deletions tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ async fn be_nice() {
let client = Client::with_base_url(mock_server.uri(), "dummy-token").unwrap();
// Drop result, answer is meaningless anyway
let _ = client
.output_of(&task.with_model(model), &How::default().be_nice())
.output_of(
&task.with_model(model),
&How {
be_nice: true,
..Default::default()
},
)
.await;

// Then
Expand All @@ -168,7 +174,10 @@ async fn client_timeout() {
let result = client
.output_of(
&TaskCompletion::from_text("Hello,", 1).with_model("any"),
&How::default().with_client_timeout(response_time / 2),
&How {
client_timeout: response_time / 2,
..Default::default()
},
)
.await
.unwrap_err();
Expand Down

0 comments on commit 02425a0

Please sign in to comment.