File size: 6,289 Bytes
68ec988
 
ed2c8ad
68ec988
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed2c8ad
68ec988
 
 
 
 
 
 
 
 
 
 
 
8721562
ed2c8ad
68ec988
ed2c8ad
68ec988
ed2c8ad
68ec988
 
 
 
ed2c8ad
68ec988
ed2c8ad
 
68ec988
 
 
ed2c8ad
 
 
 
68ec988
 
 
 
 
 
 
 
 
ed2c8ad
68ec988
 
 
 
 
 
 
 
 
 
 
ed2c8ad
68ec988
 
 
 
 
ed2c8ad
68ec988
 
 
 
ed2c8ad
68ec988
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed2c8ad
68ec988
 
 
e552ac2
 
68ec988
ed2c8ad
68ec988
 
 
 
ed2c8ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68ec988
 
 
 
ed2c8ad
68ec988
 
ed2c8ad
68ec988
 
 
ed2c8ad
68ec988
 
 
ed2c8ad
68ec988
 
 
 
8721562
ed2c8ad
 
68ec988
ed2c8ad
68ec988
 
 
ed2c8ad
68ec988
 
 
 
 
 
 
 
 
 
ed2c8ad
68ec988
 
 
 
 
 
 
a1a35ae
68ec988
 
 
 
 
 
ed2c8ad
68ec988
 
c311c48
ed2c8ad
68ec988
 
 
 
5fcdfe8
68ec988
e552ac2
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from langchain.chains import LLMChain
import os
import sqlite3
import praw
import json
from datetime import datetime, timedelta
from sentence_transformers import SentenceTransformer
from dotenv import load_dotenv
from langchain_groq import ChatGroq
from langchain.prompts import ChatPromptTemplate
from langchain.chains import ConversationChain, LLMChain
from langchain.memory import ConversationBufferMemory

load_dotenv()

# Initialize the LLM via LangChain (using Groq)
llm = ChatGroq(
    groq_api_key=os.getenv("GROQ_API_KEY"),
    model_name="meta-llama/llama-4-maverick-17b-128e-instruct",
    temperature=0.2
)

# Embedding Model
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")

# Reddit API Setup
reddit = praw.Reddit(
    client_id=os.getenv("REDDIT_CLIENT_ID"),
    client_secret=os.getenv("REDDIT_CLIENT_SECRET"),
    user_agent=os.getenv("REDDIT_USER_AGENT")
)

# SQLite DB Connection
def get_db_conn():
    return sqlite3.connect("reddit_data.db", check_same_thread=False)

# Set up the database schema
def setup_db():
    conn = get_db_conn()
    cur = conn.cursor()
    try:
        cur.execute("""
            CREATE TABLE IF NOT EXISTS reddit_posts (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                reddit_id TEXT UNIQUE,
                keyword TEXT,
                title TEXT,
                post_text TEXT,
                comments TEXT,
                created_at TEXT,
                embedding TEXT,
                metadata TEXT
            );
        """)
        conn.commit()
    except Exception as e:
        print("DB Setup Error:", e)
    finally:
        cur.close()
        conn.close()

# Keyword filter
def keyword_in_post_or_comments(post, keyword):
    keyword_lower = keyword.lower()
    combined_text = (post.title + " " + post.selftext).lower()
    if keyword_lower in combined_text:
        return True
    post.comments.replace_more(limit=None)
    for comment in post.comments.list():
        if keyword_lower in comment.body.lower():
            return True
    return False

# Fetch and process Reddit data
def fetch_reddit_data(keyword, days=7, limit=None):
    end_time = datetime.utcnow()
    start_time = end_time - timedelta(days=days)
    subreddit = reddit.subreddit("all")
    posts_generator = subreddit.search(keyword, sort="new", time_filter="all", limit=limit)

    data = []
    for post in posts_generator:
        created = datetime.utcfromtimestamp(post.created_utc)
        if created < start_time:
            break
        if not keyword_in_post_or_comments(post, keyword):
            continue

        post.comments.replace_more(limit=None)
        comments = [comment.body for comment in post.comments.list()]
        combined_text = f"{post.title}\n{post.selftext}\n{' '.join(comments)}"
        embedding = embedder.encode(combined_text).tolist()
        metadata = {
            "url": post.url,
            "subreddit": post.subreddit.display_name,
            "comments_count": len(comments)
        }
        data.append({
            "reddit_id": post.id,
            "keyword": keyword,
            "title": post.title,
            "post_text": post.selftext,
            "comments": comments,
            "created_at": created.isoformat(),
            "embedding": embedding,
            "metadata": metadata
        })
    if data:
        save_to_db(data)

# Save data into SQLite
def save_to_db(posts):
    conn = get_db_conn()
    cur = conn.cursor()
    for post in posts:
        try:
            cur.execute("""
                INSERT OR IGNORE INTO reddit_posts
                (reddit_id, keyword, title, post_text, comments, created_at, embedding, metadata)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?);
            """, (
                post["reddit_id"],
                post["keyword"],
                post["title"],
                post["post_text"],
                json.dumps(post["comments"]),
                post["created_at"],
                json.dumps(post["embedding"]),
                json.dumps(post["metadata"])
            ))
        except Exception as e:
            print("Insert Error:", e)
    conn.commit()
    cur.close()
    conn.close()

# Retrieve similar context from DB
def retrieve_context(question, keyword, reddit_id=None, top_k=10):
    lower_q = question.lower()
    requested_top_k = 50 if any(word in lower_q for word in ["summarize", "overview", "all posts"]) else top_k

    conn = get_db_conn()
    cur = conn.cursor()

    if reddit_id:
        cur.execute("""
            SELECT title, post_text, comments FROM reddit_posts
            WHERE reddit_id = ?;
        """, (reddit_id,))
    else:
        cur.execute("""
            SELECT title, post_text, comments FROM reddit_posts
            WHERE keyword = ? ORDER BY datetime(created_at) DESC LIMIT ?;
        """, (keyword, requested_top_k))

    results = cur.fetchall()
    cur.close()
    conn.close()
    return results

# Summarizer
summarize_prompt = ChatPromptTemplate.from_template("""
You are a summarizer. Summarize the following context from Reddit posts into a concise summary that preserves the key insights. Do not add extra commentary.

Context:
{context}

Summary:
""")
summarize_chain = LLMChain(llm=llm, prompt=summarize_prompt)

# Chatbot memory and prompt
memory = ConversationBufferMemory(memory_key="chat_history")
chat_prompt = ChatPromptTemplate.from_template("""
Chat History:
{chat_history}
Context from Reddit and User Question:
{input}

Act as a Professional Assistant as an incremental chat agent. Provide reasoning and answer clearly based on the context and chat history. Your response should be valid, concise, Attractive and relevant.
""")

chat_chain = LLMChain(
    llm=llm,
    prompt=chat_prompt,
    memory=memory,
    verbose=True
)


# Chatbot response
def get_chatbot_response(question, keyword, reddit_id=None):
    context_posts = retrieve_context(question, keyword, reddit_id)
    context = "\n\n".join([f"{p[0]}:\n{p[1]}" for p in context_posts])
    if len(context) > 3000:
        context = summarize_chain.invoke({"context": context})
    combined_input = f"Context:\n{context}\n\nUser Question: {question}"
    response = chat_chain.invoke({"input": combined_input})
    return response, context_posts