"torchvision/vscode:/vscode.git/clone" did not exist on "41f9c1e6cc840f32128bd94a74df14e3e06617b1"
lib.rs 2.38 KB
Newer Older
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
1
//! Text Generation gRPC client library
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
2
3

mod client;
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
4
#[allow(clippy::derive_partial_eq_without_eq)]
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
5
6
7
mod pb;
mod sharded_client;

8
use base64::{engine::general_purpose::STANDARD, Engine};
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
9
pub use client::Client;
10
pub use pb::generate::v2::input_chunk::Chunk;
Nicolas Patry's avatar
Nicolas Patry committed
11
pub use pb::generate::v2::HealthResponse;
12
pub use pb::generate::v2::Image;
Nicolas Patry's avatar
Nicolas Patry committed
13
14
pub use pb::generate::v2::InfoResponse as ShardInfo;
pub use pb::generate::v2::{
15
    Batch, CachedBatch, FinishReason, GeneratedText, Generation, GrammarType, Input, InputChunk,
drbh's avatar
drbh committed
16
    NextTokenChooserParameters, Request, StoppingCriteriaParameters, Tokens,
17
};
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
18
19
pub use sharded_client::ShardedClient;
use thiserror::Error;
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
20
use tonic::transport;
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
21
22
23
use tonic::Status;

#[derive(Error, Debug, Clone)]
Olivier Dehaene's avatar
Olivier Dehaene committed
24
pub enum ClientError {
25
    #[error("Could not connect to Text Generation server: {0}")]
Olivier Dehaene's avatar
Olivier Dehaene committed
26
    Connection(String),
27
    #[error("Server error: {0}")]
Olivier Dehaene's avatar
Olivier Dehaene committed
28
    Generation(String),
29
30
    #[error("Sharded results are empty")]
    EmptyResults,
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
31
32
33
34
}

impl From<Status> for ClientError {
    fn from(err: Status) -> Self {
35
36
37
        let err = Self::Generation(err.message().to_string());
        tracing::error!("{err}");
        err
Olivier Dehaene's avatar
Olivier Dehaene committed
38
39
40
41
42
    }
}

impl From<transport::Error> for ClientError {
    fn from(err: transport::Error) -> Self {
43
44
45
        let err = Self::Connection(err.to_string());
        tracing::error!("{err}");
        err
Olivier Dehaene's avatar
Init  
Olivier Dehaene committed
46
47
48
49
    }
}

pub type Result<T> = std::result::Result<T, ClientError>;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

// Small convenience re-wrapping of `Chunk`.
impl From<Chunk> for InputChunk {
    fn from(chunk: Chunk) -> Self {
        InputChunk { chunk: Some(chunk) }
    }
}

/// Convert input chunks to a stringly-typed input for backwards
/// compat for backends that haven't implemented chunked inputs.
pub trait ChunksToString {
    /// Convert chunks to string.
    fn chunks_to_string(&self) -> String;
}

impl ChunksToString for Vec<InputChunk> {
    fn chunks_to_string(&self) -> String {
        let mut output = String::new();
        self.iter().for_each(|c| match &c.chunk {
            Some(Chunk::Text(text)) => output.push_str(text),
            Some(Chunk::Image(Image { data, mimetype })) => {
                let encoded = STANDARD.encode(data);
                output.push_str(&format!("![](data:{};base64,{})", mimetype, encoded))
            }
            // We don't create empty chunks, so this should be unreachable.
            None => unreachable!("Chunks should never be empty"),
        });
        output
    }
}