Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

July launch PR #55

Closed
wants to merge 90 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
f3d7f89
Add Extension Template
CodeTorso Jun 2, 2024
8ecbde0
Implemented Highlight in Extension
CodeTorso Jun 3, 2024
7ceaddf
Implement extension
CodeTorso Jun 4, 2024
004c952
Add Dynamic Island
CodeTorso Jun 8, 2024
99659df
before color edits
CodeTorso Jun 9, 2024
743976d
Add Memories, Canvas, Editor
CodeTorso Jun 9, 2024
04b6add
AI code refactored
CodeTorso Jun 11, 2024
a341908
Adding keyboard shortcuts, responsive design that 99% don't need but …
CodeTorso Jun 11, 2024
5ab5d8f
minor changes and improvements
Dhravya Jun 12, 2024
aabdef4
some documentation for backend
Dhravya Jun 12, 2024
03c9fb0
full screen mode implemented
CodeTorso Jun 12, 2024
1f6d419
Icons now in our website integrated
CodeTorso Jun 12, 2024
1619488
Merge pull request #56 from CodeTorso/codetorso
CodeTorso Jun 12, 2024
ef682da
smoothening animatio
CodeTorso Jun 12, 2024
53f0856
Merge pull request #57 from CodeTorso/codetorso
CodeTorso Jun 12, 2024
1c1d14b
Merge pull request #58 from CodeTorso/v2
CodeTorso Jun 12, 2024
d376dd1
Revert "Add code for extension"
CodeTorso Jun 13, 2024
778ddec
fix backend
Dhravya Jun 13, 2024
a711d1b
added zod validation to embedQuery
Dhravya Jun 13, 2024
f59cede
Merge branch 'codetorso' of https://github.com/Dhravya/supermemory in…
Dhravya Jun 13, 2024
5569aa2
chat UI and markdown renderer ⚡
Dhravya Jun 13, 2024
a20163d
make header and menu fixed instead of absolute to prevent movement on…
Dhravya Jun 13, 2024
d5a8117
Merge pull request #59 from Dhravya/revert-58-v2
CodeTorso Jun 13, 2024
0fb924d
small stuff
Dhravya Jun 13, 2024
80a2c6b
omg
Dhravya Jun 14, 2024
ac3b161
Merge branch 'codetorso' of https://github.com/Dhravya/supermemory in…
Dhravya Jun 14, 2024
9f0fb14
[MIGRATION REQUIRED]Data fetchers and other server actions, spaces cr…
Dhravya Jun 15, 2024
84455ad
getspaces and other features integrated with the backend
Dhravya Jun 15, 2024
e755aa7
chore: Remove unused variables and dependencies
kartikk-k Jun 15, 2024
8ecfc65
Merge branch 'codetorso' of https://github.com/Dhravya/supermemory in…
kartikk-k Jun 15, 2024
4daee14
added way to save content and vectorize them. also refactored code an…
Dhravya Jun 16, 2024
e0b83ad
Update Setup Guide
CodeTorso Jun 16, 2024
39ba654
improve: /home page
kartikk-k Jun 16, 2024
4cd16ec
ui update: /chat
kartikk-k Jun 16, 2024
375609d
form to add content [PENDING LOADING STATE]
Dhravya Jun 16, 2024
a68d7f0
Merge branch 'codetorso' of https://github.com/Dhravya/supermemory in…
Dhravya Jun 16, 2024
5cb5bcd
fixed a bug
Dhravya Jun 16, 2024
2ffb054
proper URLs
Dhravya Jun 16, 2024
a9f5ebc
include metadata in response, add type to metadata
Dhravya Jun 16, 2024
c5b31e5
use a dev vectorize database to prevent pollution
Dhravya Jun 16, 2024
62c4d88
add: animated query input
kartikk-k Jun 16, 2024
9588768
added image support in the backend
Dhravya Jun 16, 2024
a87030b
OCR try
Dhravya Jun 16, 2024
0f3d5c4
store full info
Dhravya Jun 16, 2024
a4c835e
added sources to the response
Dhravya Jun 17, 2024
76c48cc
add number of chunks to the respnose and only show unique values
Dhravya Jun 17, 2024
be71655
gpt-4o is already a default model, so no error-checking required
CodeTorso Jun 17, 2024
88289d1
add js docs
CodeTorso Jun 17, 2024
5af20f7
Merge pull request #70 from CodeTorso/codetorso
Dhravya Jun 17, 2024
957f131
vector duplication no longer an issue. support for querying with mult…
Dhravya Jun 18, 2024
066833a
include all selected spaces in the fetch call to backend
Dhravya Jun 18, 2024
6aa8dc4
Merge pull request #71 from Dhravya/vector-deduplication
Dhravya Jun 18, 2024
242cbf7
Add Editor Endpoint
CodeTorso Jun 18, 2024
251b6b4
codetorso pull remote
CodeTorso Jun 18, 2024
6cfb533
EditorAI integrated! (1/4)
CodeTorso Jun 18, 2024
c5361aa
Create Embeddings for Canvas
CodeTorso Jun 18, 2024
09af2ec
darkmode by default
Dhravya Jun 18, 2024
62054df
added logic to handle justification properly
Dhravya Jun 18, 2024
c704541
fix: Cta component background color
MaheshtheDev Jun 19, 2024
898cb32
Merge pull request #75 from MaheshtheDev/fix/cta-bg
Dhravya Jun 19, 2024
770eb99
Drag and Drop in Canvas!
CodeTorso Jun 19, 2024
28a257f
Add Drag & Drop code
CodeTorso Jun 19, 2024
f28f566
Improve code, failed attempt at Streaming text
CodeTorso Jun 19, 2024
a7cca29
Another Failed Attempt at streaming
CodeTorso Jun 19, 2024
e4c6c1a
works
Dhravya Jun 20, 2024
074ea24
added multi-turn conversations
Dhravya Jun 20, 2024
af90b96
Merge branch 'codetorso' into kartik
CodeTorso Jun 20, 2024
2b2ec6f
Merge pull request #76 from Dhravya/kartik
CodeTorso Jun 20, 2024
a5f384e
Fine tuning canvas (1/2)
CodeTorso Jun 22, 2024
2ac315c
improve canvas drag and drop make sidepanel work
CodeTorso Jun 22, 2024
becb306
Unreadable Canvas code, jk 😂
CodeTorso Jun 22, 2024
3a97c2a
delete packagelock
CodeTorso Jun 22, 2024
a2f8a27
addeed chathistory functionality
Dhravya Jun 22, 2024
02d7f08
merged code
Dhravya Jun 22, 2024
5140a4d
deleted chatpage
Dhravya Jun 22, 2024
445acf7
cleanup
Dhravya Jun 22, 2024
47e7528
Merge pull request #79 from Dhravya/chathistory
Dhravya Jun 23, 2024
4b6a932
fix interactive canvas (2/2)
CodeTorso Jun 23, 2024
4bdc819
centered layout
CodeTorso Jun 23, 2024
86e2f2a
clean code+ user canvas (1/2)
CodeTorso Jun 23, 2024
9df975e
added indexes and stuff
Dhravya Jun 23, 2024
557c7b8
Add Extension (1/2)
CodeTorso Jun 23, 2024
9f751ba
feat: vector lookup and chat is twice as fast now
Dhravya Jun 23, 2024
0d069e8
Merge branch 'codetorso' of https://github.com/Dhravya/supermemory in…
Dhravya Jun 23, 2024
fff8b7a
added backend route for telegram bot and others to be possible
Dhravya Jun 23, 2024
51dd5ec
made and documented the telegram bot (HYPE)
Dhravya Jun 24, 2024
75585fd
some important housekeeping, crushed all build errors
Dhravya Jun 24, 2024
4c86f1e
ts-ignore at the right place
Dhravya Jun 24, 2024
cd304b3
reduced db calls here, we don't need that complex logic, can just do …
Dhravya Jun 24, 2024
4581b2c
Merge pull request #80 from Dhravya/telegram-bot
Dhravya Jun 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added backend route for telegram bot and others to be possible
  • Loading branch information
Dhravya committed Jun 23, 2024
commit fff8b7abd79b58cddeffbac835ffb706cd529480
16 changes: 15 additions & 1 deletion apps/cf-ai-backend/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ export async function deleteDocument({
}
}

function sanitizeKey(key: string): string {
if (!key) throw new Error("Key cannot be empty");

// Remove or replace invalid characters
let sanitizedKey = key.replace(/[.$"]/g, "_");

// Ensure key does not start with $
if (sanitizedKey.startsWith("$")) {
sanitizedKey = sanitizedKey.substring(1);
}

return sanitizedKey;
}

export async function batchCreateChunksAndEmbeddings({
store,
body,
Expand Down Expand Up @@ -172,7 +186,7 @@ export async function batchCreateChunksAndEmbeddings({
type: body.type ?? "page",
content: newPageContent,

[`user-${body.user}`]: 1,
[sanitizeKey(`user-${body.user}`)]: 1,
...body.spaces?.reduce((acc, space) => {
acc[`space-${body.user}-${space}`] = 1;
return acc;
Expand Down
155 changes: 154 additions & 1 deletion apps/cf-ai-backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "zod";
import { Hono } from "hono";
import { CoreMessage, streamText } from "ai";
import { CoreMessage, generateText, streamText, tool } from "ai";
import { chatObj, Env, vectorObj } from "./types";
import {
batchCreateChunksAndEmbeddings,
Expand Down Expand Up @@ -169,6 +169,159 @@ app.get(
},
);

// This is a special endpoint for our "chatbot-only" solutions.
// It does both - adding content AND chatting with it.
app.post(
"/api/autoChatOrAdd",
zValidator(
"query",
z.object({
query: z.string(),
user: z.string(),
}),
),
zValidator("json", chatObj),
async (c) => {
const { query, user } = c.req.valid("query");
const { chatHistory } = c.req.valid("json");

const { store, model } = await initQuery(c);

let task: "add" | "chat" = "chat";
let thingToAdd: "page" | "image" | "text" | undefined = undefined;
let addContent: string | undefined = undefined;

// This is a "router". this finds out if the user wants to add a document, or chat with the AI to get a response.
const routerQuery = await generateText({
model,
system: `You are Supermemory chatbot. You can either add a document to the supermemory database, or return a chat response. Based on this query,
You must determine what to do. Basically if it feels like a "question", then you should intiate a chat. If it feels like a "command" or feels like something that could be forwarded to the AI, then you should add a document.
You must also extract the "thing" to add and what type of thing it is.`,
prompt: `Question from user: ${query}`,
tools: {
decideTask: tool({
description:
"Decide if the user wants to add a document or chat with the AI",
parameters: z.object({
generatedTask: z.enum(["add", "chat"]),
contentToAdd: z.object({
thing: z.enum(["page", "image", "text"]),
content: z.string(),
}),
}),
execute: async ({ generatedTask, contentToAdd }) => {
task = generatedTask;
thingToAdd = contentToAdd.thing;
addContent = contentToAdd.content;
},
}),
},
});

if ((task as string) === "add") {
// addString is the plaintext string that the user wants to add to the database
let addString: string = addContent;

if (thingToAdd === "page") {
// TODO: Sometimes this query hangs, and errors out. we need to do proper error management here.
const response = await fetch("https://md.dhr.wtf/?url=" + addContent, {
headers: {
Authorization: "Bearer " + c.env.SECURITY_KEY,
},
});

addString = await response.text();
}

// At this point, we can just go ahead and create the embeddings!
await batchCreateChunksAndEmbeddings({
store,
body: {
url: addContent,
user,
type: thingToAdd,
pageContent: addString,
title: `${addString.slice(0, 30)}... (Added from chatbot)`,
},
chunks: chunkText(addString, 1536),
context: c,
});

return c.json({
status: "ok",
response:
"I added the document to your personal second brain! You can now use it to answer questions or chat with me.",
contentAdded: {
type: thingToAdd,
content: addString,
url:
thingToAdd === "page"
? addContent
: `https://supermemory.ai/note/${Date.now()}`,
},
});
} else {
const filter: VectorizeVectorMetadataFilter = {
[`user-${user}`]: 1,
};

const queryAsVector = await store.embeddings.embedQuery(query);

const resp = await c.env.VECTORIZE_INDEX.query(queryAsVector, {
topK: 5,
filter,
returnMetadata: true,
});

const minScore = Math.min(...resp.matches.map(({ score }) => score));
const maxScore = Math.max(...resp.matches.map(({ score }) => score));

// This entire chat part is basically just a dumb down version of the /api/chat endpoint.
const normalizedData = resp.matches.map((data) => ({
...data,
normalizedScore:
maxScore !== minScore
? 1 + ((data.score - minScore) / (maxScore - minScore)) * 98
: 50,
}));

const preparedContext = normalizedData.map(
({ metadata, score, normalizedScore }) => ({
context: `Website title: ${metadata!.title}\nDescription: ${metadata!.description}\nURL: ${metadata!.url}\nContent: ${metadata!.text}`,
score,
normalizedScore,
}),
);

const prompt = template({
contexts: preparedContext,
question: query,
});

const initialMessages: CoreMessage[] = [
{
role: "system",
content: `You are an AI chatbot called "Supermemory.ai". When asked a question by a user, you must take all the context provided to you and give a good, small, but helpful response.`,
},
{ role: "assistant", content: "Hello, how can I help?" },
];

const userMessage: CoreMessage = { role: "user", content: prompt };

const response = await generateText({
model,
messages: [
...initialMessages,
...((chatHistory || []) as CoreMessage[]),
userMessage,
],
});

return c.json({ status: "ok", response: response.text });
}
},
);

/* TODO: Eventually, we should not have to save each user's content in a seperate vector.
Lowkey, it makes sense. The user may save their own version of a page - like selected text from twitter.com url.
But, it's not scalable *enough*. How can we store the same vectors for the same content, without needing to duplicate for each uer?
Expand Down