File size: 8,814 Bytes
68ec988
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262


from langchain.chains import LLMChain
# chat_chain = LLMChain(
#     llm=llm,
#     prompt=chat_prompt,
#     memory=memory,
#     verbose=True  # Enable verbose logging for debugging
# )


import os
import psycopg2
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=os.getenv("MODEL_NAME"),
    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")
)

# Database connection function
import psycopg2
import os

def get_db_conn():
    return psycopg2.connect(os.getenv("DATABASE_URL"))

# Set up the database schema: store raw post text, comments, computed embedding, and metadata.

def setup_db():
    conn = get_db_conn()
    cur = conn.cursor()
    try:
        cur.execute("""  -- remove EXTENSION line
            CREATE TABLE IF NOT EXISTS reddit_posts (
                id SERIAL PRIMARY KEY,
                reddit_id VARCHAR(50) UNIQUE,
                keyword TEXT,
                title TEXT,
                post_text TEXT,
                comments JSONB,
                created_at TIMESTAMP,
                embedding VECTOR(384),
                metadata JSONB
            );
            CREATE INDEX IF NOT EXISTS idx_keyword_created_at ON reddit_posts(keyword, created_at DESC);
        """)
        conn.commit()
    except Exception as e:
        print("DB Setup Error:", e)
    finally:
        cur.close()
        conn.close()
# def setup_db():
#     conn = get_db_conn()
#     cur = conn.cursor()
#     cur.execute("""
#         CREATE EXTENSION IF NOT EXISTS vector;
#         CREATE TABLE IF NOT EXISTS reddit_posts (
#             id SERIAL PRIMARY KEY,
#             reddit_id VARCHAR(50) UNIQUE,
#             keyword TEXT,
#             title TEXT,
#             post_text TEXT,
#             comments JSONB,
#             created_at TIMESTAMP,
#             embedding VECTOR(384),
#             metadata JSONB
#         );
#         CREATE INDEX IF NOT EXISTS idx_keyword_created_at ON reddit_posts(keyword, created_at DESC);
#     """)
#     conn.commit()
#     cur.close()
#     conn.close()

# Utility: Check if the keyword appears in the post title, selftext, or any comment.
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 Reddit posts if the keyword is in the post or any comment.
# This version iterates over posts until reaching posts older than the specified day range.
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  # Since sorted by new, we break once older posts are encountered.
        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,
            "embedding": embedding,
            "metadata": metadata
        })
    save_to_db(data)

# Save posts data into PostgreSQL.
def save_to_db(posts):
    conn = get_db_conn()
    cur = conn.cursor()
    for post in posts:
        cur.execute("""
            INSERT INTO reddit_posts
            (reddit_id, keyword, title, post_text, comments, created_at, embedding, metadata)
            VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
            ON CONFLICT DO NOTHING;
        """, (
            post["reddit_id"],
            post["keyword"],
            post["title"],
            post["post_text"],
            json.dumps(post["comments"]),
            post["created_at"],
            post["embedding"],
            json.dumps(post["metadata"])
        ))
    conn.commit()
    cur.close()
    conn.close()

# Retrieve context from the DB.
# Updated retrieval: if summarization intent is detected, retrieve more posts.
def retrieve_context(question, keyword, reddit_id=None, top_k=10):
    lower_q = question.lower()
    # Check for summarization intent.
    if any(word in lower_q for word in ["summarize", "overview", "all posts"]):
        requested_top_k = 50
    else:
        requested_top_k = top_k

    # Retrieve posts based on query embedding.
    query_embedding = embedder.encode(question).tolist()
    query_embedding_str = "[" + ",".join(map(str, query_embedding)) + "]"
    
    conn = get_db_conn()
    cur = conn.cursor()
    if reddit_id:
        cur.execute("""
            SELECT title, post_text, comments FROM reddit_posts
            WHERE reddit_id = %s;
        """, (reddit_id,))
    else:
        cur.execute("""
            SELECT title, post_text, comments FROM reddit_posts
            WHERE keyword = %s
            ORDER BY embedding <-> %s::vector LIMIT %s;
        """, (keyword, query_embedding_str, requested_top_k))
    results = cur.fetchall()
    conn.close()
    
    # If there are fewer posts than requested and none were retrieved by vector search,
    # fall back to retrieving all posts for that keyword.
    if not results:
        conn = get_db_conn()
        cur = conn.cursor()
        cur.execute("""
            SELECT title, post_text, comments FROM reddit_posts
            WHERE keyword = %s ORDER BY created_at DESC;
        """, (keyword,))
        results = cur.fetchall()
        conn.close()
    return results

# --- New Summarization Step for Handling Long Context ---
# Create a summarization chain to compress the context if it exceeds a token/character threshold.
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)


# Set up conversation memory and chain.
memory = ConversationBufferMemory(memory_key="chat_history")
# Updated prompt: we now expect a single input field "input"
chat_prompt = ChatPromptTemplate.from_template("""
Chat History:
{chat_history}

Context from Reddit and User Question:
{input}

Act as an Professional Assistant as incremental chat agent and also give reasioning and Answer clearly based on context and chat history, your response should be valid and concise, and relavant .

""")

chat_chain = LLMChain(
    llm=llm,
    prompt=chat_prompt,
    memory=memory,
    verbose=True  # Enable verbose logging for debugging
)

# Get chatbot response by merging context and question into a single input.
# Updated get_chatbot_response to handle summarization if context is too long.

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])
    
    # Set a threshold (e.g., 3000 characters); if context length exceeds it, compress the context.
    if len(context) > 3000:
        context = summarize_chain.run({"context": context})
    
    combined_input = f"Context:\n{context}\n\nUser Question: {question}"
    response = chat_chain.run({"input": combined_input})
    return response, context_posts