Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(community): Add Slack Tool #7291

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions docs/core_docs/docs/integrations/tools/slack.mdx
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
hide_table_of_contents: true
---

import CodeBlock from "@theme/CodeBlock";

# Slack Tool

The Slack Tool gives your agent the ability to search, schedule and post messages to Slack Channels.


## Setup

To use the Slack Tool you need to install the following official peer dependency:

```bash npm2yarn
npm install @slack/web-api
```

## Usage, standalone

import ToolExample from "@examples/tools/slack.ts";

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/openai @langchain/core
```

<CodeBlock language="typescript">{ToolExample}</CodeBlock>

## Usage, in an Agent

import AgentExample from "@examples/agents/slack.ts";

<CodeBlock language="typescript">{AgentExample}</CodeBlock>

## Related

- Tool [conceptual guide](/docs/concepts/tools)
- Tool [how-to guides](/docs/how_to/#tools)
68 changes: 68 additions & 0 deletions examples/src/agents/slack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
SlackGetMessagesTool,
SlackGetChannelsTool,
SlackScheduleMessageTool,
SlackPostMessageTool,
} from "@langchain/community/tools/slack";
import { AgentExecutor, createOpenAIToolsAgent } from "langchain/agents";
import { HumanMessage } from "@langchain/core/messages";
import { ChatOpenAI } from "@langchain/openai";
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";

const chat = new ChatOpenAI({
model: "gpt-3.5-turbo-1106",
temperature: 0,
});

const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful assistant. You may not need to use tools for every query - the user may just want to chat!",
],
new MessagesPlaceholder("messages"),
new MessagesPlaceholder("agent_scratchpad"),
]);

const tools = [
new SlackGetMessagesTool(),
new SlackGetChannelsTool(),
new SlackScheduleMessageTool(),
new SlackPostMessageTool(),
];

const agent = await createOpenAIToolsAgent({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the createReactAgent method from @langchain/langgraph

llm: chat,
tools,
prompt,
});

const agentExecutor = new AgentExecutor({ agent, tools, maxIterations: 20, });

let res = await agentExecutor.invoke({
messages: [
new HumanMessage("send a greeting message to the general channel"),
],
});

console.log(res.output);

res = await agentExecutor.invoke({
messages: [
new HumanMessage("Schedule a greeting message to the general channel at 11:15 am on December 12, 2024 in New York time."),
],
});

console.log(res.output);

res = await agentExecutor.invoke({
messages: [
new HumanMessage("When did I say 'hi' in the slack channels?"),
],
});

console.log(res.output);


37 changes: 37 additions & 0 deletions examples/src/tools/slack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
SlackGetChannelsTool,
SlackGetMessagesTool,
SlackScheduleMessageTool,
SlackPostMessageTool,
} from "@langchain/community/tools/slack";

// Get messages given a query
const getMessageTool = new SlackGetMessagesTool();
const messageResults = await getMessageTool.invoke("Hi");
console.log(messageResults);

// Get information about Slack channels
const getChannelTool = new SlackGetChannelsTool();
const channelResults = await getChannelTool.invoke("");
console.log(channelResults);

// Schedule a slack message given a message, channel and time
const scheduleMessageTool = new SlackScheduleMessageTool();
const scheduleResults = await scheduleMessageTool.invoke(
JSON.stringify({
text: "Test",
channel_id: "C1234567890",
post_at: "2024-12-09T10:30:00+03:00",
})
);
console.log(scheduleResults);

// Post a message to a given channel
const postMessageTool = new SlackPostMessageTool();
const postResult = await postMessageTool.invoke(
JSON.stringify({
text: "Test",
channel_id: "C1234567890",
})
);
console.log(postResult);
4 changes: 4 additions & 0 deletions libs/langchain-community/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ tools/serper.cjs
tools/serper.js
tools/serper.d.ts
tools/serper.d.cts
tools/slack.cjs
tools/slack.js
tools/slack.d.ts
tools/slack.d.cts
tools/stackexchange.cjs
tools/stackexchange.js
tools/stackexchange.d.ts
Expand Down
2 changes: 2 additions & 0 deletions libs/langchain-community/langchain.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const config = {
"tools/searxng_search": "tools/searxng_search",
"tools/serpapi": "tools/serpapi",
"tools/serper": "tools/serper",
"tools/slack": "tools/slack",
"tools/stackexchange": "tools/stackexchange",
"tools/tavily_search": "tools/tavily_search",
"tools/wikipedia_query_run": "tools/wikipedia_query_run",
Expand Down Expand Up @@ -338,6 +339,7 @@ export const config = {
"tools/discord",
"tools/gmail",
"tools/google_calendar",
"tools/slack",
"agents/toolkits/aws_sfn",
"agents/toolkits/stagehand",
"callbacks/handlers/llmonitor",
Expand Down
18 changes: 18 additions & 0 deletions libs/langchain-community/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"@qdrant/js-client-rest": "^1.8.2",
"@raycast/api": "^1.83.1",
"@rockset/client": "^0.9.1",
"@slack/web-api": "^7.7.0",
"@smithy/eventstream-codec": "^2.0.5",
"@smithy/protocol-http": "^3.0.6",
"@smithy/signature-v4": "^2.0.10",
Expand Down Expand Up @@ -265,6 +266,7 @@
"@qdrant/js-client-rest": "^1.8.2",
"@raycast/api": "^1.55.2",
"@rockset/client": "^0.9.1",
"@slack/web-api": "^7.7.0",
"@smithy/eventstream-codec": "^2.0.5",
"@smithy/protocol-http": "^3.0.6",
"@smithy/signature-v4": "^2.0.10",
Expand Down Expand Up @@ -473,6 +475,9 @@
"@rockset/client": {
"optional": true
},
"@slack/web-api": {
"optional": true
},
"@smithy/eventstream-codec": {
"optional": true
},
Expand Down Expand Up @@ -928,6 +933,15 @@
"import": "./tools/serper.js",
"require": "./tools/serper.cjs"
},
"./tools/slack": {
"types": {
"import": "./tools/slack.d.ts",
"require": "./tools/slack.d.cts",
"default": "./tools/slack.d.ts"
},
"import": "./tools/slack.js",
"require": "./tools/slack.cjs"
},
"./tools/stackexchange": {
"types": {
"import": "./tools/stackexchange.d.ts",
Expand Down Expand Up @@ -3197,6 +3211,10 @@
"tools/serper.js",
"tools/serper.d.ts",
"tools/serper.d.cts",
"tools/slack.cjs",
"tools/slack.js",
"tools/slack.d.ts",
"tools/slack.d.cts",
"tools/stackexchange.cjs",
"tools/stackexchange.js",
"tools/stackexchange.d.ts",
Expand Down
1 change: 1 addition & 0 deletions libs/langchain-community/src/load/import_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const optionalImportEntrypoints: string[] = [
"langchain_community/tools/discord",
"langchain_community/tools/gmail",
"langchain_community/tools/google_calendar",
"langchain_community/tools/slack",
"langchain_community/agents/toolkits/aws_sfn",
"langchain_community/agents/toolkits/stagehand",
"langchain_community/embeddings/bedrock",
Expand Down
Loading