From a42d511735807fc4482e86530b2b54058ff3bb02 Mon Sep 17 00:00:00 2001 From: sarthakjdev Date: Sun, 14 Jul 2024 17:15:53 +0530 Subject: [PATCH] chore: wip Signed-off-by: sarthakjdev --- apps/wapi-ai-chatbot/src/index.ts | 48 +++++------ apps/wapi-ai-chatbot/src/utils/cache.ts | 22 ++--- apps/wapi-ai-chatbot/src/utils/gpt.ts | 108 ++++++++++++------------ 3 files changed, 88 insertions(+), 90 deletions(-) diff --git a/apps/wapi-ai-chatbot/src/index.ts b/apps/wapi-ai-chatbot/src/index.ts index d87114a..7759de9 100644 --- a/apps/wapi-ai-chatbot/src/index.ts +++ b/apps/wapi-ai-chatbot/src/index.ts @@ -4,39 +4,39 @@ import { askAi } from './utils/gpt' import { cacheData, computeCacheKey, getCachedData } from './utils/cache' async function init() { - try { - whatsappClient.on('Ready', () => { - console.log('Client is ready') - }) + try { + whatsappClient.on('Ready', () => { + console.log('Client is ready') + }) - whatsappClient.on('Error', error => { - console.error('Error', error.message) - }) + whatsappClient.on('Error', error => { + console.error('Error', error.message) + }) - whatsappClient.on('TextMessage', async (event: TextMessageEvent) => { - const aiResponse = await askAi(event.text.data.text, event.context.from) - await event.reply({ - message: new TextMessage({ - text: aiResponse - }) - }) - }) + whatsappClient.on('TextMessage', async (event: TextMessageEvent) => { + const aiResponse = await askAi(event.text.data.text, event.context.from) + await event.reply({ + message: new TextMessage({ + text: aiResponse + }) + }) + }) - whatsappClient.initiate() - } catch (error) { - console.error(error) - // ! TODO: you may prefer to send a notification to your slack channel or email here - } + whatsappClient.initiate() + } catch (error) { + console.error(error) + // ! TODO: you may prefer to send a notification to your slack channel or email here + } } init().catch(error => console.error(error)) process.on('unhandledRejection', error => { - console.error('unhandledRejection', error) - process.exit(1) + console.error('unhandledRejection', error) + process.exit(1) }) process.on('uncaughtException', error => { - console.error('uncaughtException', error) - process.exit(1) + console.error('uncaughtException', error) + process.exit(1) }) diff --git a/apps/wapi-ai-chatbot/src/utils/cache.ts b/apps/wapi-ai-chatbot/src/utils/cache.ts index 3985e97..1f0d1e3 100644 --- a/apps/wapi-ai-chatbot/src/utils/cache.ts +++ b/apps/wapi-ai-chatbot/src/utils/cache.ts @@ -1,27 +1,27 @@ import { caching } from 'cache-manager' const cacheStore = caching({ - store: 'memory' + store: 'memory' }) export async function cacheData(params: { key: string; data: any; ttl?: number }) { - const { key, ttl, data } = params - await cacheStore.set(key, data, { ...(ttl ? { ttl: ttl } : {}) }) + const { key, ttl, data } = params + await cacheStore.set(key, data, { ...(ttl ? { ttl: ttl } : {}) }) } export async function getCachedData(key: string): Promise { - const response = await cacheStore.get(key) - console.log(response) - return response as T + const response = await cacheStore.get(key) + console.log(response) + return response as T } export function computeCacheKey(params: { id: string; context: string }) { - return `${params.id}-${params.context}` + return `${params.id}-${params.context}` } export function getConversationContextCacheKey(phoneNumber: string) { - return computeCacheKey({ - id: phoneNumber, - context: 'conversation' - }) + return computeCacheKey({ + id: phoneNumber, + context: 'conversation' + }) } diff --git a/apps/wapi-ai-chatbot/src/utils/gpt.ts b/apps/wapi-ai-chatbot/src/utils/gpt.ts index c8e8a61..9092261 100644 --- a/apps/wapi-ai-chatbot/src/utils/gpt.ts +++ b/apps/wapi-ai-chatbot/src/utils/gpt.ts @@ -7,35 +7,34 @@ const organizationId = process.env.OPEN_AI_ORG_ID const projectId = process.env.OPEN_AI_PROJECT_ID if (!openAiApiKey || !organizationId || !projectId) { - throw new Error('OPEN_AI_API_KEY not defined!') + throw new Error('OPEN_AI_API_KEY not defined!') } const OpenApiClient = new OpenAI({ - apiKey: openAiApiKey, - project: projectId, - organization: organizationId + apiKey: openAiApiKey, + project: projectId, + organization: organizationId }) - export async function askAi(message: string, fromPhoneNumber: string): Promise { - try { - const contextCacheKey = getConversationContextCacheKey(fromPhoneNumber) - const context = await getCachedData(contextCacheKey) + try { + const contextCacheKey = getConversationContextCacheKey(fromPhoneNumber) + const context = await getCachedData(contextCacheKey) - let response = 'Sorry, I am not able to understand that.' - const responseCacheKey = computeCacheKey({ - context: 'response_cache', - id: message.trim() - }) - const cachedResponse = await getCachedData(responseCacheKey) - if (cachedResponse) { - response = cachedResponse - } else { - const chatCompletion = await OpenApiClient.chat.completions.create({ - messages: [ - { - role: 'system', - content: `You are an intelligent assistant helping a user with their queries. You can provide information, answer questions, and help the user complete tasks. + let response = 'Sorry, I am not able to understand that.' + const responseCacheKey = computeCacheKey({ + context: 'response_cache', + id: message.trim() + }) + const cachedResponse = await getCachedData(responseCacheKey) + if (cachedResponse) { + response = cachedResponse + } else { + const chatCompletion = await OpenApiClient.chat.completions.create({ + messages: [ + { + role: 'system', + content: `You are an intelligent assistant helping a user with their queries. You can provide information, answer questions, and help the user complete tasks. You are a developer relation engineer for a product named as Wapi.js. Wapi.js is an SDK to build chat bots and to integrate WhatsApp API with your application very easily. The SDK is authored by Sarthak Jain having github at https://github.com/sarthakjdev and is an open-source SDk which is free to use. @@ -44,38 +43,37 @@ export async function askAi(message: string, fromPhoneNumber: string): Promise