質問編集履歴
1
いらない部分の削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -119,82 +119,6 @@
|
|
119
119
|
|
120
120
|
}
|
121
121
|
|
122
|
-
|
123
|
-
async function whisper(player, wavData) {
|
124
|
-
const { Configuration, OpenAIApi } = require("openai");
|
125
|
-
const configuration = new Configuration({
|
126
|
-
apiKey: process.env.OPENAI_API_KEY,
|
127
|
-
});
|
128
|
-
const openai = new OpenAIApi(configuration);
|
129
|
-
|
130
|
-
try {
|
131
|
-
const data = await fs.promises.readFile(wavData);
|
132
|
-
const base64 = Buffer.from(data).toString('base64');
|
133
|
-
const resp = await openai.createTranscription(
|
134
|
-
fs.createReadStream(wavData,),
|
135
|
-
"whisper-1"
|
136
|
-
);
|
137
|
-
console.log(resp.data.text);
|
138
|
-
if ((resp.data.text ?? "").length > 3) {
|
139
|
-
try {
|
140
|
-
|
141
|
-
const { Configuration, OpenAIApi } = require("openai");
|
142
|
-
|
143
|
-
const configuration = new Configuration({
|
144
|
-
apiKey: process.env.OPENAI_API_KEY,
|
145
|
-
});
|
146
|
-
const openai = new OpenAIApi(configuration);
|
147
|
-
|
148
|
-
const completion = await openai.createChatCompletion({
|
149
|
-
model: "gpt-3.5-turbo",
|
150
|
-
temperature: 1,
|
151
|
-
max_tokens: 500,
|
152
|
-
messages: [{ role: "system", content: "botの設定、キャラ作りとか" }, { role: "user", content: "指示:必ず日本語で回答をしてください。\n内容:" + resp.data.text }],
|
153
|
-
});
|
154
|
-
client.channels.cache.find((ch) => ch.name === "general").send(completion.data.choices[0].message);
|
155
|
-
const responseText = completion.data.choices[0].message.content;
|
156
|
-
// 返答をGoogle Cloud Text-to-Speech APIで音声に変換して再生する
|
157
|
-
console.log(responseText)
|
158
|
-
const [responseAudio] = await ttsClient.synthesizeSpeech({
|
159
|
-
input: { text: responseText },
|
160
|
-
voice: { languageCode: 'ja-JP', name: "ja-JP-Standard-C", ssmlGender: 'MALE' },
|
161
|
-
audioConfig: { audioEncoding: 'MP3' },
|
162
|
-
});
|
163
|
-
|
164
|
-
// base64データを保存する
|
165
|
-
const now = new Date();
|
166
|
-
const buffer = Buffer.from(responseAudio.audioContent, 'base64');
|
167
|
-
const filename = `audio_${now.getHours()}${now.getMinutes()}${now.getSeconds()}.mp3`;
|
168
|
-
const url = `/tmp/${filename}`;
|
169
|
-
fs.writeFile(url, buffer, (err) => {
|
170
|
-
if (err) {
|
171
|
-
console.error(err);
|
172
|
-
} else {
|
173
|
-
console.log(`File saved as ${filename}`);
|
174
|
-
}
|
175
|
-
});
|
176
|
-
|
177
|
-
// Performs the text-to-speech request
|
178
|
-
// 音声ファイルを書き出します。
|
179
|
-
const writeFile = util.promisify(fs.writeFile);
|
180
|
-
await writeFile(url, responseAudio.audioContent, 'binary');
|
181
|
-
|
182
|
-
const resource2 = createAudioResource(url);
|
183
|
-
player.play(resource2);
|
184
|
-
|
185
|
-
} catch (error) {
|
186
|
-
if (error.response) {
|
187
|
-
console.log(error.response);
|
188
|
-
client.channels.cache.find((ch) => ch.name === "general").send("openAI落ちてね?" + error.response.status);
|
189
|
-
} else {
|
190
|
-
client.channels.cache.find((ch) => ch.name === "general").send(error.message);
|
191
|
-
}
|
192
|
-
}
|
193
|
-
}
|
194
|
-
|
195
|
-
} catch (err) {
|
196
|
-
console.error(err);
|
197
|
-
}
|
198
122
|
}
|
199
123
|
|
200
124
|
});
|