Skip to content

Commit

Permalink
Merge pull request #180 from CheckMateSG/bugfix/error-handling-machin…
Browse files Browse the repository at this point in the history
…e-learning-server-down

handle failures of machine learning server
  • Loading branch information
sarge1989 authored Oct 25, 2023
2 parents b021b1b + 8f0796b commit 3a8673f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
10 changes: 7 additions & 3 deletions functions/src/definitions/common/calculateSimilarity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ async function calculateSimilarity(
score?: number
parent?: admin.firestore.DocumentReference<admin.firestore.DocumentData> | null
} = {}
const embedding = await getEmbedding(text)

let embedding = null
try {
embedding = await getEmbedding(text)
} catch (e) {
functions.logger.error(`Error in getEmbedding: ${e}`)
}
//try to match db first
const matchedInstancesSnap = await db
.collectionGroup(CollectionTypes.Instances)
Expand All @@ -47,7 +51,7 @@ async function calculateSimilarity(
// don't bother with vector search if remaining message is too short to be meaningful.
functions.logger.log("Remaining message text too short to match")
similarity = {}
} else {
} else if (embedding) {
const results = await vectorSearch(
embedding,
CollectionTypes.Instances,
Expand Down
16 changes: 11 additions & 5 deletions functions/src/definitions/common/classifier.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { getL1Category } from "./machineLearningServer/operations"
import * as functions from "firebase-functions"

async function classifyText(text: string): Promise<string> {
if (text.length < 8) {
return "irrelevant_length"
}
const category = await getL1Category(text)
if (category === "trivial") {
return "irrelevant"
} else {
return category
try {
const category = await getL1Category(text)
if (category === "trivial") {
return "irrelevant"
} else {
return category
}
} catch (e) {
functions.logger.error(`Error in classifyText: ${e}`)
return "error"
}
}

Expand Down
8 changes: 7 additions & 1 deletion functions/src/definitions/eventHandlers/onMessageUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { rationaliseMessage } from "../common/genAI"

const onMessageUpdate = functions
.region("asia-southeast1")
.runWith({ secrets: ["WHATSAPP_USER_BOT_PHONE_NUMBER_ID", "WHATSAPP_TOKEN"] })
.runWith({
secrets: [
"WHATSAPP_USER_BOT_PHONE_NUMBER_ID",
"WHATSAPP_TOKEN",
"OPENAI_API_KEY",
],
})
.firestore.document("/messages/{messageId}")
.onUpdate(async (change, context) => {
// Grab the current value of what was written to Firestore.
Expand Down
2 changes: 1 addition & 1 deletion functions/src/definitions/eventHandlers/userHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ async function newTextInstanceHandler({
replyTimestamp: null,
matchType: matchType,
scamShieldConsent: null,
embedding: embedding,
embedding: embedding ?? null,
closestMatch: {
instanceRef: bestMatchingDocumentRef ?? null,
text: bestMatchingText ?? null,
Expand Down

0 comments on commit 3a8673f

Please sign in to comment.