mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-05-19 16:13:19 +00:00
spaces in the API
This commit is contained in:
parent
11be31cf8b
commit
338e4f378c
3 changed files with 45 additions and 20 deletions
|
|
@ -8,7 +8,8 @@ export async function POST(request: Request, _: CloudflareVectorizeStore, embedd
|
|||
const query = queryparams.get("q");
|
||||
const topK = parseInt(queryparams.get("topK") ?? "5");
|
||||
const user = queryparams.get("user")
|
||||
const space = queryparams.get("space")
|
||||
const spaces = queryparams.get("spaces")
|
||||
const spacesArray = spaces ? spaces.split(",") : undefined
|
||||
|
||||
const sourcesOnly = (queryparams.get("sourcesOnly") ?? "false")
|
||||
|
||||
|
|
@ -19,27 +20,48 @@ export async function POST(request: Request, _: CloudflareVectorizeStore, embedd
|
|||
if (!query) {
|
||||
return new Response(JSON.stringify({ message: "Invalid Query" }), { status: 400 });
|
||||
}
|
||||
|
||||
const filter: VectorizeVectorMetadataFilter = {
|
||||
user
|
||||
}
|
||||
|
||||
if (space) {
|
||||
filter.space
|
||||
const responses: VectorizeMatches = { matches: [], count: 0 };
|
||||
|
||||
if (spacesArray) {
|
||||
for (const space of spacesArray) {
|
||||
filter.space = space;
|
||||
|
||||
const queryAsVector = await embeddings.embedQuery(query);
|
||||
|
||||
const resp = await env!.VECTORIZE_INDEX.query(queryAsVector, {
|
||||
topK,
|
||||
filter
|
||||
});
|
||||
|
||||
if (resp.count > 0) {
|
||||
responses.matches.push(...resp.matches)
|
||||
responses.count += resp.count
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const queryAsVector = await embeddings.embedQuery(query);
|
||||
const resp = await env!.VECTORIZE_INDEX.query(queryAsVector, {
|
||||
topK,
|
||||
filter: {
|
||||
user
|
||||
}
|
||||
});
|
||||
|
||||
if (resp.count > 0) {
|
||||
responses.matches.push(...resp.matches)
|
||||
responses.count += resp.count
|
||||
}
|
||||
}
|
||||
|
||||
const queryAsVector = await embeddings.embedQuery(query);
|
||||
// if (responses.count === 0) {
|
||||
// return new Response(JSON.stringify({ message: "No Results Found" }), { status: 404 });
|
||||
// }
|
||||
|
||||
const resp = await env!.VECTORIZE_INDEX.query(queryAsVector, {
|
||||
topK,
|
||||
filter
|
||||
});
|
||||
|
||||
if (resp.count === 0) {
|
||||
return new Response(JSON.stringify({ message: "No Results Found" }), { status: 404 });
|
||||
}
|
||||
|
||||
const highScoreIds = resp.matches.filter(({ score }) => score > 0.3).map(({ id }) => id)
|
||||
const highScoreIds = responses.matches.filter(({ score }) => score > 0.35).map(({ id }) => id)
|
||||
|
||||
if (sourcesOnly === "true") {
|
||||
return new Response(JSON.stringify({ ids: highScoreIds }), { status: 200 });
|
||||
|
|
@ -47,7 +69,7 @@ export async function POST(request: Request, _: CloudflareVectorizeStore, embedd
|
|||
|
||||
const vec = await env!.VECTORIZE_INDEX.getByIds(highScoreIds)
|
||||
|
||||
const preparedContext = vec.slice(0, 3).map(({ metadata }) => `Website title: ${metadata!.title}\nDescription: ${metadata!.description}\nURL: ${metadata!.url}\nContent: ${metadata!.text}`).join("\n\n");
|
||||
const preparedContext = vec.map(({ metadata }) => `Website title: ${metadata!.title}\nDescription: ${metadata!.description}\nURL: ${metadata!.url}\nContent: ${metadata!.text}`).join("\n\n");
|
||||
|
||||
const body = await request.json() as {
|
||||
chatHistory?: Content[]
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@ export async function POST(req: NextRequest) {
|
|||
const session = { session: sessionData[0], user: user[0] }
|
||||
|
||||
const query = new URL(req.url).searchParams.get("q");
|
||||
const spaces = new URL(req.url).searchParams.get("spaces");
|
||||
|
||||
const sourcesOnly = new URL(req.url).searchParams.get("sourcesOnly") ?? "false";
|
||||
|
||||
const chatHistory = await req.json() as {
|
||||
|
|
@ -38,7 +40,7 @@ export async function POST(req: NextRequest) {
|
|||
}
|
||||
|
||||
|
||||
const resp = await fetch(`https://cf-ai-backend.dhravya.workers.dev/chat?q=${query}&user=${session.user.email ?? session.user.name}&sourcesOnly=${sourcesOnly}`, {
|
||||
const resp = await fetch(`https://cf-ai-backend.dhravya.workers.dev/chat?q=${query}&user=${session.user.email ?? session.user.name}&sourcesOnly=${sourcesOnly}&spaces=${spaces}`, {
|
||||
headers: {
|
||||
"X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -37,6 +37,8 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) {
|
|||
const textArea = useRef<HTMLDivElement>(null);
|
||||
const main = useRef<HTMLDivElement>(null);
|
||||
|
||||
const [selectedSpaces, setSelectedSpaces] = useState<string[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
const search = searchParams.get('q');
|
||||
if (search && search.trim().length > 0) {
|
||||
|
|
@ -182,7 +184,7 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) {
|
|||
ids: string[];
|
||||
};
|
||||
|
||||
setIsAiLoading(false)
|
||||
setIsAiLoading(false);
|
||||
setChatHistory((prev) => {
|
||||
const lastMessage = prev[prev.length - 1];
|
||||
return [
|
||||
|
|
@ -197,8 +199,7 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) {
|
|||
];
|
||||
});
|
||||
|
||||
// TODO: PASS THE `SPACE` TO THE API
|
||||
const response = await fetch(`/api/chat?q=${_value}`, {
|
||||
const response = await fetch(`/api/chat?q=${_value}&spaces=${selectedSpaces.join(",")}`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
chatHistory: modifyChatHistory(chatHistory),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue