add number of chunks to the respnose and only show unique values

This commit is contained in:
Dhravya 2024-06-16 19:13:32 -05:00
parent a4c835e583
commit 76c48ccb60
4 changed files with 34 additions and 7 deletions

View file

@ -46,5 +46,5 @@ export const vectorObj = z.object({
space: z.string().optional(),
url: z.string(),
user: z.string(),
type: z.string().optional(),
type: z.string().optional().default("page"),
});

View file

@ -22,6 +22,8 @@ import { code, p } from "./markdownRenderHelpers";
import { codeLanguageSubset } from "@/lib/constants";
import { z } from "zod";
import { toast } from "sonner";
import Link from "next/link";
import { sources } from "next/dist/compiled/webpack/webpack";
function ChatWindow({
q,
@ -77,11 +79,24 @@ function ChatWindow({
const newChatHistory = [...prevChatHistory];
const lastAnswer = newChatHistory[newChatHistory.length - 1];
if (!lastAnswer) return prevChatHistory;
lastAnswer.answer.sources = sourcesParsed.data.metadata.map((source) => ({
const filteredSourceUrls = new Set(
sourcesParsed.data.metadata.map((source) => source.url),
);
const uniqueSources = sourcesParsed.data.metadata.filter((source) => {
if (filteredSourceUrls.has(source.url)) {
filteredSourceUrls.delete(source.url);
return true;
}
return false;
});
lastAnswer.answer.sources = uniqueSources.map((source) => ({
title: source.title ?? "Untitled",
type: source.type ?? "page",
source: source.url ?? "https://supermemory.ai",
content: source.content ?? "No content available",
numChunks: sourcesParsed.data.metadata.filter(
(f) => f.url === source.url,
).length,
}));
return newChatHistory;
});
@ -191,15 +206,25 @@ function ChatWindow({
</>
))}
{chat.answer.sources.map((source, idx) => (
<div
<Link
href={source.source}
key={idx}
className="rounded-xl bg-secondary p-4 flex flex-col gap-2 min-w-72"
>
<div className="text-foreground-menu">
{source.type}
<div className="flex justify-between text-foreground-menu text-sm">
<span>{source.type}</span>
{source.numChunks > 1 && (
<span>{source.numChunks} chunks</span>
)}
</div>
<div>{source.title}</div>
</div>
<div className="text-base">{source.title}</div>
<div className="text-xs">
{source.content.length > 100
? source.content.slice(0, 100) + "..."
: source.content}
</div>
</Link>
))}
</AccordionContent>
</AccordionItem>

View file

@ -171,6 +171,7 @@ export const createMemory = async (input: {
// TODO: now, in the vector store, we are only saving the first space. We need to save all spaces.
space: storeToSpaces[0],
user: data.user.id,
type,
}),
headers: {
"Content-Type": "application/json",

View file

@ -10,6 +10,7 @@ export const ChatHistoryZod = z.object({
source: z.string(),
title: z.string(),
content: z.string(),
numChunks: z.number().optional().default(1),
}),
),
}),