-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96ddae9
commit a50a9fa
Showing
44 changed files
with
482 additions
and
505 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[workspace] | ||
members = ["src/public", "src/grpc_server"] | ||
members = ["src/public", "src/gpt_answer_server"] | ||
resolver = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use anyhow::Error; | ||
use tonic::{ | ||
async_trait, | ||
transport::{Channel, Endpoint}, | ||
}; | ||
|
||
use common::grpc::gpt_answer::gpt_answer::{ | ||
gpt_answer_service_client::GptAnswerServiceClient, GetAnswerPayload, | ||
}; | ||
use rust_core::{common::errors::CoreError, ports::gpt_answer::GptAnswerPort}; | ||
|
||
/// gRPC client for interacting with a GPT (Generative Pre-trained Transformer) answer service. | ||
/// | ||
/// This struct represents a client for making gRPC calls to a GPT answer service. It provides | ||
/// methods for connecting to the service, sending a question, and receiving an answer. | ||
#[derive(Clone)] | ||
pub struct GptAnswerClient { | ||
client: Option<GptAnswerServiceClient<Channel>>, | ||
endpoint: Endpoint, | ||
} | ||
|
||
impl GptAnswerClient { | ||
/// Creates a new `GptAnswerClient` instance with the provided gRPC endpoint. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `endpoint`: An `Endpoint` representing the gRPC communication endpoint. | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns a new instance of `GptAnswerClient`. | ||
fn new(endpoint: Endpoint) -> Self { | ||
Self { | ||
client: None, | ||
endpoint, | ||
} | ||
} | ||
|
||
/// Initializes a new `GptAnswerClient` instance with the provided URI. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `uri`: A `String` representing the URI of the GPT answer service. | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns a `Result` containing the initialized `GptAnswerClient` if successful, | ||
/// or a `CoreError` if an error occurs during initialization. | ||
pub async fn init(uri: String) -> Result<Self, CoreError> { | ||
// Establish connection to the gRPC server | ||
let endpoint = | ||
Channel::from_shared(uri).map_err(|err| CoreError::InternalError(err.into()))?; | ||
|
||
Ok(Self::new(endpoint)) | ||
} | ||
|
||
/// Establishes a connection to the GPT answer service at the specified URI. | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns a `Result` containing the connected `GptAnswerServiceClient` if successful, | ||
/// or a `CoreError` if an error occurs during connection. | ||
pub async fn connect(&mut self) -> Result<(), CoreError> { | ||
let client = GptAnswerServiceClient::connect(self.endpoint.clone()) | ||
.await | ||
.map_err(|err| CoreError::InternalError(err.into()))?; | ||
|
||
self.client = Some(client); | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl GptAnswerPort for GptAnswerClient { | ||
/// Sends a question to the GPT answer service and retrieves the generated answer. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `question`: A `&str` representing the question to be sent to the service. | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns a `Result` containing the generated answer as a `String` if successful, | ||
/// or a `CoreError` if an error occurs during communication with the service. | ||
async fn get_answer(&self, question: &str) -> Result<String, CoreError> { | ||
let client = self | ||
.client | ||
.as_ref() | ||
.ok_or_else(|| CoreError::InternalError(Error::msg("Client not initialized")))?; | ||
|
||
let request = tonic::Request::new(GetAnswerPayload { | ||
question: question.to_string(), | ||
}); | ||
|
||
let response = client | ||
.clone() | ||
.get_answer(request) | ||
.await | ||
.map_err(|err| CoreError::InternalError(err.into()))?; | ||
|
||
Ok(response.into_inner().answer) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
pub mod config; | ||
pub mod models; | ||
pub mod gpt_answer_client; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/// Module for gRPC service definitions related to answering questions with GPT (Generative Pre-trained Transformer) models. | ||
/// | ||
/// This module includes generated gRPC service definitions for answering questions using GPT models. | ||
/// The `tonic::include_proto!` macro is used to include the protobuf definitions, enabling easy | ||
/// integration of gRPC services into Rust code. | ||
pub mod gpt_answer { | ||
// Include the protobuf definitions for the gpt_answer service. | ||
tonic::include_proto!("gpt_answer"); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod grpc; | ||
pub mod loggers; | ||
pub mod options; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.