2022-10-18 13:19:03 +00:00
|
|
|
//! Text Generation gRPC client library
|
2022-10-08 10:30:12 +00:00
|
|
|
|
2024-06-04 13:56:56 +00:00
|
|
|
use async_trait::async_trait;
|
2022-10-08 10:30:12 +00:00
|
|
|
use thiserror::Error;
|
2022-10-18 13:19:03 +00:00
|
|
|
use tonic::transport;
|
2022-10-08 10:30:12 +00:00
|
|
|
use tonic::Status;
|
|
|
|
|
2024-07-31 08:33:10 +00:00
|
|
|
#[allow(clippy::derive_partial_eq_without_eq)]
|
|
|
|
mod pb;
|
|
|
|
|
|
|
|
mod grpc_client;
|
|
|
|
mod sharded_client;
|
|
|
|
|
|
|
|
pub use grpc_client::Client;
|
|
|
|
pub use pb::generate::v3::{
|
|
|
|
input_chunk::Chunk, Batch, CachedBatch, FinishReason, GeneratedText, Generation, GrammarType,
|
|
|
|
HealthResponse, Image, InfoResponse, Input, InputChunk, NextTokenChooserParameters, Request,
|
|
|
|
StoppingCriteriaParameters,
|
|
|
|
};
|
|
|
|
pub use sharded_client::ShardedClient;
|
2024-06-04 13:56:56 +00:00
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
pub trait Health {
|
|
|
|
/// Check if a generate server is healthy by asking it to allocate a tensor on device
|
|
|
|
async fn device_health(&self) -> Result<()>;
|
|
|
|
|
|
|
|
/// Check if a generate server is healthy by doing a forward pass.
|
|
|
|
/// EXPENSIVE
|
|
|
|
async fn model_health(&self) -> Result<()>;
|
|
|
|
}
|
|
|
|
|
2022-10-08 10:30:12 +00:00
|
|
|
#[derive(Error, Debug, Clone)]
|
2022-10-17 12:59:00 +00:00
|
|
|
pub enum ClientError {
|
2023-02-13 12:02:45 +00:00
|
|
|
#[error("Could not connect to Text Generation server: {0}")]
|
2022-10-17 12:59:00 +00:00
|
|
|
Connection(String),
|
2023-02-13 12:02:45 +00:00
|
|
|
#[error("Server error: {0}")]
|
2022-10-17 12:59:00 +00:00
|
|
|
Generation(String),
|
2023-05-10 13:48:21 +00:00
|
|
|
#[error("Sharded results are empty")]
|
|
|
|
EmptyResults,
|
2022-10-08 10:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Status> for ClientError {
|
|
|
|
fn from(err: Status) -> Self {
|
2023-02-13 12:02:45 +00:00
|
|
|
let err = Self::Generation(err.message().to_string());
|
|
|
|
tracing::error!("{err}");
|
|
|
|
err
|
2022-10-17 12:59:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<transport::Error> for ClientError {
|
|
|
|
fn from(err: transport::Error) -> Self {
|
2023-02-13 12:02:45 +00:00
|
|
|
let err = Self::Connection(err.to_string());
|
|
|
|
tracing::error!("{err}");
|
|
|
|
err
|
2022-10-08 10:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-03 07:27:22 +00:00
|
|
|
// Small convenience re-wrapping of `Chunk`.
|
|
|
|
impl From<Chunk> for InputChunk {
|
|
|
|
fn from(chunk: Chunk) -> Self {
|
|
|
|
InputChunk { chunk: Some(chunk) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-04 13:56:56 +00:00
|
|
|
static WARMUP_IMAGE_BASE64 :&str = "iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAABg2lDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV/TSotUROxQxCFDdbKLijjWKhShQqgVWnUwufQLmrQkKS6OgmvBwY/FqoOLs64OroIg+AHi7OCk6CIl/i8ptIjx4Lgf7+497t4BQqvKNDOQADTdMjKppJjLr4rBVwQQwhAERGVm1uckKQ3P8XUPH1/v4jzL+9yfY0AtmAzwicQJVjcs4g3imU2rznmfOMLKskp8Tjxh0AWJH7muuPzGueSwwDMjRjYzTxwhFks9rPQwKxsa8TRxTNV0yhdyLquctzhr1Qbr3JO/MFzQV5a5TnMUKSxiCRJEKGiggiosxGnVSTGRof2kh3/E8UvkUshVASPHAmrQIDt+8D/43a1ZnJp0k8JJoO/Ftj/GgOAu0G7a9vexbbdPAP8zcKV3/bUWMPtJerOrxY6AwW3g4rqrKXvA5Q4QfarLhuxIfppCsQi8n9E35YHhW6B/ze2ts4/TByBLXaVvgINDYLxE2ese7w719vbvmU5/PycecohsjayNAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAB3RJTUUH6AQIEQMnlTSSjwAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAASSURBVDjLY2AYBaNgFIyCoQsABMQAAeRw1DoAAAAASUVORK5CYII=";
|
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, ClientError>;
|