forked from YILING0013/AI_NovelGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnovel_generator.py
1173 lines (1001 loc) · 41.7 KB
/
novel_generator.py
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# novel_generator.py
# -*- coding: utf-8 -*-
import os
import logging
import re
import time
import traceback
import json
from typing import List, Optional, Tuple
from langchain_chroma import Chroma
from chromadb.config import Settings
from langchain.docstore.document import Document
# nltk、sentence_transformers 及文本处理相关
import nltk
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
# 工具函数
from utils import (
read_file, append_text_to_file, clear_file_content,
save_string_to_txt
)
# prompt模板
from prompt_definitions import (
core_seed_prompt,
character_dynamics_prompt,
world_building_prompt,
plot_architecture_prompt,
chapter_blueprint_prompt,
chunked_chapter_blueprint_prompt,
summary_prompt,
update_character_state_prompt,
first_chapter_draft_prompt,
next_chapter_draft_prompt,
summarize_recent_chapters_prompt,
create_character_state_prompt
)
# 章节目录解析
from chapter_directory_parser import get_chapter_info_from_blueprint
from llm_adapters import create_llm_adapter
from embedding_adapters import create_embedding_adapter
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
# ============ 通用的重试封装 ============
def call_with_retry(func, max_retries=3, sleep_time=2, fallback_return=None, **kwargs):
"""
通用的重试机制封装。
:param func: 要执行的函数
:param max_retries: 最大重试次数
:param sleep_time: 重试前的等待秒数
:param fallback_return: 如果多次重试仍失败时的返回值
:param kwargs: 传给func的命名参数
:return: func的结果,若失败则返回 fallback_return
"""
for attempt in range(1, max_retries + 1):
try:
return func(**kwargs)
except Exception as e:
logging.warning(f"[call_with_retry] Attempt {attempt} failed with error: {e}")
traceback.print_exc()
if attempt < max_retries:
time.sleep(sleep_time)
else:
logging.error("Max retries reached, returning fallback_return.")
return fallback_return
# ============ 工具函数 ============
def remove_think_tags(text: str) -> str:
"""移除 <think>...</think> 包裹的内容"""
return re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
def debug_log(prompt: str, response_content: str):
logging.info(
f"\n[######################################### Prompt #########################################]\n{prompt}\n"
)
logging.info(
f"\n[######################################### Response #########################################]\n{response_content}\n"
)
def invoke_with_cleaning(llm_adapter, prompt: str) -> str:
"""
对 LLM 的调用增加了重试封装,
如果多次失败,则返回空字符串以继续流程,而不是中断。
"""
def _invoke(prompt):
return llm_adapter.invoke(prompt)
response = call_with_retry(func=_invoke, max_retries=3, fallback_return="", prompt=prompt)
if not response:
logging.warning("No response from model after retry. Return empty.")
return ""
cleaned_text = remove_think_tags(response)
debug_log(prompt, cleaned_text)
return cleaned_text.strip()
# ============ 获取 vectorstore 路径 ============
def get_vectorstore_dir(filepath: str) -> str:
return os.path.join(filepath, "vectorstore")
# ============ 清空向量库 ============
def clear_vector_store(filepath: str) -> bool:
import shutil
store_dir = get_vectorstore_dir(filepath)
if not os.path.exists(store_dir):
logging.info("No vector store found to clear.")
return False
try:
shutil.rmtree(store_dir)
logging.info(f"Vector store directory '{store_dir}' removed.")
return True
except Exception as e:
logging.error(f"无法删除向量库文件夹,请关闭程序后手动删除 {store_dir}。\n {str(e)}")
traceback.print_exc()
return False
# ============ 根据 embedding 接口创建/加载 Chroma ============
def init_vector_store(
embedding_adapter,
texts: List[str],
filepath: str
) -> Optional[Chroma]:
"""
在 filepath 下创建/加载一个 Chroma 向量库并插入 texts。
如果Embedding失败,则返回 None,不中断任务。
"""
from langchain.embeddings.base import Embeddings as LCEmbeddings
store_dir = get_vectorstore_dir(filepath)
os.makedirs(store_dir, exist_ok=True)
documents = [Document(page_content=str(t)) for t in texts]
try:
class LCEmbeddingWrapper(LCEmbeddings):
def embed_documents(self, texts: List[str]) -> List[List[float]]:
return call_with_retry(
func=embedding_adapter.embed_documents,
max_retries=3,
fallback_return=[],
texts=texts
)
def embed_query(self, query: str) -> List[float]:
res = call_with_retry(
func=embedding_adapter.embed_query,
max_retries=3,
fallback_return=[],
query=query
)
return res
chroma_embedding = LCEmbeddingWrapper()
vectorstore = Chroma.from_documents(
documents,
embedding=chroma_embedding,
persist_directory=store_dir,
client_settings=Settings(anonymized_telemetry=False),
collection_name="novel_collection"
)
return vectorstore
except Exception as e:
logging.warning(f"Init vector store failed: {e}")
traceback.print_exc()
return None
def load_vector_store(
embedding_adapter,
filepath: str
) -> Optional[Chroma]:
"""
读取已存在的 Chroma 向量库。若不存在则返回 None。
如果加载失败(embedding 或IO问题),则返回 None。
"""
store_dir = get_vectorstore_dir(filepath)
if not os.path.exists(store_dir):
logging.info("Vector store not found. Will return None.")
return None
from langchain.embeddings.base import Embeddings as LCEmbeddings
try:
class LCEmbeddingWrapper(LCEmbeddings):
def embed_documents(self, texts: List[str]) -> List[List[float]]:
return call_with_retry(
func=embedding_adapter.embed_documents,
max_retries=3,
fallback_return=[],
texts=texts
)
def embed_query(self, query: str) -> List[float]:
res = call_with_retry(
func=embedding_adapter.embed_query,
max_retries=3,
fallback_return=[],
query=query
)
return res
chroma_embedding = LCEmbeddingWrapper()
return Chroma(
persist_directory=store_dir,
embedding_function=chroma_embedding,
client_settings=Settings(anonymized_telemetry=False),
collection_name="novel_collection"
)
except Exception as e:
logging.warning(f"Failed to load vector store: {e}")
traceback.print_exc()
return None
# ============ 文本分段工具 ============
def split_by_length(text: str, max_length: int = 500) -> List[str]:
segments = []
start_idx = 0
while start_idx < len(text):
end_idx = min(start_idx + max_length, len(text))
segment = text[start_idx:end_idx]
segments.append(segment.strip())
start_idx = end_idx
return segments
def split_text_for_vectorstore(chapter_text: str,
max_length: int = 500,
similarity_threshold: float = 0.7) -> List[str]:
"""
对新的章节文本进行分段后,再用于存入向量库。
先句子切分 -> 语义相似度合并 -> 再按 max_length 切分。
"""
if not chapter_text.strip():
return []
nltk.download('punkt', quiet=True)
nltk.download('punkt_tab', quiet=True)
sentences = nltk.sent_tokenize(chapter_text)
if not sentences:
return []
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
embeddings = model.encode(sentences)
merged_paragraphs = []
current_sentences = [sentences[0]]
current_embedding = embeddings[0]
for i in range(1, len(sentences)):
sim = cosine_similarity([current_embedding], [embeddings[i]])[0][0]
if sim >= similarity_threshold:
current_sentences.append(sentences[i])
current_embedding = (current_embedding + embeddings[i]) / 2.0
else:
merged_paragraphs.append(" ".join(current_sentences))
current_sentences = [sentences[i]]
current_embedding = embeddings[i]
if current_sentences:
merged_paragraphs.append(" ".join(current_sentences))
final_segments = []
for para in merged_paragraphs:
if len(para) > max_length:
sub_segments = split_by_length(para, max_length=max_length)
final_segments.extend(sub_segments)
else:
final_segments.append(para)
return final_segments
# ============ 更新向量库 ============
def update_vector_store(
embedding_adapter,
new_chapter: str,
filepath: str
):
"""
将最新章节文本插入到向量库中。
若库不存在则初始化;若初始化/更新失败,则跳过。
"""
splitted_texts = split_text_for_vectorstore(new_chapter)
if not splitted_texts:
logging.warning("No valid text to insert into vector store. Skipping.")
return
store = load_vector_store(embedding_adapter, filepath)
if not store:
logging.info("Vector store does not exist or failed to load. Initializing a new one for new chapter...")
store = init_vector_store(embedding_adapter, splitted_texts, filepath)
if not store:
logging.warning("Init vector store failed, skip embedding.")
else:
logging.info("New vector store created successfully.")
return
# 如果已有store,则直接往里插入
try:
docs = [Document(page_content=str(t)) for t in splitted_texts]
store.add_documents(docs)
logging.info("Vector store updated with the new chapter splitted segments.")
except Exception as e:
logging.warning(f"Failed to update vector store: {e}")
traceback.print_exc()
# ============ 向量检索上下文 ============
def get_relevant_context_from_vector_store(
embedding_adapter,
query: str,
filepath: str,
k: int = 2
) -> str:
"""
从向量库中检索与 query 最相关的 k 条文本,拼接后返回。
如果向量库加载/检索失败,则返回空字符串。
最终只返回最多2000字符的检索片段。
"""
store = load_vector_store(embedding_adapter, filepath)
if not store:
logging.info("No vector store found or load failed. Returning empty context.")
return ""
try:
docs = store.similarity_search(query, k=k)
if not docs:
logging.info(f"No relevant documents found for query '{query}'. Returning empty context.")
return ""
combined = "\n".join([d.page_content for d in docs])
# 限制长度最多2000字符
if len(combined) > 2000:
combined = combined[:2000]
return combined
except Exception as e:
logging.warning(f"Similarity search failed: {e}")
traceback.print_exc()
return ""
# ============ 从目录中获取最近 n 章文本 ============
def get_last_n_chapters_text(chapters_dir: str, current_chapter_num: int, n: int = 3) -> List[str]:
texts = []
start_chap = max(1, current_chapter_num - n)
for c in range(start_chap, current_chapter_num):
chap_file = os.path.join(chapters_dir, f"chapter_{c}.txt")
if os.path.exists(chap_file):
text = read_file(chap_file).strip()
texts.append(text)
else:
texts.append("")
return texts
# ============ 提炼(短期摘要, 下一章关键字) ============
def summarize_recent_chapters(
interface_format: str,
api_key: str,
base_url: str,
model_name: str,
temperature: float,
max_tokens: int,
chapters_text_list: List[str],
timeout: int = 600
) -> Tuple[str, str]:
"""
生成 (short_summary, next_chapter_keywords)
如果解析失败,则返回 (合并文本, "")
"""
combined_text = "\n".join(chapters_text_list).strip()
if not combined_text:
return ("", "")
llm_adapter = create_llm_adapter(
interface_format=interface_format,
base_url=base_url,
model_name=model_name,
api_key=api_key,
temperature=temperature,
max_tokens=max_tokens,
timeout=timeout
)
prompt = summarize_recent_chapters_prompt.format(combined_text=combined_text)
response_text = invoke_with_cleaning(llm_adapter, prompt)
short_summary = ""
next_chapter_keywords = ""
for line in response_text.splitlines():
line = line.strip()
if line.startswith("短期摘要:"):
short_summary = line.replace("短期摘要:", "").strip()
elif line.startswith("下一章关键字:"):
next_chapter_keywords = line.replace("下一章关键字:", "").strip()
if not short_summary and not next_chapter_keywords:
short_summary = response_text
return (short_summary, next_chapter_keywords)
# ============ 持久化:情节架构(partial_architecture.json) ============
def load_partial_architecture_data(filepath: str) -> dict:
"""
从 filepath 下的 partial_architecture.json 读取已有的阶段性数据。
如果文件不存在或无法解析,返回空 dict。
"""
partial_file = os.path.join(filepath, "partial_architecture.json")
if not os.path.exists(partial_file):
return {}
try:
with open(partial_file, "r", encoding="utf-8") as f:
data = json.load(f)
return data
except Exception as e:
logging.warning(f"Failed to load partial_architecture.json: {e}")
return {}
def save_partial_architecture_data(filepath: str, data: dict):
"""
将阶段性数据写入 partial_architecture.json。
"""
partial_file = os.path.join(filepath, "partial_architecture.json")
try:
with open(partial_file, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
except Exception as e:
logging.warning(f"Failed to save partial_architecture.json: {e}")
# ============ 1) 生成总体架构 ============
def Novel_architecture_generate(
interface_format: str,
api_key: str,
base_url: str,
llm_model: str,
topic: str,
genre: str,
number_of_chapters: int,
word_number: int,
filepath: str,
temperature: float = 0.7,
max_tokens: int = 2048,
timeout: int = 600
) -> None:
"""
依次调用:
1. core_seed_prompt
2. character_dynamics_prompt
3. world_building_prompt
4. plot_architecture_prompt
若在中间任何一步报错且重试多次失败,则将已经生成的内容写入 partial_architecture.json 并退出;
下次调用时可从该步骤继续。
最终输出 Novel_architecture.txt
新增:
- 在完成角色动力学设定后,依据该角色体系,使用 create_character_state_prompt 生成初始角色状态表,
并存储到 character_state.txt,后续维护更新。
"""
os.makedirs(filepath, exist_ok=True)
# 加载已有的阶段性数据
partial_data = load_partial_architecture_data(filepath)
llm_adapter = create_llm_adapter(
interface_format=interface_format,
base_url=base_url,
model_name=llm_model,
api_key=api_key,
temperature=temperature,
max_tokens=max_tokens,
timeout=timeout
)
# Step1: 核心种子
if "core_seed_result" not in partial_data:
logging.info("Step1: Generating core_seed_prompt (核心种子) ...")
prompt_core = core_seed_prompt.format(
topic=topic,
genre=genre,
number_of_chapters=number_of_chapters,
word_number=word_number
)
core_seed_result = invoke_with_cleaning(llm_adapter, prompt_core)
if not core_seed_result.strip():
# 多次重试依旧失败,则写入已完成内容后退出
logging.warning("core_seed_prompt generation failed and returned empty.")
save_partial_architecture_data(filepath, partial_data)
return
partial_data["core_seed_result"] = core_seed_result
save_partial_architecture_data(filepath, partial_data)
else:
logging.info("Step1 already done. Skipping...")
# Step2: 角色动力学
if "character_dynamics_result" not in partial_data:
logging.info("Step2: Generating character_dynamics_prompt ...")
prompt_character = character_dynamics_prompt.format(core_seed=partial_data["core_seed_result"].strip())
character_dynamics_result = invoke_with_cleaning(llm_adapter, prompt_character)
if not character_dynamics_result.strip():
logging.warning("character_dynamics_prompt generation failed.")
# 写入目前已有结果,然后退出
save_partial_architecture_data(filepath, partial_data)
return
partial_data["character_dynamics_result"] = character_dynamics_result
save_partial_architecture_data(filepath, partial_data)
else:
logging.info("Step2 already done. Skipping...")
# 在完成角色动力学设定后,生成初始角色状态表
if "character_dynamics_result" in partial_data and "character_state_result" not in partial_data:
logging.info("Generating initial character state from character dynamics ...")
prompt_char_state_init = create_character_state_prompt.format(
character_dynamics=partial_data["character_dynamics_result"].strip()
)
character_state_init = invoke_with_cleaning(llm_adapter, prompt_char_state_init)
if not character_state_init.strip():
logging.warning("create_character_state_prompt generation failed.")
# 写入目前已有结果,然后退出
save_partial_architecture_data(filepath, partial_data)
return
partial_data["character_state_result"] = character_state_init
# 保存到文件
character_state_file = os.path.join(filepath, "character_state.txt")
clear_file_content(character_state_file)
save_string_to_txt(character_state_init, character_state_file)
save_partial_architecture_data(filepath, partial_data)
logging.info("Initial character state created and saved.")
# Step3: 世界观
if "world_building_result" not in partial_data:
logging.info("Step3: Generating world_building_prompt ...")
prompt_world = world_building_prompt.format(core_seed=partial_data["core_seed_result"].strip())
world_building_result = invoke_with_cleaning(llm_adapter, prompt_world)
if not world_building_result.strip():
logging.warning("world_building_prompt generation failed.")
save_partial_architecture_data(filepath, partial_data)
return
partial_data["world_building_result"] = world_building_result
save_partial_architecture_data(filepath, partial_data)
else:
logging.info("Step3 already done. Skipping...")
# Step4: 三幕式情节
if "plot_arch_result" not in partial_data:
logging.info("Step4: Generating plot_architecture_prompt ...")
prompt_plot = plot_architecture_prompt.format(
core_seed=partial_data["core_seed_result"].strip(),
character_dynamics=partial_data["character_dynamics_result"].strip(),
world_building=partial_data["world_building_result"].strip()
)
plot_arch_result = invoke_with_cleaning(llm_adapter, prompt_plot)
if not plot_arch_result.strip():
logging.warning("plot_architecture_prompt generation failed.")
save_partial_architecture_data(filepath, partial_data)
return
partial_data["plot_arch_result"] = plot_arch_result
save_partial_architecture_data(filepath, partial_data)
else:
logging.info("Step4 already done. Skipping...")
# 如果能走到这里,说明全部步骤都完成了
core_seed_result = partial_data["core_seed_result"]
character_dynamics_result = partial_data["character_dynamics_result"]
world_building_result = partial_data["world_building_result"]
plot_arch_result = partial_data["plot_arch_result"]
final_content = (
"#=== 0) 小说设定 ===\n"
f"主题:{topic},类型:{genre},篇幅:约{number_of_chapters}章(每章{word_number}字)\n\n"
"#=== 1) 核心种子 ===\n"
f"{core_seed_result}\n\n"
"#=== 2) 角色动力学 ===\n"
f"{character_dynamics_result}\n\n"
"#=== 3) 世界观 ===\n"
f"{world_building_result}\n\n"
"#=== 4) 三幕式情节架构 ===\n"
f"{plot_arch_result}\n"
)
arch_file = os.path.join(filepath, "Novel_architecture.txt")
clear_file_content(arch_file)
save_string_to_txt(final_content, arch_file)
logging.info("Novel_architecture.txt has been generated successfully.")
# 全部生成完成后,可以考虑删除 partial_architecture.json,或保留做追溯
# 这里选择删除
partial_arch_file = os.path.join(filepath, "partial_architecture.json")
if os.path.exists(partial_arch_file):
os.remove(partial_arch_file)
logging.info("partial_architecture.json removed (all steps completed).")
# ============ 计算分块大小的工具函数 ============
def compute_chunk_size(number_of_chapters: int, max_tokens: int) -> int:
"""
基于“每章约100 tokens”的粗略估算,
再结合当前max_tokens,计算分块大小:
chunk_size = (floor(max_tokens/100/10)*10) - 10
并确保 chunk_size 不会小于1或大于实际章节数。
"""
tokens_per_chapter = 100.0
ratio = max_tokens / tokens_per_chapter
ratio_rounded_to_10 = int(ratio // 10) * 10
chunk_size = ratio_rounded_to_10 - 10
if chunk_size < 1:
chunk_size = 1
if chunk_size > number_of_chapters:
chunk_size = number_of_chapters
return chunk_size
def limit_chapter_blueprint(blueprint_text: str, limit_chapters: int = 100) -> str:
"""
从已有章节目录中只取最近的 limit_chapters 章,以避免 prompt 超长。
"""
pattern = r"(第\s*\d+\s*章.*?)(?=第\s*\d+\s*章|$)"
chapters = re.findall(pattern, blueprint_text, flags=re.DOTALL)
if not chapters:
return blueprint_text
if len(chapters) <= limit_chapters:
return blueprint_text
selected = chapters[-limit_chapters:]
return "\n\n".join(selected).strip()
# ============ 2) 生成章节蓝图(新增分块逻辑 + 断点续跑) ============
def Chapter_blueprint_generate(
interface_format: str,
api_key: str,
base_url: str,
llm_model: str,
filepath: str,
number_of_chapters: int,
temperature: float = 0.7,
max_tokens: int = 4096,
timeout: int = 600
) -> None:
"""
若 Novel_directory.txt 已存在且内容非空,则表示可能是之前的部分生成结果;
解析其中已有的章节数,从下一个章节继续分块生成;
对于已有章节目录,传入时仅保留最近100章目录,避免prompt过长。
否则:
- 若章节数 <= chunk_size,直接一次性生成
- 若章节数 > chunk_size,进行分块生成
生成完成后输出至 Novel_directory.txt。
"""
arch_file = os.path.join(filepath, "Novel_architecture.txt")
if not os.path.exists(arch_file):
logging.warning("Novel_architecture.txt not found. Please generate architecture first.")
return
architecture_text = read_file(arch_file).strip()
if not architecture_text:
logging.warning("Novel_architecture.txt is empty.")
return
llm_adapter = create_llm_adapter(
interface_format=interface_format,
base_url=base_url,
model_name=llm_model,
api_key=api_key,
temperature=temperature,
max_tokens=max_tokens,
timeout=timeout
)
filename_dir = os.path.join(filepath, "Novel_directory.txt")
if not os.path.exists(filename_dir):
# 如果文件不存在,就先建一个空文件
open(filename_dir, "w", encoding="utf-8").close()
existing_blueprint = read_file(filename_dir).strip()
chunk_size = compute_chunk_size(number_of_chapters, max_tokens)
logging.info(f"Number of chapters = {number_of_chapters}, computed chunk_size = {chunk_size}.")
# 如果已经有部分章节蓝图生成了,则进行断点续跑
if existing_blueprint:
logging.info("Detected existing blueprint content. Will resume chunked generation from that point.")
pattern = r"第\s*(\d+)\s*章"
existing_chapter_numbers = re.findall(pattern, existing_blueprint)
existing_chapter_numbers = [int(x) for x in existing_chapter_numbers if x.isdigit()]
if existing_chapter_numbers:
max_existing_chap = max(existing_chapter_numbers)
else:
max_existing_chap = 0
logging.info(f"Existing blueprint indicates up to chapter {max_existing_chap} has been generated.")
final_blueprint = existing_blueprint
current_start = max_existing_chap + 1
while current_start <= number_of_chapters:
current_end = min(current_start + chunk_size - 1, number_of_chapters)
limited_blueprint = limit_chapter_blueprint(final_blueprint, 100)
chunk_prompt = chunked_chapter_blueprint_prompt.format(
novel_architecture=architecture_text,
chapter_list=limited_blueprint, # 只保留最近100章
number_of_chapters=number_of_chapters,
n=current_start,
m=current_end
)
logging.info(f"Generating chapters [{current_start}..{current_end}] in a chunk...")
chunk_result = invoke_with_cleaning(llm_adapter, chunk_prompt)
if not chunk_result.strip():
logging.warning(f"Chunk generation for chapters [{current_start}..{current_end}] is empty.")
# 写入当前已经有的 final_blueprint,并结束
clear_file_content(filename_dir)
save_string_to_txt(final_blueprint.strip(), filename_dir)
return
final_blueprint += "\n\n" + chunk_result.strip()
# 实时写入
clear_file_content(filename_dir)
save_string_to_txt(final_blueprint.strip(), filename_dir)
current_start = current_end + 1
logging.info("All chapters blueprint have been generated (resumed chunked).")
return
# 如果 Novel_directory.txt 为空,则分情况:
# 1) 如果 chunk_size >= number_of_chapters,可以一次性生成
if chunk_size >= number_of_chapters:
prompt = chapter_blueprint_prompt.format(
novel_architecture=architecture_text,
number_of_chapters=number_of_chapters
)
blueprint_text = invoke_with_cleaning(llm_adapter, prompt)
if not blueprint_text.strip():
logging.warning("Chapter blueprint generation result is empty.")
return
clear_file_content(filename_dir)
save_string_to_txt(blueprint_text, filename_dir)
logging.info("Novel_directory.txt (chapter blueprint) has been generated successfully (single-shot).")
return
# 2) 如果 chunk_size < number_of_chapters,则进行分块生成
logging.info("Will generate chapter blueprint in chunked mode from scratch.")
final_blueprint = ""
current_start = 1
while current_start <= number_of_chapters:
current_end = min(current_start + chunk_size - 1, number_of_chapters)
limited_blueprint = limit_chapter_blueprint(final_blueprint, 100)
chunk_prompt = chunked_chapter_blueprint_prompt.format(
novel_architecture=architecture_text,
chapter_list=limited_blueprint, # 只保留最近100章
number_of_chapters=number_of_chapters,
n=current_start,
m=current_end
)
logging.info(f"Generating chapters [{current_start}..{current_end}] in a chunk...")
chunk_result = invoke_with_cleaning(llm_adapter, chunk_prompt)
if not chunk_result.strip():
logging.warning(f"Chunk generation for chapters [{current_start}..{current_end}] is empty.")
# 写入已经生成的 final_blueprint
clear_file_content(filename_dir)
save_string_to_txt(final_blueprint.strip(), filename_dir)
return
if final_blueprint.strip():
final_blueprint += "\n\n" + chunk_result.strip()
else:
final_blueprint = chunk_result.strip()
# 实时写入,以免中途崩溃造成丢失
clear_file_content(filename_dir)
save_string_to_txt(final_blueprint.strip(), filename_dir)
current_start = current_end + 1
logging.info("Novel_directory.txt (chapter blueprint) has been generated successfully (chunked).")
# ============ 3) 生成章节草稿 ============
def generate_chapter_draft(
api_key: str,
base_url: str,
model_name: str,
filepath: str,
novel_number: int,
word_number: int,
temperature: float,
user_guidance: str,
characters_involved: str,
key_items: str,
scene_location: str,
time_constraint: str,
embedding_api_key: str,
embedding_url: str,
embedding_interface_format: str,
embedding_model_name: str,
embedding_retrieval_k: int = 2,
interface_format: str = "openai",
max_tokens: int = 2048,
timeout: int = 600
) -> str:
"""
根据 novel_number 判断是否为第一章。
- 若是第一章,则使用 first_chapter_draft_prompt
- 否则使用 next_chapter_draft_prompt
最终将生成文本存入 chapters/chapter_{novel_number}.txt。
"""
arch_file = os.path.join(filepath, "Novel_architecture.txt")
novel_architecture_text = read_file(arch_file)
directory_file = os.path.join(filepath, "Novel_directory.txt")
blueprint_text = read_file(directory_file)
global_summary_file = os.path.join(filepath, "global_summary.txt")
global_summary_text = read_file(global_summary_file)
character_state_file = os.path.join(filepath, "character_state.txt")
character_state_text = read_file(character_state_file)
# 获取本章在目录中的信息
chapter_info = get_chapter_info_from_blueprint(blueprint_text, novel_number)
chapter_title = chapter_info["chapter_title"]
chapter_role = chapter_info["chapter_role"]
chapter_purpose = chapter_info["chapter_purpose"]
suspense_level = chapter_info["suspense_level"]
foreshadowing = chapter_info["foreshadowing"]
plot_twist_level = chapter_info["plot_twist_level"]
chapter_summary = chapter_info["chapter_summary"]
# 准备章节目录文件夹
chapters_dir = os.path.join(filepath, "chapters")
os.makedirs(chapters_dir, exist_ok=True)
# 判断是否为第一章
if novel_number == 1:
prompt_text = first_chapter_draft_prompt.format(
novel_number=novel_number,
word_number=word_number,
chapter_title=chapter_title,
chapter_role=chapter_role,
chapter_purpose=chapter_purpose,
suspense_level=suspense_level,
foreshadowing=foreshadowing,
plot_twist_level=plot_twist_level,
chapter_summary=chapter_summary,
characters_involved=characters_involved,
key_items=key_items,
scene_location=scene_location,
time_constraint=time_constraint,
user_guidance=user_guidance,
novel_setting=novel_architecture_text
)
else:
# 若不是第一章,则获取最近几章文本,并做摘要与检索
recent_3_texts = get_last_n_chapters_text(chapters_dir, novel_number, n=3)
short_summary, next_chapter_keywords = summarize_recent_chapters(
interface_format=interface_format,
api_key=api_key,
base_url=base_url,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
chapters_text_list=recent_3_texts,
timeout=timeout
)
# 从最近章节中获取最后一段作为前章结尾
previous_chapter_excerpt = ""
for text_block in reversed(recent_3_texts):
if text_block.strip():
# 取后1500字符左右
if len(text_block) > 1500:
previous_chapter_excerpt = text_block[-1500:]
else:
previous_chapter_excerpt = text_block
break
# 从向量库检索上下文
embedding_adapter = create_embedding_adapter(
embedding_interface_format,
embedding_api_key,
embedding_url,
embedding_model_name
)
retrieval_query = short_summary + " " + next_chapter_keywords
relevant_context = get_relevant_context_from_vector_store(
embedding_adapter=embedding_adapter,
query=retrieval_query,
filepath=filepath,
k=embedding_retrieval_k
)
if not relevant_context.strip():
relevant_context = "(无检索到的上下文)"
prompt_text = next_chapter_draft_prompt.format(
novel_number=novel_number,
word_number=word_number,
chapter_title=chapter_title,
chapter_role=chapter_role,
chapter_purpose=chapter_purpose,
suspense_level=suspense_level,
foreshadowing=foreshadowing,
plot_twist_level=plot_twist_level,
chapter_summary=chapter_summary,
characters_involved=characters_involved,
key_items=key_items,
scene_location=scene_location,
time_constraint=time_constraint,
user_guidance=user_guidance,
novel_setting=novel_architecture_text,
global_summary=global_summary_text,
character_state=character_state_text,
context_excerpt=relevant_context,
previous_chapter_excerpt=previous_chapter_excerpt
)
llm_adapter = create_llm_adapter(
interface_format=interface_format,
base_url=base_url,
model_name=model_name,
api_key=api_key,
temperature=temperature,
max_tokens=max_tokens,
timeout=timeout
)
chapter_content = invoke_with_cleaning(llm_adapter, prompt_text)
if not chapter_content.strip():
logging.warning("Generated chapter draft is empty.")
chapter_file = os.path.join(chapters_dir, f"chapter_{novel_number}.txt")
clear_file_content(chapter_file)
save_string_to_txt(chapter_content, chapter_file)
logging.info(f"[Draft] Chapter {novel_number} generated as a draft.")
return chapter_content
# ============ 4) 定稿章节 ============
def finalize_chapter(
novel_number: int,
word_number: int,
api_key: str,
base_url: str,
model_name: str,
temperature: float,
filepath: str,
embedding_api_key: str,
embedding_url: str,
embedding_interface_format: str,
embedding_model_name: str,
interface_format: str,
max_tokens: int,
timeout: int = 600
):
"""
对指定章节做最终处理:更新全局摘要、更新角色状态、插入向量库等。
默认无需再做扩写操作,若有需要可在外部调用 enrich_chapter_text 处理后再定稿。
"""
chapters_dir = os.path.join(filepath, "chapters")
chapter_file = os.path.join(chapters_dir, f"chapter_{novel_number}.txt")