Skip to main content
Open In ColabOpen on GitHub

Slack 工具包

这将帮助您开始使用Slack 工具包。有关所有SlackToolkit功能和配置的详细文档,请前往API参考

设置

要使用此工具包,您需要获取一个令牌,如Slack API文档中所述。一旦您收到SLACK_USER_TOKEN,您可以将其作为环境变量输入到下方。

import getpass
import os

if not os.getenv("SLACK_USER_TOKEN"):
os.environ["SLACK_USER_TOKEN"] = getpass.getpass("Enter your Slack user token: ")

如果你想从单个工具的运行中获取自动追踪,你也可以通过取消注释以下内容来设置你的 LangSmith API 密钥:

# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"

安装

这个工具包位于langchain-community包中。我们还需要Slack SDK:

%pip install -qU langchain-community slack_sdk

可选地,我们可以安装beautifulsoup4来帮助解析HTML消息:

%pip install -qU beautifulsoup4 # This is optional but is useful for parsing HTML messages

实例化

现在我们可以实例化我们的工具包:

from langchain_community.agent_toolkits import SlackToolkit

toolkit = SlackToolkit()
API Reference:SlackToolkit

工具

查看可用工具:

tools = toolkit.get_tools()

tools
[SlackGetChannel(client=<slack_sdk.web.client.WebClient object at 0x113caa8c0>),
SlackGetMessage(client=<slack_sdk.web.client.WebClient object at 0x113caa4d0>),
SlackScheduleMessage(client=<slack_sdk.web.client.WebClient object at 0x113caa440>),
SlackSendMessage(client=<slack_sdk.web.client.WebClient object at 0x113caa410>)]

此工具包加载:

在代理中使用

让我们为代理配备Slack工具包,并查询有关频道的信息。

from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

llm = ChatOpenAI(model="gpt-4o-mini")

agent_executor = create_react_agent(llm, tools)
example_query = "When was the #general channel created?"

events = agent_executor.stream(
{"messages": [("user", example_query)]},
stream_mode="values",
)
for event in events:
message = event["messages"][-1]
if message.type != "tool": # mask sensitive information
event["messages"][-1].pretty_print()
================================ Human Message =================================

When was the #general channel created?
================================== Ai Message ==================================
Tool Calls:
get_channelid_name_dict (call_NXDkALjoOx97uF1v0CoZTqtJ)
Call ID: call_NXDkALjoOx97uF1v0CoZTqtJ
Args:
================================== Ai Message ==================================

The #general channel was created on timestamp 1671043305.
example_query = "Send a friendly greeting to channel C072Q1LP4QM."

events = agent_executor.stream(
{"messages": [("user", example_query)]},
stream_mode="values",
)
for event in events:
message = event["messages"][-1]
if message.type != "tool": # mask sensitive information
event["messages"][-1].pretty_print()
================================ Human Message =================================

Send a friendly greeting to channel C072Q1LP4QM.
================================== Ai Message ==================================
Tool Calls:
send_message (call_xQxpv4wFeAZNZgSBJRIuaizi)
Call ID: call_xQxpv4wFeAZNZgSBJRIuaizi
Args:
message: Hello! Have a great day!
channel: C072Q1LP4QM
================================== Ai Message ==================================

I have sent a friendly greeting to the channel C072Q1LP4QM.

API参考

有关所有SlackToolkit功能和配置的详细文档,请访问API参考


这个页面有帮助吗?