Skip to content

Commit

Permalink
add tts to chatgpt
Browse files Browse the repository at this point in the history
  • Loading branch information
ender-null committed Apr 14, 2024
1 parent e17fc81 commit c2ee1b5
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions src/plugins/chatgpt.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import OpenAI from 'openai';
import crypto from 'crypto';
import tmp from 'tmp';
import fs from 'fs';
import { Bot, Message } from '..';
import { PluginBase } from '../plugin';
import { generateCommandHelp, getInput } from '../utils';
import { generateCommandHelp, getInput, isCommand, logger } from '../utils';

export class ChatGPTPlugin extends PluginBase {
constructor(bot: Bot) {
Expand All @@ -21,6 +23,22 @@ export class ChatGPTPlugin extends PluginBase {
friendly: `^@${this.bot.user.username}`,
description: 'Use ChatGPT to provide text outputs in response to inputs',
},
{
command: '/voice',
friendly: '^say ',
aliases: ['/v', '/tts'],
parameters: [
{
name: 'language',
required: false,
},
{
name: 'text',
required: true,
},
],
description: 'Sends an audio file with the input',
},
];
}

Expand All @@ -30,18 +48,35 @@ export class ChatGPTPlugin extends PluginBase {
return this.bot.replyMessage(msg, generateCommandHelp(this, msg.content));
}

const messages = [
{ role: 'system', content: this.bot.config.apiKeys.openAIPrompt },
{ role: 'user', content: input },
];

const openai = new OpenAI({ apiKey: this.bot.config.apiKeys.openAIKey });
const completion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
user: crypto.createHash('md5').update(String(msg.sender.id)).digest('hex'),
messages: messages as any,
});

this.bot.replyMessage(msg, completion.choices[0].message.content, 'text', null, { format: 'Markdown' });
if (isCommand(this, 1, msg.content)) {
const messages = [{ role: 'system', content: this.bot.config.apiKeys.openAIPrompt }];
let message = msg;
while (message.reply) {
messages.splice(1, 0, { role: 'user', content: message.reply.content });
message = message.reply;
}
messages.push({ role: 'user', content: input });
logger.info(JSON.stringify(messages, null, 4))

const completion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
user: crypto.createHash('md5').update(String(msg.sender.id)).digest('hex'),
messages: messages as any,
});

this.bot.replyMessage(msg, completion.choices[0].message.content, 'text', null, { format: 'Markdown' });
} else if (isCommand(this, 2, msg.content)) {
const speechFile = tmp.fileSync({ mode: 0o644, postfix: `.ogg` });
const opus = await openai.audio.speech.create({
model: 'tts-1',
voice: 'nova',
input,
});
const buffer = Buffer.from(await opus.arrayBuffer());
await fs.promises.writeFile(speechFile.name, buffer);
this.bot.replyMessage(msg, speechFile.name, 'voice');
}
}
}

0 comments on commit c2ee1b5

Please sign in to comment.