Skip to content

Commit

Permalink
refactor: make body_to_output associated method to share between threads
Browse files Browse the repository at this point in the history
  • Loading branch information
moldhouse committed Oct 24, 2024
1 parent 565d4ed commit a2ad4fb
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'a> Task for TaskChat<'a> {
client.post(format!("{base}/chat/completions")).json(&body)
}

fn body_to_output(&self, mut response: Self::ResponseBody) -> Self::Output {
fn body_to_output(mut response: Self::ResponseBody) -> Self::Output {
response.choices.pop().unwrap()
}
}
2 changes: 1 addition & 1 deletion src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl Task for TaskCompletion<'_> {
client.post(format!("{base}/complete")).json(&body)
}

fn body_to_output(&self, mut response: Self::ResponseBody) -> Self::Output {
fn body_to_output(mut response: Self::ResponseBody) -> Self::Output {
response.completions.pop().unwrap()
}
}
2 changes: 1 addition & 1 deletion src/detokenization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'a> Task for TaskDetokenization<'a> {
client.post(format!("{base}/detokenize")).json(&body)
}

fn body_to_output(&self, response: Self::ResponseBody) -> Self::Output {
fn body_to_output(response: Self::ResponseBody) -> Self::Output {
DetokenizationOutput::from(response)
}
}
2 changes: 1 addition & 1 deletion src/explanation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl Task for TaskExplanation<'_> {
client.post(format!("{base}/explain")).json(&body)
}

fn body_to_output(&self, response: Self::ResponseBody) -> Self::Output {
fn body_to_output(response: Self::ResponseBody) -> Self::Output {
ExplanationOutput::from(response)
}
}
10 changes: 5 additions & 5 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait Job {
fn build_request(&self, client: &reqwest::Client, base: &str) -> RequestBuilder;

/// Parses the response of the server into higher level structs for the user.
fn body_to_output(&self, response: Self::ResponseBody) -> Self::Output;
fn body_to_output(response: Self::ResponseBody) -> Self::Output;
}

/// A task send to the Aleph Alpha Api using the http client. Requires to specify a model before it
Expand All @@ -45,7 +45,7 @@ pub trait Task {
fn build_request(&self, client: &reqwest::Client, base: &str, model: &str) -> RequestBuilder;

/// Parses the response of the server into higher level structs for the user.
fn body_to_output(&self, response: Self::ResponseBody) -> Self::Output;
fn body_to_output(response: Self::ResponseBody) -> Self::Output;

/// Turn your task into [`Job`] by annotating it with a model name.
fn with_model<'a>(&'a self, model: &'a str) -> MethodJob<'a, Self>
Expand Down Expand Up @@ -77,8 +77,8 @@ where
self.task.build_request(client, base, self.model)
}

fn body_to_output(&self, response: T::ResponseBody) -> T::Output {
self.task.body_to_output(response)
fn body_to_output(response: Self::ResponseBody) -> Self::Output {
T::body_to_output(response)
}
}

Expand Down Expand Up @@ -161,7 +161,7 @@ impl HttpClient {
pub async fn output_of<T: Job>(&self, task: &T, how: &How) -> Result<T::Output, Error> {
let response = self.request(task, how).await?;
let response_body: T::ResponseBody = response.json().await?;
let answer = task.body_to_output(response_body);
let answer = T::body_to_output(response_body);
Ok(answer)
}

Expand Down
6 changes: 3 additions & 3 deletions src/semantic_embedding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Task for TaskSemanticEmbedding<'_> {
client.post(format!("{base}/semantic_embed")).json(&body)
}

fn body_to_output(&self, response: Self::ResponseBody) -> Self::Output {
fn body_to_output(response: Self::ResponseBody) -> Self::Output {
response
}
}
Expand All @@ -94,7 +94,7 @@ impl Job for TaskSemanticEmbedding<'_> {
client.post(format!("{base}/semantic_embed")).json(&body)
}

fn body_to_output(&self, response: Self::ResponseBody) -> Self::Output {
fn body_to_output(response: Self::ResponseBody) -> Self::Output {
response
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ impl Job for TaskBatchSemanticEmbedding<'_> {
.json(&body)
}

fn body_to_output(&self, response: Self::ResponseBody) -> Self::Output {
fn body_to_output(response: Self::ResponseBody) -> Self::Output {
response
}
}
2 changes: 1 addition & 1 deletion src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Task for TaskStreamCompletion<'_> {
client.post(format!("{base}/complete")).json(&body)
}

fn body_to_output(&self, response: Self::ResponseBody) -> Self::Output {
fn body_to_output(response: Self::ResponseBody) -> Self::Output {
response
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tokenization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl Task for TaskTokenization<'_> {
client.post(format!("{base}/tokenize")).json(&body)
}

fn body_to_output(&self, response: Self::ResponseBody) -> Self::Output {
fn body_to_output(response: Self::ResponseBody) -> Self::Output {
TokenizationOutput::from(response)
}
}

0 comments on commit a2ad4fb

Please sign in to comment.