-
-
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.
- Loading branch information
Showing
20 changed files
with
323 additions
and
406 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use crate::model::chatgpt::{RequestMessageModel, ResponseCompletionResultModel}; | ||
use anyhow::Context; | ||
use async_openai::config::OpenAIConfig; | ||
use async_openai::types::CreateChatCompletionRequestArgs; | ||
use async_openai::Client; | ||
use std::time::Duration; | ||
use tokio::time::timeout; | ||
|
||
pub static SYSTEM_CONTEXT: &str = "回答時は以下のルールに従うこと.\n- 1900文字以内に収めること。"; | ||
static TIMEOUT_DURATION: Duration = Duration::from_secs(180); | ||
|
||
async fn create_chatgpt_client() -> anyhow::Result<Client<OpenAIConfig>> { | ||
Ok(Client::new()) | ||
} | ||
|
||
pub async fn request_chatgpt_message( | ||
request: RequestMessageModel, | ||
) -> anyhow::Result<ResponseCompletionResultModel> { | ||
let client = create_chatgpt_client().await?; | ||
|
||
let client_request = CreateChatCompletionRequestArgs::default() | ||
.max_tokens(512u16) | ||
.model(request.model) | ||
.messages(request.replies) | ||
.build()?; | ||
|
||
let response = timeout(TIMEOUT_DURATION, client.chat().create(client_request)) | ||
.await | ||
.context("Timeout. Please try again.")??; | ||
|
||
let choice = response | ||
.choices | ||
.get(0) | ||
.context("No response message found.")?; | ||
let (input_token, output_token, total_token) = response | ||
.usage | ||
.map(|usage| { | ||
( | ||
usage.prompt_tokens, | ||
usage.completion_tokens, | ||
usage.total_tokens, | ||
) | ||
}) | ||
.unwrap_or_default(); | ||
|
||
Ok(ResponseCompletionResultModel::builder() | ||
.response_message(choice.message.content.clone().unwrap_or_default()) | ||
.input_token(input_token) | ||
.output_token(output_token) | ||
.total_token(total_token) | ||
.build()) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use crate::model::chatgpt::{usage_pricing, ResponseCompletionResultModel}; | ||
use crate::model::discord::DiscordReplyMessageModel; | ||
use anyhow::Context; | ||
|
||
pub async fn reply_completion_result( | ||
reply_message: DiscordReplyMessageModel, | ||
) -> anyhow::Result<()> { | ||
reply_message | ||
.target_message | ||
.reply_ping(reply_message.http, reply_message.formatted_result) | ||
.await | ||
.context("Failed to reply.")?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn format_result(result: ResponseCompletionResultModel, model: &str) -> String { | ||
let pricing = usage_pricing(result.input_token, result.output_token, model); | ||
format!( | ||
"{}\n\n`利用料金: ¥{:.2}` - `合計トークン: {}` - `使用モデル: {}`", | ||
result.response_message, pricing, result.total_token, model | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod chatgpt; | ||
|
||
pub mod discord; |
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,13 @@ | ||
use crate::model::EvHandler; | ||
use anyhow::Context; | ||
use serenity::{prelude::GatewayIntents, Client}; | ||
|
||
pub struct EvHandler; | ||
|
||
pub async fn start_discord_client(token: &str) -> anyhow::Result<()> { | ||
// メッセージ内容の取得とギルドメッセージの取得を有効化 | ||
pub async fn create_discord_client(token: &str) -> anyhow::Result<Client> { | ||
let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT; | ||
|
||
let mut client = Client::builder(token, intents) | ||
let client = Client::builder(token, intents) | ||
.event_handler(EvHandler) | ||
.await | ||
.context("クライアントの作成に失敗しました.")?; | ||
.context("Failed to create discord client")?; | ||
|
||
client | ||
.start() | ||
.await | ||
.context("クライアントの起動に失敗しました.") | ||
Ok(client) | ||
} |
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,2 +1 @@ | ||
pub mod discord; | ||
pub mod openai; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.