Skip to content

Commit

Permalink
feat: add voice
Browse files Browse the repository at this point in the history
  • Loading branch information
leifermendez committed Mar 28, 2024
1 parent 678e40d commit c8dc403
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 20 deletions.
Binary file added file-1711615972612.oga
Binary file not shown.
Binary file added file-1711616007252.oga
Binary file not shown.
Binary file added file-1711616171723.oga
Binary file not shown.
Binary file added file-1711616227382.oga
Binary file not shown.
Binary file added file-1711616252320.oga
Binary file not shown.
Binary file added file-1711616395289.oga
Binary file not shown.
Binary file added file-1711616438382.oga
Binary file not shown.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
"license": "ISC",
"dependencies": {
"@builderbot-plugins/telegram": "^1.0.0",
"@builderbot/bot": "1.0.23-alpha.0",
"@builderbot/provider-baileys": "1.0.23-alpha.0",
"@builderbot/bot": "1.0.27-alpha.0",
"@builderbot/provider-baileys": "1.0.27-alpha.0",
"@ffmpeg-installer/ffmpeg": "^1.1.0",
"date-fns": "^3.3.1",
"date-fns-tz": "3.0.0-beta.2",
"dotenv": "^16.4.2",
"fluent-ffmpeg": "^2.1.2",
"multer": "1.4.5-lts.1",
"openai": "^4.27.0"
},
Expand Down
45 changes: 34 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/flows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { welcomeFlow } from "./welcome.flow";
import { flowSeller } from "./seller.flow";
import { flowSchedule } from "./schedule.flow";
import { flowConfirm } from "./confirm.flow";
import { voiceFlow } from "./voice.flow";


export default createFlow([
welcomeFlow,
flowSeller,
flowSchedule,
flowConfirm
flowConfirm,
voiceFlow
])
7 changes: 4 additions & 3 deletions src/flows/seller.flow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { addKeyword, EVENTS } from "@builderbot/bot";
import { generateTimer } from "../utils/generateTimer";
import { getHistoryParse, handleHistory } from "../utils/handleHistory";
import { getHistory, getHistoryParse, handleHistory } from "../utils/handleHistory";
import AIClass from "../services/ai";
import { getFullCurrentDate } from "src/utils/currentDate";
import { pdfQuery } from "src/services/pdf";
Expand Down Expand Up @@ -43,13 +43,14 @@ export const generatePromptSeller = (history: string, database: string) => {

const flowSeller = addKeyword(EVENTS.ACTION)
.addAnswer(`⏱️`)
.addAction(async (ctx, { state, flowDynamic, extensions }) => {
.addAction(async (_, { state, flowDynamic, extensions }) => {
try {

const ai = extensions.ai as AIClass
const lastMessage = getHistory(state).at(-1)
const history = getHistoryParse(state)

const dataBase = await pdfQuery(ctx.body)
const dataBase = await pdfQuery(lastMessage.content)
console.log({ dataBase })
const promptInfo = generatePromptSeller(history, dataBase)

Expand Down
26 changes: 26 additions & 0 deletions src/flows/voice.flow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { EVENTS, addKeyword } from "@builderbot/bot";
import { BaileysProvider } from "@builderbot/provider-baileys";
import AIClass from "src/services/ai";
import { processAudio } from "src/utils/process";
import { welcomeFlow } from "./welcome.flow";
import mainLayer from "src/layers/main.layer";
import { handleHistory } from "src/utils/handleHistory";

const voiceFlow = addKeyword<BaileysProvider>(EVENTS.VOICE_NOTE)
.addAction(async (_, { flowDynamic }) => {
await flowDynamic(`dame un momento para esucharte...`)
})
.addAction(async (ctx: any, { provider, extensions, gotoFlow, state }) => {
try{
const ai = extensions.ai as AIClass
const pathVoice = await provider.saveFile(ctx)
const mp3Path = await processAudio(pathVoice)
const text = await ai.voiceToText(mp3Path)
await handleHistory({ content: text, role: 'user' }, state)
return gotoFlow(welcomeFlow)
}catch(e){
console.log(`Err`,e)}
})
.addAction(mainLayer)

export { voiceFlow }
1 change: 1 addition & 0 deletions src/layers/conversational.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import { handleHistory } from "../utils/handleHistory";
* Su funcion es almancenar en el state todos los mensajes que el usuario escriba
*/
export default async ({ body }: BotContext, { state, }: BotMethods) => {
if(body.includes('_event_')) return
await handleHistory({ content: body, role: 'user' }, state)
}
6 changes: 3 additions & 3 deletions src/layers/main.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ Por favor, analiza la siguiente conversación y determina la intención del usua
export default async (_: BotContext, { state, gotoFlow, extensions }: BotMethods) => {
const ai = extensions.ai as AIClass
const history = getHistoryParse(state)
const prompt = PROMPT_DISCRIMINATOR
const prompt = PROMPT_DISCRIMINATOR.replace('{HISTORY}', history)


console.log(prompt.replace('{HISTORY}', history))
console.log(prompt)

const { prediction } = await ai.determineChatFn([
{
role: 'system',
content: prompt.replace('{HISTORY}', history)
content: prompt
}
])

Expand Down
27 changes: 27 additions & 0 deletions src/utils/process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import ffmpeg from "fluent-ffmpeg";
import {join} from "path";
import {tmpdir} from "os";
import { path as ffmpegPath } from '@ffmpeg-installer/ffmpeg';
ffmpeg.setFfmpegPath(ffmpegPath);

const convertOggMp3 = async (inputStream: string, outStream: string) => {
return new Promise((resolve) => {
ffmpeg(inputStream)
.audioQuality(96)
.toFormat("mp3")
.save(outStream)
.on("progress", () => null)
.on("end", () => {
resolve(true);
});
});
};

/**
* Return mp3 path
*/
export const processAudio = async (pathOgg: string ) => {
const pathTmpMp3 = join(tmpdir(),`voice-note-${Date.now()}.mp3`);
await convertOggMp3(pathOgg, pathTmpMp3);
return pathTmpMp3
}

0 comments on commit c8dc403

Please sign in to comment.