-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy path2_write_novel.py
More file actions
36 lines (28 loc) · 1.44 KB
/
2_write_novel.py
File metadata and controls
36 lines (28 loc) · 1.44 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
# 工作流: 写小说
from GeneralAgent import Agent
from GeneralAgent import skills
from dotenv import load_dotenv
load_dotenv()
# 步骤0: 定义Agent
agent = Agent('你是一个小说家')
# 步骤1: 从用户处获取小说的名称和主题
# topic = skills.input('请输入小说的名称和主题: ')
topic = '小白兔吃糖不刷牙的故事'
# 步骤2: 小说的概要
summary = agent.run(f'小说的名称和主题是: {topic},扩展和完善一下小说概要。要求具备文艺性、教育性、娱乐性。')
# 步骤3: 小说的章节名称和概要列表
chapters = agent.run('输出小说的章节名称和每个章节的概要,返回列表 [(chapter_title, chapter_summary), ....]', return_type=list)
# 步骤4: 生成小说每一章节的详细内容
contents = []
for index, (chapter_title, chapter_summary) in enumerate(chapters):
content = agent.run(f'对于章节: {chapter_title}\n{chapter_summary}. \n输出章节的详细内容,注意只返回内容,不要标题。')
content = '\n'.join([x.strip() for x in content.split('\n')])
contents.append(content)
# 步骤5: 将小说格式化写入文件
with open('novel.md', 'w') as f:
for index in range(len(chapters)):
f.write(f'### {chapters[index][0]}\n')
f.write(f'{contents[index]}\n\n')
# 步骤6(可选): 将markdown文件转换为pdf文件
# 步骤7: 输出小说文件给用户
skills.output('你的小说已经生成[novel.md](novel.md)\n')