Skip to content

Commit 4206ef3

Browse files
committed
feat: deleting the index deletes files from s3
1 parent ca2999b commit 4206ef3

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/server/services/rag/embedder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
LAAMA_API_TOKEN,
99
LAAMA_API_URL,
1010
OLLAMA_EMBEDDER_MODEL,
11-
OLLAMA_URL,
11+
// OLLAMA_URL,
1212
} from '../../util/config'
1313

1414
const _azureOpenAIEmbedder = new AzureOpenAIEmbeddings({
@@ -21,7 +21,7 @@ const _azureOpenAIEmbedder = new AzureOpenAIEmbeddings({
2121

2222
const _ollamaEmbedder = new OllamaEmbeddings({
2323
model: OLLAMA_EMBEDDER_MODEL,
24-
baseUrl: OLLAMA_URL,
24+
baseUrl: LAAMA_API_URL,
2525
fetch: (input: RequestInfo | URL, init?: RequestInit) => {
2626
return fetch(input, {
2727
...init,

src/server/services/rag/fileStore.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GetObjectCommand, DeleteObjectCommand, S3Client, PutObjectCommand } from '@aws-sdk/client-s3'
1+
import { GetObjectCommand, DeleteObjectCommand, PutObjectCommand, ListObjectsV2Command, DeleteObjectsCommand, ListObjectsV2CommandOutput } from '@aws-sdk/client-s3'
22
import type { RagFile, RagIndex } from '../../db/models'
33
import { ApplicationError } from '../../util/ApplicationError'
44
import { pdfToText } from '../../util/pdfToText'
@@ -20,10 +20,27 @@ export const FileStore = {
2020
async deleteRagIndexDocuments(ragIndex: RagIndex) {
2121
const prefix = FileStore.getRagIndexPrefix(ragIndex)
2222
try {
23-
// List all objects with the prefix and delete them
24-
// This requires using ListObjectsV2Command and DeleteObjectsCommand from AWS SDK v3, omitted for brevity
25-
console.log(`Delete all objects with prefix ${prefix} from S3 bucket ${S3_BUCKET}`)
26-
// Implement listing and batch deletion if needed
23+
let continuationToken: string | undefined = undefined
24+
do {
25+
const listCommand = new ListObjectsV2Command({
26+
Bucket: S3_BUCKET,
27+
Prefix: prefix,
28+
ContinuationToken: continuationToken,
29+
})
30+
const listResponse = await s3Client.send(listCommand) as ListObjectsV2CommandOutput
31+
const keys = (listResponse.Contents || []).map(obj => ({ Key: obj.Key! }))
32+
33+
if (keys.length > 0) {
34+
const deleteCommand = new DeleteObjectsCommand({
35+
Bucket: S3_BUCKET,
36+
Delete: { Objects: keys }
37+
})
38+
const deleteResponse = await s3Client.send(deleteCommand)
39+
console.log("Deleted:", deleteResponse.Deleted?.length, "objects.")
40+
}
41+
42+
continuationToken = listResponse.NextContinuationToken
43+
} while (continuationToken)
2744
} catch (error) {
2845
console.warn(`Failed to delete S3 objects with prefix ${prefix}:`, error)
2946
}

0 commit comments

Comments
 (0)