agnixcode commited on
Commit
3a356ab
·
verified ·
1 Parent(s): 7abecd5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py CHANGED
@@ -12,6 +12,57 @@ embed_model = SentenceTransformer("all-MiniLM-L6-v2")
12
  index = None
13
  chunks = []
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # Groq client with HF Secrets
16
  client = OpenAI(
17
  api_key=os.getenv("GROQ_API_KEY"),
 
12
  index = None
13
  chunks = []
14
 
15
+ # Add after globals:
16
+ chat_history = [] # Session memory
17
+
18
+ def chat(user_input, history):
19
+ global chat_history
20
+
21
+ # Build full context (PDF + conversation history)
22
+ full_context = "\n".join([f"User: {h['content']}\nBot: {h.get('bot_response', '')}"
23
+ for h in chat_history[-5:]]) if chat_history else ""
24
+
25
+ answer = generate_answer(user_input, full_context)
26
+
27
+ # Store in memory
28
+ chat_history.append({"user": user_input, "bot": answer})
29
+
30
+ # Update UI history
31
+ new_history = history + [
32
+ {"role": "user", "content": user_input},
33
+ {"role": "assistant", "content": answer}
34
+ ]
35
+
36
+ return new_history, new_history
37
+
38
+ def generate_answer(query, conversation_context=""):
39
+ if index is None:
40
+ return "⚠️ Please load a PDF first."
41
+
42
+ rag_context = retrieve(query)
43
+ rag_text = "\n\n".join(rag_context)
44
+
45
+ # ✅ Combine RAG + Conversation Memory
46
+ full_prompt = f"""You are a smart financial AI assistant that remembers conversations.
47
+
48
+ Previous conversation:
49
+ {conversation_context}
50
+
51
+ PDF Context (use ONLY this for facts):
52
+ {rag_text}
53
+
54
+ Question: {query}
55
+
56
+ Respond naturally and helpfully, referencing past discussion when relevant."""
57
+
58
+ response = client.chat.completions.create(
59
+ model="llama-3.1-8b-instant",
60
+ messages=[{"role": "user", "content": full_prompt}],
61
+ temperature=0.7,
62
+ max_tokens=600
63
+ )
64
+ return response.choices[0].message.content
65
+
66
  # Groq client with HF Secrets
67
  client = OpenAI(
68
  api_key=os.getenv("GROQ_API_KEY"),