-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
MadisonMajarais
wants to merge
1
commit into
langchain-ai:main
Choose a base branch
from
MadisonMajarais:slack-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the |
||
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); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use the proper template from here?
https://github.com/langchain-ai/langchainjs/tree/main/libs/langchain-scripts/src/cli/docs/templates