Skip to content

Commit

Permalink
handle failures of machine learning server
Browse files Browse the repository at this point in the history
  • Loading branch information
bwsarge committed Oct 22, 2023
1 parent b021b1b commit 8e9bf1f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 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
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 8e9bf1f

Please sign in to comment.