import time from database.database import get_session from database.tcontentdispatch.curd import get_content_by_title_and_category, update from llm.local.ollama import Ollama from log.log_manager import log from task.manager_task import execute_task def ai_edit(input_message: str) -> str: log(f"ai_edit start execute at {time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}") ollama = Ollama() response = ollama.generate_text(input_message) log(f"ai_edit end execute at {time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}") return response def revisal_task(): with get_session() as db: # 1. 构建今日新闻文章标题,格式:今日新闻yyyy-MM-dd title = ("今日新闻" + time.strftime("%Y", time.localtime()) + '年' + time.strftime("%m", time.localtime()) + '月' + time.strftime("%d", time.localtime()) + '日') # 2. 从内容分发数据表获取当前标题和分类的文章是否存在 content_dispatch = get_content_by_title_and_category(db, title, "新鲜事") ai_content = "" if content_dispatch and content_dispatch.content: # 3. 执行AI编辑 input_message = (('按照规则编辑提供的内容。规则如下:\n' '1 去除重复内容\n' '2 不要故意删除内容\n' '3 重新编号\n' '4 不要出现空行\n' '5 不要出现类似"以下是根据您提供的规则编辑后的内容"等提示信息,直接输出编辑后的内容\n' '内容如下:\n') + content_dispatch.content) ai_content = ai_edit(input_message) print(content_dispatch.content) print("-----------------------------------------------------------") print(ai_content) # 4. 去掉ai_content中的空行 ai_content = "\n".join([line for line in ai_content.split("\n") if line.strip()]) # 5. 把content写入数据库 if ai_content: content_dispatch.ai_content = ai_content content_dispatch.is_sent = False update(db) # 获取ai_content的行数 lines = ai_content.strip().split("\n") log(f"revisal news task finish, news count: {len(lines)}, news words: {len(ai_content)}") if __name__ == "__main__": execute_task(revisal_task)