本页还没有译文
本页 25 段还没译成简体中文,下面显示的是英文原文。Discord integration
还没翻译译自英文(docs.langchain.com)本页 25 段都还没译
This guide provides a quick overview for getting started with Discord tooling in langchain_discord. For more details on each tool and configuration, see the docstrings in your repository or relevant doc pages.
Overview
Integration details
| Class | Package | Serializable | JS support | Version |
|---|---|---|---|---|
DiscordReadMessages, DiscordSendMessage | langchain-discord-shikenso | N/A | TBD |
Tool features
DiscordReadMessages: Reads messages from a specified channel.DiscordSendMessage: Sends messages to a specified channel.
Setup
The integration is provided by the langchain-discord-shikenso package. Install it as follows:
pip install --quiet -U langchain-discord-shikensoCredentials
This integration requires you to set DISCORD_BOT_TOKEN as an environment variable to authenticate with the 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")You can optionally set up LangSmith for tracing or observability:
os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass()Instantiation
Below is an example showing how to instantiate the Discord tools in langchain_discord. Adjust as needed for your specific usage.
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)Invocation
Direct invocation with args
Below is a simple example of calling the tool with keyword arguments in a dictionary.
invocation_args = {"channel_id": "1234567890", "limit": 3}
response = read_tool(invocation_args)
responseInvocation with ToolCall
If you have a model-generated ToolCall, pass it to tool.invoke() in the format shown below.
tool_call = {
"args": {"channel_id": "1234567890", "limit": 2},
"id": "1",
"name": read_tool.name,
"type": "tool_call",
}
tool.invoke(tool_call)Chaining
Below is a complete example showing how you might integrate the DiscordReadMessages and DiscordSendMessage tools in a chain or agent with an LLM. This example assumes you have a function (like create_agent) that sets up a LangChain-style agent capable of calling tools when appropriate.
# Example: Using Discord Tools in an Agent
from langchain.agents import create_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))
model = ...
# 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_agent(model, [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)
stream = agent_executor.stream_events(
{"messages": [("user", example_query)]},
version="v3",
)
# 6. Print out the model's responses (and any tool outputs) as they arrive
for snapshot in stream.values:
snapshot["messages"][-1].pretty_print()API reference
See the docstrings in:
for usage details, parameters, and advanced configurations.