From 76c48ccb600d172f362d7cd237094d6082454825 Mon Sep 17 00:00:00 2001 From: Dhravya Date: Sun, 16 Jun 2024 19:13:32 -0500 Subject: [PATCH] add number of chunks to the respnose and only show unique values --- apps/cf-ai-backend/src/types.ts | 2 +- apps/web/app/(dash)/chat/chatWindow.tsx | 37 +++++++++++++++++++++---- apps/web/app/actions/doers.ts | 1 + packages/shared-types/index.ts | 1 + 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/apps/cf-ai-backend/src/types.ts b/apps/cf-ai-backend/src/types.ts index 3db62553..5f6d0583 100644 --- a/apps/cf-ai-backend/src/types.ts +++ b/apps/cf-ai-backend/src/types.ts @@ -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"), }); diff --git a/apps/web/app/(dash)/chat/chatWindow.tsx b/apps/web/app/(dash)/chat/chatWindow.tsx index 3fd08657..77c1f32b 100644 --- a/apps/web/app/(dash)/chat/chatWindow.tsx +++ b/apps/web/app/(dash)/chat/chatWindow.tsx @@ -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) => ( -
-
- {source.type} +
+ {source.type} + + {source.numChunks > 1 && ( + {source.numChunks} chunks + )}
-
{source.title}
-
+
{source.title}
+
+ {source.content.length > 100 + ? source.content.slice(0, 100) + "..." + : source.content} +
+ ))} diff --git a/apps/web/app/actions/doers.ts b/apps/web/app/actions/doers.ts index 3aac1f5a..f94ed8ec 100644 --- a/apps/web/app/actions/doers.ts +++ b/apps/web/app/actions/doers.ts @@ -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", diff --git a/packages/shared-types/index.ts b/packages/shared-types/index.ts index 46e3edba..b8792369 100644 --- a/packages/shared-types/index.ts +++ b/packages/shared-types/index.ts @@ -10,6 +10,7 @@ export const ChatHistoryZod = z.object({ source: z.string(), title: z.string(), content: z.string(), + numChunks: z.number().optional().default(1), }), ), }),