Skip to content

Commit

Permalink
Merge pull request #770 from bounswe/be-769/vector-db-bugfix
Browse files Browse the repository at this point in the history
vector db fix
  • Loading branch information
batuhancetin authored Dec 25, 2023
2 parents 2c89a9e + 91dad97 commit 63f5e26
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 16 deletions.
20 changes: 18 additions & 2 deletions app/backend/src/moderator/moderator.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,33 @@ import { Poll } from '../poll/entities/poll.entity';
import { JwtModule } from '@nestjs/jwt';
import { Report } from '../user/entities/report.entity';
import { User } from '../user/entities/user.entity';
import { PollService } from '../poll/poll.service';
import { PollRepository } from '../poll/repository/poll.repository';
import { Like } from '../like/entities/like.entity';
import { Comment } from '../comment/entities/comment.entity';
import { UserService } from '../user/user.service';
import { Pinecone } from '@pinecone-database/pinecone';
import { RankingService } from '../ranking/ranking.service';
import { VoteService } from '../vote/vote.service';
import { BadgeService } from '../badge/badge.service';
import { Ranking } from '../ranking/entities/ranking.entity';
import { Vote } from '../vote/entities/vote.entity';
import { OptionService } from '../option/option.service';
import { Badge } from '../badge/entities/badge.entity';
import { Option } from '../option/entities/option.entity';
import { Tag } from '../tag/entities/tag.entity';
import { TagService } from '../tag/tag.service';

@Module({
imports: [
TypeOrmModule.forFeature([Moderator, Poll, Report, User]),
TypeOrmModule.forFeature([Moderator, Poll, Report, User, Like, Option, Badge, Vote, Ranking, Comment, Tag]),
JwtModule.register({
global: true,
secret: 'very-secret-key',
signOptions: { expiresIn: '1h' },
}),
],
controllers: [ModeratorController],
providers: [ModeratorService],
providers: [ModeratorService, PollService, PollRepository, OptionService, UserService, Pinecone, RankingService, VoteService, BadgeService, TagService],
})
export class ModeratorModule {}
10 changes: 10 additions & 0 deletions app/backend/src/moderator/moderator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { LoginModeratorDto } from './dto/login-moderator.dto';
import { Report } from '../user/entities/report.entity';
import { User } from '../user/entities/user.entity';
import { Settle } from '../poll/enums/settle.enum';
import { PollService } from '../poll/poll.service';

@Injectable()
export class ModeratorService {
Expand All @@ -31,6 +32,7 @@ export class ModeratorService {
private readonly userRepository: Repository<User>,
private mailerService: MailerService,
private jwtService: JwtService,
private readonly pollService: PollService
) {}

public async searchModerators(
Expand Down Expand Up @@ -196,6 +198,14 @@ export class ModeratorService {
pollId: string,
approveDto: ApproveDTO,
): Promise<void> {
if (approveDto.approveStatus === true) {
const poll = await this.pollRepository.findOne({
where: {
id: pollId,
},
})
await this.pollService.addVectorDB(poll);
}
await this.pollRepository.update(pollId, approveDto);
}

Expand Down
25 changes: 13 additions & 12 deletions app/backend/src/poll/poll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,32 @@ export class PollService {
);

savedPoll.tags = tags;

return await this.pollRepository.save(savedPoll);
}

public async addVectorDB(poll: Poll): Promise<void> {
try {
await this.pineconeStore.addDocuments(
[
new Document({
metadata: { id: savedPoll.id },
metadata: { id: poll.id },
pageContent:
savedPoll.question +
' ' +
savedPoll.description +
poll.question +
' ' +
savedPoll.tags.map((tag) => tag.name).join(' '),
poll.description
}),
],
{
ids: [savedPoll.id],
ids: [poll.id],
},
);
} catch (e) {
console.log(e);
}

return await this.pollRepository.save(savedPoll);
}


public async settleRequest(
user: User,
id: string,
Expand Down Expand Up @@ -656,6 +658,7 @@ export class PollService {

public async syncVectorStore(): Promise<any> {
const polls = await this.pollRepository.find({
where: { approveStatus: true },
relations: ['options', 'tags'],
});

Expand All @@ -665,9 +668,7 @@ export class PollService {
pageContent:
poll.question +
' ' +
poll.description +
' ' +
poll.tags.map((tag) => tag.name).join(' '),
poll.description
});
});

Expand All @@ -682,7 +683,7 @@ export class PollService {
): Promise<Poll[]> {
const polls = await this.pineconeStore.similaritySearchWithScore(query, 5);
const pollIDs = polls
.filter((result) => result[1] > 0.7)
.filter((result) => result[1] > 0.8)
.map((result) => result[0].metadata.id);

const results = await Promise.all(
Expand Down
18 changes: 16 additions & 2 deletions app/backend/src/tag/tag.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@ import { Moderator } from '../moderator/entities/moderator.entity';
import { Poll } from '../poll/entities/poll.entity';
import { Report } from '../user/entities/report.entity';
import { User } from '../user/entities/user.entity';
import { Option } from '../option/entities/option.entity';
import { PollService } from '../poll/poll.service';
import { PollRepository } from '../poll/repository/poll.repository';
import { Like } from '../like/entities/like.entity';
import { Comment } from '../comment/entities/comment.entity';
import { UserService } from '../user/user.service';
import { Pinecone } from '@pinecone-database/pinecone';
import { RankingService } from '../ranking/ranking.service';
import { VoteService } from '../vote/vote.service';
import { BadgeService } from '../badge/badge.service';
import { Ranking } from '../ranking/entities/ranking.entity';
import { Vote } from '../vote/entities/vote.entity';
import { OptionService } from '../option/option.service';
import { Badge } from '../badge/entities/badge.entity';


@Module({
imports: [TypeOrmModule.forFeature([Tag, Moderator, Poll, Report, User])],
imports: [TypeOrmModule.forFeature([Tag, Moderator, Poll, Report, User, Option, Like, Comment, Ranking, Vote, Badge])],
controllers: [TagController],
providers: [TagService, ModeratorService],
providers: [TagService, ModeratorService, PollService, PollRepository, UserService, Pinecone, RankingService, VoteService, BadgeService, OptionService],
exports: [TagService],
})
export class TagModule {}

0 comments on commit 63f5e26

Please sign in to comment.