From 46391e31ec373943eab42f6561b0358e24e27aaa Mon Sep 17 00:00:00 2001 From: your_username Date: Tue, 5 Dec 2023 18:38:01 -0500 Subject: [PATCH 1/3] updated to single object instead of multiple args --- .../src/agents/tests/discord.int.test.ts | 4 +- langchain/src/tools/discord.ts | 145 +++++++++++------- langchain/src/tools/tests/discord.int.test.ts | 32 ++-- 3 files changed, 111 insertions(+), 70 deletions(-) diff --git a/langchain/src/agents/tests/discord.int.test.ts b/langchain/src/agents/tests/discord.int.test.ts index f5710bdd5842..65780a7824d2 100644 --- a/langchain/src/agents/tests/discord.int.test.ts +++ b/langchain/src/agents/tests/discord.int.test.ts @@ -9,14 +9,14 @@ test.skip("DiscordSendMessagesTool should tell a joke in the discord channel", a temperature: 0, }); - const tools = [new DiscordSendMessagesTool(), new DadJokeAPI()]; + const tools = [new DiscordSendMessagesTool({}), new DadJokeAPI()]; const executor = await initializeAgentExecutorWithOptions(tools, model, { agentType: "zero-shot-react-description", verbose: true, }); - const res = await executor.call({ + const res = await executor.invoke({ input: `Tell a joke in the discord channel`, }); diff --git a/langchain/src/tools/discord.ts b/langchain/src/tools/discord.ts index efc3ee5a0ea9..cf8cb31e6fae 100644 --- a/langchain/src/tools/discord.ts +++ b/langchain/src/tools/discord.ts @@ -8,6 +8,21 @@ import { import { getEnvironmentVariable } from "../util/env.js"; import { Tool } from "./base.js"; +interface DiscordGetMessagesToolParams { + botToken?: string; + messageLimit?: number; +} + +interface DiscordSendMessageToolParams { + botToken?: string; + channelId?: string; +} + +interface DiscordToolParams { + botToken?: string; +} + + /** * A tool for retrieving messages from a discord channel using a bot. * It extends the base Tool class and implements the _call method to @@ -36,19 +51,23 @@ export class DiscordGetMessagesTool extends Tool { intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages], }); - constructor( - botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN"), - messageLimit: number | undefined = 10 - ) { - super(...arguments); + constructor(params: DiscordGetMessagesToolParams) { + super(); + + const { botToken, messageLimit = 10 } = params; if (!botToken) { - throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file." - ); + const envBotToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"); + if (!envBotToken) { + throw new Error( + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" + ); + } + this.botToken = envBotToken; + } else { + this.botToken = botToken; } - this.botToken = botToken; this.messageLimit = messageLimit; } @@ -96,17 +115,22 @@ export class DiscordGetGuildsTool extends Tool { intents: [GatewayIntentBits.Guilds], }); - constructor( - botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN") - ) { - super(...arguments); + constructor(params: DiscordToolParams) { + super(); + + const { botToken } = params; if (!botToken) { - throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file." - ); + const envBotToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"); + if (!envBotToken) { + throw new Error( + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" + ); + } + this.botToken = envBotToken; + } else { + this.botToken = botToken; } - this.botToken = botToken; } /** @ignore */ @@ -149,17 +173,22 @@ export class DiscordGetTextChannelsTool extends Tool { intents: [GatewayIntentBits.Guilds], }); - constructor( - botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN") - ) { - super(...arguments); + constructor(params: DiscordToolParams) { + super(); + + const { botToken } = params; if (!botToken) { - throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file." - ); + const envBotToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"); + if (!envBotToken) { + throw new Error( + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" + ); + } + this.botToken = envBotToken; + } else { + this.botToken = botToken; } - this.botToken = botToken; } /** @ignore */ @@ -208,24 +237,35 @@ export class DiscordSendMessagesTool extends Tool { intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages], }); - constructor( - botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN"), - channelId: string | undefined = getEnvironmentVariable("DISCORD_CHANNEL_ID") - ) { - super(...arguments); + constructor(params: DiscordSendMessageToolParams) { + super(); + + const { botToken, channelId } = params; if (!botToken) { - throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file." - ); + const envBotToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"); + if (!envBotToken) { + throw new Error( + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" + ); + } + this.botToken = envBotToken; + } else { + this.botToken = botToken; } - this.botToken = botToken; + if (!channelId) { - throw new Error( - "Discord channel not set. You can set it as DISCORD_CHANNEL_ID in your .env file." - ); + const envChannelId = getEnvironmentVariable("DISCORD_CHANNEL_ID"); + if (!envChannelId) { + throw new Error( + "Discord API key not set. You can set it as DISCORD_CHANNEL_ID" + ); + } + this.channelId = envChannelId; + } else { + this.channelId = channelId; } - this.channelId = channelId; + } /** @ignore */ @@ -285,24 +325,23 @@ export class DiscordChannelSearchTool extends Tool { intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages], }); - constructor( - botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN"), - channelId: string | undefined = getEnvironmentVariable("DISCORD_CHANNEL_ID") - ) { - super(...arguments); + constructor(params: DiscordToolParams) { + super(); + + const { botToken } = params; if (!botToken) { - throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file." - ); - } - this.botToken = botToken; - if (!channelId) { - throw new Error( - "Discord channel not set. You can set it as DISCORD_CHANNEL_ID in your .env file." - ); + const envBotToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"); + if (!envBotToken) { + throw new Error( + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" + ); + } + this.botToken = envBotToken; + } else { + this.botToken = botToken; } - this.channelId = channelId; + } /** @ignore */ diff --git a/langchain/src/tools/tests/discord.int.test.ts b/langchain/src/tools/tests/discord.int.test.ts index 1c86f1e5f461..6cd7b142a4cf 100644 --- a/langchain/src/tools/tests/discord.int.test.ts +++ b/langchain/src/tools/tests/discord.int.test.ts @@ -7,50 +7,52 @@ import { DiscordGetTextChannelsTool, } from "../discord.js"; -test.skip("DiscordGetMessagesTool", async () => { - const tool = new DiscordGetMessagesTool(); + +test("DiscordGetMessagesTool", async () => { + const tool = new DiscordGetMessagesTool({}); + try { - const result = await tool.call("1153400523718938780"); + const result = await tool.invoke("1153400523718938780"); console.log(result); } catch (error) { console.error(error); } }); -test.skip("DiscordGetGuildsTool", async () => { - const tool = new DiscordGetGuildsTool(); +test("DiscordGetGuildsTool", async () => { + const tool = new DiscordGetGuildsTool({}); try { - const result = await tool.call(""); + const result = await tool.invoke(""); console.log(result); } catch (error) { console.error(error); } }); -test.skip("DiscordChannelSearchTool", async () => { - const tool = new DiscordChannelSearchTool(); +test("DiscordChannelSearchTool", async () => { + const tool = new DiscordChannelSearchTool({}); try { - const result = await tool.call("Test"); + const result = await tool.invoke("Test"); console.log(result); } catch (error) { console.error(error); } }); -test.skip("DiscordGetTextChannelsTool", async () => { - const tool = new DiscordGetTextChannelsTool(); +test("DiscordGetTextChannelsTool", async () => { + const tool = new DiscordGetTextChannelsTool({}); try { - const result = await tool.call("1153400523718938775"); + const result = await tool.invoke("1153400523718938775"); console.log(result); } catch (error) { console.error(error); } }); -test.skip("DiscordSendMessagesTool", async () => { - const tool = new DiscordSendMessagesTool(); +test("DiscordSendMessagesTool", async () => { + const tool = new DiscordSendMessagesTool({}); try { - const result = await tool.call("test message from new code"); + const result = await tool.invoke("test message from new code"); console.log(result); } catch (err) { console.log(err); From 0d085b6fb350267352a71b8ab3b7e6bc05ee4a61 Mon Sep 17 00:00:00 2001 From: maanethdesilva Date: Tue, 5 Dec 2023 19:24:04 -0500 Subject: [PATCH 2/3] fixed discord arguments to use fields --- .../src/agents/tests/discord.int.test.ts | 4 +- langchain/src/tools/discord.ts | 146 +++++++++--------- langchain/src/tools/tests/discord.int.test.ts | 21 ++- 3 files changed, 84 insertions(+), 87 deletions(-) diff --git a/langchain/src/agents/tests/discord.int.test.ts b/langchain/src/agents/tests/discord.int.test.ts index 65780a7824d2..0d5095582196 100644 --- a/langchain/src/agents/tests/discord.int.test.ts +++ b/langchain/src/agents/tests/discord.int.test.ts @@ -9,7 +9,7 @@ test.skip("DiscordSendMessagesTool should tell a joke in the discord channel", a temperature: 0, }); - const tools = [new DiscordSendMessagesTool({}), new DadJokeAPI()]; + const tools = [new DiscordSendMessagesTool(), new DadJokeAPI()]; const executor = await initializeAgentExecutorWithOptions(tools, model, { agentType: "zero-shot-react-description", @@ -17,7 +17,7 @@ test.skip("DiscordSendMessagesTool should tell a joke in the discord channel", a }); const res = await executor.invoke({ - input: `Tell a joke in the discord channel`, + input: `Tell 2 jokes in the discord channel`, }); console.log(res.output); diff --git a/langchain/src/tools/discord.ts b/langchain/src/tools/discord.ts index cf8cb31e6fae..7144433fbbb6 100644 --- a/langchain/src/tools/discord.ts +++ b/langchain/src/tools/discord.ts @@ -8,21 +8,33 @@ import { import { getEnvironmentVariable } from "../util/env.js"; import { Tool } from "./base.js"; -interface DiscordGetMessagesToolParams { +/** + * Base tool parameters for the Discord tools + */ +interface DiscordToolParams { botToken?: string; +} + +/** + * Tool parameters for the DiscordGetMessagesTool + */ +interface DiscordGetMessagesToolParams extends DiscordToolParams { messageLimit?: number; } -interface DiscordSendMessageToolParams { - botToken?: string; +/** + * Tool parameters for the DiscordSendMessageTool + */ +interface DiscordSendMessageToolParams extends DiscordToolParams { channelId?: string; } -interface DiscordToolParams { - botToken?: string; +/** + * Tool parameters for the DiscordChannelSearch + */ +interface DiscordChannelSearchParams extends DiscordToolParams { + channelId?: string; } - - /** * A tool for retrieving messages from a discord channel using a bot. * It extends the base Tool class and implements the _call method to @@ -51,23 +63,21 @@ export class DiscordGetMessagesTool extends Tool { intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages], }); - constructor(params: DiscordGetMessagesToolParams) { + constructor(fields: DiscordGetMessagesToolParams = {}) { super(); - const { botToken, messageLimit = 10 } = params; + const { + botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"), + messageLimit = 10, + } = fields; if (!botToken) { - const envBotToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"); - if (!envBotToken) { - throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" - ); - } - this.botToken = envBotToken; - } else { - this.botToken = botToken; + throw new Error( + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" + ); } + this.botToken = botToken; this.messageLimit = messageLimit; } @@ -115,22 +125,18 @@ export class DiscordGetGuildsTool extends Tool { intents: [GatewayIntentBits.Guilds], }); - constructor(params: DiscordToolParams) { + constructor(fields: DiscordToolParams = {}) { super(); - const { botToken } = params; + const { botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN") } = + fields || {}; if (!botToken) { - const envBotToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"); - if (!envBotToken) { - throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" - ); - } - this.botToken = envBotToken; - } else { - this.botToken = botToken; + throw new Error( + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" + ); } + this.botToken = botToken; } /** @ignore */ @@ -173,22 +179,18 @@ export class DiscordGetTextChannelsTool extends Tool { intents: [GatewayIntentBits.Guilds], }); - constructor(params: DiscordToolParams) { + constructor(fields: DiscordToolParams = {}) { super(); - const { botToken } = params; + const { botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN") } = + fields || {}; if (!botToken) { - const envBotToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"); - if (!envBotToken) { - throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" - ); - } - this.botToken = envBotToken; - } else { - this.botToken = botToken; + throw new Error( + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" + ); } + this.botToken = botToken; } /** @ignore */ @@ -215,7 +217,7 @@ export class DiscordGetTextChannelsTool extends Tool { /** * A tool for sending messages to a discord channel using a bot. * It extends the base Tool class and implements the _call method to - * perform the retrieve operation. Requires a bot token which can be set + * perform the retrieve operation. Requires a bot token and channelId which can be set * in the environment variables. The _call method takes the message to be * sent as the input argument. */ @@ -237,35 +239,27 @@ export class DiscordSendMessagesTool extends Tool { intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages], }); - constructor(params: DiscordSendMessageToolParams) { + constructor(fields: DiscordSendMessageToolParams = {}) { super(); - const { botToken, channelId } = params; + const { + botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"), + channelId = getEnvironmentVariable("DISCORD_CHANNEL_ID"), + } = fields; if (!botToken) { - const envBotToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"); - if (!envBotToken) { - throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" - ); - } - this.botToken = envBotToken; - } else { - this.botToken = botToken; + throw new Error( + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" + ); } if (!channelId) { - const envChannelId = getEnvironmentVariable("DISCORD_CHANNEL_ID"); - if (!envChannelId) { - throw new Error( - "Discord API key not set. You can set it as DISCORD_CHANNEL_ID" - ); - } - this.channelId = envChannelId; - } else { - this.channelId = channelId; + throw new Error( + "Discord API key not set. You can set it as DISCORD_CHANNEL_ID" + ); } - + this.botToken = botToken; + this.channelId = channelId; } /** @ignore */ @@ -325,23 +319,27 @@ export class DiscordChannelSearchTool extends Tool { intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages], }); - constructor(params: DiscordToolParams) { + constructor(fields: DiscordChannelSearchParams = {}) { super(); - const { botToken } = params; + const { + botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"), + channelId = getEnvironmentVariable("DISCORD_CHANNEL_ID"), + } = fields; if (!botToken) { - const envBotToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"); - if (!envBotToken) { - throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" - ); - } - this.botToken = envBotToken; - } else { - this.botToken = botToken; + throw new Error( + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" + ); } + if (!channelId) { + throw new Error( + "Discord API key not set. You can set it as DISCORD_CHANNEL_ID" + ); + } + this.botToken = botToken; + this.channelId = channelId; } /** @ignore */ @@ -373,7 +371,7 @@ export class DiscordChannelSearchTool extends Tool { return JSON.stringify(results); } catch (err) { await this.client.destroy(); - return "Error sending message"; + return "Error searching through channel."; } } } diff --git a/langchain/src/tools/tests/discord.int.test.ts b/langchain/src/tools/tests/discord.int.test.ts index 6cd7b142a4cf..00f5bbb431d4 100644 --- a/langchain/src/tools/tests/discord.int.test.ts +++ b/langchain/src/tools/tests/discord.int.test.ts @@ -7,9 +7,8 @@ import { DiscordGetTextChannelsTool, } from "../discord.js"; - -test("DiscordGetMessagesTool", async () => { - const tool = new DiscordGetMessagesTool({}); +test.skip("DiscordGetMessagesTool", async () => { + const tool = new DiscordGetMessagesTool(); try { const result = await tool.invoke("1153400523718938780"); @@ -19,8 +18,8 @@ test("DiscordGetMessagesTool", async () => { } }); -test("DiscordGetGuildsTool", async () => { - const tool = new DiscordGetGuildsTool({}); +test.skip("DiscordGetGuildsTool", async () => { + const tool = new DiscordGetGuildsTool(); try { const result = await tool.invoke(""); console.log(result); @@ -29,8 +28,8 @@ test("DiscordGetGuildsTool", async () => { } }); -test("DiscordChannelSearchTool", async () => { - const tool = new DiscordChannelSearchTool({}); +test.skip("DiscordChannelSearchTool", async () => { + const tool = new DiscordChannelSearchTool(); try { const result = await tool.invoke("Test"); console.log(result); @@ -39,8 +38,8 @@ test("DiscordChannelSearchTool", async () => { } }); -test("DiscordGetTextChannelsTool", async () => { - const tool = new DiscordGetTextChannelsTool({}); +test.skip("DiscordGetTextChannelsTool", async () => { + const tool = new DiscordGetTextChannelsTool(); try { const result = await tool.invoke("1153400523718938775"); console.log(result); @@ -49,8 +48,8 @@ test("DiscordGetTextChannelsTool", async () => { } }); -test("DiscordSendMessagesTool", async () => { - const tool = new DiscordSendMessagesTool({}); +test.skip("DiscordSendMessagesTool", async () => { + const tool = new DiscordSendMessagesTool(); try { const result = await tool.invoke("test message from new code"); console.log(result); From 652417cd30496fdd816f255c804dd81d6c54c439 Mon Sep 17 00:00:00 2001 From: maanethdesilva Date: Tue, 5 Dec 2023 19:25:17 -0500 Subject: [PATCH 3/3] updated agent test --- langchain/src/agents/tests/discord.int.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/langchain/src/agents/tests/discord.int.test.ts b/langchain/src/agents/tests/discord.int.test.ts index 0d5095582196..df716f3f0001 100644 --- a/langchain/src/agents/tests/discord.int.test.ts +++ b/langchain/src/agents/tests/discord.int.test.ts @@ -17,7 +17,7 @@ test.skip("DiscordSendMessagesTool should tell a joke in the discord channel", a }); const res = await executor.invoke({ - input: `Tell 2 jokes in the discord channel`, + input: `Tell a joke in the discord channel`, }); console.log(res.output);