-
-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
121 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
import { embeddings } from "../../utils/embeddings"; | ||
import { DialoqbaseVectorStore } from "../../utils/store"; | ||
import { chatModelProvider } from "../../utils/models"; | ||
import { ConversationalRetrievalQAChain } from "langchain/chains"; | ||
const prisma = new PrismaClient(); | ||
|
||
export const telegramBotHandler = async ( | ||
identifer: string, | ||
message: string, | ||
user_id: number, | ||
) => { | ||
const bot_id = identifer.split("-")[2]; | ||
|
||
const bot = await prisma.bot.findFirst({ | ||
where: { | ||
id: bot_id, | ||
}, | ||
}); | ||
|
||
if (!bot) { | ||
return "Opps! Bot not found"; | ||
} | ||
|
||
const chat_history = await prisma.botTelegramHistory.findMany({ | ||
where: { | ||
chat_id: user_id, | ||
identifier: identifer, | ||
}, | ||
}); | ||
|
||
if (chat_history.length > 10) { | ||
chat_history.splice(0, chat_history.length - 10); | ||
} | ||
|
||
let history = chat_history.map((chat) => { | ||
return `Human: ${chat.human}\nAssistant: ${chat.bot}`; | ||
}).join("\n"); | ||
|
||
const temperature = bot.temperature; | ||
|
||
const sanitizedQuestion = message.trim().replaceAll("\n", " "); | ||
const embeddingModel = embeddings(bot.embedding); | ||
|
||
const vectorstore = await DialoqbaseVectorStore.fromExistingIndex( | ||
embeddingModel, | ||
{ | ||
botId: bot.id, | ||
sourceId: null, | ||
}, | ||
); | ||
|
||
const model = chatModelProvider( | ||
bot.provider, | ||
bot.model, | ||
temperature, | ||
); | ||
|
||
const chain = ConversationalRetrievalQAChain.fromLLM( | ||
model, | ||
vectorstore.asRetriever(), | ||
{ | ||
qaTemplate: bot.qaPrompt, | ||
questionGeneratorTemplate: bot.questionGeneratorPrompt, | ||
returnSourceDocuments: true, | ||
}, | ||
); | ||
|
||
const response = await chain.call({ | ||
question: sanitizedQuestion, | ||
chat_history: history, | ||
}); | ||
|
||
const bot_response = response["text"]; | ||
|
||
await prisma.botTelegramHistory.create({ | ||
data: { | ||
identifier: identifer, | ||
chat_id: user_id, | ||
human: message, | ||
bot: bot_response, | ||
}, | ||
}); | ||
|
||
return bot_response; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters