Fix AI processor error handling for empty AI service responses
This commit is contained in:
parent
e29e5dbc76
commit
876cfddce7
@ -72,34 +72,48 @@ class FactExtractor:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# Call the AI service with gpt-oss model for fact extraction
|
# Call the AI service with gpt-oss model for fact extraction
|
||||||
response = requests.post(
|
try:
|
||||||
extraction_url,
|
response = requests.post(
|
||||||
json={
|
extraction_url,
|
||||||
"model": self.model,
|
json={
|
||||||
"messages": [
|
"model": self.model,
|
||||||
{"role": "system", "content": "You are a helpful assistant that extracts structured facts from articles."},
|
"messages": [
|
||||||
{"role": "user", "content": prompt}
|
{"role": "system", "content": "You are a helpful assistant that extracts structured facts from articles."},
|
||||||
],
|
{"role": "user", "content": prompt}
|
||||||
"temperature": 0.3,
|
],
|
||||||
"max_tokens": 1000
|
"temperature": 0.3,
|
||||||
},
|
"max_tokens": 1000
|
||||||
headers=self._get_headers(),
|
},
|
||||||
timeout=60
|
headers=self._get_headers(),
|
||||||
)
|
timeout=60
|
||||||
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
logger.error(f"Request failed for article '{title}': {e}")
|
||||||
|
# Return basic structure if request fails
|
||||||
|
return self._create_basic_fact_structure(article_content, title)
|
||||||
|
|
||||||
# Parse the response
|
# Parse the response
|
||||||
result = response.json()
|
|
||||||
extracted_text = result['choices'][0]['message']['content'].strip()
|
|
||||||
|
|
||||||
# Try to parse the JSON from the response
|
|
||||||
try:
|
try:
|
||||||
facts = json.loads(extracted_text)
|
result = response.json()
|
||||||
except json.JSONDecodeError:
|
extracted_text = result['choices'][0]['message']['content'].strip()
|
||||||
# If JSON parsing fails, create a basic structure
|
except json.JSONDecodeError as e:
|
||||||
logger.warning(f"Failed to parse JSON from AI response for article '{title}'. Creating basic structure.")
|
logger.error(f"Failed to parse JSON response from AI service for article '{title}': {e}")
|
||||||
|
# Return basic structure if response parsing fails
|
||||||
|
return self._create_basic_fact_structure(article_content, title)
|
||||||
|
|
||||||
|
# Check if the response is empty or invalid
|
||||||
|
if not extracted_text or extracted_text.strip() == "":
|
||||||
|
logger.warning(f"Empty response from AI service for article '{title}'. Creating basic structure.")
|
||||||
facts = self._create_basic_fact_structure(article_content, title)
|
facts = self._create_basic_fact_structure(article_content, title)
|
||||||
|
else:
|
||||||
|
# Try to parse the JSON from the response
|
||||||
|
try:
|
||||||
|
facts = json.loads(extracted_text)
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
# If JSON parsing fails, create a basic structure
|
||||||
|
logger.warning(f"Failed to parse JSON from AI response for article '{title}': {e}. Creating basic structure.")
|
||||||
|
facts = self._create_basic_fact_structure(article_content, title)
|
||||||
|
|
||||||
# Ensure all required fields are present
|
# Ensure all required fields are present
|
||||||
facts = self._ensure_required_fields(facts, title, article_content)
|
facts = self._ensure_required_fields(facts, title, article_content)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user