This commit is contained in:
53
task/summary/ai_summary_ollama.py
Normal file
53
task/summary/ai_summary_ollama.py
Normal file
@ -0,0 +1,53 @@
|
||||
from database.database import get_session
|
||||
from database.tmaterial.crud import get_material_need_summary, update_material
|
||||
from llm.local.ollama import Ollama
|
||||
from log.log_manager import log
|
||||
from task.manager_task import execute_task
|
||||
|
||||
|
||||
def ai_summary(input_message: str, instance) -> str:
|
||||
response = instance.generate(input_message)
|
||||
log(response)
|
||||
return response
|
||||
|
||||
def ai_summary_task():
|
||||
with get_session() as db:
|
||||
news_list = get_material_need_summary(db)
|
||||
if len(news_list) == 0:
|
||||
log("ai_summary_task finish, task size 0")
|
||||
return
|
||||
log(f"ai summary task size {len(news_list)}")
|
||||
ollama = Ollama()
|
||||
if not ollama.is_service_running():
|
||||
log("ai_summary_task finish, ollama service not running")
|
||||
return
|
||||
for news in news_list:
|
||||
input_message = (
|
||||
"""
|
||||
请为以下新闻生成严格单段落的中文摘要,要求:
|
||||
1. 保持段落连贯性,不使用任何分段符号(包括空行、缩进或序号)
|
||||
2. 核心要素按此顺序呈现:
|
||||
[时间]>[地点]>[主体机构]>[关键事件]>[量化影响]
|
||||
3. 采用"总-分"结构:
|
||||
- 首句陈述核心事实(包含最关键的时间地点主体)
|
||||
- 中间展开关键细节(使用衔接词:同时/此外/值得注意的是)
|
||||
- 结尾说明当前状态/后续影响
|
||||
4. 字数严格控制在100个字符以内
|
||||
5. 禁止使用项目符号、引文格式等非连贯文本元素
|
||||
|
||||
新闻原文:
|
||||
"""
|
||||
+ news.content
|
||||
)
|
||||
summary = ollama.generate_text(input_message)
|
||||
log(f'{news.url} {summary}')
|
||||
# 判断summary是否是一段话
|
||||
if '\n' in summary:
|
||||
summary = 'summary formate error'
|
||||
updates = {"ai_summary": summary}
|
||||
update_material(db, news.id, updates)
|
||||
log(f"ai_summary_task finish, task size {len(news_list)}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
execute_task(ai_summary_task)
|
||||
Reference in New Issue
Block a user