Discord 工具
Discord 工具使您的代理能够搜索、读取和写入 Discord 频道中的消息。当您需要与 Discord 频道交互时,它非常有用。
设置
要使用 Discord 工具,您需要安装以下官方对等依赖项
- npm
- Yarn
- pnpm
npm install discord.js
yarn add discord.js
pnpm add discord.js
用法,独立
提示
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/core
yarn add @langchain/openai @langchain/core
pnpm add @langchain/openai @langchain/core
import {
DiscordGetMessagesTool,
DiscordChannelSearchTool,
DiscordSendMessagesTool,
DiscordGetGuildsTool,
DiscordGetTextChannelsTool,
} from "@langchain/community/tools/discord";
// Get messages from a channel given channel ID
const getMessageTool = new DiscordGetMessagesTool();
const messageResults = await getMessageTool.invoke("1153400523718938780");
console.log(messageResults);
// Get guilds/servers
const getGuildsTool = new DiscordGetGuildsTool();
const guildResults = await getGuildsTool.invoke("");
console.log(guildResults);
// Search results in a given channel (case-insensitive)
const searchTool = new DiscordChannelSearchTool();
const searchResults = await searchTool.invoke("Test");
console.log(searchResults);
// Get all text channels of a server
const getChannelsTool = new DiscordGetTextChannelsTool();
const channelResults = await getChannelsTool.invoke("1153400523718938775");
console.log(channelResults);
// Send a message
const sendMessageTool = new DiscordSendMessagesTool();
const sendMessageResults = await sendMessageTool.invoke("test message");
console.log(sendMessageResults);
API 参考
- DiscordGetMessagesTool 来自
@langchain/community/tools/discord
- DiscordChannelSearchTool 来自
@langchain/community/tools/discord
- DiscordSendMessagesTool 来自
@langchain/community/tools/discord
- DiscordGetGuildsTool 来自
@langchain/community/tools/discord
- DiscordGetTextChannelsTool 来自
@langchain/community/tools/discord
用法,在代理中
import { ChatOpenAI } from "@langchain/openai";
import { initializeAgentExecutorWithOptions } from "langchain/agents";
import { DiscordSendMessagesTool } from "@langchain/community/tools/discord";
import { DadJokeAPI } from "@langchain/community/tools/dadjokeapi";
const model = new ChatOpenAI({
temperature: 0,
});
const tools = [new DiscordSendMessagesTool(), new DadJokeAPI()];
const executor = await initializeAgentExecutorWithOptions(tools, model, {
agentType: "zero-shot-react-description",
verbose: true,
});
const res = await executor.invoke({
input: `Tell a joke in the discord channel`,
});
console.log(res.output);
// "What's the best thing about elevator jokes? They work on so many levels."
API 参考
- ChatOpenAI 来自
@langchain/openai
- initializeAgentExecutorWithOptions 来自
langchain/agents
- DiscordSendMessagesTool 来自
@langchain/community/tools/discord
- DadJokeAPI 来自
@langchain/community/tools/dadjokeapi