Skip to content

Commit

Permalink
Remove /palm (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
DuckyBlender authored Dec 15, 2024
1 parent 1906d6e commit 0364069
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 142 deletions.
65 changes: 0 additions & 65 deletions src/apis/makersuite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,6 @@ pub struct SafetyRating {
pub blocked: bool,
}

#[derive(Deserialize)]
pub struct ContentFilter {
pub reason: String,
pub message: Option<String>,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Google error {}: {}", self.code, self.message)
Expand Down Expand Up @@ -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<Vec<TextCompletionResponse>>,
pub filters: Option<Vec<ContentFilter>>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TextCompletionResponse {
pub output: String,
pub citation_metadata: Option<CitationMetadata>,
}

pub async fn generate_text(
http_client: reqwest::Client,
prompt: &str,
max_output_tokens: u16,
) -> Result<Result<GenerateTextResponse, Error>, 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::<ErrorResponse>().await?.error))
}
}
76 changes: 0 additions & 76 deletions src/commands/makersuite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64> {
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::<Vec<_>>()
.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<PartResponse>,
finish_reason: Option<String>,
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 0364069

Please sign in to comment.