next_web_ai/chat/client/
chat_client.rs1use bytes::Bytes;
2use next_web_core::async_trait;
3use serde::de::DeserializeOwned;
4
5use crate::chat::{model::chat_response::ChatResponse, prompt::prompt::Prompt};
6
7pub trait ChatClient: Send + Sync {
8 fn prompt(&self, prompt: Prompt) -> impl ChatClientRequestSpec;
9
10 fn prompt_from_content<T>(&self, message: T) -> impl ChatClientRequestSpec
11 where
12 T: Into<Bytes>;
13
14 fn prompt_from_default(&self) -> impl ChatClientRequestSpec;
15}
16
17#[async_trait]
18pub trait ChatClientRequestSpec: Send + Sync {
19 async fn call(&self) -> impl CallResponseSpec;
20
21 async fn stream(&self) -> impl StreamResponseSpec;
22
23 fn user<T>(&mut self, text: T)
24 where
25 T: Into<Bytes>;
26
27 fn system<T>(&mut self, text: T)
28 where
29 T: Into<Bytes>;
30}
31
32pub trait CallResponseSpec: Send + Sync {
33 fn entity<T>(&self) -> T
34 where
35 T: DeserializeOwned;
36
37 fn chat_response(&self) -> ChatResponse;
38
39 fn content(&self) -> Bytes;
40
41 fn response_entity<T>(&self) -> T
42 where
43 T: DeserializeOwned;
44}
45
46#[async_trait]
47pub trait StreamResponseSpec: Send + Sync {
48 async fn chat_response<S>(
49 &self,
50 ) -> impl futures_core::Stream<Item = ChatResponse> + Send + 'static;
51
52 async fn content<S>(&self) -> impl futures_core::Stream<Item = ChatResponse> + Send + 'static;
53}