From 740ca2413a1992d151c0a9ea62c12e69321c0973 Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 26 Apr 2024 07:43:35 +0800 Subject: refactor: simplify impl client trait (#445) --- src/client/common.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'src/client/common.rs') diff --git a/src/client/common.rs b/src/client/common.rs index 55e7b62..4509805 100644 --- a/src/client/common.rs +++ b/src/client/common.rs @@ -139,7 +139,6 @@ macro_rules! openai_compatible_module { use $crate::utils::PromptKind; use anyhow::Result; - use async_trait::async_trait; use reqwest::{Client as ReqwestClient, RequestBuilder}; use serde::Deserialize; @@ -154,7 +153,12 @@ macro_rules! openai_compatible_module { pub extra: Option, } - openai_compatible_client!($client); + impl_client_trait!( + $client, + $crate::client::openai::openai_send_message, + $crate::client::openai::openai_send_message_streaming + ); + impl $client { list_models_fn!( @@ -218,9 +222,9 @@ macro_rules! client_common_fns { } #[macro_export] -macro_rules! openai_compatible_client { - ($client:ident) => { - #[async_trait] +macro_rules! impl_client_trait { + ($client:ident, $send_message:path, $send_message_streaming:path) => { + #[async_trait::async_trait] impl $crate::client::Client for $crate::client::$client { client_common_fns!(); @@ -230,7 +234,7 @@ macro_rules! openai_compatible_client { data: $crate::client::SendData, ) -> anyhow::Result { let builder = self.request_builder(client, data)?; - $crate::client::openai::openai_send_message(builder).await + $send_message(builder).await } async fn send_message_streaming_inner( @@ -240,7 +244,7 @@ macro_rules! openai_compatible_client { data: $crate::client::SendData, ) -> Result<()> { let builder = self.request_builder(client, data)?; - $crate::client::openai::openai_send_message_streaming(builder, handler).await + $send_message_streaming(builder, handler).await } } }; -- cgit v1.2.3