diff options
| author | sigoden <sigoden@gmail.com> | 2023-11-01 10:28:54 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-01 10:28:54 +0800 |
| commit | 7f2210dbcab017ee30e8f95771e68494b4860c59 (patch) | |
| tree | 7a0c7bdc8f6930e262bf8cfdc8ffbd57aa843e40 /src/client/localai.rs | |
| parent | 64202758ec8693dd18951c554728bb5c6b7e5eeb (diff) | |
| download | aichat-7f2210dbcab017ee30e8f95771e68494b4860c59.tar.gz | |
refactor: clients/* and config.rs (#193)
- add register_clients macro to make it easier to add a new client
- no create_client_config, just add const PROMPTS
- move ModelInfo from clients/ to config/
- model's max_tokens are optional
- improve code quanity on config/mod.rs
- add/use macro config_get_fn
Diffstat (limited to 'src/client/localai.rs')
| -rw-r--r-- | src/client/localai.rs | 82 |
1 files changed, 18 insertions, 64 deletions
diff --git a/src/client/localai.rs b/src/client/localai.rs index fb5a353..131d92b 100644 --- a/src/client/localai.rs +++ b/src/client/localai.rs @@ -1,8 +1,5 @@ use super::openai::{openai_build_body, openai_send_message, openai_send_message_streaming}; -use super::{ - prompt_input_api_base, prompt_input_api_key_optional, prompt_input_max_token, - prompt_input_model_name, Client, ClientConfig, ExtraConfig, ModelInfo, SendData, -}; +use super::{Client, ExtraConfig, LocalAIClient, ModelInfo, PromptKind, PromptType, SendData}; use crate::config::SharedConfig; use crate::repl::ReplyStreamHandler; @@ -13,13 +10,6 @@ use reqwest::{Client as ReqwestClient, RequestBuilder}; use serde::Deserialize; use std::env; -#[derive(Debug)] -pub struct LocalAIClient { - global_config: SharedConfig, - config: LocalAIConfig, - model_info: ModelInfo, -} - #[derive(Debug, Clone, Deserialize)] pub struct LocalAIConfig { pub name: Option<String>, @@ -33,17 +23,13 @@ pub struct LocalAIConfig { #[derive(Debug, Clone, Deserialize)] pub struct LocalAIModel { name: String, - max_tokens: usize, + max_tokens: Option<usize>, } #[async_trait] impl Client for LocalAIClient { - fn config(&self) -> &SharedConfig { - &self.global_config - } - - fn extra_config(&self) -> &Option<ExtraConfig> { - &self.config.extra + fn config(&self) -> (&SharedConfig, &Option<ExtraConfig>) { + (&self.global_config, &self.config.extra) } async fn send_message_inner(&self, client: &ReqwestClient, data: SendData) -> Result<String> { @@ -63,27 +49,19 @@ impl Client for LocalAIClient { } impl LocalAIClient { - pub const NAME: &str = "localai"; - - pub fn init(global_config: SharedConfig) -> Option<Box<dyn Client>> { - let model_info = global_config.read().model_info.clone(); - let config = { - if let ClientConfig::LocalAI(c) = &global_config.read().clients[model_info.index] { - c.clone() - } else { - return None; - } - }; - Some(Box::new(Self { - global_config, - config, - model_info, - })) - } - - pub fn name(local_config: &LocalAIConfig) -> &str { - local_config.name.as_deref().unwrap_or(Self::NAME) - } + config_get_fn!(api_key, get_api_key); + + pub const PROMPTS: [PromptType<'static>; 4] = [ + ("api_base", "API Base:", true, PromptKind::String), + ("api_key", "API Key:", false, PromptKind::String), + ("models[].name", "Model Name:", true, PromptKind::String), + ( + "models[].max_tokens", + "Max Tokens:", + false, + PromptKind::Integer, + ), + ]; pub fn list_models(local_config: &LocalAIConfig, index: usize) -> Vec<ModelInfo> { let client = Self::name(local_config); @@ -95,32 +73,8 @@ impl LocalAIClient { .collect() } - pub fn create_config() -> Result<String> { - let mut client_config = format!("clients:\n - type: {}\n", Self::NAME); - - let api_base = prompt_input_api_base()?; - client_config.push_str(&format!(" api_base: {api_base}\n")); - - let api_key = prompt_input_api_key_optional()?; - client_config.push_str(&format!(" api_key: {api_key}\n")); - - let model_name = prompt_input_model_name()?; - - let max_tokens = prompt_input_max_token()?; - - client_config.push_str(&format!( - " models:\n - name: {model_name}\n max_tokens: {max_tokens}\n" - )); - - Ok(client_config) - } - fn request_builder(&self, client: &ReqwestClient, data: SendData) -> Result<RequestBuilder> { - let api_key = self.config.api_key.clone(); - let api_key = api_key.or_else(|| { - let env_prefix = Self::name(&self.config).to_uppercase(); - env::var(format!("{env_prefix}_API_KEY")).ok() - }); + let api_key = self.get_api_key().ok(); let body = openai_build_body(data, self.model_info.name.clone()); |
