diff --git a/.env.example b/.env.example index 199070f..2355b1c 100644 --- a/.env.example +++ b/.env.example @@ -54,6 +54,7 @@ TRANSCRIPTION_LANGUAGE="auto" # Example: "pt" (portuguese), "en" (english), "es # Enable or disable Google Calendar. If enabled, the bot will be able to create and manage events in your Google Calendar. # You will need to use OpenAI's GPT model to use this feature. Therefore, you will need to have an OpenAI API key. +# You can learn more about this feature here: https://js.langchain.com/docs/integrations/tools/google_calendar # Accepted values are "true" or "false" ENABLE_GOOGLE_CALENDAR="false" @@ -65,9 +66,15 @@ GOOGLE_CALENDAR_CALENDAR_ID="" # This is the API that OpenRouter will use to search in the web for information. # You can get one at https://www.searchapi.io/ +# You can learn more about this feature here: https://js.langchain.com/docs/integrations/tools/searchapi # Leave this empty if you're not going to use it SEARCH_API="" +# Enable or disable the web browser tool. +# This uses OpenAI's GPT model, so an OpenAI API key is required. +# You can learn more about this feature here: https://js.langchain.com/docs/integrations/tools/webbrowser +ENABLE_WEB_BROWSER_TOOL="false" + # This is the memory that OpenRouter will use, options are "buffer" or "summary" # Buffer saves OPENROUTER_MSG_MEMORY_LIMIT ammount of messages in memory to use for context, anything past that is ignored # Summary makes a summary of the conversation and uses that as context. @@ -82,6 +89,7 @@ OPENROUTER_MEMORY_TYPE="summary" # The one here will usually be an okay model with free cost, but be careful because pricing may change. SUMMARY_LLM_MODEL="gryphe/mythomist-7b:free" + # ============================== # Optional Environment Variables # ============================== diff --git a/package.json b/package.json index 0f4a9f5..4885a5f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "whatsapp-ai-assistant", - "version": "2.4.0", + "version": "2.4.1", "description": "WhatsApp chatbot", "module": "src/index.ts", "type": "module", diff --git a/src/clients/tools-openrouter.ts b/src/clients/tools-openrouter.ts index d4e510d..35c9f6f 100644 --- a/src/clients/tools-openrouter.ts +++ b/src/clients/tools-openrouter.ts @@ -1,6 +1,7 @@ import { SearchApi } from "@langchain/community/tools/searchapi"; import { ENABLE_GOOGLE_CALENDAR, + ENABLE_WEB_BROWSER_TOOL, GOOGLE_CALENDAR_CALENDAR_ID, GOOGLE_CALENDAR_CLIENT_EMAIL, GOOGLE_CALENDAR_PRIVATE_KEY, @@ -17,19 +18,25 @@ import { const OPENROUTER_BASE_URL = "https://openrouter.ai"; -const model = new ChatOpenAI( - { - modelName: "openai/gpt-3.5-turbo", - temperature: 0, - openAIApiKey: OPENROUTER_API_KEY, - }, - { - basePath: `${OPENROUTER_BASE_URL}/api/v1`, - } -); let googleCalendarCreateTool = null; let googleCalendarViewTool = null; +let searchTool = null; +let webBrowserTool = null; +if (ENABLE_WEB_BROWSER_TOOL === "true") { + const model = new ChatOpenAI( + { + modelName: "openai/gpt-3.5-turbo", + temperature: 0, + openAIApiKey: OPENROUTER_API_KEY, + }, + { + basePath: `${OPENROUTER_BASE_URL}/api/v1`, + } + ); + const embeddings = new OpenAIEmbeddings(); + webBrowserTool = new WebBrowser({ model, embeddings }); +} if (ENABLE_GOOGLE_CALENDAR === "true") { const googleCalendarModel = new OpenAI({ @@ -54,21 +61,15 @@ if (ENABLE_GOOGLE_CALENDAR === "true") { googleCalendarViewTool = new GoogleCalendarViewTool(googleCalendarParams); } - -const embeddings = new OpenAIEmbeddings(); - -let searchTool = null; if (SEARCH_API !== '') { searchTool = new SearchApi(SEARCH_API, { engine: "google_news", }); } -const webBrowserTool = new WebBrowser({ model, embeddings }); - export const tools = [ ...(searchTool ? [searchTool] : []), - webBrowserTool, + ...(webBrowserTool ? [webBrowserTool] : []), ...(googleCalendarCreateTool ? [googleCalendarCreateTool] : []), ...(googleCalendarViewTool ? [googleCalendarViewTool] : []), ]; diff --git a/src/constants.ts b/src/constants.ts index 735f7dd..f8bf6e1 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -51,4 +51,5 @@ export const BING_COOKIES = process.env.BING_COOKIES as string; export const ENABLE_GOOGLE_CALENDAR = process.env.ENABLE_GOOGLE_CALENDAR as string; export const GOOGLE_CALENDAR_CLIENT_EMAIL = process.env.GOOGLE_CALENDAR_CLIENT_EMAIL as string; export const GOOGLE_CALENDAR_PRIVATE_KEY = process.env.GOOGLE_CALENDAR_PRIVATE_KEY as string; -export const GOOGLE_CALENDAR_CALENDAR_ID = process.env.GOOGLE_CALENDAR_CALENDAR_ID as string; \ No newline at end of file +export const GOOGLE_CALENDAR_CALENDAR_ID = process.env.GOOGLE_CALENDAR_CALENDAR_ID as string; +export const ENABLE_WEB_BROWSER_TOOL = process.env.ENABLE_WEB_BROWSER_TOOL as string; \ No newline at end of file diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index 0c835c8..36d19f0 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -26,6 +26,13 @@ export function checkEnv() { ); } } + if (process.env.ENABLE_WEB_BROWSER_TOOL === "true") { + if (!process.env.OPENAI_API_KEY || process.env.OPENAI_API_KEY === "") { + throw new Error( + `Invalid OPENAI_API_KEY="${process.env.OPENAI_API_KEY}" provided. Please check the OPENAI_API_KEY variable in your .env file.` + ); + } + } } if (!process.env.BING_COOKIES) {