Rivalcoder commited on
Commit
d2a3fbf
Β·
1 Parent(s): c9841ce
Files changed (1) hide show
  1. llm.py +24 -11
llm.py CHANGED
@@ -31,10 +31,16 @@ def extract_https_links(chunks):
31
  def fetch_all_links(links, timeout=10, max_workers=10):
32
  """
33
  Fetch all HTTPS links in parallel, with per-link timing.
 
34
  Returns a dict {link: content or error}.
35
  """
36
  fetched_data = {}
37
 
 
 
 
 
 
38
  def fetch(link):
39
  start = time.perf_counter()
40
  try:
@@ -48,38 +54,42 @@ def fetch_all_links(links, timeout=10, max_workers=10):
48
  print(f"❌ {link} β€” {elapsed:.2f}s β€” ERROR: {e}")
49
  return link, f"ERROR: {e}"
50
 
51
- # Fetch all links in parallel (no banned filtering, no special prioritization)
 
 
 
 
 
52
  t0 = time.perf_counter()
53
  with ThreadPoolExecutor(max_workers=max_workers) as executor:
54
- future_to_link = {executor.submit(fetch, link): link for link in links}
55
  for future in as_completed(future_to_link):
56
  link, content = future.result()
57
  fetched_data[link] = content
58
-
59
  print(f"[TIMER] Total link fetching: {time.perf_counter() - t0:.2f}s")
 
60
  return fetched_data
61
 
62
-
63
  def query_gemini(questions, contexts, max_retries=3):
64
  import itertools
65
 
66
  total_start = time.perf_counter()
67
 
68
- # Join context & questions fresh every call, no caching
69
  t0 = time.perf_counter()
70
  context = "\n\n".join(contexts)
71
  questions_text = "\n".join([f"{i+1}. {q}" for i, q in enumerate(questions)])
72
  print(f"[TIMER] Context join: {time.perf_counter() - t0:.2f}s")
73
 
74
- # Extract links and fetch all links, with special URL prioritized
75
  links = extract_https_links(contexts)
76
  if links:
77
  fetched_results = fetch_all_links(links)
78
  for link, content in fetched_results.items():
79
- if not content.startswith("ERROR") and content != "BANNED":
80
  context += f"\n\nRetrieved from {link}:\n{content}"
81
 
82
- # Build prompt fresh each time
83
  t0 = time.perf_counter()
84
  prompt = fr"""
85
  - You are an expert insurance assistant generating formal yet user-facing answers to policy questions and Other Human Questions. Your goal is to write professional, structured answers that reflect the language of policy documents β€” but are still human-readable and easy to understand.
@@ -150,12 +160,14 @@ Respond with only the following JSON β€” no explanations, no comments, no markdo
150
 
151
 
152
  """
 
153
  print(f"[TIMER] Prompt build: {time.perf_counter() - t0:.2f}s")
154
 
155
  last_exception = None
156
  total_attempts = len(api_keys) * max_retries
157
  key_cycle = itertools.cycle(api_keys)
158
 
 
159
  for attempt in range(total_attempts):
160
  key = next(key_cycle)
161
  try:
@@ -166,14 +178,15 @@ Respond with only the following JSON β€” no explanations, no comments, no markdo
166
  api_time = time.perf_counter() - t0
167
  print(f"[TIMER] Gemini API call (attempt {attempt+1}): {api_time:.2f}s")
168
 
 
169
  t0 = time.perf_counter()
170
  response_text = getattr(response, "text", "").strip()
171
  if not response_text:
172
  raise ValueError("Empty response received from Gemini API.")
173
 
174
- if response_text.startswith("json"):
175
- response_text = response_text.replace("json", "").replace("", "").strip()
176
- elif response_text.startswith(""):
177
  response_text = response_text.replace("```", "").strip()
178
 
179
  parsed = json.loads(response_text)
 
31
  def fetch_all_links(links, timeout=10, max_workers=10):
32
  """
33
  Fetch all HTTPS links in parallel, with per-link timing.
34
+ Skips banned links.
35
  Returns a dict {link: content or error}.
36
  """
37
  fetched_data = {}
38
 
39
+ # Internal banned list
40
+ banned_links = [
41
+
42
+ ]
43
+
44
  def fetch(link):
45
  start = time.perf_counter()
46
  try:
 
54
  print(f"❌ {link} β€” {elapsed:.2f}s β€” ERROR: {e}")
55
  return link, f"ERROR: {e}"
56
 
57
+ # Filter out banned links before starting fetch
58
+ links_to_fetch = [l for l in links if l not in banned_links]
59
+ for banned in set(links) - set(links_to_fetch):
60
+ print(f"β›” Skipped banned link: {banned}")
61
+ fetched_data[banned] = "BANNED"
62
+
63
  t0 = time.perf_counter()
64
  with ThreadPoolExecutor(max_workers=max_workers) as executor:
65
+ future_to_link = {executor.submit(fetch, link): link for link in links_to_fetch}
66
  for future in as_completed(future_to_link):
67
  link, content = future.result()
68
  fetched_data[link] = content
 
69
  print(f"[TIMER] Total link fetching: {time.perf_counter() - t0:.2f}s")
70
+ print(fetched_data)
71
  return fetched_data
72
 
 
73
  def query_gemini(questions, contexts, max_retries=3):
74
  import itertools
75
 
76
  total_start = time.perf_counter()
77
 
78
+ # Context join
79
  t0 = time.perf_counter()
80
  context = "\n\n".join(contexts)
81
  questions_text = "\n".join([f"{i+1}. {q}" for i, q in enumerate(questions)])
82
  print(f"[TIMER] Context join: {time.perf_counter() - t0:.2f}s")
83
 
84
+ # Link extraction & fetching
85
  links = extract_https_links(contexts)
86
  if links:
87
  fetched_results = fetch_all_links(links)
88
  for link, content in fetched_results.items():
89
+ if not content.startswith("ERROR"):
90
  context += f"\n\nRetrieved from {link}:\n{content}"
91
 
92
+ # Prompt building
93
  t0 = time.perf_counter()
94
  prompt = fr"""
95
  - You are an expert insurance assistant generating formal yet user-facing answers to policy questions and Other Human Questions. Your goal is to write professional, structured answers that reflect the language of policy documents β€” but are still human-readable and easy to understand.
 
160
 
161
 
162
  """
163
+
164
  print(f"[TIMER] Prompt build: {time.perf_counter() - t0:.2f}s")
165
 
166
  last_exception = None
167
  total_attempts = len(api_keys) * max_retries
168
  key_cycle = itertools.cycle(api_keys)
169
 
170
+ # Gemini API calls
171
  for attempt in range(total_attempts):
172
  key = next(key_cycle)
173
  try:
 
178
  api_time = time.perf_counter() - t0
179
  print(f"[TIMER] Gemini API call (attempt {attempt+1}): {api_time:.2f}s")
180
 
181
+ # Response parsing
182
  t0 = time.perf_counter()
183
  response_text = getattr(response, "text", "").strip()
184
  if not response_text:
185
  raise ValueError("Empty response received from Gemini API.")
186
 
187
+ if response_text.startswith("```json"):
188
+ response_text = response_text.replace("```json", "").replace("```", "").strip()
189
+ elif response_text.startswith("```"):
190
  response_text = response_text.replace("```", "").strip()
191
 
192
  parsed = json.loads(response_text)