Tirath5504 commited on
Commit
596aa39
·
verified ·
1 Parent(s): 6c03205

Update pipeline/meta_review.py

Browse files
Files changed (1) hide show
  1. pipeline/meta_review.py +57 -49
pipeline/meta_review.py CHANGED
@@ -14,6 +14,13 @@ client = OpenAI(
14
  api_key=os.getenv("OPENROUTER_API_KEY"),
15
  )
16
 
 
 
 
 
 
 
 
17
  class MetaReviewResult(BaseModel):
18
  meta_review: str
19
 
@@ -25,15 +32,6 @@ def construct_meta_review_prompt(
25
  ) -> tuple:
26
  """
27
  Construct prompt for meta-review generation
28
-
29
- Args:
30
- paper_title: Paper title
31
- paper_abstract: Paper abstract
32
- resolutions: List of disagreement resolutions
33
- search_results: Search and retrieval results
34
-
35
- Returns:
36
- Tuple of (system_prompt, user_prompt)
37
  """
38
  # Aggregate all resolutions
39
  all_accepted = {}
@@ -110,20 +108,10 @@ async def generate_meta_review(
110
  paper_abstract: str,
111
  resolutions: List[Dict],
112
  search_results: Dict,
113
- retries: int = 5
114
  ) -> str:
115
  """
116
- Generate a meta-review using DeepSeek-R1
117
-
118
- Args:
119
- paper_title: Paper title
120
- paper_abstract: Paper abstract
121
- resolutions: List of disagreement resolutions
122
- search_results: Search and retrieval results
123
- retries: Maximum retry attempts
124
-
125
- Returns:
126
- Generated meta-review text
127
  """
128
  if not resolutions:
129
  return "Unable to generate meta-review: No disagreement resolutions available."
@@ -140,31 +128,51 @@ async def generate_meta_review(
140
  {"role": "user", "content": user_prompt},
141
  ]
142
 
143
- for attempt in range(retries):
144
- try:
145
- response = await asyncio.to_thread(
146
- client.chat.completions.create,
147
- model="deepseek/deepseek-r1",
148
- messages=messages,
149
- )
150
-
151
- if not response.choices or not response.choices[0].message.content.strip():
152
- raise ValueError("Empty response from DeepSeek-R1")
153
-
154
- meta_review_text = response.choices[0].message.content.strip()
155
-
156
- # Remove any JSON formatting if present
157
- if meta_review_text.startswith("```"):
158
- lines = meta_review_text.split("\n")
159
- meta_review_text = "\n".join(lines[1:-1])
160
-
161
- return meta_review_text
162
-
163
- except Exception as e:
164
- wait_time = 2 ** attempt
165
- print(f"Meta-review generation attempt {attempt + 1} failed: {e}")
166
-
167
- if attempt < retries - 1:
168
- await asyncio.sleep(wait_time)
169
- else:
170
- return f"Error generating meta-review: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  api_key=os.getenv("OPENROUTER_API_KEY"),
15
  )
16
 
17
+ # Priority list of models (Consistency with other files)
18
+ MODELS = [
19
+ "deepseek/deepseek-r1",
20
+ "deepseek/deepseek-r1-distill-llama-70b",
21
+ "google/gemini-2.0-flash-exp:free"
22
+ ]
23
+
24
  class MetaReviewResult(BaseModel):
25
  meta_review: str
26
 
 
32
  ) -> tuple:
33
  """
34
  Construct prompt for meta-review generation
 
 
 
 
 
 
 
 
 
35
  """
36
  # Aggregate all resolutions
37
  all_accepted = {}
 
108
  paper_abstract: str,
109
  resolutions: List[Dict],
110
  search_results: Dict,
111
+ retries: int = 3 # Reduced retries per model
112
  ) -> str:
113
  """
114
+ Generate a meta-review using DeepSeek-R1 (with fallback)
 
 
 
 
 
 
 
 
 
 
115
  """
116
  if not resolutions:
117
  return "Unable to generate meta-review: No disagreement resolutions available."
 
128
  {"role": "user", "content": user_prompt},
129
  ]
130
 
131
+ last_exception = None
132
+
133
+ for model in MODELS:
134
+ print(f"Generating Meta-Review with model: {model}")
135
+
136
+ for attempt in range(retries):
137
+ try:
138
+ response = await asyncio.to_thread(
139
+ client.chat.completions.create,
140
+ model=model,
141
+ messages=messages,
142
+ # CRITICAL FIX: Limit max_tokens to prevent 402 Error
143
+ # 8192 is plenty for a meta-review (~6000 words) but much cheaper than 64k
144
+ max_tokens=8192,
145
+ )
146
+
147
+ if not response.choices or not response.choices[0].message.content.strip():
148
+ raise ValueError("Empty response from AI")
149
+
150
+ meta_review_text = response.choices[0].message.content.strip()
151
+
152
+ # Remove any JSON formatting/thinking tags if present (cleanup)
153
+ if meta_review_text.startswith("```"):
154
+ lines = meta_review_text.split("\n")
155
+ # Simple heuristic to strip markdown fences
156
+ if lines[0].startswith("```"):
157
+ lines = lines[1:]
158
+ if lines and lines[-1].startswith("```"):
159
+ lines = lines[:-1]
160
+ meta_review_text = "\n".join(lines)
161
+
162
+ return meta_review_text
163
+
164
+ except Exception as e:
165
+ last_exception = e
166
+ error_msg = str(e)
167
+ print(f"Model {model} - Attempt {attempt + 1} failed: {error_msg}")
168
+
169
+ # Immediate fallback on payment errors
170
+ if "402" in error_msg or "insufficient_quota" in error_msg:
171
+ print("Insufficient credits detected. Switching to cheaper model...")
172
+ break
173
+
174
+ wait_time = 2 ** attempt
175
+ if attempt < retries - 1:
176
+ await asyncio.sleep(wait_time)
177
+
178
+ return f"Error generating meta-review after trying all models: {str(last_exception)}"