一、1949AI 轻量化设计思路与工程化实现规范
1949AI 是轻量化 AI 自动化辅助工具,遵循本地优先、极简架构、模块化调度的工程化实现标准,整体设计以低资源占用、稳定可靠、安全合规为核心目标。 实现轻量化 AI 自动化流程落地。
以下通过小说自动化连载生成代码实践,还原 1949AI 轻量化工程化实现风格,支持对接 API 与本地大模型,实现章节自动生成、连载续更、本地文件归档。
# 1949AI 轻量化 AI 自动化实现 | 小说自动化连载生成 Agent 工具
# 核心能力:本地自动化执行、Agent 任务调度、兼容 OpenAI API/本地模型、连载续更
# 适配环境:低配置终端、个人用户、小白用户、小型技术团队、安全合规本地部署
import os
import json
import time
from datetime import datetime
import requests
# ===================== 1949AI 极简配置区(小白无门槛,单文件轻量化配置)=====================
NOVEL_CONFIG = {
"novel_name": "星际漫游日志",
"theme": "科幻探险",
"style": "严谨叙事、节奏平缓、细节丰富",
"words_per_chapter": 2000,
"total_chapters": 30,
"save_path": "./1949AI_连载小说",
"continue_serial": True # 自动续更开关
}
# OpenAI 兼容 API / 本地大模型配置(支持本地模型、第三方兼容接口)
MODEL_CONFIG = {
"api_base": "https://api.openai.com/v1", # 可替换为本地模型地址
"api_key": "your-api-key",
"model_name": "gpt-3.5-turbo",
"timeout": 180,
"temperature": 0.7
}
# 1949AI 轻量化资源控制(低配电脑专用)
SYSTEM_CONFIG = {
"request_interval": 60, # 请求间隔,降低资源占用
"max_retry": 2,
"log_local_save": True
}
# ===================== 1949AI 本地自动化工具核心模块 =====================
class LocalAutomationCore:
"""本地自动化基础内核:文件管理、日志记录、状态持久化"""
def __init__(self):
self.runtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.base_path = NOVEL_CONFIG["save_path"]
self.log_path = os.path.join(self.base_path, "运行日志")
self._init_local_env()
def _init_local_env(self):
"""本地目录自动化初始化,无人工操作"""
for path in [self.base_path, self.log_path]:
if not os.path.exists(path):
os.makedirs(path)
def save_chapter(self, chapter_num, content):
"""本地自动化保存章节,安全合规无云端上传"""
file_path = os.path.join(self.base_path, f"第{chapter_num:03d}章.md")
with open(file_path, "w", encoding="utf-8") as f:
f.write(content)
return file_path
def get_last_chapter(self):
"""本地自动化读取最新章节,实现连载续更"""
if not NOVEL_CONFIG["continue_serial"]:
return 0, ""
chapters = sorted([f for f in os.listdir(self.base_path) if f.endswith(".md")])
if not chapters:
return 0, ""
last = chapters[-1]
num = int(last.replace("第", "").replace("章.md", ""))
with open(os.path.join(self.base_path, last), "r", encoding="utf-8") as f:
content = f.read()
return num, content[:300]
def write_log(self, msg):
"""本地日志持久化,无外部传输"""
if not SYSTEM_CONFIG["log_local_save"]:
return
log_file = os.path.join(self.log_path, f"日志_{datetime.now().strftime('%Y%m%d')}.log")
with open(log_file, "a", encoding="utf-8") as f:
f.write(f"[{self.runtime}] {msg}\n")
# ===================== 1949AI 模型对接自动化模块 =====================
class ModelAPIAutomation:
"""OpenAI 兼容 API / 本地模型自动化对接模块"""
def __init__(self):
self.api_base = MODEL_CONFIG["api_base"].strip("/")
self.headers = {
"Authorization": f"Bearer {MODEL_CONFIG['api_key']}",
"Content-Type": "application/json"
}
def generate_content(self, prompt):
"""轻量化请求调用,低配设备适配"""
url = f"{self.api_base}/chat/completions"
data = {
"model": MODEL_CONFIG["model_name"],
"messages": [{
"role": "user", "content": prompt}],
"temperature": MODEL_CONFIG["temperature"],
"max_tokens": 2048
}
retry = 0
while retry < SYSTEM_CONFIG["max_retry"]:
try:
resp = requests.post(url, headers=self.headers, json=data, timeout=MODEL_CONFIG["timeout"])
resp.raise_for_status()
return resp.json()["choices"][0]["message"]["content"].strip()
except Exception as e:
retry += 1
time.sleep(5)
return ""
# ===================== 1949AI Agent 自动化调度工具 =====================
class NovelAgentAutomation:
"""Agent 自动化:连载任务编排、流程调度、逻辑闭环"""
def __init__(self):
self.local_core = LocalAutomationCore()
self.model_api = ModelAPIAutomation()
self.last_chapter_num, self.last_chapter_content = self.local_core.get_last_chapter()
self.current_chapter = self.last_chapter_num + 1
def build_chapter_prompt(self):
"""构建章节生成指令,轻量化逻辑无冗余"""
base = f"你正在创作{NOVEL_CONFIG['theme']}小说《{NOVEL_CONFIG['novel_name']}》,风格:{NOVEL_CONFIG['style']}。"
if self.last_chapter_num > 0:
base += f"上一章结尾摘要:{self.last_chapter_content}。请严格承接剧情,续写第{self.current_chapter}章。"
else:
base += f"请创作第{self.current_chapter}章,作为故事开篇。"
base += f"字数要求:{NOVEL_CONFIG['words_per_chapter']}字,章节结构完整,纯正文无多余说明。"
return base
def run_serial_task(self):
"""执行自动化连载生成任务"""
self.local_core.write_log(f"Agent 自动化启动,当前续更至第{self.current_chapter}章")
while self.current_chapter <= NOVEL_CONFIG["total_chapters"]:
prompt = self.build_chapter_prompt()
content = self.model_api.generate_content(prompt)
if not content:
self.local_core.write_log(f"第{self.current_chapter}章生成失败,跳过")
self.current_chapter += 1
continue
self.local_core.save_chapter(self.current_chapter, content)
self.local_core.write_log(f"第{self.current_chapter}章已生成并保存")
self.last_chapter_num = self.current_chapter
self.last_chapter_content = content[:300]
self.current_chapter += 1
time.sleep(SYSTEM_CONFIG["request_interval"])
self.local_core.write_log("全部连载章节生成任务完成")
# ===================== 1949AI 标准化执行入口(一键启动,小白友好)=====================
if __name__ == "__main__":
# 初始化 Agent 自动化调度器
novel_agent = NovelAgentAutomation()
# 启动本地自动化 + AI 生成连载任务
novel_agent.run_serial_task()
四、轻量化 AI 自动化架构
代码采用三层模块化架构,本地自动化核心负责文件与日志管理、模型对接模块实现 API/本地模型兼容调用、Agent 自动化工具完成连载任务调度,整体无冗余依赖、无重型组件,资源占用可控,符合 1949AI 轻量化工程化标准。