Skip to content

Commit

Permalink
Refactor chatgpt::client::ChatGPT and chatgpt::config::ModelConfigura…
Browse files Browse the repository at this point in the history
…tionBuilder in src/discord/bot.rs
  • Loading branch information
kasugamirai committed Apr 1, 2024
1 parent b4278ff commit 1895f6c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/bin/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ async fn main() {
// Set the intents for the bot
let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::DIRECT_MESSAGES;

let handler_result = Handler::new(&gpt_api_key);
let handler = match handler_result {
Ok(handler) => handler,
Err(e) => panic!("Error creating handler: {}", e),
};

// Create a new client with the discord token
let mut client = match Client::builder(&discord_token, intents)
.event_handler(Handler::new(&gpt_api_key))
.event_handler(handler)
.await
{
Ok(client) => client,
Expand Down
48 changes: 38 additions & 10 deletions src/discord/bot.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
use chatgpt::prelude::*;
use chatgpt::client::ChatGPT;
use chatgpt::config::{ChatGPTEngine, ModelConfiguration, ModelConfigurationBuilder};
use chatgpt::types::ResponseChunk;
use core::fmt;
use futures::StreamExt;
use serenity::client::{Context, EventHandler as SerenityEventHandler};
use serenity::{async_trait, builder::EditMessage, model::channel::Message};
use std::time::Duration;
use tokio::select;
use tokio::time::interval;

#[derive(Debug)]
pub enum Error {
ModelConfigurationBuilderError(chatgpt::config::ModelConfigurationBuilderError),
ChatGPT(chatgpt::err::Error),
}

impl From<chatgpt::config::ModelConfigurationBuilderError> for Error {
fn from(err: chatgpt::config::ModelConfigurationBuilderError) -> Self {
Error::ModelConfigurationBuilderError(err)
}
}

impl From<chatgpt::err::Error> for Error {
fn from(err: chatgpt::err::Error) -> Self {
Error::ChatGPT(err)
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::ModelConfigurationBuilderError(err) => {
write!(f, "ModelConfigurationBuilderError: {}", err)
}
Error::ChatGPT(err) => write!(f, "ChatGPTError: {}", err),
}
}
}

// Define a trait representing handler behavior
#[async_trait]
pub trait ChatHandler {
// Create a new instance of the handler
fn new(api_key: &String) -> Self
fn new(api_key: &String) -> Result<Self, Error>
where
Self: Sized;

Expand All @@ -25,17 +57,13 @@ pub struct Handler {

#[async_trait]
impl ChatHandler for Handler {
fn new(api_key: &String) -> Self {
fn new(api_key: &String) -> Result<Self, Error> {
let config: ModelConfiguration = ModelConfigurationBuilder::default()
.engine(ChatGPTEngine::Gpt4)
.timeout(Duration::from_secs(50))
.build()
.unwrap_or_else(|e| {
log::error!("Failed to build ModelConfiguration: {}", e);
ModelConfiguration::default()
});
let gpt_client = ChatGPT::new_with_config(api_key, config).unwrap();
Self { gpt_client }
.build()?;
let gpt_client = ChatGPT::new_with_config(api_key, config)?;
Ok(Handler { gpt_client })
}

async fn process_message(&self, msg: &Message) -> Option<String> {
Expand Down

0 comments on commit 1895f6c

Please sign in to comment.