I have a Streamchat webhook that triggers a Firebase Cloud Function everytime a chat is sent. The webhook expects a response within 3 seconds but my logic takes longer than that to run. How can I make my cloud function close the webhook within the 3 seconds and continue running? The function is also taking a long time to actually send the response message to the chat.
export const chatBot = onRequest(
{ timeoutSeconds: 15, region: ["europe-west2"], maxInstances: 5 },
async (request, response) => {
try {
if (request.body.user.id !== "system-user") {
//I thought this would close the webhook before continuing to run the function
response.status(200);
const client = StreamChat.getInstance("abc", "abc123");
const signature = request.headers["x-signature"] as string;
const valid = client.verifyWebhook(request.rawBody, signature);
if (!valid) {
return;
} else {
const channel = client.channel(
request?.body?.channel?.type,
request?.body?.channel?.id
);
const messageHistory = new FirestoreChatMessageHistory({
collections: ["users", "chats"],
docs: [request.body.user.id, request.body.channel.name],
sessionId: request.body.user.id,
userId: request.body.user.id,
config: {
projectId: "xyz",
credential: admin.credential.cert({
projectId: "xyz",
privateKey:
"-----BEGIN PRIVATE KEY----------END PRIVATE KEY-----n",
clientEmail: "[email protected]",
}),
},
});
const llm = new ChatOpenAI({
modelName: "gpt-3.5-turbo-1106",
temperature: 0,
openAIApiKey: "abc123",
});
const prompt = ChatPromptTemplate.fromMessages([
["system", "You are a helpful assistant"],
new MessagesPlaceholder("chat_history"),
["user", "{input}"],
new MessagesPlaceholder("agent_scratchpad"),
]);
const tools = [
new DynamicTool({
name: "date-time",
description:
"Call this to get the date and time right now. Input should be an empty string.",
func: async () => new Date(Date.now()).toString(),
}),
];
const agent = await createOpenAIToolsAgent({
llm,
tools,
prompt,
});
const agentExecutor = new AgentExecutor({
agent,
tools,
});
const conversationalAgentExecutor = new RunnableWithMessageHistory({
runnable: agentExecutor,
getMessageHistory: (_sessionId) => messageHistory,
inputMessagesKey: "input",
outputMessagesKey: "output",
historyMessagesKey: "chat_history",
});
const chatBotRes = await conversationalAgentExecutor.invoke(
{
input: request.body.message.text,
},
{
configurable: {
sessionId: request?.body?.user?.id,
},
}
);
await channel.sendMessage({
user_id: "system-user",
text: chatBotRes.output,
});
}
}
response.status(200).end();
return;
} catch (err) {
logger.info(err);
response.status(200).end();
return;
}
}
);
Is there something wrong? I keep getting an axios timeout error. Despite the timeout error, it still works but I don’t know if it causes any performance issues or if this is why it’s taking so long for the message to be sent back. The chatbot response takes about 3 minutes to be sent to the chat. Thank you for your help!