-
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 66f554f
Showing
39 changed files
with
268 additions
and
336 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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_service"] | ||
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
pub mod config; | ||
pub mod models; |
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,40 +1,80 @@ | ||
use rust_core::common::errors::CoreError; | ||
use tonic::transport::Channel; | ||
|
||
use grpc_interface::interfaces::gpt_answer::gpt_answer::{ | ||
use common::grpc::gpt_answer::gpt_answer::{ | ||
gpt_answer_service_client::GptAnswerServiceClient, GetAnswerPayload, | ||
}; | ||
use rust_core::common::errors::CoreError; | ||
|
||
/// 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. | ||
pub struct GptAnswerGrpcClient { | ||
client: GptAnswerServiceClient<Channel>, | ||
} | ||
|
||
impl GptAnswerGrpcClient { | ||
/// Creates a new `GptAnswerGrpcClient` instance with the provided gRPC channel. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `channel`: A `Channel` representing the gRPC communication channel. | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns a new instance of `GptAnswerGrpcClient`. | ||
fn new(channel: Channel) -> Self { | ||
let client = GptAnswerServiceClient::new(channel); | ||
Self { client } | ||
} | ||
|
||
pub async fn get_instance(uri: &'static str) -> Result<Self, CoreError> { | ||
let channel = Channel::from_static(uri).connect().await.map_err(|err| { | ||
eprintln!("Error connecting to GPT: {:?}", err); | ||
CoreError::InternalError | ||
})?; | ||
/// Establishes a connection to the GPT answer service at the specified URI. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `uri`: The URI of the GPT answer service. | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns a `Result` containing the connected `GptAnswerGrpcClient` if successful, | ||
/// or a `CoreError` if an error occurs during connection. | ||
pub async fn connect(uri: &'static str) -> Result<Self, CoreError> { | ||
// Establish connection to the gRPC server | ||
let channel: Channel = match Channel::from_static(uri).connect().await { | ||
Ok(channel) => channel, | ||
Err(err) => { | ||
return Err(CoreError::InternalError(err.into())); | ||
} | ||
}; | ||
|
||
// Create a new client instance with the connected channel | ||
let client = Self::new(channel); | ||
Ok(client) | ||
} | ||
|
||
/// 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. | ||
pub async fn get_answer(&mut self, question: &str) -> Result<String, CoreError> { | ||
// Create a gRPC request with the question payload | ||
let request = tonic::Request::new(GetAnswerPayload { | ||
question: question.to_string(), | ||
}); | ||
|
||
// Send the request to the gRPC server and await the response | ||
let response = self.client.get_answer(request).await.map_err(|err| { | ||
eprintln!("Error getting answer from GPT: {:?}", err); | ||
CoreError::InternalError | ||
CoreError::InternalError(err.into()) | ||
})?; | ||
|
||
// Extract and return the answer from the response | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,4 @@ features = ["derive"] | |
version = "1.0.57" | ||
|
||
[dependencies] | ||
anyhow = "1.0.80" | ||
anyhow = "1.0.80" |
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.