-
Notifications
You must be signed in to change notification settings - Fork 7
/
novelist_agent_limit.py
167 lines (137 loc) · 5.54 KB
/
novelist_agent_limit.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
import autogen
import requests
import json
import os
# OpenRouter API configuration
OPENROUTER_API_KEY = os.environ['OPENROUTER_API_KEY']
class OpenRouterLLM:
def __init__(self, model="openai/gpt-4o-2024-08-06"):
self.model = model
def create_completion(self, messages):
response = requests.post(
url="https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {OPENROUTER_API_KEY}"
},
data=json.dumps({
"model": self.model,
"messages": messages,
"max_tokens": 4000,
"temperature": 0.7,
})
)
return response.json()
llm = OpenRouterLLM()
story_planner_config = {
"name": "StoryPlanner",
"system_message": """You are a story planner specialized in game-related narratives.
Your responsibilities:
- Create detailed chapter outlines (at least 15 chapters)
- Define key story beats and progression
- Ensure each chapter contributes at least 700 words to reach the 10,000+ word goal
- Coordinate with other agents to maintain story consistency
- Track word count and story progress"""
}
character_developer_config = {
"name": "CharacterDeveloper",
"system_message": """You are a character development specialist.
Your responsibilities:
- Create detailed character profiles with backstories
- Define complex character relationships and dynamics
- Ensure character development spans the entire novel
- Provide character details that support 10,000+ words of content"""
}
narrative_writer_config = {
"name": "NarrativeWriter",
"system_message": """You are a narrative writer focusing on game-related stories.
Your responsibilities:
- Write detailed, engaging prose and dialogue
- Maintain consistent pacing and detailed descriptions
- Ensure each chapter is sufficiently detailed (700+ words)
- Track word count to meet the 10,000+ word goal
- Use rich descriptions and well-developed scenes"""
}
class NovelProgress:
def __init__(self):
self.total_words = 0
self.chapters = []
self.target_words = 10000
def add_content(self, content):
words = len(content.split())
self.total_words += words
self.chapters.append({
'content': content,
'word_count': words
})
def is_complete(self):
return self.total_words >= self.target_words
def get_progress(self):
return f"Current progress: {self.total_words}/{self.target_words} words ({len(self.chapters)} chapters)"
novel_progress = NovelProgress()
def create_messages(system_message, user_message):
return [
{"role": "system", "content": system_message},
{"role": "user", "content": f"{user_message}\n\nCurrent progress: {novel_progress.get_progress()}"}
]
class CustomAssistantAgent(autogen.AssistantAgent):
def generate_reply(self, sender=None, messages=None):
if messages is None:
messages = self._oai_messages
if not messages:
return None
system_message = self.system_message
last_message = messages[-1]
if isinstance(last_message, dict):
last_message_content = last_message.get('content', '')
else:
last_message_content = str(last_message)
api_messages = create_messages(system_message, last_message_content)
response = llm.create_completion(api_messages)
try:
reply = response['choices'][0]['message']['content']
if self.name == "narrative_writer":
novel_progress.add_content(reply)
return reply
except KeyError:
return "I apologize, but I encountered an error processing your request."
story_planner = CustomAssistantAgent(
name="story_planner",
system_message=story_planner_config["system_message"]
)
character_developer = CustomAssistantAgent(
name="character_developer",
system_message=character_developer_config["system_message"]
)
narrative_writer = CustomAssistantAgent(
name="narrative_writer",
system_message=narrative_writer_config["system_message"]
)
user_proxy = autogen.UserProxyAgent(
name="user_proxy",
human_input_mode="TERMINATE",
max_consecutive_auto_reply=10
)
groupchat = autogen.GroupChat(
agents=[user_proxy, story_planner, character_developer, narrative_writer],
messages=[],
max_round=50
)
manager = autogen.GroupChatManager(groupchat=groupchat)
initial_message = """Let's write a novel about a professional esports player's journey.
Requirements:
1. The novel must be at least 10,000 words
2. Include detailed character development
3. Each chapter should be at least 700 words
4. Include rich descriptions and well-developed scenes
5. Cover the character's complete journey from amateur to professional
Please start by creating a detailed outline and character profile."""
# Continue generating content until word count is met
while not novel_progress.is_complete():
if len(novel_progress.chapters) == 0:
user_proxy.initiate_chat(manager, message=initial_message)
else:
next_chapter_prompt = f"""Continue writing the next chapter.
Current progress: {novel_progress.get_progress()}
Make this chapter at least 700 words with rich details and character development."""
user_proxy.initiate_chat(manager, message=next_chapter_prompt)
print(f"\nFinal novel statistics:\n{novel_progress.get_progress()}")