ChatGPTでレスポンスを生成するようにした

This commit is contained in:
ntki72 2024-09-08 23:39:40 +09:00
parent 6a2c4663d3
commit 1022ab1462

37
bot.js
View File

@ -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);