-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoriginal-script-all.py
More file actions
1297 lines (1157 loc) · 48.9 KB
/
original-script-all.py
File metadata and controls
1297 lines (1157 loc) · 48.9 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
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
import asyncio
import hashlib
import logging
import os
import re
import json
from typing import List, Dict, Optional, Tuple, Union
import gradio as gr
import tiktoken
from dotenv import load_dotenv
from llama_index import (
VectorStoreIndex,
ServiceContext,
VectorIndexRetriever,
PromptHelper,
HuggingFaceSummarization,
HuggingFaceEmbedding,
PromptTemplate,
FewShotPromptTemplate,
PromptType,
Workflow,
Event,
StartEvent,
StopEvent,
)
from llama_index.llms import OpenAI
from llama_index.node_parser import SimpleNodeParser
from llama_index.query_engine import RetrieverQueryEngine
from llama_index.vector_stores import LanceDBVectorStore
from llama_index.core.workflow import step
from llama_index.core.memory import ChatSummaryMemoryBuffer
from llama_index.core.llms import ChatMessage, MessageRole
from pydantic import BaseModel, Field, ValidationError
from textstat import textstat
from tortoise import Tortoise, fields, models, run_async
from tortoise.contrib.pydantic import pydantic_model_creator
# --- Configuration and Constants ---
class Config:
DATABASE_URL: str = os.getenv(
"DATABASE_URL", "sqlite://:memory:"
) # Using in-memory SQLite for testing
OPENAI_API_KEY: str = os.getenv("OPENAI_API_KEY") # Make sure to set your API key
DEFAULT_LLM_MODEL: str = "gpt-4o-mini" # Sticking with GPT-4o-mini
DEFAULT_LANGUAGE: str = "Portuguese"
CONTEXT_WINDOW: int = 4096
LANCEDB_URI: str = "./lancedb"
SUMMARIZATION_MODEL: str = "facebook/bart-large-cnn"
EMBEDDING_MODEL: str = "BAAI/bge-small-en-v1.5"
MAX_MEMORY_WORDS: int = 500 # Adjusted for GPT-4o-mini
MAX_MEMORY_SENTENCES: int = 20 # Adjusted for GPT-4o-mini
MAX_MEMORY_TOKENS: int = 4096 # Adjusted for GPT-4o-mini
MAX_RETRIES: int = 3
CHAT_MEMORY_TOKEN_LIMIT: int = 512 # Adjusted for GPT-4o-mini
CONCURRENCY_COUNT: int = 5
config = Config()
# --- Configure Logging ---
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# --- Language Prompts ---
LANGUAGE_PROMPTS = {
"Portuguese": {
"init_prompt": """
Por favor, escreva um livro técnico abrangente sobre {book_type} sobre {description} com 20 capítulos.
Siga o formato abaixo com precisão:
* **Título:** <O título do livro>
* **Índice:** <Lista de capítulos com seus títulos>
* **Capítulo 1: Introdução à {book_type}**
* **Capítulo 2: Conceitos Chave em {book_type}**
* **Capítulo 3: Tópicos Avançados em {book_type}**
...
* **Resumo:** <Um breve resumo dos três primeiros capítulos>
* **Instrução 1:** <Uma instrução sobre o que deve ser abordado no Capítulo 4>
* **Instrução 2:** <Outra instrução para um possível tópico de capítulo>
* **Instrução 3:** <Uma terceira instrução para uma direção potencial do capítulo>
Certifique-se de ser preciso e seguir estritamente o formato de saída.
""",
"human_prompt": """
Agora imagine que você é um assistente útil que ajuda um autor a estruturar um livro técnico sobre IA.
Você receberá um tópico atual, um resumo do tópico e 3 planos diferentes para desenvolver o próximo tópico.
Preciso que você:
1. Selecione o plano mais relevante e abrangente proposto.
2. Explique por que escolheu esse plano.
3. Revise o plano selecionado em um esboço detalhado para o próximo tópico.
Tópico Atual:
{current_topic}
O resumo do tópico atual:
{summary}
Três planos para o próximo tópico:
{plans}
Agora comece escolhendo e revisando, organizando sua saída seguindo estritamente o seguinte formato:
Plano Selecionado:
<copia o plano selecionado aqui>
Razão:
<Explique por que escolheu o plan>
Plano Revisado:
<string do plano revisado>, mantenha detalhado, cerca de 5-7 frases.
""",
"chapter_section_prompt": """
### {section_title}
{section_content}
**Perguntas e Respostas:**
* Pergunta 1: {question1}
* Resposta 1: {answer1}
* Pergunta 2: {question2}
* Resposta 2: {answer2}
""",
"chapter_prompt": """
## Capítulo {chapter_number}: {chapter_title}
{chapter_sections}
""",
},
# Add prompts for other languages here
}
# --- Database Models ---
class Book(models.Model):
id = fields.IntField(pk=True)
title = fields.CharField(max_length=255, null=False)
table_of_contents = fields.TextField(null=False)
language = fields.CharField(max_length=50, null=False)
model = fields.CharField(max_length=50, null=False)
class Meta:
table = "books"
class Chapter(models.Model):
id = fields.IntField(pk=True)
book = fields.ForeignKeyField("models.Book", related_name="chapters")
chapter_number = fields.IntField(null=False)
title = fields.CharField(max_length=255, null=False)
content = fields.TextField(null=False)
class Meta:
table = "chapters"
# --- Pydantic Models ---
BookSchema = pydantic_model_creator(Book, name="Book")
ChapterSchema = pydantic_model_creator(Chapter, name="Chapter")
class BookOutputInstruction(BaseModel):
Instruction_1: str = Field(
..., min_length=5, description="A possible interesting module for the topic"
)
Instruction_2: str = Field(
..., min_length=5, description="Another possible module direction"
)
Instruction_3: str = Field(
..., min_length=5, description="Yet another option for module development"
)
class BookOutputMemory(BaseModel):
rational: str = Field(..., description="Explanation of memory updates")
updated_memory: str = Field(
...,
description="Rewritten memory summary",
max_length=config.MAX_MEMORY_WORDS,
)
class BookOutput(BaseModel):
output_modules: Dict[str, str] = Field(
..., description="Modules covering key concepts"
)
output_summary: str = Field(
..., min_length=20, description="Summary of the topic"
)
output_questions_answers: Dict[str, str] = Field(
..., description="Questions and answers for reinforcement"
)
output_instruction: BookOutputInstruction
output_memory: BookOutputMemory
# --- Utility Functions ---
def get_content_between_a_b(a: str, b: str, text: str) -> str:
"""Extracts content between two substrings."""
pattern = re.compile(f"{re.escape(a)}(.*?){re.escape(b)}", re.DOTALL)
match = pattern.search(text)
return match.group(1).strip() if match else ""
def hash_cookie(cookie: str) -> str:
"""Hashes the cookie."""
return hashlib.sha256(cookie.encode()).hexdigest()
# --- Readability Metrics ---
def pre_process_text(text: str) -> str:
"""Preprocesses text for readability calculations."""
text = re.sub(r"\n\s*\n", "\n\n", text.strip())
sections = [
section.strip() for section in re.split(r"\n{2,}", text) if section.strip()
]
return " ".join(sections)
def automated_readability_index(text: str) -> float:
"""Calculates the Automated Readability Index (ARI)."""
processed_text = pre_process_text(text)
return textstat.automated_readability_index(processed_text)
def flesch_reading_ease(text: str) -> float:
"""Calculates the Flesch Reading Ease score."""
processed_text = pre_process_text(text)
return textstat.flesch_reading_ease(processed_text)
def gunning_fog_index(text: str) -> float:
"""Calculates the Gunning Fog Index."""
processed_text = pre_process_text(text)
return textstat.gunning_fog(processed_text)
def categorize_metrics(
ari: float, flesch: float, gunning_fog: float
) -> Tuple[str, str, str]:
"""Categorizes readability metrics."""
clarity_category = (
"Low" if ari <= 5.0 else "Normal" if ari <= 10.0 else "High"
)
understandability_category = (
"Low" if flesch <= 29.0 else "Normal" if flesch <= 69.0 else "High"
)
conciseness_category = (
"Low"
if gunning_fog <= 6.0
else "Normal"
if gunning_fog <= 12.0
else "High"
)
return clarity_category, understandability_category, conciseness_category
def calculate_readability_metrics(
text: str,
) -> Tuple[str, float, float, float, str, str, str]:
"""Calculates and categorizes readability metrics."""
ari = automated_readability_index(text)
flesch = flesch_reading_ease(text)
gunning_fog = gunning_fog_index(text)
clarity, understandability, conciseness = categorize_metrics(
ari, flesch, gunning_fog
)
return clarity, flesch, gunning_fog, ari, understandability, conciseness
# --- Session Management ---
_CACHE = {}
# --- Custom Workflow Events ---
class StoryGenerationDone(Event):
output: str
input_data: dict
class OutputValidationError(Event):
error: str
wrong_output: str
input_data: dict
# --- Workflows ---
class RecurrentGPTWorkflow(Workflow):
def __init__(
self,
model_name: str = config.DEFAULT_LLM_MODEL,
language: str = config.DEFAULT_LANGUAGE,
):
super().__init__(timeout=120, verbose=True)
self.model_name = model_name
self.language = language
self.llm = OpenAI(
api_key=config.OPENAI_API_KEY, temperature=0, model_name=self.model_name
)
self.embedder = HuggingFaceEmbedding(model_name=config.EMBEDDING_MODEL)
self.vector_store = LanceDBVectorStore(
uri=config.LANCEDB_URI, mode="append"
)
self.service_context = ServiceContext.from_defaults(
llm=self.llm,
prompt_helper=PromptHelper(context_window=config.CONTEXT_WINDOW),
embed_model=self.embedder,
)
self.long_term_memory_index = VectorStoreIndex(
[], service_context=self.service_context, vector_store=self.vector_store
)
self.retriever = VectorIndexRetriever(
index=self.long_term_memory_index, similarity_top_k=5
)
self.query_engine = RetrieverQueryEngine(retriever=self.retriever)
self.chat_memory = ChatSummaryMemoryBuffer.from_defaults(
llm=self.llm,
token_limit=config.CHAT_MEMORY_TOKEN_LIMIT,
tokenizer_fn=tiktoken.encoding_for_model(self.model_name).encode,
)
self.long_memory: List[str] = []
self.short_memory: str = ""
self.output: Dict = {}
self.node_parser = SimpleNodeParser()
self.summarizer = HuggingFaceSummarization(
model_name=config.SUMMARIZATION_MODEL,
service_context=self.service_context,
)
self.tokenizer = tiktoken.encoding_for_model(self.model_name).encode
self.few_shot_book_prompt = self.get_story_prompt_template()
def get_story_prompt_template(self) -> FewShotPromptTemplate:
language = self.language
prompt_templates = LANGUAGE_PROMPTS
if language not in prompt_templates:
logger.warning(
f"Unsupported language '{language}'. Defaulting to English."
)
language = "English"
return FewShotPromptTemplate(
example_prompt=PromptTemplate(
prompt=prompt_templates[language]["init_prompt"],
prompt_type=PromptType.CUSTOM,
),
examples=[], # Few-shot examples can be dynamically added or loaded
suffix="""
## Technical Book Writing Assistant
**Your Task:** Develop the next section of our technical book on {book_type}. Consider the previous topic, the current memory, and the instructions for what should be covered next.
**Context:**
* Current Topic: {input_topic}
* Current Memory (Summary of the Book So Far): {current_memory}
* Related Past Topics: {related_topics}
* Instructions for What Should Happen Next: {input_instruction}
* New Concept Prompt: {new_concept_prompt}
* Relevant Memories: {relevant_memories}
**Output Format:**
```json
{{
"output_modules": {{
"Module_1": "<First key concept>",
"Module_2": "<Second key concept>",
"Module_3": "<Third key concept>"
}},
"output_summary": "<Summary of the topic>",
"output_questions_answers": {{
"Q1": "<First question>",
"A1": "<Answer to first question>",
"Q2": "<Second question>",
"A2": "<Answer to second question>"
}},
"output_instruction": {{
"Instruction_1": "<A possible interesting module for the next topic>",
"Instruction_2": "<Another possible module direction>",
"Instruction_3": "<Yet another option for module development>"
}},
"output_memory": {{
"rational": "<Explanation of memory updates>",
"updated_memory": "<Updated summary of the book so far>"
}}
}}
Use code with caution.
Python
Important:
Keep the Updated Memory concise, around 10-20 sentences.
Never exceed {max_memory_words} words in the Updated Memory.
Ensure technical accuracy and clarity. The content should be informative and educational.
""".format(
max_memory_words=config.MAX_MEMORY_WORDS
),
)
@step
async def generate_book_section(
self, ev: Union[StartEvent, OutputValidationError]
) -> Union[StoryGenerationDone, StopEvent]:
"""Generates the next section of the technical book."""
current_retries = await self.ctx.get("retries", default=0)
if current_retries >= config.MAX_RETRIES:
logger.warning("Max retries reached.")
return StopEvent(result="Max retries reached")
await self.ctx.set("retries", current_retries + 1)
if isinstance(ev, StartEvent):
input_data = ev.get("input_data")
if not input_data:
logger.error("No input data provided.")
return StopEvent(result="Please provide input data")
reflection_prompt = ""
elif isinstance(ev, OutputValidationError):
input_data = ev.input_data
reflection_prompt = (
f"The previous output was not in the correct format. "
f"Here's the error: {ev.error}\n"
f"Please try again, ensuring the output strictly follows the specified format."
)
else:
logger.error("Unknown event type.")
return StopEvent(result="Unknown event type")
# Prepare the prompt
prompt = self.few_shot_book_prompt.format(**input_data) + reflection_prompt
logger.info(f"Generating book section with prompt: {prompt[:500]}...")
try:
response = await self.llm.acomplete(prompt)
logger.info("Book section generated successfully.")
except Exception as e:
logger.error(f"Error during LLM query: {e}")
return StopEvent(result="Error generating book section.")
return StoryGenerationDone(output=response, input_data=input_data)
@step
async def validate_output(
self, ev: StoryGenerationDone
) -> Union[StopEvent, OutputValidationError]:
"""Validates the LLM output."""
try:
# Parse the output
parsed_output = BookOutput.parse_raw(ev.output)
logger.info("Output parsed successfully.")
# Enforce memory limits
parsed_output.output_memory.updated_memory = (
self._manage_memory_limits(
parsed_output.output_memory.updated_memory
)
)
logger.info("Memory limits enforced.")
# Update long-term memory
self.long_memory.append(parsed_output.output_summary)
summary = self.summarizer.summarize(parsed_output.output_summary)
self.long_term_memory_index.insert(summary)
logger.info("Long-term memory updated.")
# Update short-term memory
self.short_memory = parsed_output.output_memory.updated_memory
self.chat_memory.put(
ChatMessage(
role=MessageRole.USER,
content=parsed_output.output_instruction.Instruction_1,
)
)
self.chat_memory.put(
ChatMessage(
role=MessageRole.ASSISTANT, content=parsed_output.output_summary
)
)
logger.info("Short-term memory updated.")
self.output = parsed_output.dict()
return StopEvent(result=self.output)
except ValidationError as e:
logger.error(f"Validation failed: {e}")
return OutputValidationError(
error=str(e), wrong_output=ev.output, input_data=ev.input_data
)
except Exception as e:
logger.error(f"Unexpected error during validation: {e}")
return OutputValidationError(
error=str(e), wrong_output=ev.output, input_data=ev.input_data
)
def _manage_memory_limits(self, new_memory: str) -> str:
"""Enforces memory size limits."""
# Enforce sentence and word limits
sentences = new_memory.split(".")
if len(sentences) > config.MAX_MEMORY_SENTENCES:
new_memory = ".".join(sentences[: config.MAX_MEMORY_SENTENCES])
logger.debug("Memory truncated based on sentence limit.")
words = new_memory.split()
if len(words) > config.MAX_MEMORY_WORDS:
new_memory = " ".join(words[: config.MAX_MEMORY_WORDS])
logger.debug("Memory truncated based on word limit.")
# Enforce token limits
tokens = self.tokenizer(new_memory)
if len(tokens) > config.MAX_MEMORY_TOKENS:
new_memory = self.tokenizer.decode(
tokens[: config.MAX_MEMORY_TOKENS]
) # Fixed: Use self.tokenizer.decode
logger.debug("Memory truncated based on token limit.")
return new_memory
async def run_step(self, input_data: Dict) -> Dict:
"""Runs the workflow."""
start_event = StartEvent(input_data=input_data)
result_event = await self.arun(start_event)
return result_event.result if isinstance(result_event, StopEvent) else {}
class HumanWorkflow(Workflow):
def __init__(
self,
model_name: str = config.DEFAULT_LLM_MODEL,
language: str = config.DEFAULT_LANGUAGE,
):
super().__init__(timeout=120, verbose=True)
self.model_name = model_name
self.language = language
self.llm = OpenAI(
api_key=config.OPENAI_API_KEY, temperature=0, model_name=self.model_name
)
@step
async def select_and_revise_plan(
self, ev: StartEvent
) -> Union[StopEvent, OutputValidationError]:
"""Allows human to select and revise a plan."""
input_data = ev.get("input_data") # Consistent use of get method
if not input_data:
logger.error("No input data provided for human workflow.")
return StopEvent(result="No input data provided")
prompt = self.prepare_prompt(input_data)
logger.info(f"Human workflow prompt: {prompt[:500]}...")
try:
response = await self.llm.acomplete(prompt)
logger.info("Human workflow response received.")
except Exception as e:
logger.error(f"Error during human workflow LLM query: {e}")
return OutputValidationError(
error="Error during human workflow LLM query.",
wrong_output="",
input_data=input_data,
)
revised_plan = self.parse_revised_plan(response)
if not revised_plan:
logger.error("Revised plan not found in response.")
return OutputValidationError(
error="Revised plan not found in response.",
wrong_output=response,
input_data=input_data,
)
output = {"output_instruction": revised_plan}
logger.info("Revised plan parsed successfully.")
return StopEvent(result=output)
def prepare_prompt(self, input_data: Dict) -> str:
"""Prepares the prompt for human interaction."""
language = self.language
prompt_templates = LANGUAGE_PROMPTS
if language not in prompt_templates:
logger.warning(
f"Unsupported language '{language}'. Defaulting to English."
)
language = "English"
plans = input_data.get("output_instruction", {})
plans_list = [
plans.get("Instruction_1", ""),
plans.get("Instruction_2", ""),
plans.get("Instruction_3", ""),
]
plans_formatted = "\n".join(
[f"{i+1}. {plan}" for i, plan in enumerate(plans_list) if plan]
)
prompt = prompt_templates[language]["human_prompt"].format(
current_topic=input_data.get("input_topic", ""),
summary=input_data.get("output_summary", ""),
plans=plans_formatted,
)
return prompt
def parse_revised_plan(self, response: str) -> Optional[str]:
"""Parses the revised plan from the human's response."""
language = self.language
if language == "English":
revised_plan = get_content_between_a_b("Revised Plan:", "", response)
elif language == "Spanish":
revised_plan = get_content_between_a_b(
"Plan Revisado:", "", response
)
elif language == "Portuguese":
revised_plan = get_content_between_a_b(
"Plano Revisado:", "", response
)
else:
revised_plan = get_content_between_a_b("Revised Plan:", "", response)
return revised_plan if revised_plan else None
async def run_step(self, input_data: Dict) -> Optional[str]:
"""Runs the human interaction workflow."""
start_event = StartEvent(input_data=input_data)
result_event = await self.arun(start_event)
return (
result_event.result.get("output_instruction")
if isinstance(result_event, StopEvent)
else None
)
# --- Gradio Interface Functions ---
def init_prompt(book_type: str, description: str, language: str) -> str:
"""Creates the initial prompt for book generation."""
language = language if language in LANGUAGE_PROMPTS else "English"
prompt_template = LANGUAGE_PROMPTS[language]["init_prompt"]
return prompt_template.format(book_type=book_type, description=description)
async def generate_chapter(
book: Book,
chapter_number: int,
input_data: Dict,
recurrent_workflow: RecurrentGPTWorkflow,
) -> None:
"""Generates and saves a chapter to the database."""
recurrent_output = await recurrent_workflow.run_step(input_data)
logger.info(f"Recurrent Workflow Output: {recurrent_output}")
chapter_title = recurrent_output["output_modules"]["Module_1"]
chapter_sections = []
# Generate sections within the chapter
for i in range(1, 4): # Generate 3 sections per chapter
section_title = recurrent_output["output_modules"][f"Module_{i}"]
section_content_initial = recurrent_output["output_summary"]
# Generate questions and answers for each section
qa_prompt = f"Generate two insightful questions and their comprehensive answers based on the following section content:\n\n{section_content_initial}"
qa_response = await recurrent_workflow.llm.acomplete(qa_prompt)
logger.info(f"Q&A Response: {qa_response}")
try:
qa_json = json.loads(qa_response)
question1 = qa_json.get("Q1", "")
answer1 = qa_json.get("A1", "")
question2 = qa_json.get("Q2", "")
answer2 = qa_json.get("A2", "")
except json.JSONDecodeError:
logger.error(f"Error decoding Q&A response as JSON: {qa_response}")
question1 = ""
answer1 = ""
question2 = ""
answer2 = ""
# Generate the section content with Q&A
section_content = LANGUAGE_PROMPTS["Portuguese"][
"chapter_section_prompt"
].format(
section_title=section_title,
section_content=section_content_initial,
question1=question1,
answer1=answer1,
question2=question2,
answer2=answer2,
)
chapter_sections.append(section_content)
# Combine sections into the final chapter content
chapter_content = LANGUAGE_PROMPTS["Portuguese"]["chapter_prompt"].format(
chapter_number=chapter_number,
chapter_title=chapter_title,
chapter_sections="\n".join(chapter_sections),
)
chapter = await Chapter.create(
book=book,
chapter_number=chapter_number,
title=chapter_title,
content=chapter_content,
)
logger.info(f"Chapter {chapter.chapter_number} saved: {chapter.title}")
# --- Gradio Functions ---
def on_select_plan(
selected_plan: str,
instruction1: str,
instruction2: str,
instruction3: str,
) -> str:
"""Updates the selected instruction based on the radio button choice."""
if selected_plan == "Instruction 1":
return instruction1
elif selected_plan == "Instruction 2":
return instruction2
elif selected_plan == "Instruction 3":
return instruction3
else:
return ""
# --- Gradio App ---
with gr.Blocks(
title="RecurrentGPT for AI Technical Books",
css="footer {visibility: hidden}",
theme="sudeepshouche/minimalist",
) as demo:
gr.Markdown(
"""
# RecurrentGPT for AI Technical Books
Interactive Generation of Comprehensive AI Technical Books with Human-in-the-Loop
"""
)
# --- State Variables ---
current_chapter = gr.State(1)
book_instance = gr.State(None)
recurrent_workflow = gr.State(None)
human_workflow = gr.State(None)
with gr.Tab("Auto-Generation"):
with gr.Row():
with gr.Column():
with gr.Box():
with gr.Row():
with gr.Column(scale=1, min_width=200):
book_type = gr.Textbox(
label="Book Type",
placeholder="e.g. Machine Learning, Deep Learning",
)
with gr.Column(scale=2, min_width=400):
description = gr.Textbox(label="Description")
with gr.Row():
with gr.Column(scale=1, min_width=200):
model_selection = gr.Radio(
["gpt-4o", "gpt-4o-mini"], # Corrected model names
label="Select Language Model",
value="gpt-4o-mini",
)
with gr.Column(scale=2, min_width=400):
language_selection = gr.Radio(
["English", "Spanish", "Portuguese"],
label="Select Language",
value="English",
)
btn_init = gr.Button(
"Init Book Generation", variant="primary"
)
gr.Examples(
[
"Machine Learning",
"Deep Learning",
"Natural Language Processing",
"Computer Vision",
"Reinforcement Learning",
"AI Ethics",
"Neural Networks",
"Data Science",
"Robotics",
],
inputs=[book_type],
)
written_paras = gr.Textbox(
label="Written Content (editable)",
max_lines=21,
lines=21,
)
with gr.Box():
gr.Markdown("### Readability Metrics\n")
clarity = gr.Textbox(label="Clarity")
flesch_score = gr.Number(
label="Flesch Reading Ease", precision=2
)
g_fog = gr.Number(
label="Gunning Fog Index", precision=2
)
calculate_button = gr.Button("Calculate Metrics")
with gr.Column():
with gr.Box():
gr.Markdown("### Memory Module\n")
short_memory = gr.Textbox(
label="Short-Term Memory (editable)",
max_lines=3,
lines=3,
)
long_memory = gr.Textbox(
label="Long-Term Memory (editable)",
max_lines=6,
lines=6,
)
with gr.Box():
gr.Markdown("### Instruction Module\n")
with gr.Row():
instruction1 = gr.Textbox(
label="Instruction 1 (editable)",
max_lines=4,
lines=4,
)
instruction2 = gr.Textbox(
label="Instruction 2 (editable)",
max_lines=4,
lines=4,
)
instruction3 = gr.Textbox(
label="Instruction 3 (editable)",
max_lines=4,
lines=4,
)
with gr.Row():
with gr.Column(scale=1, min_width=100):
selected_plan = gr.Radio(
[
"Instruction 1",
"Instruction 2",
"Instruction 3",
],
label="Instruction Selection",
)
with gr.Column(scale=3, min_width=300):
selected_instruction = gr.Textbox(
label="Selected Instruction (editable)",
max_lines=5,
lines=5,
)
btn_step = gr.Button("Next Step", variant="primary")
# Define Readability Metrics Calculation
def update_metrics(text):
(
clarity_cat,
flesch,
g_fog_idx,
ari,
understand_cat,
conciseness_cat,
) = calculate_readability_metrics(text)
return clarity_cat, flesch, g_fog_idx
# Initialize Book Generation
async def initialize_book(
book_type: str,
description: str,
language: str,
model: str,
request: gr.Request,
):
"""Initializes the book generation process."""
try:
# Create a new book instance
book = await Book.create(
title=book_type,
table_of_contents="To be generated...",
language=language,
model=model,
)
book_instance = book
# Initialize the workflows
recurrent_workflow = RecurrentGPTWorkflow(model_name=model, language=language)
human_workflow = HumanWorkflow(model_name=model, language=language)
# Prepare initial input data
input_data = {
"book_type": book_type,
"description": description,
"input_topic": "Introduction",
"current_memory": "",
"related_topics": [],
"input_instruction": {
"Instruction_1": "Write an engaging introduction to the book.",
"Instruction_2": "Provide a brief overview of the topics covered.",
"Instruction_3": "Outline the target audience for the book.",
},
"new_concept_prompt": "",
"relevant_memories": "",
}
# Run the first step of the recurrent workflow
recurrent_output = await recurrent_workflow.run_step(input_data)
logger. info(f"Recurrent Workflow Output: {recurrent_output}")
# Update the Gradio interface with the initial output
short_memory_text = recurrent_output["output_memory"]["updated_memory"]
long_memory_text = ""
written_paras_text = recurrent_output["output_summary"]
instruction1_text = recurrent_output["output_instruction"]["Instruction_1"]
instruction2_text = recurrent_output["output_instruction"]["Instruction_2"]
instruction3_text = recurrent_output["output_instruction"]["Instruction_3"]
return (
short_memory_text,
long_memory_text,
written_paras_text,
instruction1_text,
instruction2_text,
instruction3_text,
book_instance,
recurrent_workflow,
human_workflow,
)
except Exception as e:
logger.error(f"Error initializing book: {e}")
return "", "", "", "", "", "", None, None, None
btn_init.click(
initialize_book,
inputs=[
book_type,
description,
language_selection,
model_selection,
gr.Request(),
],
outputs=[
short_memory,
long_memory,
written_paras,
instruction1,
instruction2,
instruction3,
book_instance,
recurrent_workflow,
human_workflow,
],
queue=False,
)
# Execute Next Step
async def execute_next_step(
short_memory: str,
long_memory: str,
instruction1: str,
instruction2: str,
instruction3: str,
written_paras: str,
request: gr.Request,
response_file: str,
book_instance: Book,
recurrent_workflow: RecurrentGPTWorkflow,
human_workflow: HumanWorkflow,
) -> Tuple[str, str, str, str, str, str, Book, RecurrentGPTWorkflow, HumanWorkflow]:
"""Executes the next step in the book generation process."""
try:
# Prepare input data for the next step
input_data = {
"book_type": book_instance.title,
"description": description.value,
"input_topic": f"Chapter {current_chapter.value}",
"current_memory": short_memory,
"related_topics": recurrent_workflow.long_memory,
"input_instruction": {
"Instruction_1": instruction1,
"Instruction_2": instruction2,
"Instruction_3": instruction3,
},
"new_concept_prompt": "",
"relevant_memories": "",
}
# Run the recurrent workflow
recurrent_output = await recurrent_workflow.run_step(input_data)
logger.info(f"Recurrent Workflow Output: {recurrent_output}")
# Generate and save the chapter
await generate_chapter(
book_instance, current_chapter.value, input_data, recurrent_workflow
)
# Update the current chapter number
current_chapter.value += 1
# Update the Gradio interface with the output
short_memory_text = recurrent_output["output_memory"]["updated_memory"]
long_memory_text = "\n".join(recurrent_workflow.long_memory)
written_paras_text = recurrent_output["output_summary"]
instruction1_text = recurrent_output["output_instruction"]["Instruction_1"]
instruction2_text = recurrent_output["output_instruction"]["Instruction_2"]
instruction3_text = recurrent_output["output_instruction"]["Instruction_3"]
return (
short_memory_text,
long_memory_text,
written_paras_text,
"Instruction 1", # Reset selected plan
instruction1_text,
instruction2_text,