Read and Analyze Reddit Posts
LlamaIndex reader that searches subreddits by keyword and loads posts and comments.
Why it matters
Leverage Reddit discussions for insights by extracting and analyzing posts and comments from specified subreddits based on keywords.
Outcomes
What it gets done
Fetch posts and top-level comments from chosen subreddits.
Filter Reddit content using specific search keywords.
Load extracted Reddit data for use with LLMs and LangChain.
Query indexed Reddit data to answer specific questions.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-reader-readers-reddit | bash Overview
Reddit Reader
The Reddit Reader searches subreddits by keyword and loads matching posts and top-level comments into LlamaIndex documents, requiring a Reddit app's API credentials plus your Reddit account login. Use it when you need Reddit discussion from specific subreddits, filtered by keyword, loaded into LlamaIndex or LangChain. It requires a full set of Reddit API credentials.
What it does
The Reddit Reader searches specified subreddits for posts matching given keywords, then loads the post text and top-level comments into documents you can feed to an LLM or LangChain. You give it a list of subreddits, search keywords, and a post limit.
When to use - and when NOT to
Use it when you need Reddit discussion -- posts and top-level comments from specific subreddits, filtered by keyword -- pulled into LlamaIndex for indexing or querying, for example "What are the pain points of PyTorch users?" against r/MachineLearning. It requires a full set of Reddit API credentials (client ID, client secret, user agent, plus your Reddit username and password), created via a Reddit app in Reddit's App Preferences, so it is not usable without registering an app first.
Inputs and outputs
Install with:
pip install llama-index-readers-reddit
Setting up credentials means creating a Reddit app (Reddit App Preferences, "create another app...", filling in name, description, and redirect URL) and storing five environment variables: REDDIT_CLIENT_ID, REDDIT_CLIENT_SECRET, and REDDIT_USER_AGENT from the app itself, plus REDDIT_USERNAME and REDDIT_PASSWORD for your Reddit account.
from llama_index.core import VectorStoreIndex, download_loader
from llama_index.readers.reddit import RedditReader
subreddits = ["MachineLearning"]
search_keys = ["PyTorch", "deploy"]
post_limit = 10
loader = RedditReader()
documents = loader.load_data(
subreddits=subreddits, search_keys=search_keys, post_limit=post_limit
)
index = VectorStoreIndex.from_documents(documents)
index.query("What are the pain points of PyTorch users?")
load_data takes subreddits (a list), search_keys (a list of keywords), and post_limit (how many posts to pull). The resulting index can also be wrapped as a LangChain Tool and handed to an agent for conversational querying.
Who it's for
Developers building LlamaIndex or LangChain pipelines that need Reddit discussion -- posts and comments from specific subreddits -- searchable by keyword.
Source README
Reddit Reader
pip install llama-index-readers-reddit
For any subreddit(s) you're interested in, search for relevant posts using keyword(s) and load the resulting text in the post and top-level comments into LLMs/ LangChains.
Get your Reddit credentials ready
- Visit Reddit App Preferences (https://www.reddit.com/prefs/apps) or https://old.reddit.com/prefs/apps/
- Scroll to the bottom and click "create another app..."
- Fill out the name, description, and redirect url for your app, then click "create app"
- Now you should be able to see the personal use script, secret, and name of your app. Store those as environment variables REDDIT_CLIENT_ID, REDDIT_CLIENT_SECRET, and REDDIT_USER_AGENT respectively.
- Additionally store the environment variables REDDIT_USERNAME and REDDIT_PASSWORD, which correspond to the credentials for your Reddit account.
Usage
LlamaIndex
from llama_index.core import VectorStoreIndex, download_loader
from llama_index.readers.reddit import RedditReader
subreddits = ["MachineLearning"]
search_keys = ["PyTorch", "deploy"]
post_limit = 10
loader = RedditReader()
documents = loader.load_data(
subreddits=subreddits, search_keys=search_keys, post_limit=post_limit
)
index = VectorStoreIndex.from_documents(documents)
index.query("What are the pain points of PyTorch users?")
LangChain
from llama_index.core import VectorStoreIndex, download_loader
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
from langchain.chains.conversation.memory import ConversationBufferMemory
from llama_index.readers.reddit import RedditReader
subreddits = ["MachineLearning"]
search_keys = ["PyTorch", "deploy"]
post_limit = 10
loader = RedditReader()
documents = loader.load_data(
subreddits=subreddits, search_keys=search_keys, post_limit=post_limit
)
index = VectorStoreIndex.from_documents(documents)
tools = [
Tool(
name="Reddit Index",
func=lambda q: index.query(q),
description=f"Useful when you want to read relevant posts and top-level comments in subreddits.",
),
]
llm = OpenAI(temperature=0)
memory = ConversationBufferMemory(memory_key="chat_history")
agent_chain = initialize_agent(
tools, llm, agent="zero-shot-react-description", memory=memory
)
output = agent_chain.run(input="What are the pain points of PyTorch users?")
print(output)
This loader is designed to be used as a way to load data into GPT Index.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.