跳到主要内容
Open In ColabOpen on GitHub

Discord

本笔记本概述了如何在 langchain_discord 中开始使用 Discord 工具。有关每个工具和配置的更多详细信息,请参阅存储库或相关文档页面中的文档字符串。

概述

集成详细信息

可序列化JS 支持最新包
DiscordReadMessages, DiscordSendMessagelangchain-discord-shikensoN/A待定PyPI - Version

工具功能

  • DiscordReadMessages:从指定频道读取消息。
  • DiscordSendMessage:向指定频道发送消息。

设置

集成由 langchain-discord-shikenso 包提供。按如下方式安装它

%pip install --quiet -U langchain-discord-shikenso

凭据

此集成需要您设置 DISCORD_BOT_TOKEN 作为环境变量,以便通过 Discord API 进行身份验证。

export DISCORD_BOT_TOKEN="your-bot-token"
import getpass
import os

# Example prompt to set your token if not already set:
# if not os.environ.get("DISCORD_BOT_TOKEN"):
# os.environ["DISCORD_BOT_TOKEN"] = getpass.getpass("DISCORD Bot Token:\n")

您可以选择设置 LangSmith 以进行跟踪或可观察性

# os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass()

实例化

以下示例显示了如何在 langchain_discord 中实例化 Discord 工具。根据您的具体用法进行调整。

from langchain_discord.tools.discord_read_messages import DiscordReadMessages
from langchain_discord.tools.discord_send_messages import DiscordSendMessage

read_tool = DiscordReadMessages()
send_tool = DiscordSendMessage()

# Example usage:
# response = read_tool({"channel_id": "1234567890", "limit": 5})
# print(response)
#
# send_result = send_tool({"message": "Hello from notebook!", "channel_id": "1234567890"})
# print(send_result)

调用

使用 args 直接调用

以下是使用字典中的关键字参数调用工具的简单示例。

invocation_args = {"channel_id": "1234567890", "limit": 3}
response = read_tool(invocation_args)
response

使用 ToolCall 调用

如果您有模型生成的 ToolCall,请将其传递给 tool.invoke(),格式如下所示。

tool_call = {
"args": {"channel_id": "1234567890", "limit": 2},
"id": "1",
"name": read_tool.name,
"type": "tool_call",
}

tool.invoke(tool_call)

链接

以下是一个更完整的示例,展示了如何在链或代理中集成 DiscordReadMessagesDiscordSendMessage 工具以及 LLM。此示例假设您有一个函数(如 create_react_agent),该函数设置了一个 LangChain 风格的代理,能够在适当的时候调用工具。

# Example: Using Discord Tools in an Agent

from langgraph.prebuilt import create_react_agent
from langchain_discord.tools.discord_read_messages import DiscordReadMessages
from langchain_discord.tools.discord_send_messages import DiscordSendMessage

# 1. Instantiate or configure your language model
# (Replace with your actual LLM, e.g., ChatOpenAI(temperature=0))
llm = ...

# 2. Create instances of the Discord tools
read_tool = DiscordReadMessages()
send_tool = DiscordSendMessage()

# 3. Build an agent that has access to these tools
agent_executor = create_react_agent(llm, [read_tool, send_tool])

# 4. Formulate a user query that may invoke one or both tools
example_query = "Please read the last 5 messages in channel 1234567890"

# 5. Execute the agent in streaming mode (or however your code is structured)
events = agent_executor.stream(
{"messages": [("user", example_query)]},
stream_mode="values",
)

# 6. Print out the model's responses (and any tool outputs) as they arrive
for event in events:
event["messages"][-1].pretty_print()
API 参考:create_react_agent

API 参考

请参阅以下文档字符串:

以获取用法详情、参数和高级配置。


此页面是否对您有帮助?