From 036406911a325773a0370054df8175820669885c Mon Sep 17 00:00:00 2001 From: DuckyBlender Date: Sun, 15 Dec 2024 09:54:54 +0100 Subject: [PATCH] Remove /palm (#26) --- src/apis/makersuite.rs | 65 -------------------------------- src/commands/makersuite.rs | 76 -------------------------------------- src/main.rs | 1 - 3 files changed, 142 deletions(-) diff --git a/src/apis/makersuite.rs b/src/apis/makersuite.rs index f6e22da..b05f582 100644 --- a/src/apis/makersuite.rs +++ b/src/apis/makersuite.rs @@ -211,12 +211,6 @@ pub struct SafetyRating { pub blocked: bool, } -#[derive(Deserialize)] -pub struct ContentFilter { - pub reason: String, - pub message: Option, -} - impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Google error {}: {}", self.code, self.message) @@ -319,66 +313,7 @@ pub async fn stream_generate_content( tx.send(Ok(serde_json::from_str(&String::from_utf8_lossy(&buffer)).unwrap())).unwrap(); } -#[derive(Serialize)] -#[serde(rename_all = "camelCase")] -struct GenerateTextRequest<'a> { - prompt: TextPrompt<'a>, - safety_settings: &'a [SafetySetting], - max_output_tokens: u16, -} - #[derive(Serialize)] pub struct TextPrompt<'a> { text: &'a str, } - -#[derive(Deserialize)] -pub struct GenerateTextResponse { - pub candidates: Option>, - pub filters: Option>, -} - -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct TextCompletionResponse { - pub output: String, - pub citation_metadata: Option, -} - -pub async fn generate_text( - http_client: reqwest::Client, - prompt: &str, - max_output_tokens: u16, -) -> Result, CommandError> { - let response = http_client - .post( - Url::parse_with_params( - concat!( - "https://generativelanguage.googleapis.com", - "/v1beta/models/text-bison-001:generateText" - ), - [("key", env::var("MAKERSUITE_API_KEY").unwrap())], - ) - .unwrap(), - ) - .json(&GenerateTextRequest { - prompt: TextPrompt { text: prompt }, - max_output_tokens, - safety_settings: &[ - SafetySetting { category: "HARM_CATEGORY_DEROGATORY", threshold: "BLOCK_NONE" }, - SafetySetting { category: "HARM_CATEGORY_TOXICITY", threshold: "BLOCK_NONE" }, - SafetySetting { category: "HARM_CATEGORY_VIOLENCE", threshold: "BLOCK_NONE" }, - SafetySetting { category: "HARM_CATEGORY_SEXUAL", threshold: "BLOCK_NONE" }, - SafetySetting { category: "HARM_CATEGORY_MEDICAL", threshold: "BLOCK_NONE" }, - SafetySetting { category: "HARM_CATEGORY_DANGEROUS", threshold: "BLOCK_NONE" }, - ], - }) - .send() - .await?; - - if response.status() == StatusCode::OK { - Ok(Ok(response.json().await?)) - } else { - Ok(Err(response.json::().await?.error)) - } -} diff --git a/src/commands/makersuite.rs b/src/commands/makersuite.rs index d7bcae5..b7eb7ac 100644 --- a/src/commands/makersuite.rs +++ b/src/commands/makersuite.rs @@ -199,82 +199,6 @@ impl CommandTrait for GoogleGemini { Ok(()) } } - -pub struct GooglePalm; - -#[async_trait] -impl CommandTrait for GooglePalm { - fn command_names(&self) -> &[&str] { - &["palm", "palm2"] - } - - fn description(&self) -> Option<&'static str> { - Some("ask Google PaLM 2 (Legacy)") - } - - fn rate_limit(&self) -> RateLimiter { - RateLimiter::new(3, 45) - } - - async fn execute(&self, ctx: &CommandContext, arguments: String) -> CommandResult { - let StringGreedyOrReply(prompt) = ConvertArgument::convert(ctx, &arguments).await?.0; - - ctx.send_typing().await?; - - let response = - makersuite::generate_text(ctx.bot_state.http_client.clone(), &prompt, 512).await?; - - let response = match response { - Ok(response) => response, - Err(response) => { - return Err(CommandError::Custom(response.to_string())); - } - }; - - if let Some(filters) = response.filters { - let reasons = filters - .into_iter() - .map(|filter| { - if let Some(message) = filter.message { - format!("{}: {message}", filter.reason) - } else { - filter.reason - } - }) - .collect::>() - .join(", "); - - ctx.reply(format!("request blocked by Google: {reasons}.")).await?; - return Ok(()); - } - - let Some(candidate) = - response.candidates.and_then(|candidates| candidates.into_iter().next()) - else { - return Err(CommandError::Custom("no response generated.".into())); - }; - - if candidate.output.is_empty() { - return Err(CommandError::Custom("no text generated.".into())); - } - - let mut text = candidate.output; - - if let Some(citation_metadata) = candidate.citation_metadata { - text.push_str("\n\n"); - text.push_str(&format_citations(&citation_metadata.citation_sources)); - } - - let enums::FormattedText::FormattedText(formatted_text) = - functions::parse_markdown(FormattedText { text, ..Default::default() }, ctx.client_id) - .await?; - - ctx.reply_formatted_text(formatted_text).await?; - - Ok(()) - } -} - struct GenerationProgress { parts: Vec, finish_reason: Option, diff --git a/src/main.rs b/src/main.rs index 05fc07f..909a55f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,6 @@ async fn main() { bot.add_command(commands::different_dimension_me::DifferentDimensionMe); bot.add_command(commands::makersuite::GoogleGemini::gemini()); bot.add_command(commands::makersuite::GoogleGemini::gemini2()); - bot.add_command(commands::makersuite::GooglePalm); bot.add_command(commands::groq::Llama); bot.add_command(commands::translate::Translate); bot.add_command(commands::badtranslate::BadTranslate);