llm_gateway/
lib.rs

1mod error;
2pub mod types;
3mod config;
4mod provider;
5mod router;
6
7pub use error::*;
8pub use types::*;
9pub use config::{Config, ProviderConfig, CallOptions};
10
11use futures_util::stream::BoxStream;
12
13pub struct Client {
14    router: router::Router,
15}
16
17impl Client {
18    pub fn new() -> Self {
19        Self::with_config(Config::default())
20    }
21
22    pub fn with_config(cfg: Config) -> Self {
23        let router = router::Router::new(cfg);
24        Self { router }
25    }
26
27    pub fn from_env() -> Self {
28        Self::with_config(Config::from_env())
29    }
30
31    // README 示例中的便捷方法
32    pub fn from_yaml_file_auto() -> Option<Self> {
33        Config::from_yaml_file_auto().map(Self::with_config)
34    }
35
36    /// 列出可用模型(形式:provider/model),仅本地配置/默认值
37    pub fn list_models(&self) -> Vec<String> {
38        self.router.list_models()
39    }
40
41    /// 自动聚合模型列表(根据配置决定在线/离线,默认 CallOptions)
42    pub async fn list_models_auto(&self) -> Result<Vec<String>, error::LlmProxyError> {
43        self.router.list_models_auto(&CallOptions::default()).await
44    }
45
46    /// 自动聚合模型列表(可自定义调用参数)
47    pub async fn list_models_auto_with(&self, opts: CallOptions) -> Result<Vec<String>, error::LlmProxyError> {
48        self.router.list_models_auto(&opts).await
49    }
50
51    /// 非流式对话
52    pub async fn chat(&self, req: types::ChatRequest) -> Result<types::ChatResponse, error::LlmProxyError> {
53        self.router.chat(req, &CallOptions::default()).await
54    }
55
56    /// 非流式对话(可选调用参数)
57    pub async fn chat_with(
58        &self,
59        req: types::ChatRequest,
60        opts: CallOptions,
61    ) -> Result<types::ChatResponse, error::LlmProxyError> {
62        self.router.chat(req, &opts).await
63    }
64
65    /// 流式对话
66    pub async fn chat_stream(
67        &self,
68        req: types::ChatRequest,
69    ) -> Result<BoxStream<'static, Result<String, error::LlmProxyError>>, error::LlmProxyError> {
70        self.router.chat_stream(req, &CallOptions::default()).await
71    }
72
73    /// 流式对话(可选调用参数)
74    pub async fn chat_stream_with(
75        &self,
76        req: types::ChatRequest,
77        opts: CallOptions,
78    ) -> Result<BoxStream<'static, Result<String, error::LlmProxyError>>, error::LlmProxyError> {
79        self.router.chat_stream(req, &opts).await
80    }
81}