From 1022ab14625dde01b50f2f1a210b7f5c2310316d Mon Sep 17 00:00:00 2001 From: ntki72 Date: Sun, 8 Sep 2024 23:39:40 +0900 Subject: [PATCH] =?UTF-8?q?ChatGPT=E3=81=A7=E3=83=AC=E3=82=B9=E3=83=9D?= =?UTF-8?q?=E3=83=B3=E3=82=B9=E3=82=92=E7=94=9F=E6=88=90=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.js | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/bot.js b/bot.js index bf8ab6e..6bec12d 100644 --- a/bot.js +++ b/bot.js @@ -174,9 +174,19 @@ async function transcribeAudio(filePath) { response_format: 'json' // レスポンス形式をJSONに指定 }); - // 文字起こし結果を表示 + // 文字起こし結果を取得 if (response && response.text) { - console.log('Transcription:', response.text); + const transcription = response.text; + console.log('Transcription:', transcription); + + // ChatGPTを使用して返答を生成 + const reply = await generateChatResponse(transcription); + console.log('ChatGPT response:', reply); + + // 必要に応じて、返信内容をメッセージとして送信 + // 例えば、メッセージを元のチャンネルに送信するなど + // message.channel.send(reply); // メッセージを送信する場合 + } else { console.error('Error: Transcription response is missing "text" field.'); } @@ -185,4 +195,27 @@ async function transcribeAudio(filePath) { } } +// ChatGPTに応答を生成させる関数 +async function generateChatResponse(transcription) { + try { + const chatResponse = await openai.chat.completions.create({ + model: 'gpt-3.5-turbo', // ChatGPTのモデルを指定 + messages: [ + { role: 'system', content: 'You are a helpful assistant.' }, + { role: 'user', content: transcription } + ] + }); + + if (chatResponse && chatResponse.choices && chatResponse.choices.length > 0) { + return chatResponse.choices[0].message.content; // 応答内容を返す + } else { + console.error('Error: ChatGPT response is missing choices.'); + return null; + } + } catch (error) { + console.error('Error during ChatGPT response generation:', error.response ? error.response.data : error.message); + return null; + } +} + client.login(process.env.BOT_TOKEN);