이 가이드는 langchain_discord에서 Discord 도구를 시작하기 위한 빠른 개요를 제공합니다. 각 도구 및 구성에 대한 자세한 내용은 저장소의 docstring 또는 관련 문서 페이지를 참조하세요.

Overview

Integration details

ClassPackageSerializableJS supportVersion
DiscordReadMessages, DiscordSendMessagelangchain-discord-shikensoN/ATBDPyPI - Version

Tool features

  • DiscordReadMessages: 지정된 채널에서 메시지를 읽습니다.
  • DiscordSendMessage: 지정된 채널로 메시지를 전송합니다.

Setup

이 integration은 langchain-discord-shikenso 패키지에서 제공됩니다. 다음과 같이 설치하세요:
pip install --quiet -U langchain-discord-shikenso

Credentials

이 integration을 사용하려면 Discord API 인증을 위해 DISCORD_BOT_TOKEN을 환경 변수로 설정해야 합니다.
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()

Instantiation

다음은 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)

Invocation

Direct invocation with args

다음은 dictionary에 키워드 인수를 사용하여 도구를 호출하는 간단한 예제입니다.
invocation_args = {"channel_id": "1234567890", "limit": 3}
response = read_tool(invocation_args)
response

Invocation with 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)

Chaining

다음은 LLM과 함께 chain 또는 agent에서 DiscordReadMessagesDiscordSendMessage 도구를 통합하는 방법을 보여주는 보다 완전한 예제입니다. 이 예제는 적절한 경우 도구를 호출할 수 있는 LangChain 스타일의 agent를 설정하는 함수(예: create_agent)가 있다고 가정합니다.
# 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)
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 reference

사용 세부 정보, 매개변수 및 고급 구성은 다음의 docstring을 참조하세요:
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I