From 527da63d18def9cec29c89387de988b43acb7d24 Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 22 Mar 2024 19:24:22 +0800 Subject: refactor: do not automatically convert temperature value (#368) --- src/client/claude.rs | 2 +- src/client/ernie.rs | 30 +++++++++++++++++------------- src/client/openai.rs | 2 +- src/client/qianwen.rs | 9 +++++++-- 4 files changed, 26 insertions(+), 17 deletions(-) (limited to 'src/client') diff --git a/src/client/claude.rs b/src/client/claude.rs index ea17f66..d1b39d7 100644 --- a/src/client/claude.rs +++ b/src/client/claude.rs @@ -204,7 +204,7 @@ fn build_body(data: SendData, model: String) -> Result { }); if let Some(v) = temperature { - body["temperature"] = (v / 2.0).into(); + body["temperature"] = v.into(); } if stream { body["stream"] = true.into(); diff --git a/src/client/ernie.rs b/src/client/ernie.rs index caa61e7..5c8b563 100644 --- a/src/client/ernie.rs +++ b/src/client/ernie.rs @@ -5,11 +5,12 @@ use crate::{render::ReplyHandler, utils::PromptKind}; use anyhow::{anyhow, bail, Context, Result}; use async_trait::async_trait; use futures_util::StreamExt; +use lazy_static::lazy_static; use reqwest::{Client as ReqwestClient, RequestBuilder}; use reqwest_eventsource::{Error as EventSourceError, Event, RequestBuilderExt}; use serde::Deserialize; use serde_json::{json, Value}; -use std::env; +use std::{env, sync::Mutex}; const API_BASE: &str = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1"; const ACCESS_TOKEN_URL: &str = "https://aip.baidubce.com/oauth/2.0/token"; @@ -36,7 +37,9 @@ const MODELS: [(&str, usize, &str); 6] = [ ("ernie-lite-8k", 7168, "/wenxinworkshop/chat/ernie-lite-8k"), ]; -static mut ACCESS_TOKEN: String = String::new(); // safe under linear operation +lazy_static! { + static ref ACCESS_TOKEN: Mutex> = Mutex::new(None); +} #[derive(Debug, Clone, Deserialize, Default)] pub struct ErnieConfig { @@ -78,9 +81,7 @@ impl ErnieClient { let client_name = Self::name(local_config); MODELS .into_iter() - .map(|(name, max_input_tokens, _)| { - Model::new(client_name, name).set_max_input_tokens(Some(max_input_tokens)) - }) + .map(|(name, _, _)| Model::new(client_name, name)) // ERNIE tokenizer is different from cl100k_base .collect() } @@ -93,9 +94,13 @@ impl ErnieClient { .find(|(v, _, _)| v == &model) .ok_or_else(|| anyhow!("Miss Model '{}'", self.model.id()))?; - let url = format!("{API_BASE}{chat_endpoint}?access_token={}", unsafe { - &ACCESS_TOKEN - }); + let access_token = ACCESS_TOKEN + .lock() + .unwrap() + .clone() + .ok_or_else(|| anyhow!("Failed to load access token"))?; + + let url = format!("{API_BASE}{chat_endpoint}?access_token={access_token}"); debug!("Ernie Request: {url} {body}"); @@ -105,8 +110,7 @@ impl ErnieClient { } async fn prepare_access_token(&self) -> Result<()> { - if unsafe { ACCESS_TOKEN.is_empty() } { - // Note: cannot use config_get_fn! + if ACCESS_TOKEN.lock().unwrap().is_none() { let env_prefix = Self::name(&self.config).to_uppercase(); let api_key = self.config.api_key.clone(); let api_key = api_key @@ -122,7 +126,7 @@ impl ErnieClient { let token = fetch_access_token(&client, &api_key, &secret_key) .await .with_context(|| "Failed to fetch access token")?; - unsafe { ACCESS_TOKEN = token }; + *ACCESS_TOKEN.lock().unwrap() = Some(token); } Ok(()) } @@ -189,7 +193,7 @@ fn check_error(data: &Value) -> Result<()> { if let Some(err_msg) = data["error_msg"].as_str() { if let Some(code) = data["error_code"].as_number().and_then(|v| v.as_u64()) { if code == 110 { - unsafe { ACCESS_TOKEN = String::new() } + *ACCESS_TOKEN.lock().unwrap() = None; } bail!("{err_msg}. err_code: {code}"); } else { @@ -213,7 +217,7 @@ fn build_body(data: SendData, _model: String) -> Value { }); if let Some(temperature) = temperature { - body["temperature"] = (temperature / 2.0).into(); + body["temperature"] = temperature.into(); } if stream { body["stream"] = true.into(); diff --git a/src/client/openai.rs b/src/client/openai.rs index 2c8aabf..1abdd48 100644 --- a/src/client/openai.rs +++ b/src/client/openai.rs @@ -115,7 +115,7 @@ pub async fn openai_send_message_streaming( bail!("{err_msg}"); } else { bail!("Request failed, {data}"); - } + } } EventSourceError::StreamEnded => {} _ => { diff --git a/src/client/qianwen.rs b/src/client/qianwen.rs index 1858030..3e69dae 100644 --- a/src/client/qianwen.rs +++ b/src/client/qianwen.rs @@ -1,4 +1,6 @@ -use super::{message::*, Client, ExtraConfig, Model, PromptType, QianwenClient, SendData}; +use super::{ + message::*, Client, ExtraConfig, Model, PromptType, QianwenClient, SendData, TokensCountFactors, +}; use crate::{ render::ReplyHandler, @@ -35,6 +37,8 @@ const MODELS: [(&str, usize, &str); 6] = [ ("qwen-vl-max", 0, "text,vision"), ]; +const TOKENS_COUNT_FACTORS: TokensCountFactors = (4, 14); + #[derive(Debug, Clone, Deserialize, Default)] pub struct QianwenConfig { pub name: Option, @@ -84,6 +88,7 @@ impl QianwenClient { Model::new(client_name, name) .set_capabilities(capabilities.into()) .set_max_input_tokens(Some(max_input_tokens)) + .set_tokens_count_factors(TOKENS_COUNT_FACTORS) }) .collect() } @@ -221,7 +226,7 @@ fn build_body(data: SendData, model: String, is_vl: bool) -> Result<(Value, bool let mut parameters = json!({}); if let Some(v) = temperature { - parameters["top_k"] = ((v * 50.0).round() as usize).into(); + parameters["temperature"] = v.into(); } (input, parameters) } else { -- cgit v1.2.3