From 1277b301e038cc543d4964741b10032518ec0a1a Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Fri, 26 Jul 2024 00:11:02 +0530 Subject: [PATCH] chore: Update package.json with date-fns dependency version 3.6.0 --- server/package.json | 1 + .../20240725173556_auto_reset/migration.sql | 3 +++ server/prisma/schema.prisma | 2 ++ .../integration/handlers/discord.handler.ts | 13 +++++++++++++ .../integration/handlers/telegram.handler.ts | 18 +++++++++++++++++- .../integration/handlers/whatsapp.handler.ts | 15 +++++++++++++++ server/yarn.lock | 5 +++++ 7 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 server/prisma/migrations/20240725173556_auto_reset/migration.sql diff --git a/server/package.json b/server/package.json index d12daf84..610ee509 100644 --- a/server/package.json +++ b/server/package.json @@ -62,6 +62,7 @@ "concurrently": "^7.0.0", "copyfiles": "^2.4.1", "d3-dsv": "2", + "date-fns": "^3.6.0", "discord.js": "^14.11.0", "fastify": "^4.26.2", "fastify-cli": "6.1.1", diff --git a/server/prisma/migrations/20240725173556_auto_reset/migration.sql b/server/prisma/migrations/20240725173556_auto_reset/migration.sql new file mode 100644 index 00000000..4fd4633d --- /dev/null +++ b/server/prisma/migrations/20240725173556_auto_reset/migration.sql @@ -0,0 +1,3 @@ +-- AlterTable +ALTER TABLE "Bot" ADD COLUMN "autoResetSession" BOOLEAN NOT NULL DEFAULT false, +ADD COLUMN "inactivityTimeout" INTEGER DEFAULT 3600; diff --git a/server/prisma/schema.prisma b/server/prisma/schema.prisma index 0c25f2c7..413368c1 100644 --- a/server/prisma/schema.prisma +++ b/server/prisma/schema.prisma @@ -29,6 +29,8 @@ model Bot { embedding String @default("openai") streaming Boolean @default(false) showRef Boolean @default(false) + inactivityTimeout Int? @default(3600) + autoResetSession Boolean @default(false) questionGeneratorPrompt String @default("Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question. Chat History: {chat_history} Follow Up Input: {question} Standalone question:") qaPrompt String @default("You are a helpful AI assistant. Use the following pieces of context to answer the question at the end. If you don't know the answer, just say you don't know. DO NOT try to make up an answer. If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context. {context} Question: {question} Helpful answer in markdown:") voice_to_text_type String @default("web_api") diff --git a/server/src/integration/handlers/discord.handler.ts b/server/src/integration/handlers/discord.handler.ts index 4549b37e..aed634a2 100644 --- a/server/src/integration/handlers/discord.handler.ts +++ b/server/src/integration/handlers/discord.handler.ts @@ -7,6 +7,7 @@ import { Document } from "langchain/document"; import { BaseRetriever } from "@langchain/core/retrievers"; import { createChain } from "../../chain"; import { getModelInfo } from "../../utils/get-model-info"; +import { differenceInSeconds } from "date-fns"; const prisma = new PrismaClient(); export const discordBotHandler = async ( @@ -48,6 +49,18 @@ export const discordBotHandler = async ( if (history.length > 20) { history.splice(0, history.length - 20); } + const lastMessageTimestamp = chat_history[chat_history.length - 1]?.createdAt || new Date().toISOString(); + + const inactivityPeriod = differenceInSeconds( + new Date(), + lastMessageTimestamp + ); + + if (bot.autoResetSession) { + if (inactivityPeriod > bot.inactivityTimeout) { + history = []; + } + } const temperature = bot.temperature; diff --git a/server/src/integration/handlers/telegram.handler.ts b/server/src/integration/handlers/telegram.handler.ts index 0be218d1..d64d56cf 100644 --- a/server/src/integration/handlers/telegram.handler.ts +++ b/server/src/integration/handlers/telegram.handler.ts @@ -6,6 +6,8 @@ import { DialoqbaseHybridRetrival } from "../../utils/hybrid"; import { BaseRetriever } from "@langchain/core/retrievers"; import { createChain } from "../../chain"; import { getModelInfo } from "../../utils/get-model-info"; +import { differenceInSeconds } from 'date-fns'; + const prisma = new PrismaClient(); export const telegramBotHandler = async ( @@ -44,6 +46,20 @@ export const telegramBotHandler = async ( history.splice(0, history.length - 20); } + const lastMessageTimestamp = chat_history[chat_history.length - 1]?.createdAt || new Date().toISOString(); + + const inactivityPeriod = differenceInSeconds( + new Date(), + lastMessageTimestamp + ); + + if (bot.autoResetSession) { + if (inactivityPeriod > bot.inactivityTimeout) { + history = []; + } + } + + const temperature = bot.temperature; const sanitizedQuestion = message.trim().replaceAll("\n", " "); @@ -87,7 +103,7 @@ export const telegramBotHandler = async ( prisma, type: "chat", }); - + if (!modelinfo) { return "Unable to find model"; } diff --git a/server/src/integration/handlers/whatsapp.handler.ts b/server/src/integration/handlers/whatsapp.handler.ts index bfed6e21..62037cfe 100644 --- a/server/src/integration/handlers/whatsapp.handler.ts +++ b/server/src/integration/handlers/whatsapp.handler.ts @@ -6,6 +6,7 @@ import { BaseRetriever } from "@langchain/core/retrievers"; import { DialoqbaseHybridRetrival } from "../../utils/hybrid"; import { createChain } from "../../chain"; import { getModelInfo } from "../../utils/get-model-info"; +import { differenceInSeconds } from "date-fns"; const prisma = new PrismaClient(); export const whatsappBotHandler = async ( @@ -53,6 +54,20 @@ export const whatsappBotHandler = async ( ai: message.bot, })); + const lastMessageTimestamp = chat_history[chat_history.length - 1]?.createdAt || new Date().toISOString(); + + const inactivityPeriod = differenceInSeconds( + new Date(), + lastMessageTimestamp + ); + + if (bot.autoResetSession) { + if (inactivityPeriod > bot.inactivityTimeout) { + history = []; + } + } + + const temperature = bot.temperature; const sanitizedQuestion = message.trim().replaceAll("\n", " "); diff --git a/server/yarn.lock b/server/yarn.lock index 10854dc3..2d163044 100644 --- a/server/yarn.lock +++ b/server/yarn.lock @@ -2623,6 +2623,11 @@ date-fns@^2.29.1: dependencies: "@babel/runtime" "^7.21.0" +date-fns@^3.6.0: + version "3.6.0" + resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-3.6.0.tgz#f20ca4fe94f8b754951b24240676e8618c0206bf" + integrity sha512-fRHTG8g/Gif+kSh50gaGEdToemgfj74aRX3swtiouboip5JDLAyDE9F11nHMIcvOaXeOC6D7SpNhi7uFyB7Uww== + dateformat@^4.6.3: version "4.6.3" resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-4.6.3.tgz#556fa6497e5217fedb78821424f8a1c22fa3f4b5"