-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from approvers/feat/gpt-4
feat: GPT-4のモデルをサポート
- Loading branch information
Showing
6 changed files
with
49 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |