igerasimov commited on
Commit
32c52f2
·
1 Parent(s): 42eee89

perf: strip definitions from prompt token context to clear 429 rate limit errors

Browse files
Files changed (1) hide show
  1. app.py +9 -13
app.py CHANGED
@@ -9,14 +9,15 @@ from langchain_core.prompts import ChatPromptTemplate
9
  from langchain_openai import ChatOpenAI
10
 
11
  # ==========================================================
12
- # 1. DATA INGESTION & NOTEBOOK INDEX GENERATOR
13
  # ==========================================================
14
  with open("gcmd_hierarchy.json", "r") as f:
15
  gcmd_data = json.load(f)
16
 
17
  def build_gcmd_indices(gcmd_json):
18
  """
19
- Parses the GCMD hierarchy exactly as done in the working notebook.
 
20
  """
21
  topic_list = []
22
  sub_tree_indices = {}
@@ -36,13 +37,9 @@ def build_gcmd_indices(gcmd_json):
36
  # Append current node name to path
37
  node_path = f"{current_path} > {node_name}" if current_path else node_name
38
 
39
- # If it's a Variable or leaf term with text context, preserve it
40
  if "Variable" in node.get("level", "") or not node.get("children"):
41
- definition = node.get("definition", "")
42
- if definition:
43
- collected_paths.append(f"{node_path} (Definition: {definition})")
44
- else:
45
- collected_paths.append(node_path)
46
 
47
  # Dig deeper into children
48
  for child in node.get("children", []):
@@ -110,11 +107,11 @@ def classify_individual_topic(topic_name: str):
110
  response = llm.invoke(prompt.format(title=state["title"], abstract=state["abstract"], sub_tree=target_sub_tree))
111
  raw_keywords = [k.strip() for k in response.content.split(",") if k.strip()]
112
 
 
113
  valid_set = set()
114
  for line in target_sub_tree.split("\n"):
115
  if line.strip():
116
  path = line.replace("- ", "").strip()
117
- path = re.sub(r"\s*\(Definition:.*?\)$", "", path).strip()
118
  valid_set.add(path)
119
 
120
  valid_kws = [kw for kw in raw_keywords if kw in valid_set]
@@ -160,7 +157,7 @@ def run_agent_classifier(title, abstract):
160
 
161
  inputs = {"title": title, "abstract": abstract}
162
 
163
- # Execute your exact notebook graph synchronously
164
  output = app.invoke(inputs)
165
 
166
  chosen_topics = output.get("chosen_topics", [])
@@ -169,11 +166,10 @@ def run_agent_classifier(title, abstract):
169
 
170
  topics_str = ", ".join(chosen_topics)
171
 
172
- # Requirement 1: Format output keywords to prepend their corresponding parent Topic
173
  formatted_keywords = []
174
  for kw in predicted_kws:
175
  matched_topic = "UNKNOWN"
176
- # Find which topic sub-tree this validated keyword originally belonged to
177
  for topic in chosen_topics:
178
  sub_tree_text = SUB_TREE_LOOKUP.get(topic, "")
179
  if kw in sub_tree_text:
@@ -186,7 +182,7 @@ def run_agent_classifier(title, abstract):
186
  else:
187
  keywords_str = "\n".join(formatted_keywords)
188
 
189
- # Requirement 2: Format separate display for caught and filtered hallucinations
190
  if not invalid_kws:
191
  invalid_str = "None! The agent validation pass achieved 100% data integrity."
192
  else:
 
9
  from langchain_openai import ChatOpenAI
10
 
11
  # ==========================================================
12
+ # 1. LOAD DATA & OPTIMIZED INDEX GENERATOR (REDUCES TOKENS)
13
  # ==========================================================
14
  with open("gcmd_hierarchy.json", "r") as f:
15
  gcmd_data = json.load(f)
16
 
17
  def build_gcmd_indices(gcmd_json):
18
  """
19
+ Parses the GCMD hierarchy exactly like the notebook but strips
20
+ heavy definitions to prevent 429 Token Rate Limit errors.
21
  """
22
  topic_list = []
23
  sub_tree_indices = {}
 
37
  # Append current node name to path
38
  node_path = f"{current_path} > {node_name}" if current_path else node_name
39
 
40
+ # If it's a Variable or leaf term, save the path only
41
  if "Variable" in node.get("level", "") or not node.get("children"):
42
+ collected_paths.append(node_path)
 
 
 
 
43
 
44
  # Dig deeper into children
45
  for child in node.get("children", []):
 
107
  response = llm.invoke(prompt.format(title=state["title"], abstract=state["abstract"], sub_tree=target_sub_tree))
108
  raw_keywords = [k.strip() for k in response.content.split(",") if k.strip()]
109
 
110
+ # Immediate validation pass inside the node branch
111
  valid_set = set()
112
  for line in target_sub_tree.split("\n"):
113
  if line.strip():
114
  path = line.replace("- ", "").strip()
 
115
  valid_set.add(path)
116
 
117
  valid_kws = [kw for kw in raw_keywords if kw in valid_set]
 
157
 
158
  inputs = {"title": title, "abstract": abstract}
159
 
160
+ # Execute graph synchronously
161
  output = app.invoke(inputs)
162
 
163
  chosen_topics = output.get("chosen_topics", [])
 
166
 
167
  topics_str = ", ".join(chosen_topics)
168
 
169
+ # Format output keywords to prepend their corresponding parent Topic
170
  formatted_keywords = []
171
  for kw in predicted_kws:
172
  matched_topic = "UNKNOWN"
 
173
  for topic in chosen_topics:
174
  sub_tree_text = SUB_TREE_LOOKUP.get(topic, "")
175
  if kw in sub_tree_text:
 
182
  else:
183
  keywords_str = "\n".join(formatted_keywords)
184
 
185
+ # Format separate display for caught and filtered hallucinations
186
  if not invalid_kws:
187
  invalid_str = "None! The agent validation pass achieved 100% data integrity."
188
  else: