-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path37_workflow_write_novel_demo.py
162 lines (142 loc) · 6.31 KB
/
37_workflow_write_novel_demo.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
# -*- coding: utf-8 -*-
"""
@author:XuMing(xuming624@qq.com)
@description:
"""
import sys
from textwrap import dedent
from loguru import logger
sys.path.append('..')
from agentica import Agent, OpenAIChat, AzureOpenAIChat
from agentica import Workflow, RunResponse, RunEvent, SqlWorkflowStorage, pprint_run_response
from agentica.tools.search_serper_tool import SearchSerperTool
class WriteNovelWorkflow(Workflow):
description: str = "Generate a comprehensive novel on a given topic."
outlines: Agent = Agent(
model=AzureOpenAIChat(id="gpt-4o"),
instructions=[
dedent("""
## context
You need to generate a novel named 'xxx'.
Be creative and thorough while generating the outline. The output should be formatted as specified below.
## format example
[CONTENT]
{
"name": "Novel Name",
"user_group": "Target User Group",
"outlines": [
"Chapter 1: ...",
"Chapter 2: ...",
"Chapter 3: ..."
],
"background": "...",
"character_names": [
"Character1",
"Character2",
"Character3"
],
"conflict": "...",
"plot": "...",
"ending": "..."
}
[/CONTENT]
## nodes: "<node>: <type> # <instruction>"
- name: <class 'str'> # The name of the novel.
- user_group: <class 'str'> # The target audience for the novel.
- outlines: typing.List[str] # The outlines of the novel. No more than 10 chapters.
- background: <class 'str'> # The background setting of the novel.
- character_names: typing.List[str] # List of main characters in the novel.
- conflict: <class 'str'> # The primary conflict in the novel.
- plot: <class 'str'> # Summary of the novel's plot.
- ending: <class 'str'> # The novel's ending.
## constraint
Language: The output should be in the language of this prompt.
Format: Ensure the output is wrapped inside [CONTENT][/CONTENT] tags like the format example above.
## action
Generate the outlines for the novel and ensure it follows the formatting guidelines.
"""),
],
)
reflection: Agent = Agent(
model=AzureOpenAIChat(id="gpt-4o"),
tools=[SearchSerperTool()],
instructions=[
dedent("""
## context
Review the previously generated novel outlines and use Google search results to reflect and improve upon them.
## format example
[CONTENT]
{
"name": "Novel Name",
"user_group": "Target User Group",
"outlines": [
"Chapter 1: ...",
"Chapter 2: ...",
"Chapter 3: ..."
],
"background": "...",
"character_names": [
"Character1",
"Character2",
"Character3"
],
"conflict": "...",
"plot": "...",
"ending": "..."
}
[/CONTENT]
## nodes: "<node>: <type> # <instruction>"
- name: <class 'str'> # The name of the novel.
- user_group: <class 'str'> # The target audience for the novel.
- outlines: typing.List[str] # The outlines of the novel. No more than 10 chapters.
- background: <class 'str'> # The background setting of the novel.
- character_names: typing.List[str] # List of main characters in the novel.
- conflict: <class 'str'> # The primary conflict in the novel.
- plot: <class 'str'> # Summary of the novel's plot.
- ending: <class 'str'> # The novel's ending.
## constraint
Language: The output should be in the language of this prompt.
Format: Ensure the output is wrapped inside [CONTENT][/CONTENT] tags like the format example above.
## action
搜索时,可以适当调整搜索关键词,以获取更多相关信息
Conduct searches using the provided tools to gather information and insights. Reflect on the gathered results and improve the original outlines accordingly.
"""),
],
)
writer: Agent = Agent(
model=AzureOpenAIChat(id="gpt-4o"),
instructions=[
dedent("""
## context
You need to write detailed content for the novel, based on the generated outlines.
## output format example
输出markdown格式的小说,章节内容结构清晰。不用给```markdown```标记,直接输出md格式的内容。用中文写。
## action
Write the detailed novel chapters and ensure they follow the formatting guidelines.
"""),
],
)
def run(self, topic: str):
logger.info(f"Generating a novel on: {topic}")
outlines_response = self.outlines.run(topic)
if outlines_response is None or not outlines_response.content:
yield RunResponse(run_id=self.run_id, content="Sorry, could not generate the novel outlines.")
return
logger.info(f"Reflecting and improving the novel outlines on: {topic}")
reflection_response = self.reflection.run(outlines_response.content)
if reflection_response is None or not reflection_response.content:
yield RunResponse(run_id=self.run_id, content="Sorry, could not reflect and improve the novel outlines.")
return
logger.info(f"Writing the novel content on: {topic}")
yield from self.writer.run(reflection_response.content, stream=True)
if __name__ == "__main__":
topic = "月球求生"
write_novel_workflow = WriteNovelWorkflow(
session_id=f"write-novel-on-{topic}",
storage=SqlWorkflowStorage(
table_name="write_novel_workflows",
db_file="outputs/novel_workflows.db",
),
)
novel_stream = write_novel_workflow.run(topic=topic)
pprint_run_response(novel_stream, markdown=True)