Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
OurBuilding Ambient Agents with LangGraph course is now available on LangChain Academy!
Open In ColabOpen on GitHub

Discord

This notebook provides a quick overview for getting started with Discord tooling inlangchain_discord. For more details on each tool and configuration, see the docstrings in your repository or relevant doc pages.

Overview

Integration details

ClassPackageSerializableJS supportPackage latest
DiscordReadMessages,DiscordSendMessagelangchain-discord-shikensoN/ATBDPyPI - Version

Tool features

  • DiscordReadMessages: Reads messages from a specified channel.
  • DiscordSendMessage: Sends messages to a specified channel.

Setup

The integration is provided by thelangchain-discord-shikenso package. Install it as follows:

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

Credentials

This integration requires you to setDISCORD_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 upLangSmith 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 inlangchain_discord. Adjust as needed for your specific usage.

from langchain_discord.tools.discord_read_messagesimport DiscordReadMessages
from langchain_discord.tools.discord_send_messagesimport 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)
response

Invocation with ToolCall

If you have a model-generatedToolCall, pass it totool.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 more complete example showing how you might integrate theDiscordReadMessages andDiscordSendMessage tools in a chain or agent with an LLM. This example assumes you have a function (likecreate_react_agent) that sets up a LangChain-style agent capable of calling tools when appropriate.

# Example: Using Discord Tools in an Agent

from langgraph.prebuiltimport create_react_agent
from langchain_discord.tools.discord_read_messagesimport DiscordReadMessages
from langchain_discord.tools.discord_send_messagesimport 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 eventin events:
event["messages"][-1].pretty_print()
API Reference:create_react_agent

API reference

See the docstrings in:

for usage details, parameters, and advanced configurations.

Related


[8]ページ先頭

©2009-2025 Movatter.jp