mirror of
https://github.com/hwchase17/langchain
synced 2024-11-08 07:10:35 +00:00
ed58eeb9c5
Moved the following modules to new package langchain-community in a backwards compatible fashion: ``` mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community ``` Moved the following to core ``` mv langchain/langchain/utils/json_schema.py core/langchain_core/utils mv langchain/langchain/utils/html.py core/langchain_core/utils mv langchain/langchain/utils/strings.py core/langchain_core/utils cat langchain/langchain/utils/env.py >> core/langchain_core/utils/env.py rm langchain/langchain/utils/env.py ``` See .scripts/community_split/script_integrations.sh for all changes
91 lines
3.0 KiB
Python
91 lines
3.0 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Sequence
|
|
|
|
from langchain_core.documents import Document
|
|
|
|
from langchain_community.document_loaders.base import BaseLoader
|
|
|
|
if TYPE_CHECKING:
|
|
import mastodon
|
|
|
|
|
|
def _dependable_mastodon_import() -> mastodon:
|
|
try:
|
|
import mastodon
|
|
except ImportError:
|
|
raise ImportError(
|
|
"Mastodon.py package not found, "
|
|
"please install it with `pip install Mastodon.py`"
|
|
)
|
|
return mastodon
|
|
|
|
|
|
class MastodonTootsLoader(BaseLoader):
|
|
"""Load the `Mastodon` 'toots'."""
|
|
|
|
def __init__(
|
|
self,
|
|
mastodon_accounts: Sequence[str],
|
|
number_toots: Optional[int] = 100,
|
|
exclude_replies: bool = False,
|
|
access_token: Optional[str] = None,
|
|
api_base_url: str = "https://mastodon.social",
|
|
):
|
|
"""Instantiate Mastodon toots loader.
|
|
|
|
Args:
|
|
mastodon_accounts: The list of Mastodon accounts to query.
|
|
number_toots: How many toots to pull for each account. Defaults to 100.
|
|
exclude_replies: Whether to exclude reply toots from the load.
|
|
Defaults to False.
|
|
access_token: An access token if toots are loaded as a Mastodon app. Can
|
|
also be specified via the environment variables "MASTODON_ACCESS_TOKEN".
|
|
api_base_url: A Mastodon API base URL to talk to, if not using the default.
|
|
Defaults to "https://mastodon.social".
|
|
"""
|
|
mastodon = _dependable_mastodon_import()
|
|
access_token = access_token or os.environ.get("MASTODON_ACCESS_TOKEN")
|
|
self.api = mastodon.Mastodon(
|
|
access_token=access_token, api_base_url=api_base_url
|
|
)
|
|
self.mastodon_accounts = mastodon_accounts
|
|
self.number_toots = number_toots
|
|
self.exclude_replies = exclude_replies
|
|
|
|
def load(self) -> List[Document]:
|
|
"""Load toots into documents."""
|
|
results: List[Document] = []
|
|
for account in self.mastodon_accounts:
|
|
user = self.api.account_lookup(account)
|
|
toots = self.api.account_statuses(
|
|
user.id,
|
|
only_media=False,
|
|
pinned=False,
|
|
exclude_replies=self.exclude_replies,
|
|
exclude_reblogs=True,
|
|
limit=self.number_toots,
|
|
)
|
|
docs = self._format_toots(toots, user)
|
|
results.extend(docs)
|
|
return results
|
|
|
|
def _format_toots(
|
|
self, toots: List[Dict[str, Any]], user_info: dict
|
|
) -> Iterable[Document]:
|
|
"""Format toots into documents.
|
|
|
|
Adding user info, and selected toot fields into the metadata.
|
|
"""
|
|
for toot in toots:
|
|
metadata = {
|
|
"created_at": toot["created_at"],
|
|
"user_info": user_info,
|
|
"is_reply": toot["in_reply_to_id"] is not None,
|
|
}
|
|
yield Document(
|
|
page_content=toot["content"],
|
|
metadata=metadata,
|
|
)
|