cryogenic22 commited on
Commit
5df3314
·
verified ·
1 Parent(s): 87ceabc

Update multi_agent_book_workflow.py

Browse files
Files changed (1) hide show
  1. multi_agent_book_workflow.py +58 -55
multi_agent_book_workflow.py CHANGED
@@ -20,26 +20,27 @@ from langchain_openai import ChatOpenAI
20
  from langchain_anthropic import ChatAnthropic
21
  from langchain.memory import ConversationBufferMemory
22
 
 
23
  class BookWritingOrchestrator:
24
  def __init__(self, api_key=None):
25
  """
26
  Initialize the book writing orchestrator with multi-agent setup
27
-
28
  Args:
29
  api_key (str, optional): API key for models if not using environment variables
30
  """
31
  # Generate a unique project identifier
32
  self.project_id = str(uuid.uuid4())
33
-
34
  # Comprehensive API key management
35
  self._setup_api_keys(api_key)
36
-
37
  # Initialize context management
38
  self._initialize_context_store()
39
-
40
  # Setup AI models
41
  self._setup_ai_models()
42
-
43
  # Initialize agents with robust error handling
44
  try:
45
  self.setup_agents()
@@ -55,7 +56,7 @@ class BookWritingOrchestrator:
55
  self.openai_api_key = api_key or os.getenv('OPENAI_API_KEY')
56
  if not self.openai_api_key:
57
  st.warning("OpenAI API key is missing. Some features may be limited.")
58
-
59
  # Anthropic API Key
60
  self.anthropic_api_key = api_key or os.getenv('ANTHROPIC_API_KEY')
61
  if not self.anthropic_api_key:
@@ -68,15 +69,15 @@ class BookWritingOrchestrator:
68
  try:
69
  # Initialize embeddings
70
  self.embeddings = OpenAIEmbeddings(api_key=self.openai_api_key)
71
-
72
  # Create initial empty FAISS vector store
73
  self.context_store = FAISS.from_documents(
74
  documents=[
75
  Document(
76
- page_content="Initial project context",
77
  metadata={"project_id": self.project_id}
78
  )
79
- ],
80
  embedding=self.embeddings
81
  )
82
  except Exception as e:
@@ -84,9 +85,9 @@ class BookWritingOrchestrator:
84
  raise
85
 
86
  def _setup_ai_models(self):
87
- """
88
- Configure AI models with robust error handling
89
- """
90
  try:
91
  # LLM Configurations
92
  self.openai_llm = ChatOpenAI(
@@ -94,14 +95,14 @@ class BookWritingOrchestrator:
94
  temperature=0.7,
95
  api_key=self.openai_api_key
96
  )
97
-
98
  # Updated Anthropic model configuration
99
  self.anthropic_llm = ChatAnthropic(
100
  model_name="claude-3-sonnet-20240229", # Updated model name
101
  temperature=0.7,
102
  anthropic_api_key=self.anthropic_api_key
103
  )
104
-
105
  # Global memory for cross-chapter context
106
  self.global_memory = ConversationBufferMemory(
107
  memory_key="book_writing_context",
@@ -114,10 +115,10 @@ class BookWritingOrchestrator:
114
  def _fallback_book_concept(self, initial_prompt: str) -> Dict[str, Any]:
115
  """
116
  Provide a fallback response when book concept generation fails
117
-
118
  Args:
119
  initial_prompt (str): Original user prompt
120
-
121
  Returns:
122
  Dict: Basic book concept structure
123
  """
@@ -134,19 +135,19 @@ class BookWritingOrchestrator:
134
  def _fallback_chapter_content(self, book_concept: Dict[str, Any], chapter_number: int) -> str:
135
  """
136
  Provide fallback chapter content when generation fails
137
-
138
  Args:
139
  book_concept (Dict): Book concept data
140
  chapter_number (int): Chapter number
141
-
142
  Returns:
143
  str: Basic chapter content
144
  """
145
  return f"""
146
  Chapter {chapter_number}
147
-
148
  [Placeholder content for {book_concept.get('title', 'Untitled')}]
149
-
150
  This is auto-generated fallback content due to an error in chapter generation.
151
  Please try regenerating this chapter or contact support if the issue persists.
152
  """
@@ -165,7 +166,7 @@ class BookWritingOrchestrator:
165
  llm=self.openai_llm,
166
  memory=True
167
  )
168
-
169
  # Research Agent
170
  self.research_agent = Agent(
171
  role='Literary Research Specialist',
@@ -175,7 +176,7 @@ class BookWritingOrchestrator:
175
  llm=self.anthropic_llm,
176
  memory=True
177
  )
178
-
179
  # Content Generation Agent
180
  self.writing_agent = Agent(
181
  role='Master Storyteller',
@@ -185,7 +186,7 @@ class BookWritingOrchestrator:
185
  llm=self.openai_llm,
186
  memory=True
187
  )
188
-
189
  # Editing Agent
190
  self.editing_agent = Agent(
191
  role='Narrative Refinement Specialist',
@@ -199,10 +200,10 @@ class BookWritingOrchestrator:
199
  def generate_book_concept(self, initial_prompt: str) -> Dict[str, Any]:
200
  """
201
  Generate a comprehensive book concept using multi-agent collaboration
202
-
203
  Args:
204
  initial_prompt (str): User's initial book idea
205
-
206
  Returns:
207
  Dict containing book concept details
208
  """
@@ -211,7 +212,7 @@ class BookWritingOrchestrator:
211
  description=f"""
212
  Develop a comprehensive book concept based on this initial idea:
213
  {initial_prompt}
214
-
215
  Provide detailed outputs including:
216
  1. Unique Book Title
217
  2. Genre and Subgenre
@@ -223,62 +224,62 @@ class BookWritingOrchestrator:
223
  agent=self.concept_agent,
224
  expected_output="A detailed JSON-like structure of the book concept"
225
  )
226
-
227
  # Research Validation Task
228
  research_task = Task(
229
  description="Validate and enrich the book concept with additional research insights",
230
  agent=self.research_agent,
231
  expected_output="Research-backed annotations and potential depth areas"
232
  )
233
-
234
  # Create Crew for Collaborative Processing
235
  book_concept_crew = Crew(
236
  agents=[self.concept_agent, self.research_agent],
237
  tasks=[concept_task, research_task],
238
  verbose=True # Changed from 2 to True
239
  )
240
-
241
  # Execute Collaborative Workflow
242
  try:
243
  result = book_concept_crew.kickoff()
244
-
245
  # Store Context in FAISS Vector Store
246
  self._store_context('book_concept', result)
247
-
248
  # Parse and return concept
249
  return self._parse_concept(result)
250
-
251
  except Exception as e:
252
  st.error(f"Book concept generation failed: {e}")
253
  return self._fallback_book_concept(initial_prompt)
254
 
255
  def generate_chapter_content(
256
- self,
257
- book_concept: Dict[str, Any],
258
  chapter_number: int
259
  ) -> str:
260
  """
261
  Generate content for a specific chapter using multi-agent workflow
262
-
263
  Args:
264
  book_concept (Dict): Comprehensive book concept
265
  chapter_number (int): Chapter to generate
266
-
267
  Returns:
268
  str: Generated chapter content
269
  """
270
  # Retrieve Previous Context
271
  previous_context = self._retrieve_context(chapter_number)
272
-
273
  # Content Generation Task
274
  writing_task = Task(
275
  description=f"""
276
  Write Chapter {chapter_number} for the book
277
-
278
  Book Concept: {book_concept}
279
-
280
  Previous Context: {previous_context}
281
-
282
  Generate a draft that:
283
  1. Maintains narrative continuity
284
  2. Advances the story
@@ -288,43 +289,43 @@ class BookWritingOrchestrator:
288
  agent=self.writing_agent,
289
  expected_output="A complete chapter draft with narrative coherence"
290
  )
291
-
292
  # Editing and Refinement Task
293
  editing_task = Task(
294
  description="Review and refine the generated chapter draft",
295
  agent=self.editing_agent,
296
  expected_output="Polished chapter content with suggested improvements"
297
  )
298
-
299
  # Create Crew for Chapter Generation
300
  chapter_crew = Crew(
301
  agents=[self.writing_agent, self.editing_agent],
302
  tasks=[writing_task, editing_task],
303
  verbose=True # Changed from 2 to True
304
  )
305
-
306
  try:
307
  # Execute Collaborative Workflow
308
  chapter_content = chapter_crew.kickoff()
309
-
310
  # Store Chapter Context in FAISS
311
  self._store_context(f'chapter_{chapter_number}', chapter_content)
312
-
313
  # Update global memory with chapter context
314
  self.global_memory.chat_memory.add_user_message(chapter_content)
315
-
316
  return chapter_content
317
-
318
  except Exception as e:
319
  st.error(f"Chapter generation failed: {e}")
320
  return self._fallback_chapter_content(book_concept, chapter_number)
321
 
322
  # Rest of the implementation remains the same as in the previous version
323
-
324
  def _store_context(self, context_key: str, content: str):
325
  """
326
  Store context in FAISS vector store
327
-
328
  Args:
329
  context_key (str): Unique identifier for the context
330
  content (str): Content to store
@@ -332,22 +333,23 @@ class BookWritingOrchestrator:
332
  try:
333
  # Create a document with metadata
334
  document = Document(
335
- page_content=content,
336
  metadata={
337
  "project_id": self.project_id,
338
  "context_key": context_key
339
  }
340
  )
341
-
342
  # Add document to FAISS vector store
343
  new_store = FAISS.from_documents([document], self.embeddings)
344
  self.context_store.merge_from(new_store)
345
-
346
  except Exception as e:
347
  st.error(f"Context storage failed: {e}")
348
 
349
  # Remaining methods stay the same as in the previous implementation
350
 
 
351
  def main():
352
  """
353
  Demonstration of BookWritingOrchestrator
@@ -355,18 +357,19 @@ def main():
355
  try:
356
  # Create orchestrator
357
  orchestrator = BookWritingOrchestrator()
358
-
359
  # Generate book concept
360
  initial_prompt = "A science fiction story about space exploration"
361
  book_concept = orchestrator.generate_book_concept(initial_prompt)
362
  print("Book Concept:", book_concept)
363
-
364
  # Generate first chapter
365
  first_chapter = orchestrator.generate_chapter_content(book_concept, 1)
366
  print("\nFirst Chapter:\n", first_chapter)
367
-
368
  except Exception as e:
369
  print(f"An error occurred: {e}")
370
 
 
371
  if __name__ == "__main__":
372
  main()
 
20
  from langchain_anthropic import ChatAnthropic
21
  from langchain.memory import ConversationBufferMemory
22
 
23
+
24
  class BookWritingOrchestrator:
25
  def __init__(self, api_key=None):
26
  """
27
  Initialize the book writing orchestrator with multi-agent setup
28
+
29
  Args:
30
  api_key (str, optional): API key for models if not using environment variables
31
  """
32
  # Generate a unique project identifier
33
  self.project_id = str(uuid.uuid4())
34
+
35
  # Comprehensive API key management
36
  self._setup_api_keys(api_key)
37
+
38
  # Initialize context management
39
  self._initialize_context_store()
40
+
41
  # Setup AI models
42
  self._setup_ai_models()
43
+
44
  # Initialize agents with robust error handling
45
  try:
46
  self.setup_agents()
 
56
  self.openai_api_key = api_key or os.getenv('OPENAI_API_KEY')
57
  if not self.openai_api_key:
58
  st.warning("OpenAI API key is missing. Some features may be limited.")
59
+
60
  # Anthropic API Key
61
  self.anthropic_api_key = api_key or os.getenv('ANTHROPIC_API_KEY')
62
  if not self.anthropic_api_key:
 
69
  try:
70
  # Initialize embeddings
71
  self.embeddings = OpenAIEmbeddings(api_key=self.openai_api_key)
72
+
73
  # Create initial empty FAISS vector store
74
  self.context_store = FAISS.from_documents(
75
  documents=[
76
  Document(
77
+ page_content="Initial project context",
78
  metadata={"project_id": self.project_id}
79
  )
80
+ ],
81
  embedding=self.embeddings
82
  )
83
  except Exception as e:
 
85
  raise
86
 
87
  def _setup_ai_models(self):
88
+ """
89
+ Configure AI models with robust error handling
90
+ """
91
  try:
92
  # LLM Configurations
93
  self.openai_llm = ChatOpenAI(
 
95
  temperature=0.7,
96
  api_key=self.openai_api_key
97
  )
98
+
99
  # Updated Anthropic model configuration
100
  self.anthropic_llm = ChatAnthropic(
101
  model_name="claude-3-sonnet-20240229", # Updated model name
102
  temperature=0.7,
103
  anthropic_api_key=self.anthropic_api_key
104
  )
105
+
106
  # Global memory for cross-chapter context
107
  self.global_memory = ConversationBufferMemory(
108
  memory_key="book_writing_context",
 
115
  def _fallback_book_concept(self, initial_prompt: str) -> Dict[str, Any]:
116
  """
117
  Provide a fallback response when book concept generation fails
118
+
119
  Args:
120
  initial_prompt (str): Original user prompt
121
+
122
  Returns:
123
  Dict: Basic book concept structure
124
  """
 
135
  def _fallback_chapter_content(self, book_concept: Dict[str, Any], chapter_number: int) -> str:
136
  """
137
  Provide fallback chapter content when generation fails
138
+
139
  Args:
140
  book_concept (Dict): Book concept data
141
  chapter_number (int): Chapter number
142
+
143
  Returns:
144
  str: Basic chapter content
145
  """
146
  return f"""
147
  Chapter {chapter_number}
148
+
149
  [Placeholder content for {book_concept.get('title', 'Untitled')}]
150
+
151
  This is auto-generated fallback content due to an error in chapter generation.
152
  Please try regenerating this chapter or contact support if the issue persists.
153
  """
 
166
  llm=self.openai_llm,
167
  memory=True
168
  )
169
+
170
  # Research Agent
171
  self.research_agent = Agent(
172
  role='Literary Research Specialist',
 
176
  llm=self.anthropic_llm,
177
  memory=True
178
  )
179
+
180
  # Content Generation Agent
181
  self.writing_agent = Agent(
182
  role='Master Storyteller',
 
186
  llm=self.openai_llm,
187
  memory=True
188
  )
189
+
190
  # Editing Agent
191
  self.editing_agent = Agent(
192
  role='Narrative Refinement Specialist',
 
200
  def generate_book_concept(self, initial_prompt: str) -> Dict[str, Any]:
201
  """
202
  Generate a comprehensive book concept using multi-agent collaboration
203
+
204
  Args:
205
  initial_prompt (str): User's initial book idea
206
+
207
  Returns:
208
  Dict containing book concept details
209
  """
 
212
  description=f"""
213
  Develop a comprehensive book concept based on this initial idea:
214
  {initial_prompt}
215
+
216
  Provide detailed outputs including:
217
  1. Unique Book Title
218
  2. Genre and Subgenre
 
224
  agent=self.concept_agent,
225
  expected_output="A detailed JSON-like structure of the book concept"
226
  )
227
+
228
  # Research Validation Task
229
  research_task = Task(
230
  description="Validate and enrich the book concept with additional research insights",
231
  agent=self.research_agent,
232
  expected_output="Research-backed annotations and potential depth areas"
233
  )
234
+
235
  # Create Crew for Collaborative Processing
236
  book_concept_crew = Crew(
237
  agents=[self.concept_agent, self.research_agent],
238
  tasks=[concept_task, research_task],
239
  verbose=True # Changed from 2 to True
240
  )
241
+
242
  # Execute Collaborative Workflow
243
  try:
244
  result = book_concept_crew.kickoff()
245
+
246
  # Store Context in FAISS Vector Store
247
  self._store_context('book_concept', result)
248
+
249
  # Parse and return concept
250
  return self._parse_concept(result)
251
+
252
  except Exception as e:
253
  st.error(f"Book concept generation failed: {e}")
254
  return self._fallback_book_concept(initial_prompt)
255
 
256
  def generate_chapter_content(
257
+ self,
258
+ book_concept: Dict[str, Any],
259
  chapter_number: int
260
  ) -> str:
261
  """
262
  Generate content for a specific chapter using multi-agent workflow
263
+
264
  Args:
265
  book_concept (Dict): Comprehensive book concept
266
  chapter_number (int): Chapter to generate
267
+
268
  Returns:
269
  str: Generated chapter content
270
  """
271
  # Retrieve Previous Context
272
  previous_context = self._retrieve_context(chapter_number)
273
+
274
  # Content Generation Task
275
  writing_task = Task(
276
  description=f"""
277
  Write Chapter {chapter_number} for the book
278
+
279
  Book Concept: {book_concept}
280
+
281
  Previous Context: {previous_context}
282
+
283
  Generate a draft that:
284
  1. Maintains narrative continuity
285
  2. Advances the story
 
289
  agent=self.writing_agent,
290
  expected_output="A complete chapter draft with narrative coherence"
291
  )
292
+
293
  # Editing and Refinement Task
294
  editing_task = Task(
295
  description="Review and refine the generated chapter draft",
296
  agent=self.editing_agent,
297
  expected_output="Polished chapter content with suggested improvements"
298
  )
299
+
300
  # Create Crew for Chapter Generation
301
  chapter_crew = Crew(
302
  agents=[self.writing_agent, self.editing_agent],
303
  tasks=[writing_task, editing_task],
304
  verbose=True # Changed from 2 to True
305
  )
306
+
307
  try:
308
  # Execute Collaborative Workflow
309
  chapter_content = chapter_crew.kickoff()
310
+
311
  # Store Chapter Context in FAISS
312
  self._store_context(f'chapter_{chapter_number}', chapter_content)
313
+
314
  # Update global memory with chapter context
315
  self.global_memory.chat_memory.add_user_message(chapter_content)
316
+
317
  return chapter_content
318
+
319
  except Exception as e:
320
  st.error(f"Chapter generation failed: {e}")
321
  return self._fallback_chapter_content(book_concept, chapter_number)
322
 
323
  # Rest of the implementation remains the same as in the previous version
324
+
325
  def _store_context(self, context_key: str, content: str):
326
  """
327
  Store context in FAISS vector store
328
+
329
  Args:
330
  context_key (str): Unique identifier for the context
331
  content (str): Content to store
 
333
  try:
334
  # Create a document with metadata
335
  document = Document(
336
+ page_content=content,
337
  metadata={
338
  "project_id": self.project_id,
339
  "context_key": context_key
340
  }
341
  )
342
+
343
  # Add document to FAISS vector store
344
  new_store = FAISS.from_documents([document], self.embeddings)
345
  self.context_store.merge_from(new_store)
346
+
347
  except Exception as e:
348
  st.error(f"Context storage failed: {e}")
349
 
350
  # Remaining methods stay the same as in the previous implementation
351
 
352
+
353
  def main():
354
  """
355
  Demonstration of BookWritingOrchestrator
 
357
  try:
358
  # Create orchestrator
359
  orchestrator = BookWritingOrchestrator()
360
+
361
  # Generate book concept
362
  initial_prompt = "A science fiction story about space exploration"
363
  book_concept = orchestrator.generate_book_concept(initial_prompt)
364
  print("Book Concept:", book_concept)
365
+
366
  # Generate first chapter
367
  first_chapter = orchestrator.generate_chapter_content(book_concept, 1)
368
  print("\nFirst Chapter:\n", first_chapter)
369
+
370
  except Exception as e:
371
  print(f"An error occurred: {e}")
372
 
373
+
374
  if __name__ == "__main__":
375
  main()