From edcebea8b215b8ccb78979d232058598e5620429 Mon Sep 17 00:00:00 2001 From: Sean Sundberg Date: Mon, 6 Nov 2023 11:15:37 -0600 Subject: [PATCH] Expose findRelevantPassages as REST service (#135) Signed-off-by: Sean Sundberg --- .../data-extraction/data-extraction.controller.ts | 15 +++++++++++++-- .../data-extraction/data-extraction.api.ts | 2 ++ .../data-extraction/data-extraction.impl.ts | 4 ++++ .../data-extraction/data-extraction.mock.ts | 4 ++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/controllers/data-extraction/data-extraction.controller.ts b/src/controllers/data-extraction/data-extraction.controller.ts index fc83ca6..856fca3 100644 --- a/src/controllers/data-extraction/data-extraction.controller.ts +++ b/src/controllers/data-extraction/data-extraction.controller.ts @@ -1,4 +1,4 @@ -import {Controller, Get, Query} from "@nestjs/common"; +import {Controller, Get, Post, Query} from "@nestjs/common"; import {DataExtractionApi} from "../../services/data-extraction"; import {ApiTags} from "@nestjs/swagger"; @@ -6,7 +6,8 @@ import {ApiTags} from "@nestjs/swagger"; @ApiTags('data-extraction') @Controller('data-extraction') export class DataExtractionController { - constructor(private readonly service: DataExtractionApi) {} + constructor(private readonly service: DataExtractionApi) { + } @Get('questions') listQuestions() { @@ -36,4 +37,14 @@ export class DataExtractionController { return this.service.extractData(customer, questionIds.map(id => ({id}))) .catch(err => console.error(err)) } + + @Get('findRelevantPassages') + async findRelevantPassages( + @Query('question') question: string, + @Query('passages') passages: string[] = [] + ): Promise { + const passageVals = Array.isArray(passages) ? passages : [passages] + + return this.service.findRelevantPassages(question, passageVals) + } } diff --git a/src/services/data-extraction/data-extraction.api.ts b/src/services/data-extraction/data-extraction.api.ts index 5574496..9975fe6 100644 --- a/src/services/data-extraction/data-extraction.api.ts +++ b/src/services/data-extraction/data-extraction.api.ts @@ -10,4 +10,6 @@ export abstract class DataExtractionApi { abstract extractDataForQuestion(customer: string, question: {id: string}): Promise; abstract extractDataObservable(customer: string, questions: Array<{id: string}>): Observable; + + abstract findRelevantPassages(question: string, passages: string[]): Promise; } diff --git a/src/services/data-extraction/data-extraction.impl.ts b/src/services/data-extraction/data-extraction.impl.ts index b87c79f..b232942 100644 --- a/src/services/data-extraction/data-extraction.impl.ts +++ b/src/services/data-extraction/data-extraction.impl.ts @@ -200,6 +200,10 @@ export class DataExtractionImpl extends DataExtractionCsv { const url = process.env.RELEVANT_PASSAGES_URL || 'https://similarity-check.18xu6cedovu0.us-south.codeengine.appdomain.cloud/api/find_relevant_passage' + if (passages.length === 1) { + return passages[0] + } + return await queue .add(async () => { console.log('Getting relevant passage') diff --git a/src/services/data-extraction/data-extraction.mock.ts b/src/services/data-extraction/data-extraction.mock.ts index 5b892d0..a153c9d 100644 --- a/src/services/data-extraction/data-extraction.mock.ts +++ b/src/services/data-extraction/data-extraction.mock.ts @@ -28,4 +28,8 @@ export class DataExtractionMock extends DataExtractionCsv<{}, {}> implements Dat return {}; } + async findRelevantPassages(question: string, passages: string[]): Promise { + return '' + } + }