-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfix_and_link.py
More file actions
192 lines (148 loc) · 6.29 KB
/
Copy pathfix_and_link.py
File metadata and controls
192 lines (148 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
"""
一键修复脚本 — 修复低分文章 + 全量内链 (PostgreSQL)
=====================================================
直接用 LLM 修复所有 <80 分的文章,然后为所有已发布文章建立内链。
用法:
DB_HOST=localhost python fix_and_link.py
"""
import os, sys, time, hashlib, logging
os.environ.setdefault("OTEL_SDK_DISABLED", "true")
os.environ.setdefault("DB_HOST", "localhost")
from dotenv import load_dotenv
load_dotenv()
import psycopg2
import psycopg2.extras
from langchain_openai import ChatOpenAI
from core.quality_checker import QualityChecker
from core.auto_fixer import AutoFixer
from core.linker import AutoLinker
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(message)s", datefmt="%H:%M:%S")
log = logging.getLogger("FIX")
# ─── LLM ───
_llm_model = os.environ.get("GEO_LLM_MODEL") or os.environ.get("OPENAI_MODEL") or "deepseek-v4-pro"
_llm_base = os.environ.get("GEO_LLM_BASE_URL") or os.environ.get("OPENAI_BASE_URL") or "https://api.deepseek.com"
_llm_key = os.environ.get("GEO_LLM_API_KEY") or os.environ.get("DEEPSEEK_API_KEY") or os.environ.get("OPENAI_API_KEY", "")
llm = ChatOpenAI(
model=_llm_model,
openai_api_key=_llm_key,
openai_api_base=_llm_base,
temperature=0.3,
max_tokens=8000,
)
PASS_THRESHOLD = 80
MAX_ATTEMPTS = 3
def get_db():
return psycopg2.connect(
host=os.getenv("DB_HOST", "localhost"),
port=int(os.getenv("DB_PORT", "5432")),
user=os.getenv("DB_USER", "geo_app"),
password=os.getenv("DB_PASSWORD", "change-this-password"),
dbname=os.getenv("DB_NAME", "geo_engine"),
connect_timeout=5,
)
def update_article(article_id, **fields):
cnx = get_db()
try:
cursor = cnx.cursor()
sets = ", ".join(f"{k}=%s" for k in fields)
cursor.execute(f"UPDATE geo_articles SET {sets} WHERE id=%s", (*fields.values(), article_id))
cnx.commit()
cursor.close()
finally:
cnx.close()
def fix_article(article_id: int, title: str, content: str) -> bool:
"""修复单篇文章直到通过质检或放弃"""
checker = QualityChecker()
fixer = AutoFixer()
for attempt in range(1, MAX_ATTEMPTS + 1):
score, report = checker.evaluate_article(title, content)
failed = [k for k, v in report.items() if not v]
passed_dims = [k for k, v in report.items() if v]
log.info(f" 第{attempt}/{MAX_ATTEMPTS}次 [{score}分] ✅{','.join(passed_dims)} ❌{','.join(failed)}")
update_article(article_id, quality_score=score)
if score >= PASS_THRESHOLD:
update_article(article_id, publish_status=1, quality_score=score)
log.info(f" ✅ 通过! [{score}分]")
return True
if attempt >= MAX_ATTEMPTS:
log.warning(f" 💀 放弃 [{score}分]")
return False
fix_prompt = fixer.generate_fix_prompt(content, report)
if not fix_prompt:
log.warning(" AutoFixer 未生成指令")
return False
try:
result = llm.invoke(fix_prompt)
new_content = result.content if hasattr(result, "content") else str(result)
if len(new_content.strip()) < 500:
log.warning(f" 返修结果过短 ({len(new_content)}字符)")
continue
content_hash = hashlib.md5(new_content.encode("utf-8")).hexdigest()
update_article(article_id, content_markdown=new_content, content_hash=content_hash)
content = new_content
log.info(f" 🔧 返修完成 ({len(new_content)}字符)")
except Exception as e:
log.error(f" LLM错误: {e}")
continue
time.sleep(1)
return False
def main():
log.info("=" * 60)
log.info(" 一键修复脚本启动 (PostgreSQL)")
log.info("=" * 60)
cnx = get_db()
cursor = cnx.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
# ═══ 阶段 1: 修复低分文章 ═══
log.info("\n📋 阶段 1: 修复低于80分的文章")
cursor.execute(
"SELECT id, title, content_markdown, quality_score "
"FROM geo_articles "
"WHERE (quality_score < 80 OR quality_score IS NULL) "
"AND content_markdown IS NOT NULL "
"AND LENGTH(content_markdown) > 200 "
"ORDER BY quality_score DESC"
)
low_articles = cursor.fetchall()
log.info(f" 发现 {len(low_articles)} 篇需要修复")
fixed_count = 0
failed_count = 0
for i, article in enumerate(low_articles, 1):
title = article["title"] or ""
content = article["content_markdown"] or ""
score = article["quality_score"] or 0
log.info(f"\n[{i}/{len(low_articles)}] #{article['id']} [{score}分] {title[:40]}...")
if fix_article(article["id"], title, content):
fixed_count += 1
else:
failed_count += 1
time.sleep(2)
log.info(f"\n📊 修复结果: ✅通过 {fixed_count} | ❌未通过 {failed_count}")
# ═══ 阶段 2: 全量内链 ═══
log.info("\n🔗 阶段 2: 全量内链构建")
linker = AutoLinker()
cursor.execute("SELECT id, title FROM geo_articles WHERE publish_status >= 1 ORDER BY id")
published = cursor.fetchall()
log.info(f" {len(published)} 篇已发布文章")
total_out, total_in = 0, 0
for article in published:
r = linker.link_article(article["id"])
total_out += r["outgoing"]
total_in += r["incoming"]
if r["outgoing"] or r["incoming"]:
log.info(f" [{article['id']}] 出{r['outgoing']} 入{r['incoming']}")
log.info(f"\n🔗 内链结果: 出链 {total_out}, 入链 {total_in}")
# ═══ 最终统计 ═══
cursor.execute("SELECT COUNT(*) as c FROM geo_articles WHERE publish_status >= 1")
total_pub = cursor.fetchone()["c"]
cursor.execute("SELECT COUNT(*) as c FROM geo_links")
total_links = cursor.fetchone()["c"]
cursor.execute("SELECT ROUND(AVG(quality_score),1) as avg FROM geo_articles WHERE quality_score > 0")
avg = cursor.fetchone()["avg"]
log.info("\n" + "=" * 60)
log.info(f" ✅ 完成! 已发布: {total_pub}篇 | 内链: {total_links}条 | 平均分: {avg}")
log.info("=" * 60)
cursor.close()
cnx.close()
if __name__ == "__main__":
main()