Skip to content

Commit

Permalink
Merge pull request #761 from bounswe/be/fix-semantic-search
Browse files Browse the repository at this point in the history
Hopefully search is fixed
  • Loading branch information
Alputer authored Dec 25, 2023
2 parents d5150e7 + f0e51d0 commit 1c0b02e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 3 additions & 1 deletion app/backend/src/poll/poll.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ export class PollController {
@Post('pinecone/search')
public async pineconeSearch(
@Query('searchQuery') searchQuery: string,
@Req() req: any,
): Promise<Poll[]> {
return await this.pollService.searchSemanticPolls(searchQuery);
const userId = req.user?.sub; // Realize that it is not id instead sub. I do not know why but middleware gives this field.
return await this.pollService.searchSemanticPolls(searchQuery, userId);
}

@UseGuards(AuthGuard, VerificationGuard)
Expand Down
3 changes: 2 additions & 1 deletion app/backend/src/poll/poll.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import { OptionService } from '../option/option.service';
GoogleGenerativeAIEmbeddings,
RankingService,
VoteService,
OptionService
OptionService,
],
exports: [PollService],
})
Expand All @@ -71,6 +71,7 @@ export class PollModule implements NestModule {
.forRoutes(
{ path: '/poll', method: RequestMethod.GET },
{ path: '/poll/:param', method: RequestMethod.GET },
{ path: '/poll/pinecone/search', method: RequestMethod.GET },
);
}
}
20 changes: 13 additions & 7 deletions app/backend/src/poll/poll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,14 +651,20 @@ export class PollService {
});
}

public async searchSemanticPolls(query: string): Promise<Poll[]> {
let results = await this.pineconeStore.similaritySearchWithScore(query, 5);
results = results
public async searchSemanticPolls(
query: string,
userId?: string | null,
): Promise<Poll[]> {
const polls = await this.pineconeStore.similaritySearchWithScore(query, 5);
const pollIDs = polls
.filter((result) => result[1] > 0.7)
.map((result) => result[0].metadata.id);
return await this.pollRepository.find({
where: { id: In(results) },
relations: ['options', 'tags', 'creator', 'likes', 'comments', 'votes'],
});

const results = await Promise.all(
pollIDs.map(async (pollId) => {
return await this.findPollById(pollId, userId);
}),
);
return results;
}
}

0 comments on commit 1c0b02e

Please sign in to comment.