Implement optimized database schema and AI prompt - removed title field, summary to fact, removed financial_impact and main_points
This commit is contained in:
parent
9344a69636
commit
332f49eeee
@ -41,29 +41,22 @@ class AIEndpointClient:
|
|||||||
|
|
||||||
def extract_facts(self, text_content: str, prompt: str, model: str = "gpt-oss") -> Dict[str, Any]:
|
def extract_facts(self, text_content: str, prompt: str, model: str = "gpt-oss") -> Dict[str, Any]:
|
||||||
"""Extract facts from text using AI"""
|
"""Extract facts from text using AI"""
|
||||||
# Default prompt from requirements
|
# Default prompt from requirements - optimized for facts extraction
|
||||||
default_prompt = """Extract key facts from the following article in structured JSON format.
|
default_prompt = """Extract key facts from the following article in structured JSON format.
|
||||||
Return only valid JSON without any additional text.
|
Return only valid JSON without any additional text.
|
||||||
|
|
||||||
Article Title: {title}
|
|
||||||
Article Content: {article_content[:3000]}...
|
Article Content: {article_content[:3000]}...
|
||||||
|
|
||||||
Extract the following information:
|
Extract the following information:
|
||||||
1. Main topic/subject
|
1. Key entities (companies, people, locations, organizations)
|
||||||
2. Key entities (companies, people, locations, organizations)
|
2. Key dates or time periods mentioned
|
||||||
3. Financial impact or implications
|
3. Main facts from the article
|
||||||
4. Key dates or time periods mentioned
|
|
||||||
5. Summary of main points
|
|
||||||
|
|
||||||
Format the response as a JSON object with these fields:
|
Format the response as a JSON object with these fields:
|
||||||
{
|
{
|
||||||
"title": "{title}",
|
"fact": "main fact extracted from the article",
|
||||||
"summary": "brief summary",
|
|
||||||
"main_topic": "main topic",
|
|
||||||
"key_entities": ["entity1", "entity2"],
|
"key_entities": ["entity1", "entity2"],
|
||||||
"financial_impact": "positive/negative/neutral",
|
"key_dates": ["date1", "date2"]
|
||||||
"key_dates": ["date1", "date2"],
|
|
||||||
"main_points": ["point1", "point2", "point3"]
|
|
||||||
}"""
|
}"""
|
||||||
|
|
||||||
# Use provided prompt or default
|
# Use provided prompt or default
|
||||||
|
|||||||
@ -29,13 +29,9 @@ class DatabaseManager:
|
|||||||
CREATE TABLE IF NOT EXISTS facts (
|
CREATE TABLE IF NOT EXISTS facts (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
table_name TEXT NOT NULL,
|
table_name TEXT NOT NULL,
|
||||||
title TEXT,
|
fact TEXT,
|
||||||
summary TEXT,
|
|
||||||
main_topic TEXT,
|
|
||||||
key_entities TEXT,
|
key_entities TEXT,
|
||||||
financial_impact TEXT,
|
|
||||||
key_dates TEXT,
|
key_dates TEXT,
|
||||||
main_points TEXT,
|
|
||||||
file_path TEXT,
|
file_path TEXT,
|
||||||
extracted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
extracted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
processed BOOLEAN DEFAULT FALSE
|
processed BOOLEAN DEFAULT FALSE
|
||||||
@ -125,22 +121,16 @@ class DatabaseManager:
|
|||||||
# Convert lists to JSON strings for storage
|
# Convert lists to JSON strings for storage
|
||||||
key_entities = str(fact_data.get('key_entities', []))
|
key_entities = str(fact_data.get('key_entities', []))
|
||||||
key_dates = str(fact_data.get('key_dates', []))
|
key_dates = str(fact_data.get('key_dates', []))
|
||||||
main_points = str(fact_data.get('main_points', []))
|
|
||||||
|
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
INSERT INTO facts (
|
INSERT INTO facts (
|
||||||
table_name, title, summary, main_topic, key_entities,
|
table_name, fact, key_entities, key_dates, file_path
|
||||||
financial_impact, key_dates, main_points, file_path
|
) VALUES (?, ?, ?, ?, ?)
|
||||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
||||||
''', (
|
''', (
|
||||||
table_name,
|
table_name,
|
||||||
fact_data.get('title'),
|
fact_data.get('fact'),
|
||||||
fact_data.get('summary'),
|
|
||||||
fact_data.get('main_topic'),
|
|
||||||
key_entities,
|
key_entities,
|
||||||
fact_data.get('financial_impact'),
|
|
||||||
key_dates,
|
key_dates,
|
||||||
main_points,
|
|
||||||
fact_data.get('file_path')
|
fact_data.get('file_path')
|
||||||
))
|
))
|
||||||
|
|
||||||
@ -166,8 +156,7 @@ class DatabaseManager:
|
|||||||
with self.get_connection() as conn:
|
with self.get_connection() as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
SELECT id, table_name, title, summary, main_topic, key_entities,
|
SELECT id, table_name, fact, key_entities, key_dates, file_path, extracted_at
|
||||||
financial_impact, key_dates, main_points, file_path, extracted_at
|
|
||||||
FROM facts
|
FROM facts
|
||||||
WHERE table_name = ?
|
WHERE table_name = ?
|
||||||
ORDER BY extracted_at DESC
|
ORDER BY extracted_at DESC
|
||||||
@ -185,8 +174,6 @@ class DatabaseManager:
|
|||||||
fact['key_entities'] = eval(fact['key_entities'])
|
fact['key_entities'] = eval(fact['key_entities'])
|
||||||
if fact['key_dates']:
|
if fact['key_dates']:
|
||||||
fact['key_dates'] = eval(fact['key_dates'])
|
fact['key_dates'] = eval(fact['key_dates'])
|
||||||
if fact['main_points']:
|
|
||||||
fact['main_points'] = eval(fact['main_points'])
|
|
||||||
facts.append(fact)
|
facts.append(fact)
|
||||||
|
|
||||||
return facts
|
return facts
|
||||||
@ -196,8 +183,7 @@ class DatabaseManager:
|
|||||||
with self.get_connection() as conn:
|
with self.get_connection() as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
SELECT id, table_name, title, summary, main_topic, key_entities,
|
SELECT id, table_name, fact, key_entities, key_dates, file_path, extracted_at
|
||||||
financial_impact, key_dates, main_points, file_path, extracted_at
|
|
||||||
FROM facts
|
FROM facts
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
''', (fact_id,))
|
''', (fact_id,))
|
||||||
@ -211,8 +197,6 @@ class DatabaseManager:
|
|||||||
fact['key_entities'] = eval(fact['key_entities'])
|
fact['key_entities'] = eval(fact['key_entities'])
|
||||||
if fact['key_dates']:
|
if fact['key_dates']:
|
||||||
fact['key_dates'] = eval(fact['key_dates'])
|
fact['key_dates'] = eval(fact['key_dates'])
|
||||||
if fact['main_points']:
|
|
||||||
fact['main_points'] = eval(fact['main_points'])
|
|
||||||
return fact
|
return fact
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user