Skip to content

Commit

Permalink
Set API request timeout and adjust bot loop interval
Browse files Browse the repository at this point in the history
Added a timeout of 50 seconds to the API request settings for more reliable connections and to avoid rate limit issues. Also, increased the interval duration in the bot's main loop from 1000ms to 1200ms to optimize performance.
  • Loading branch information
kasugamirai committed Feb 18, 2024
1 parent 7163765 commit 6e4e07a
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
148 changes: 148 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ dotenv = "0.15"
env_logger = "0.11.2"
config = "0.14.0"
url = "2.5.0"
reqwest = "0.11.24"
1 change: 1 addition & 0 deletions src/bin/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async fn main() {
env::var("OPENAI_API_KEY").expect("Expected a OPEN AI key in the environment");
// Set the intents for the bot
let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::DIRECT_MESSAGES;

// Create a new client with the discord token
let mut client = Client::builder(&discord_token, intents)
.event_handler(Handler::new(gpt_api_key).await.unwrap())
Expand Down
4 changes: 3 additions & 1 deletion src/discord/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ impl Handler {
pub async fn new(api_key: String) -> Result<Self> {
let config: ModelConfiguration = ModelConfigurationBuilder::default()
.engine(ChatGPTEngine::Gpt4)
// Set the timeout for the API request
.timeout(Duration::from_secs(50))
.build()
.unwrap_or_else(|e| {
log::error!("Failed to build ModelConfiguration: {}", e);
Expand Down Expand Up @@ -54,7 +56,7 @@ impl EventHandler for Handler {
};

let mut result = String::new();
let mut interval = interval(Duration::from_millis(1000));
let mut interval = interval(Duration::from_millis(1200));

loop {
select! {
Expand Down

0 comments on commit 6e4e07a

Please sign in to comment.