1use async_trait::async_trait;
2use futures::stream::Stream;
3use std::pin::Pin;
4
5pub mod provider;
6pub mod types;
7
8pub use provider::{LlamaClient, OpenAIClient};
9pub use types::{ChatMessage, ChatRequest, ChatResponse, Role};
10
11pub type LlmResult<T> = Result<T, LlmError>;
13
14pub type TextStream = Pin<Box<dyn Stream<Item = LlmResult<String>> + Send>>;
16
17#[derive(Debug, thiserror::Error)]
19pub enum LlmError {
20 #[error("API error: {0}")]
21 ApiError(String),
22
23 #[error("Network error: {0}")]
24 NetworkError(String),
25
26 #[error("Invalid request: {0}")]
27 InvalidRequest(String),
28
29 #[error("Rate limit exceeded")]
30 RateLimitExceeded,
31
32 #[error("Authentication failed: {0}")]
33 AuthenticationFailed(String),
34
35 #[error("Response parsing error: {0}")]
36 ParseError(String),
37}
38
39#[async_trait]
41pub trait ChatClient: Send + Sync {
42 async fn chat(&self, request: ChatRequest) -> LlmResult<ChatResponse>;
44
45 async fn chat_stream(&self, request: ChatRequest) -> LlmResult<TextStream>;
47
48 fn model(&self) -> &str;
50
51 fn provider(&self) -> &str;
53}