Seth0330 commited on
Commit
c019cc4
·
verified ·
1 Parent(s): 5c5d420

Update backend/app/openrouter_client.py

Browse files
Files changed (1) hide show
  1. backend/app/openrouter_client.py +35 -11
backend/app/openrouter_client.py CHANGED
@@ -15,9 +15,15 @@ MODEL_NAME = "qwen/qwen3-vl-235b-a22b-instruct"
15
  def _file_to_image_block(file_bytes: bytes, content_type: str) -> Dict[str, Any]:
16
  """
17
  Encode the file as a data URL to feed into the multimodal model.
18
- For demo purposes we treat PDFs and images the same way here.
 
 
19
  """
20
  b64 = base64.b64encode(file_bytes).decode("utf-8")
 
 
 
 
21
  return {
22
  "type": "input_image",
23
  "image_url": f"data:{content_type};base64,{b64}",
@@ -101,7 +107,13 @@ async def extract_fields_from_document(
101
  data = resp.json()
102
 
103
  # OpenRouter returns choices[0].message.content
 
 
 
104
  content = data["choices"][0]["message"]["content"]
 
 
 
105
 
106
  # content may be a string or a list of content blocks
107
  if isinstance(content, list):
@@ -109,34 +121,46 @@ async def extract_fields_from_document(
109
  else:
110
  text = content
111
 
 
 
 
112
  # Try to parse JSON from the model output
113
  # The model might return JSON wrapped in markdown code blocks or with extra text
114
  try:
115
  # First, try direct JSON parsing
116
- return json.loads(text)
117
- except json.JSONDecodeError:
 
 
 
118
  # Try to extract JSON from markdown code blocks
119
  json_match = re.search(r'```(?:json)?\s*(\{.*?\})\s*```', text, re.DOTALL)
120
  if json_match:
121
  try:
122
- return json.loads(json_match.group(1))
123
- except json.JSONDecodeError:
124
- pass
 
 
125
 
126
  # Try to find JSON object in the text (look for {...})
127
  json_match = re.search(r'\{.*\}', text, re.DOTALL)
128
  if json_match:
129
  try:
130
- return json.loads(json_match.group(0))
131
- except json.JSONDecodeError:
132
- pass
 
 
133
 
134
  # If all parsing fails, return a default structure with the raw text
 
135
  return {
136
  "doc_type": "other",
137
  "confidence": 50.0,
138
  "fields": {
139
- "raw_text": text[:500], # First 500 chars
140
- "error": "Could not parse JSON from model response"
 
141
  }
142
  }
 
15
  def _file_to_image_block(file_bytes: bytes, content_type: str) -> Dict[str, Any]:
16
  """
17
  Encode the file as a data URL to feed into the multimodal model.
18
+ Note: PDFs may not be directly supported by vision models.
19
+ For images (PNG, JPG, etc.), this works fine.
20
+ For PDFs, the model might not be able to process them.
21
  """
22
  b64 = base64.b64encode(file_bytes).decode("utf-8")
23
+
24
+ # Log file type for debugging
25
+ print(f"[DEBUG] Encoding file as image block. Content type: {content_type}, Size: {len(file_bytes)} bytes")
26
+
27
  return {
28
  "type": "input_image",
29
  "image_url": f"data:{content_type};base64,{b64}",
 
107
  data = resp.json()
108
 
109
  # OpenRouter returns choices[0].message.content
110
+ if "choices" not in data or len(data["choices"]) == 0:
111
+ raise ValueError("No choices in OpenRouter response")
112
+
113
  content = data["choices"][0]["message"]["content"]
114
+
115
+ # Log the raw response for debugging (first 500 chars)
116
+ print(f"[DEBUG] OpenRouter response preview: {str(content)[:500]}")
117
 
118
  # content may be a string or a list of content blocks
119
  if isinstance(content, list):
 
121
  else:
122
  text = content
123
 
124
+ if not text or not text.strip():
125
+ raise ValueError("Empty response from OpenRouter API")
126
+
127
  # Try to parse JSON from the model output
128
  # The model might return JSON wrapped in markdown code blocks or with extra text
129
  try:
130
  # First, try direct JSON parsing
131
+ parsed = json.loads(text)
132
+ print(f"[DEBUG] Successfully parsed JSON directly")
133
+ return parsed
134
+ except json.JSONDecodeError as e:
135
+ print(f"[DEBUG] Direct JSON parse failed: {e}")
136
  # Try to extract JSON from markdown code blocks
137
  json_match = re.search(r'```(?:json)?\s*(\{.*?\})\s*```', text, re.DOTALL)
138
  if json_match:
139
  try:
140
+ parsed = json.loads(json_match.group(1))
141
+ print(f"[DEBUG] Successfully parsed JSON from markdown code block")
142
+ return parsed
143
+ except json.JSONDecodeError as e2:
144
+ print(f"[DEBUG] Markdown code block parse failed: {e2}")
145
 
146
  # Try to find JSON object in the text (look for {...})
147
  json_match = re.search(r'\{.*\}', text, re.DOTALL)
148
  if json_match:
149
  try:
150
+ parsed = json.loads(json_match.group(0))
151
+ print(f"[DEBUG] Successfully parsed JSON from regex match")
152
+ return parsed
153
+ except json.JSONDecodeError as e3:
154
+ print(f"[DEBUG] Regex match parse failed: {e3}")
155
 
156
  # If all parsing fails, return a default structure with the raw text
157
+ print(f"[WARNING] All JSON parsing attempts failed. Returning fallback structure.")
158
  return {
159
  "doc_type": "other",
160
  "confidence": 50.0,
161
  "fields": {
162
+ "raw_response": text[:1000], # First 1000 chars for debugging
163
+ "error": "Could not parse JSON from model response",
164
+ "note": "Check server logs for full response"
165
  }
166
  }