aryan195a commited on
Commit
3f64150
·
1 Parent(s): ea46838

modified graph.py

Browse files
Files changed (1) hide show
  1. graph.py +29 -17
graph.py CHANGED
@@ -7,26 +7,38 @@ from transformers import pipeline
7
  # 🧠 Retrieval Node
8
  def retrieve(state: Dict[str, Any]) -> Dict[str, Any]:
9
  query = state["query"]
10
- retriever = state["retriever"]
11
  top_k = state.get("top_k", 3)
12
  summarizer = state.get("summarizer")
13
- raw_text = state.get("raw_text") # <-- add this
14
-
15
- # Retrieve top-k documents
16
- retrieved_docs = retriever.invoke(query, k=top_k)
17
-
18
- # --- NEW: Use uploaded file if no docs retrieved ---
19
- if not retrieved_docs and raw_text:
20
- context = raw_text # use uploaded file directly
21
- elif not retrieved_docs:
22
- # fallback to sample.txt only if no uploaded file
23
- with open("data/sample.txt", "r", encoding="utf-8") as f:
24
- context = f.read().strip()
25
- else:
26
- # Limit each chunk to 500 chars to reduce prompt size
 
27
  context = "\n\n".join([doc.page_content[:500] for doc in retrieved_docs])
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- # Summarize if context too large
30
  if len(context) > 1500 and summarizer:
31
  context = summarizer(
32
  context,
@@ -42,7 +54,7 @@ def retrieve(state: Dict[str, Any]) -> Dict[str, Any]:
42
  "context": context,
43
  "llm": state["llm"],
44
  "temperature": state.get("temperature", 0.7),
45
- "raw_text": raw_text # keep it in state for next steps
46
  }
47
 
48
  # 🧠 Generation Node
 
7
  # 🧠 Retrieval Node
8
  def retrieve(state: Dict[str, Any]) -> Dict[str, Any]:
9
  query = state["query"]
10
+ retriever = state.get("retriever") # optional
11
  top_k = state.get("top_k", 3)
12
  summarizer = state.get("summarizer")
13
+ raw_text = state.get("raw_text") # uploaded file
14
+
15
+ retrieved_docs = []
16
+ context = ""
17
+
18
+ # --- Use FAISS retriever if available ---
19
+ if retriever:
20
+ try:
21
+ retrieved_docs = retriever.invoke(query, k=top_k)
22
+ except Exception:
23
+ retrieved_docs = []
24
+
25
+ # --- Determine context ---
26
+ if retrieved_docs:
27
+ # Limit each chunk to 500 chars
28
  context = "\n\n".join([doc.page_content[:500] for doc in retrieved_docs])
29
+ elif raw_text:
30
+ # Use uploaded file directly if no retrieved docs
31
+ context = raw_text
32
+ else:
33
+ # Fallback to sample.txt
34
+ sample_path = "data/sample.txt"
35
+ if os.path.exists(sample_path):
36
+ with open(sample_path, "r", encoding="utf-8") as f:
37
+ context = f.read().strip()
38
+ else:
39
+ context = "No content available."
40
 
41
+ # --- Summarize if context too long ---
42
  if len(context) > 1500 and summarizer:
43
  context = summarizer(
44
  context,
 
54
  "context": context,
55
  "llm": state["llm"],
56
  "temperature": state.get("temperature", 0.7),
57
+ "raw_text": raw_text
58
  }
59
 
60
  # 🧠 Generation Node