基类:EventBasePydanticReader
Discord 阅读器。
从频道读取对话。
参数:
| 名称 |
类型 |
描述 |
默认 |
discord_token
|
Optional[str]
|
Discord令牌。如果未提供,我们假设环境变量 DISCORD_TOKEN 已设置。
|
None
|
workflows/handler.py 中的源代码llama_index/readers/discord/base.py
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 | class DiscordReader(BasePydanticReader):
"""
Discord reader.
Reads conversations from channels.
Args:
discord_token (Optional[str]): Discord token. If not provided, we
assume the environment variable `DISCORD_TOKEN` is set.
"""
is_remote: bool = True
discord_token: str
def __init__(self, discord_token: Optional[str] = None) -> None:
"""Initialize with parameters."""
try:
import discord # noqa: F401
except ImportError:
raise ImportError(
"`discord.py` package not found, please run `pip install discord.py`"
)
if discord_token is None:
discord_token = os.environ["DISCORD_TOKEN"]
if discord_token is None:
raise ValueError(
"Must specify `discord_token` or set environment "
"variable `DISCORD_TOKEN`."
)
super().__init__(discord_token=discord_token)
@classmethod
def class_name(cls) -> str:
"""Get the name identifier of the class."""
return "DiscordReader"
def _read_channel(
self, channel_id: int, limit: Optional[int] = None, oldest_first: bool = True
) -> List[Document]:
"""Read channel."""
return asyncio.get_event_loop().run_until_complete(
read_channel(
self.discord_token, channel_id, limit=limit, oldest_first=oldest_first
)
)
def load_data(
self,
channel_ids: List[int],
limit: Optional[int] = None,
oldest_first: bool = True,
) -> List[Document]:
"""
Load data from the input directory.
Args:
channel_ids (List[int]): List of channel ids to read.
limit (Optional[int]): Maximum number of messages to read.
oldest_first (bool): Whether to read oldest messages first.
Defaults to `True`.
Returns:
List[Document]: List of documents.
"""
results: List[Document] = []
for channel_id in channel_ids:
if not isinstance(channel_id, int):
raise ValueError(
f"Channel id {channel_id} must be an integer, "
f"not {type(channel_id)}."
)
channel_documents = self._read_channel(
channel_id, limit=limit, oldest_first=oldest_first
)
results += channel_documents
return results
|
class_name
classmethod
获取类的名称标识符。
workflows/handler.py 中的源代码llama_index/readers/discord/base.py
| @classmethod
def class_name(cls) -> str:
"""Get the name identifier of the class."""
return "DiscordReader"
|
load_data
load_data(channel_ids: List[int], limit: Optional[int] = None, oldest_first: bool = True) -> List[文档]
从输入目录加载数据。
参数:
| 名称 |
类型 |
描述 |
默认 |
channel_ids
|
List[int]
|
|
required
|
limit
|
Optional[int]
|
|
None
|
oldest_first
|
bool
|
|
True
|
返回:
workflows/handler.py 中的源代码llama_index/readers/discord/base.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 | def load_data(
self,
channel_ids: List[int],
limit: Optional[int] = None,
oldest_first: bool = True,
) -> List[Document]:
"""
Load data from the input directory.
Args:
channel_ids (List[int]): List of channel ids to read.
limit (Optional[int]): Maximum number of messages to read.
oldest_first (bool): Whether to read oldest messages first.
Defaults to `True`.
Returns:
List[Document]: List of documents.
"""
results: List[Document] = []
for channel_id in channel_ids:
if not isinstance(channel_id, int):
raise ValueError(
f"Channel id {channel_id} must be an integer, "
f"not {type(channel_id)}."
)
channel_documents = self._read_channel(
channel_id, limit=limit, oldest_first=oldest_first
)
results += channel_documents
return results
|