From a2ad4fb1186ac6ab850e37bb8fd4b6f764dbcf47 Mon Sep 17 00:00:00 2001 From: Moritz Althaus Date: Thu, 24 Oct 2024 23:18:00 +0200 Subject: [PATCH] refactor: make body_to_output associated method to share between threads --- src/chat.rs | 2 +- src/completion.rs | 2 +- src/detokenization.rs | 2 +- src/explanation.rs | 2 +- src/http.rs | 10 +++++----- src/semantic_embedding.rs | 6 +++--- src/stream.rs | 2 +- src/tokenization.rs | 2 +- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index b4a29fd..030383d 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -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() } } diff --git a/src/completion.rs b/src/completion.rs index 849000f..49da450 100644 --- a/src/completion.rs +++ b/src/completion.rs @@ -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() } } diff --git a/src/detokenization.rs b/src/detokenization.rs index 644fc10..70721ea 100644 --- a/src/detokenization.rs +++ b/src/detokenization.rs @@ -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) } } diff --git a/src/explanation.rs b/src/explanation.rs index 7720a10..6c725be 100644 --- a/src/explanation.rs +++ b/src/explanation.rs @@ -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) } } diff --git a/src/http.rs b/src/http.rs index cf7b4f7..ab5d6c4 100644 --- a/src/http.rs +++ b/src/http.rs @@ -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 @@ -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> @@ -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) } } @@ -161,7 +161,7 @@ impl HttpClient { pub async fn output_of(&self, task: &T, how: &How) -> Result { 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) } diff --git a/src/semantic_embedding.rs b/src/semantic_embedding.rs index c64501f..7e57066 100644 --- a/src/semantic_embedding.rs +++ b/src/semantic_embedding.rs @@ -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 } } @@ -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 } } @@ -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 } } diff --git a/src/stream.rs b/src/stream.rs index 2e06c69..dcd6b07 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -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 } } diff --git a/src/tokenization.rs b/src/tokenization.rs index e552622..e9b7163 100644 --- a/src/tokenization.rs +++ b/src/tokenization.rs @@ -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) } }