Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add call to simiarity-check api to perform cosine similarity #116

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add call to simiarity-check api to perform cosine similarity
Signed-off-by: Sean Sundberg <seansund@us.ibm.com>
seansund committed Oct 25, 2023
commit da47b7c5accb8ba6a840e42d4f180e9b258de7a4
23 changes: 16 additions & 7 deletions src/services/data-extraction/data-extraction.impl.ts
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import {DataExtractionConfig, DataExtractionCsv} from "./data-extraction.csv";
import {kycCaseSummaryApi, KycCaseSummaryApi} from "../kyc-case-summary";
import {DataExtractionResultModel} from "../../models";
import {first, GenAiModel, GenerativeResponse} from "../../utils";
import axios from "axios";

export interface DataExtractionBackendConfig {
identityUrl: string;
@@ -154,11 +155,11 @@ export class DataExtractionImpl extends DataExtractionCsv<WatsonBackends, Contex
}
})

const textWithHtml: string = !passagesPerDocument
const passages: string[] = !passagesPerDocument
? this.handleDiscoveryPassages(response.result)
: this.handleDiscoveryResult(response.result, customer);

const text = striptags(textWithHtml.trim())
const text: string = await this.findRelevantPassages(naturalLanguageQuery, passages)

console.log('1. Text extracted from Discovery:', {naturalLanguageQuery, text})

@@ -175,19 +176,27 @@ export class DataExtractionImpl extends DataExtractionCsv<WatsonBackends, Contex
})
}

handleDiscoveryResult(result: DiscoveryV2.QueryResponse, customer: string): string {
handleDiscoveryResult(result: DiscoveryV2.QueryResponse, customer: string): string[] {
return this.filterDocuments(result, customer)
.map(result => result.document_passages
.map(passage => passage.passage_text)
.join(' ')
)
.join(' ')
.reduce((result: string[], current: string[]) => {
return result.concat(...current)
}, [])
}

handleDiscoveryPassages(result: DiscoveryV2.QueryResponse): string {
handleDiscoveryPassages(result: DiscoveryV2.QueryResponse): string[] {
return result.passages
.map(passage => passage.passage_text)
.join(' ')
}

async findRelevantPassages(question: string, passages: string[]): Promise<string> {
const url = process.env.RELEVANT_PASSAGES_URL || 'https://similarity-check.18xu6cedovu0.us-south.codeengine.appdomain.cloud/api/find_relevant_passage'

return axios
.post<{relevant_passage: string}>(url, {question, passages})
.then(response => response.data.relevant_passage)
}

async generateResponse(customer: string, config: DataExtractionConfig, text: string, backends: WatsonBackends): Promise<{watsonxResponse: string, prompt: string}> {