From bb867c4fcbc6f42770471f0a0117cd8909f660d7 Mon Sep 17 00:00:00 2001 From: sigoden Date: Tue, 11 Jun 2024 11:00:12 +0800 Subject: feat: support bot (#579) * feat: support bots * refactor with RoleLike * improve exiting session * make bot works with rag * refactor repl assert state * add bot banner * repl complete bots according bots.txt * fix on windows * remove threadpool executing function callings * adjust repl left_prompt * move bot config to global config.yaml * `.bot` throw err if funciton callings is not configured --- src/client/model.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'src/client') diff --git a/src/client/model.rs b/src/client/model.rs index 22ad4d9..d555232 100644 --- a/src/client/model.rs +++ b/src/client/model.rs @@ -1,8 +1,10 @@ use super::{ + list_chat_models, message::{Message, MessageContent}, EmbeddingsData, }; +use crate::config::Config; use crate::utils::{estimate_token_length, format_option_value}; use anyhow::{bail, Result}; @@ -41,9 +43,16 @@ impl Model { .collect() } - pub fn find(models: &[&Self], value: &str) -> Option { + pub fn retrieve(config: &Config, model_id: &str) -> Result { + match Self::find(&list_chat_models(config), model_id) { + Some(v) => Ok(v), + None => bail!("Invalid model '{model_id}'"), + } + } + + pub fn find(models: &[&Self], model_id: &str) -> Option { let mut model = None; - let (client_name, model_name) = match value.split_once(':') { + let (client_name, model_name) = match model_id.split_once(':') { Some((client_name, model_name)) => { if model_name.is_empty() { (client_name, None) @@ -51,11 +60,11 @@ impl Model { (client_name, Some(model_name)) } } - None => (value, None), + None => (model_id, None), }; match model_name { Some(model_name) => { - if let Some(found) = models.iter().find(|v| v.id() == value) { + if let Some(found) = models.iter().find(|v| v.id() == model_id) { model = Some((*found).clone()); } else if let Some(found) = models.iter().find(|v| v.client_name == client_name) { let mut found = (*found).clone(); @@ -73,7 +82,11 @@ impl Model { } pub fn id(&self) -> String { - format!("{}:{}", self.client_name, self.data.name) + if self.data.name.is_empty() { + self.client_name.to_string() + } else { + format!("{}:{}", self.client_name, self.data.name) + } } pub fn client_name(&self) -> &str { -- cgit v1.2.3