diff --git a/functions/src/definitions/common/calculateSimilarity.ts b/functions/src/definitions/common/calculateSimilarity.ts index 35453823..3e6ba0f6 100644 --- a/functions/src/definitions/common/calculateSimilarity.ts +++ b/functions/src/definitions/common/calculateSimilarity.ts @@ -25,8 +25,12 @@ async function calculateSimilarity( score?: number parent?: admin.firestore.DocumentReference | 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) @@ -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, diff --git a/functions/src/definitions/common/classifier.ts b/functions/src/definitions/common/classifier.ts index 7d2b5a23..94688541 100644 --- a/functions/src/definitions/common/classifier.ts +++ b/functions/src/definitions/common/classifier.ts @@ -1,14 +1,20 @@ import { getL1Category } from "./machineLearningServer/operations" +import * as functions from "firebase-functions" async function classifyText(text: string): Promise { 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" } } diff --git a/functions/src/definitions/eventHandlers/onMessageUpdate.ts b/functions/src/definitions/eventHandlers/onMessageUpdate.ts index 6fe53e2c..79b2ca7a 100644 --- a/functions/src/definitions/eventHandlers/onMessageUpdate.ts +++ b/functions/src/definitions/eventHandlers/onMessageUpdate.ts @@ -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. diff --git a/functions/src/definitions/eventHandlers/userHandlers.ts b/functions/src/definitions/eventHandlers/userHandlers.ts index 566ab213..d565ba4e 100644 --- a/functions/src/definitions/eventHandlers/userHandlers.ts +++ b/functions/src/definitions/eventHandlers/userHandlers.ts @@ -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,