discord_openai/discord/commands/
davinci.rs

1use serenity::builder::CreateApplicationCommand;
2use serenity::model::prelude::command::CommandOptionType;
3use serenity::model::prelude::interaction::application_command::{
4    CommandDataOption, CommandDataOptionValue,
5};
6
7use std::sync::Arc;
8
9pub const DAVINCI_ERROR_MSG: &str = "Something went wrong when talking to DAVINCI";
10pub const NOT_SURE_HOW_YOU_GOT_HERE_ERROR: &str = "I'm not sure how you managed to send this request without a prompt. Not even an empty string. Congratulations, I think?";
11pub const RESOLUTION_ERROR: &str =
12    "Something went wrong while trying to process your discord message";
13pub const NO_PROMPT_PROVIDED: &str = "Please supply a prompt with your message";
14
15pub async fn run(
16    options: &[CommandDataOption],
17    open_ai_client: Arc<openairs::client::OpenAIClient>,
18) -> String {
19    if let Some(command_data_option) = options.get(0) {
20        if let Some(command_data_option_value) = &command_data_option.resolved {
21            if let CommandDataOptionValue::String(prompt) = command_data_option_value {
22                if prompt.is_empty() {
23                    return NO_PROMPT_PROVIDED.to_owned();
24                }
25
26                match open_ai_client
27                    .complete(&openairs::models::TEXT_DAVINCI_003, prompt)
28                    .await
29                {
30                    Ok(response) => response.choices[0].text.to_owned(),
31                    Err(_) => DAVINCI_ERROR_MSG.to_owned(),
32                }
33            } else {
34                NOT_SURE_HOW_YOU_GOT_HERE_ERROR.to_owned()
35            }
36        } else {
37            RESOLUTION_ERROR.to_owned()
38        }
39    } else {
40        NO_PROMPT_PROVIDED.to_owned()
41    }
42}
43
44pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
45    command
46        .name("davinci")
47        .description("Ask Davinci anything")
48        .create_option(|option| {
49            option
50                .name("prompt")
51                .description("Supply any prompt to Davinci")
52                .kind(CommandOptionType::String)
53                .required(true)
54        })
55}