Skip to content

Commit

Permalink
Merge pull request #9 from approvers/feat/gpt-4
Browse files Browse the repository at this point in the history
feat: GPT-4のモデルをサポート
  • Loading branch information
Sho Sakuma authored Jul 28, 2023
2 parents 2f12a1c + a3df875 commit 16036c1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
7 changes: 7 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 @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.72"
chatgpt_rs = { version = "1.1" }
once_cell = "1.18"
tokio = { version = "1.29", features = ["macros", "rt-multi-thread"] }
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ichiyoAI が起動しているBotに対してチャット内容と共にメン

- ChatGPT からのレスポンスが2000文字を超えた場合はテキストファイルに変換して返信します。
- メッセージのどの位置にメンションを挿入しても反応します。
- デフォルトで `GPT-4` をモデルとして使用します。

## Installation

Expand Down
23 changes: 15 additions & 8 deletions src/api/chatgpt.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
use std::env;

use chatgpt::prelude::ChatGPT;
use chatgpt::config::ModelConfigurationBuilder;
use chatgpt::prelude::{ChatGPT, ChatGPTEngine};
use chatgpt::types::CompletionResponse;
use once_cell::sync::Lazy;

static CHATGPT_API_TOKEN: Lazy<String> = Lazy::new(|| {
env::var("CHATGPT_API_TOKEN").expect("Expected a token in the environment (CHATGPT_API_TOKEN)")
});

pub async fn chat_completion(message: &String) -> CompletionResponse {
let client =
ChatGPT::new(&*CHATGPT_API_TOKEN).expect("ChatGPI APIのクライアント初期化に失敗しました。");
pub async fn chat_completion(
message: &str,
model: Option<ChatGPTEngine>,
) -> chatgpt::Result<CompletionResponse> {
let model = model.unwrap_or(ChatGPTEngine::Gpt35Turbo);
let client = ChatGPT::new_with_config(
&*CHATGPT_API_TOKEN,
ModelConfigurationBuilder::default()
.engine(model)
.build()
.unwrap(),
)?;

client
.send_message(message)
.await
.expect("ChatGPI APIとのチャットに失敗しました。")
client.send_message(message).await
}
15 changes: 14 additions & 1 deletion src/events/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::events::message::chat_ai;

pub(crate) struct Handler;

const ADMINISTRATOR: u64 = 586824421470109716;

#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
Expand All @@ -15,7 +17,18 @@ impl EventHandler for Handler {
}

if let Ok(true) = msg.mentions_me(&ctx).await {
chat_ai(&ctx, msg).await;
if let Err(why) = chat_ai(&ctx, &msg).await {
let _ = msg
.channel_id
.send_message(&ctx, |m| {
m.content(&format!(
"<@{}> エラーが発生しました。ログを確認してください。",
ADMINISTRATOR
))
})
.await;
eprintln!("{:?}", why)
}
}
}

Expand Down
16 changes: 11 additions & 5 deletions src/events/message.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
use anyhow::Context as _;
use chatgpt::config::ChatGPTEngine;
use serenity::model::prelude::Message;
use serenity::prelude::Context;

use crate::api::chatgpt::chat_completion;
use crate::api::discord::{edit_response, edit_response_with_file, reply};
use crate::utils::create_temp_file;

pub async fn chat_ai(ctx: &Context, msg: Message) {
let waiting_message = reply(ctx, &msg, "思考中... 🤔").await;
pub async fn chat_ai(ctx: &Context, msg: &Message) -> anyhow::Result<()> {
let waiting_message = reply(ctx, msg, "思考中... 🤔").await;

let response = chat_completion(&msg.content).await;
let response = chat_completion(&msg.content, Some(ChatGPTEngine::Gpt4))
.await
.context("ChatGPT APIとのやり取りに失敗しました。")?;
let response_content = &response.message().content;

match response_content.chars().count() {
count if count > 2000 => {
create_temp_file(response_content.to_string());
edit_response_with_file(&ctx, waiting_message, "temp/temp.txt").await;
edit_response_with_file(ctx, waiting_message, "temp/temp.txt").await;
}
_ => edit_response(&ctx, waiting_message, response_content).await,
_ => edit_response(ctx, waiting_message, response_content).await,
}

Ok(())
}

0 comments on commit 16036c1

Please sign in to comment.