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

handle failures of machine learning server #180

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
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
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be cleaner to put the try catch in getEmbedding instead and let it return string | 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