Skip to content

Commit 7449ba0

Browse files
authored
feat: Dalle tool and basic Calculator tool (#286)
* feat: dalle tool * feat: basic calculator tool
1 parent 1f488b0 commit 7449ba0

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
lines changed

.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ SEARCH_API=""
7575
# You can learn more about this feature here: https://js.langchain.com/docs/integrations/tools/webbrowser
7676
ENABLE_WEB_BROWSER_TOOL="false"
7777

78+
# Enable or disable the Dalle image generation tool.
79+
# This uses OpenAI's DALL·E model, so an OpenAI API key is required.
80+
# You can learn more about this feature here: https://js.langchain.com/docs/integrations/tools/dalle
81+
ENABLE_DALLE_TOOL="false"
82+
83+
# Model to use for Dalle.
84+
DALLE_MODEL="dall-e-3" # Options are "dall-e-3" or "dall-e-2"
85+
86+
# There are more options for dalle such as image quality, quantity of images generated, etc. You can change it at src/clients/tools-openrouter.ts
87+
88+
7889
# This is the memory that OpenRouter will use, options are "buffer" or "summary"
7990
# Buffer saves OPENROUTER_MSG_MEMORY_LIMIT ammount of messages in memory to use for context, anything past that is ignored
8091
# Summary makes a summary of the conversation and uses that as context.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "whatsapp-ai-assistant",
3-
"version": "2.4.1",
3+
"version": "2.5.0",
44
"description": "WhatsApp chatbot",
55
"module": "src/index.ts",
66
"type": "module",

src/clients/tools-openrouter.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
import {
2+
GoogleCalendarCreateTool,
3+
GoogleCalendarViewTool,
4+
} from "@langchain/community/tools/google_calendar";
15
import { SearchApi } from "@langchain/community/tools/searchapi";
6+
import { ChatOpenAI, DallEAPIWrapper, OpenAI, OpenAIEmbeddings } from "@langchain/openai";
7+
import { Calculator } from "langchain/tools/calculator";
8+
import { WebBrowser } from "langchain/tools/webbrowser";
29
import {
10+
DALLE_MODEL,
11+
ENABLE_DALLE_TOOL,
312
ENABLE_GOOGLE_CALENDAR,
413
ENABLE_WEB_BROWSER_TOOL,
514
GOOGLE_CALENDAR_CALENDAR_ID,
@@ -9,19 +18,22 @@ import {
918
OPENROUTER_API_KEY,
1019
SEARCH_API
1120
} from "../constants";
12-
import { WebBrowser } from "langchain/tools/webbrowser";
13-
import { ChatOpenAI, OpenAI, OpenAIEmbeddings } from "@langchain/openai";
14-
import {
15-
GoogleCalendarCreateTool,
16-
GoogleCalendarViewTool,
17-
} from "@langchain/community/tools/google_calendar";
1821

1922
const OPENROUTER_BASE_URL = "https://openrouter.ai";
2023

2124
let googleCalendarCreateTool = null;
2225
let googleCalendarViewTool = null;
2326
let searchTool = null;
2427
let webBrowserTool = null;
28+
let dalleTool = null;
29+
30+
if (ENABLE_DALLE_TOOL === "true") {
31+
dalleTool = new DallEAPIWrapper({
32+
n: 1, // Default
33+
modelName: DALLE_MODEL, // Default
34+
openAIApiKey: OPENAI_API_KEY, // Default
35+
});
36+
}
2537

2638
if (ENABLE_WEB_BROWSER_TOOL === "true") {
2739
const model = new ChatOpenAI(
@@ -67,10 +79,14 @@ if (SEARCH_API !== '') {
6779
});
6880
}
6981

82+
const calculatorTool = new Calculator()
83+
7084
export const tools = [
7185
...(searchTool ? [searchTool] : []),
7286
...(webBrowserTool ? [webBrowserTool] : []),
7387
...(googleCalendarCreateTool ? [googleCalendarCreateTool] : []),
7488
...(googleCalendarViewTool ? [googleCalendarViewTool] : []),
89+
...(dalleTool ? [dalleTool] : []),
90+
calculatorTool
7591
];
7692
export const toolNames = tools.map((tool) => tool.name);

src/constants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@ export const ENABLE_GOOGLE_CALENDAR = process.env.ENABLE_GOOGLE_CALENDAR as stri
5252
export const GOOGLE_CALENDAR_CLIENT_EMAIL = process.env.GOOGLE_CALENDAR_CLIENT_EMAIL as string;
5353
export const GOOGLE_CALENDAR_PRIVATE_KEY = process.env.GOOGLE_CALENDAR_PRIVATE_KEY as string;
5454
export const GOOGLE_CALENDAR_CALENDAR_ID = process.env.GOOGLE_CALENDAR_CALENDAR_ID as string;
55-
export const ENABLE_WEB_BROWSER_TOOL = process.env.ENABLE_WEB_BROWSER_TOOL as string;
55+
export const ENABLE_WEB_BROWSER_TOOL = process.env.ENABLE_WEB_BROWSER_TOOL as string;
56+
export const ENABLE_DALLE_TOOL = process.env.ENABLE_DALLE_TOOL as string;
57+
export const DALLE_MODEL = process.env.DALLE_MODEL as string;

src/helpers/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ export function checkEnv() {
3333
);
3434
}
3535
}
36+
if (process.env.ENABLE_DALLE_TOOL === "true") {
37+
if (!process.env.OPENAI_API_KEY || process.env.OPENAI_API_KEY === "") {
38+
throw new Error(
39+
`Invalid OPENAI_API_KEY="${process.env.OPENAI_API_KEY}" provided. Please check the OPENAI_API_KEY variable in your .env file.`
40+
);
41+
}
42+
if (process.env.DALLE_MODEL !== "dall-e-3" && process.env.DALLE_MODEL !== "dall-e-2") {
43+
throw new Error(
44+
`Invalid DALLE_MODEL="${process.env.DALLE_MODEL}" provided. Please check the DALLE_MODEL variable in your .env file.`
45+
);
46+
}
47+
}
3648
}
3749

3850
if (!process.env.BING_COOKIES) {

0 commit comments

Comments
 (0)