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 all commits
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
15 changes: 9 additions & 6 deletions SETUP-GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
3. Create a `.dev.vars` file in `apps/web` with the following content:

```bash
GOOGLE_CLIENT_ID="-"
GOOGLE_CLIENT_SECRET="-"
GOOGLE_CLIENT_ID="-" // required, visit https://developers.google.com/identity/protocols/oauth2
GOOGLE_CLIENT_SECRET="-" // required
NEXTAUTH_SECRET='nextauthsecret'
DATABASE_URL='database.sqlite'
NEXTAUTH_URL='http:https://localhost:3000'
BACKEND_SECURITY_KEY='veryrandomsecuritykey'
BACKEND_BASE_URL="where your backend is hosted"
```

4. Setup the database:
Expand All @@ -28,10 +29,10 @@ First, edit the `wrangler.toml` file in `apps/web` to point the d1 database to y
You can create a d1 database by running this command

```
wrangler d1 create DATABASE_NAME
bunx wrangler d1 create <YOUR_DATABASE_NAME>
```

And then replace these values
And then replace database_name and database_id with the values

```
[[d1_databases]]
Expand All @@ -43,10 +44,12 @@ database_id = "YOUR_DB_ID"
Simply run this command in `apps/web`

```
wrangler d1 execute dev-d1-anycontext --local --file=db/prepare.sql
bunx wrangler d1 migrations apply <YOUR_DATABASE_NAME>
```

If it runs, you can set up the cloud database as well by removing the `--local` flag.
If it runs, you can set up the cloud database as well by removing the `--local` flag,

if you just want to contribute to frontend then just run `bun run dev` in the root of the project and done! (you won't be able to try ai stuff), otherwise continue...

5. You need to host your own worker for the `apps/cf-ai-backend` module.

Expand Down
2 changes: 1 addition & 1 deletion apps/browser-rendering
74 changes: 33 additions & 41 deletions apps/cf-ai-backend/README.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,50 @@
# Hono minimal project
baseURL: https://new-cf-ai-backend.dhravya.workers.dev

This is a minimal project with [Hono](https://github.com/honojs/hono/) for Cloudflare Workers.
Authentication:
You must authenticate with a header and `Authorization: bearer token` for each request in `/api/*` routes.

## Features
### Add content:

- Minimal
- TypeScript
- Wrangler to develop and deploy.
- [Jest](https://jestjs.io/ja/) for testing.

## Usage

Initialize
POST `/api/add` with

```
npx create-cloudflare my-app https://github.com/honojs/hono-minimal
body {
pageContent: z.string(),
title: z.string().optional(),
description: z.string().optional(),
space: z.string().optional(),
url: z.string(),
user: z.string(),
}
```

Install
### Query without user data

```
yarn install
```
GET `/api/ask` with
query `?query=testing`

Develop
(this is temp but works perfectly, will change soon for chat use cases specifically)

```
yarn dev
```
### Query vectorize and get results in natural language

Test
POST `/api/chat` with

```
yarn test
```

Deploy
query paramters (?query=...&" {
query: z.string(),
topK: z.number().optional().default(10),
user: z.string(),
spaces: z.string().optional(),
sourcesOnly: z.string().optional().default("false"),
model: z.string().optional().default("gpt-4o"),
}

body z.object({
chatHistory: z.array(contentObj).optional(),
});
```
yarn deploy
```

## Examples

See: <https://github.com/honojs/examples>

## For more information

See: <https://honojs.dev>

## Author

Yusuke Wada <https://github.com/yusukebe>

## License
### Delete vectors

MIT
DELETE `/api/delete` with
query param websiteUrl, user
2 changes: 1 addition & 1 deletion apps/cf-ai-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"test": "jest --verbose",
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"dev": "wrangler dev --remote --port 8686",
"start": "wrangler dev",
"unsafe-reset-vector-db": "wrangler vectorize delete supermem-vector && wrangler vectorize create --dimensions=1536 supermem-vector-1 --metric=cosine"
},
Expand Down
119 changes: 93 additions & 26 deletions apps/cf-ai-backend/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export async function initQuery(
index: c.env.VECTORIZE_INDEX,
});

const DEFAULT_MODEL = "gpt-4o";

let selectedModel:
| ReturnType<ReturnType<typeof createOpenAI>>
| ReturnType<ReturnType<typeof createGoogleGenerativeAI>>
Expand Down Expand Up @@ -52,12 +50,6 @@ export async function initQuery(
break;
}

if (!selectedModel) {
throw new Error(
`Model ${model} not found and default model ${DEFAULT_MODEL} is also not available.`,
);
}

return { store, model: selectedModel };
}

Expand All @@ -72,19 +64,60 @@ export async function deleteDocument({
c: Context<{ Bindings: Env }>;
store: CloudflareVectorizeStore;
}) {
const toBeDeleted = `${url}-${user}`;
const toBeDeleted = `${url}#supermemory-web`;
const random = seededRandom(toBeDeleted);

const uuid =
random().toString(36).substring(2, 15) +
random().toString(36).substring(2, 15);

await c.env.KV.list({ prefix: uuid }).then(async (keys) => {
for (const key of keys.keys) {
await c.env.KV.delete(key.name);
await store.delete({ ids: [key.name] });
const allIds = await c.env.KV.list({ prefix: uuid });

if (allIds.keys.length > 0) {
const savedVectorIds = allIds.keys.map((key) => key.name);
const vectors = await c.env.VECTORIZE_INDEX.getByIds(savedVectorIds);
// We don't actually delete document directly, we just remove the user from the metadata.
// If there's no user left, we can delete the document.
const newVectors = vectors.map((vector) => {
delete vector.metadata[`user-${user}`];

// Get count of how many users are left
const userCount = Object.keys(vector.metadata).filter((key) =>
key.startsWith("user-"),
).length;

// If there's no user left, we can delete the document.
// need to make sure that every chunk is deleted otherwise it would be problematic.
if (userCount === 0) {
store.delete({ ids: savedVectorIds });
void Promise.all(savedVectorIds.map((id) => c.env.KV.delete(id)));
return null;
}

return vector;
});

// If all vectors are null (deleted), we can delete the KV too. Otherwise, we update (upsert) the vectors.
if (newVectors.every((v) => v === null)) {
await c.env.KV.delete(uuid);
} else {
await c.env.VECTORIZE_INDEX.upsert(newVectors.filter((v) => v !== null));
}
});
}
}

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({
Expand All @@ -98,19 +131,47 @@ export async function batchCreateChunksAndEmbeddings({
chunks: string[];
context: Context<{ Bindings: Env }>;
}) {
const ourID = `${body.url}-${body.user}`;
//! NOTE that we use #supermemory-web to ensure that
//! If a user saves it through the extension, we don't want other users to be able to see it.
// Requests from the extension should ALWAYS have a unique ID with the USERiD in it.
// I cannot stress this enough, important for security.
const ourID = `${body.url}#supermemory-web`;
const random = seededRandom(ourID);
const uuid =
random().toString(36).substring(2, 15) +
random().toString(36).substring(2, 15);

await deleteDocument({ url: body.url, user: body.user, c: context, store });
const allIds = await context.env.KV.list({ prefix: uuid });

const random = seededRandom(ourID);
// If some chunks for that content already exist, we'll just update the metadata to include
// the user.
if (allIds.keys.length > 0) {
const savedVectorIds = allIds.keys.map((key) => key.name);
const vectors = await context.env.VECTORIZE_INDEX.getByIds(savedVectorIds);

// Now, we'll update all vector metadatas with one more userId and all spaceIds
const newVectors = vectors.map((vector) => {
vector.metadata = {
...vector.metadata,
[`user-${body.user}`]: 1,

// For each space in body, add the spaceId to the vector metadata
...(body.spaces ?? [])?.reduce((acc, space) => {
acc[`space-${body.user}-${space}`] = 1;
return acc;
}, {}),
};

return vector;
});

await context.env.VECTORIZE_INDEX.upsert(newVectors);
return;
}

for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
const uuid =
random().toString(36).substring(2, 15) +
random().toString(36).substring(2, 15) +
"-" +
i;
const chunkId = `${uuid}-${i}`;

const newPageContent = `Title: ${body.title}\nDescription: ${body.description}\nURL: ${body.url}\nContent: ${chunk}`;

Expand All @@ -121,19 +182,25 @@ export async function batchCreateChunksAndEmbeddings({
metadata: {
title: body.title?.slice(0, 50) ?? "",
description: body.description ?? "",
space: body.space ?? "",
url: body.url,
user: body.user,
type: body.type ?? "page",
content: newPageContent,

[sanitizeKey(`user-${body.user}`)]: 1,
...body.spaces?.reduce((acc, space) => {
acc[`space-${body.user}-${space}`] = 1;
return acc;
}, {}),
},
},
],
{
ids: [uuid],
ids: [chunkId],
},
);

console.log("Docs added: ", docs);

await context.env.KV.put(uuid, ourID);
await context.env.KV.put(chunkId, ourID);
}
}
13 changes: 0 additions & 13 deletions apps/cf-ai-backend/src/index.test.ts

This file was deleted.

Loading