async_llm/chat/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::fmt::Debug;
use std::pin::Pin;

use crate::{error::Error, http::HttpClient, request::Requestable, Client, Provider};

use futures::Stream;

#[derive(Debug, Clone)]
pub struct Chat<'c, P: Provider, H: HttpClient> {
    pub(crate) client: &'c Client<P, H>,
}

impl<'c, P, H> Chat<'c, P, H>
where
    P: Provider,
    H: HttpClient,
{
    pub fn new(client: &'c Client<P, H>) -> Self {
        Self { client }
    }

    pub async fn create<T>(&self, request: T) -> Result<P::ChatResponse, Error>
    where
        T: TryInto<P::ChatRequest>,
        T::Error: Debug,
    {
        let request: P::ChatRequest = request.try_into().map_err(|e| {
            Error::InvalidArgument(format!("Failed to convert to ChatRequest. Error = {e:?}"))
        })?;
        let stream = request.stream();
        match stream {
            true => Err(Error::InvalidArgument(
                "When stream is true, use the client.create_stream function instead".into(),
            )),
            false => {
                self.client
                    .provider
                    .chat(&self.client.http_client, request)
                    .await
            }
        }
    }
    pub async fn create_stream<T>(
        &self,
        request: T,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<P::ChatResponseStream, Error>> + Send>>, Error>
    where
        T: TryInto<P::ChatRequest>,
        T::Error: Debug,
    {
        let request: P::ChatRequest = request.try_into().map_err(|e| {
            Error::InvalidArgument(format!("Failed to convert to ChatRequest. Error = {e:?}"))
        })?;
        let stream = request.stream();
        match stream {
            false => Err(Error::InvalidArgument(
                "When stream is false, use the client.create function instead".into(),
            )),
            true => {
                self.client
                    .provider
                    .chat_stream(&self.client.http_client, request)
                    .await
            }
        }
    }
}