Compare commits

...

4 Commits

Author SHA1 Message Date
086b8abdf4 Merge 8097610baf into 09661ae11d 2025-04-05 17:57:22 -07:00
8097610baf Adding user session for history 2025-04-05 17:43:04 -07:00
09661ae11d feat(prompts): fix typo, closes #715 2025-04-02 13:02:28 +05:30
a8d410bc2f Merge pull request #716 from ItzCrazyKns/feat/system-instructions
Feat/system instructions
2025-04-01 15:59:18 +05:30
7 changed files with 64 additions and 5 deletions

View File

@ -29,6 +29,7 @@ type Message = {
messageId: string;
chatId: string;
content: string;
userSessionId: string;
};
type ChatModel = {
@ -138,6 +139,7 @@ const handleHistorySave = async (
where: eq(chats.id, message.chatId),
});
let currentDate = new Date();
if (!chat) {
await db
.insert(chats)
@ -147,6 +149,8 @@ const handleHistorySave = async (
createdAt: new Date().toString(),
focusMode: focusMode,
files: files.map(getFileDetails),
userSessionId: message.userSessionId,
timestamp: currentDate.toISOString(),
})
.execute();
}

View File

@ -1,10 +1,47 @@
import db from '@/lib/db';
import { chats } from '@/lib/db/schema';
import { eq, sql} from 'drizzle-orm';
export const GET = async (req: Request) => {
try {
let chats = await db.query.chats.findMany();
chats = chats.reverse();
return Response.json({ chats: chats }, { status: 200 });
// get header from request
const headers = await req.headers;
let userSessionId = headers.get('user-session-id')?.toString() ?? '';
if (userSessionId == '') {
return Response.json({ chats: {} }, { status: 200 });
}
let chatsRes = await db.query.chats.findMany({
where: eq(chats.userSessionId, userSessionId),
});
chatsRes = chatsRes.reverse();
// Keep only the latest 20 records in the database. Delete older records.
let maxRecordLimit = 20;
if (chatsRes.length > maxRecordLimit) {
const deleteChatsQuery = sql`DELETE FROM chats
WHERE userSessionId = ${userSessionId} AND (
timestamp IS NULL OR
timestamp NOT in (
SELECT timestamp FROM chats
WHERE userSessionId = ${userSessionId}
ORDER BY timestamp DESC
LIMIT ${maxRecordLimit}
)
)
`;
await db.run(deleteChatsQuery);
// Delete messages that no longer link with the chat from the database.
const deleteMessagesQuery = sql`DELETE FROM messages
WHERE chatId NOT IN (
SELECT id FROM chats
)
`;
await db.run(deleteMessagesQuery);
}
return Response.json({ chats: chatsRes }, { status: 200 });
} catch (err) {
console.error('Error in getting chats: ', err);
return Response.json(

View File

@ -125,7 +125,7 @@ export const POST = async (req: Request) => {
embeddings,
body.optimizationMode,
[],
"",
'',
);
if (!body.stream) {

View File

@ -1,5 +1,6 @@
'use client';
import crypto from 'crypto';
import DeleteChat from '@/components/DeleteChat';
import { cn, formatTimeDifference } from '@/lib/utils';
import { BookOpenText, ClockIcon, Delete, ScanEye } from 'lucide-react';
@ -21,10 +22,17 @@ const Page = () => {
const fetchChats = async () => {
setLoading(true);
let userSessionId = localStorage.getItem('userSessionId');
if (!userSessionId) {
userSessionId = crypto.randomBytes(20).toString('hex');
localStorage.setItem('userSessionId', userSessionId)
}
const res = await fetch(`/api/chats`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'user-session-id': userSessionId!,
},
});

View File

@ -95,6 +95,12 @@ const checkConfig = async (
if (!embeddingModel || !embeddingModelProvider) {
const embeddingModelProviders = providers.embeddingModelProviders;
let userSessionId = localStorage.getItem('userSessionId');
if (!userSessionId) {
userSessionId = crypto.randomBytes(20).toString('hex');
localStorage.setItem('userSessionId', userSessionId!)
}
if (
!embeddingModelProviders ||
Object.keys(embeddingModelProviders).length === 0
@ -342,6 +348,7 @@ const ChatWindow = ({ id }: { id?: string }) => {
let added = false;
messageId = messageId ?? crypto.randomBytes(7).toString('hex');
let userSessionId = localStorage.getItem('userSessionId');
setMessages((prevMessages) => [
...prevMessages,
@ -466,6 +473,7 @@ const ChatWindow = ({ id }: { id?: string }) => {
messageId: messageId,
chatId: chatId!,
content: message,
userSessionId: userSessionId,
},
chatId: chatId!,
files: fileIds,

View File

@ -25,4 +25,6 @@ export const chats = sqliteTable('chats', {
files: text('files', { mode: 'json' })
.$type<File[]>()
.default(sql`'[]'`),
userSessionId: text('userSessionId'),
timestamp: text('timestamp'),
});

View File

@ -1,6 +1,6 @@
export const webSearchRetrieverPrompt = `
You are an AI question rephraser. You will be given a conversation and a follow-up question, you will have to rephrase the follow up question so it is a standalone question and can be used by another LLM to search the web for information to answer it.
If it is a smple writing task or a greeting (unless the greeting contains a question after it) like Hi, Hello, How are you, etc. than a question then you need to return \`not_needed\` as the response (This is because the LLM won't need to search the web for finding information on this topic).
If it is a simple writing task or a greeting (unless the greeting contains a question after it) like Hi, Hello, How are you, etc. than a question then you need to return \`not_needed\` as the response (This is because the LLM won't need to search the web for finding information on this topic).
If the user asks some question from some URL or wants you to summarize a PDF or a webpage (via URL) you need to return the links inside the \`links\` XML block and the question inside the \`question\` XML block. If the user wants to you to summarize the webpage or the PDF you need to return \`summarize\` inside the \`question\` XML block in place of a question and the link to summarize in the \`links\` XML block.
You must always return the rephrased question inside the \`question\` XML block, if there are no links in the follow-up question then don't insert a \`links\` XML block in your response.