Skip to content

Commit

Permalink
fix: Openai key failure error (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luisotee authored Mar 14, 2024
1 parent 2358678 commit 303a866
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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.
Expand All @@ -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
# ==============================
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
35 changes: 18 additions & 17 deletions src/clients/tools-openrouter.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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({
Expand All @@ -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] : []),
];
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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;
7 changes: 7 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 303a866

Please sign in to comment.