Skip to content

Commit

Permalink
fix(assignments-service): Don't delete non-task embeddings
Browse files Browse the repository at this point in the history
  • Loading branch information
Clashsoft committed Nov 18, 2023
1 parent 74963ea commit 51cc90c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions services/apps/assignments/src/embedding/embedding.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export class EmbeddingHandler {
const taskIds = new Set<string>();
const assignmentId = assignment._id.toString();
this.upsertTasks(apiKey, assignmentId, assignment.tasks, '', taskIds);
await this.embeddingService.deleteNotIn(assignmentId, [...taskIds]);
await this.embeddingService.deleteTasksNotIn(assignmentId, [...taskIds]);
}

@OnEvent('assignments.*.deleted')
async onAssignmentDeleted(assignment: AssignmentDocument) {
await this.embeddingService.deleteNotIn(assignment._id.toString(), []);
await this.embeddingService.deleteAll(assignment._id.toString());
}

private upsertTasks(apiKey: string, assignment: string, tasks: Task[], prefix: string, taskIds: Set<string>) {
Expand Down
19 changes: 18 additions & 1 deletion services/apps/assignments/src/embedding/embedding.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,15 @@ export class EmbeddingService implements OnModuleInit {
return response.hits.hits.map(({_score, _source}) => ({...(_source as Embeddable), _score: _score || 0}));
}

async deleteNotIn(assignment: string, tasks: string[]): Promise<number> {
async deleteTasksNotIn(assignment: string, tasks: string[]): Promise<number> {
const body = await this.elasticsearchService.deleteByQuery({
index: 'embeddings',
query: {
bool: {
must: {
term: {
assignment,
type: 'task',
},
},
must_not: {
Expand All @@ -217,6 +218,22 @@ export class EmbeddingService implements OnModuleInit {
return body.deleted || 0;
}

async deleteAll(assignment: string): Promise<number> {
const body = await this.elasticsearchService.deleteByQuery({
index: 'embeddings',
query: {
bool: {
must: {
term: {
assignment,
},
},
},
},
});
return body.deleted || 0;
}

async deleteBySolution(assignment: string, solution: string): Promise<number> {
const body = await this.elasticsearchService.deleteByQuery({
index: 'embeddings',
Expand Down

0 comments on commit 51cc90c

Please sign in to comment.