Skip to content

Commit

Permalink
refactor: Prompts and Load book config
Browse files Browse the repository at this point in the history
  • Loading branch information
Estrada Irribarra, Rodrigo Andres committed Oct 15, 2024
1 parent acdb2ca commit bd39fb0
Show file tree
Hide file tree
Showing 17 changed files with 350 additions and 157 deletions.
1 change: 1 addition & 0 deletions examples/example_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ storycraftr outline general-outline "Summarize the overall plot of a dystopian s

storycraftr outline character-summary "Summarize the character of Zevid, a villainous mastermind who seeks to destroy both the ruling elite and the workers in a dystopian world where advanced technology mimics magic."

storycraftr outline plot-points "Identify the key plot points of a dystopian novel where a villain manipulates both the elite and the workers to achieve ultimate control in a world where advanced technology mimics magic."



Expand Down
50 changes: 24 additions & 26 deletions storycraftr/agent/chapters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import os
from storycraftr.agent.agents import create_or_get_assistant, get_thread, create_message, update_agent_files
from storycraftr.utils.core import get_config
from storycraftr.utils.core import load_book_config, load_book_config
from storycraftr.utils.markdown import save_to_markdown
from storycraftr.prompts.chapters import (
CHAPTER_PROMPT_NEW,
CHAPTER_PROMPT_REFINE,
COVER_PROMPT,
BACK_COVER_PROMPT,
EPILOGUE_PROMPT_NEW,
EPILOGUE_PROMPT_REFINE
)
from rich.console import Console

console = Console()
Expand All @@ -20,7 +28,7 @@ def generate_chapter(book_name, prompt, chapter_number):
# Check if the file exists and pass it as an attachment
if os.path.exists(file_path):
console.print(f"[yellow]Existing chapter found at {file_path}. Attaching for further refinement...[/yellow]") # Progress message
content = f"Use the attached chapter file as a reference to evolve and improve the content based on this prompt: {prompt}. Write it in {get_config(book_name).primary_language}."
content = CHAPTER_PROMPT_REFINE.format(prompt=prompt, language=load_book_config(book_name).primary_language)
chapter_content = create_message(
thread_id=thread.id,
content=content,
Expand All @@ -29,7 +37,7 @@ def generate_chapter(book_name, prompt, chapter_number):
)
else:
console.print("[yellow]No existing chapter found. Generating new content...[/yellow]") # Progress message
content = f"Write a detailed chapter for the following book premise: {prompt}. Write it in {get_config(book_name).primary_language}."
content = CHAPTER_PROMPT_NEW.format(prompt=prompt, language=load_book_config(book_name).primary_language)
chapter_content = create_message(
thread_id=thread.id,
content=content,
Expand All @@ -48,42 +56,32 @@ def generate_cover(book_name, prompt):
and a prompt for additional guidance.
"""
console.print("[bold blue]Generating book cover...[/bold blue]") # Progress message
# Obtener los datos del archivo de configuración
config = get_config(book_name)
language = config.primary_language
title = config.book_name
author = config.default_author
genre = config.genre
alternate_languages = ', '.join(config.alternate_languages)

# Crear o obtener el asistente
config = load_book_config(book_name)
assistant = create_or_get_assistant(book_name)
thread = get_thread()

# Prompt para generar la portada completa en markdown, incluyendo todos los datos relevantes
prompt_content = (
f"Create a professional book cover in markdown format for the book titled '{title}'. "
f"Include the title, author (which is '{author}'), genre ('{genre}'), "
f"and alternate languages ('{alternate_languages}'). Use this information to format a typical "
f"book cover with a detailed description. Use this prompt as additional context: {prompt}. "
f"Write the content in {language}."
# Generate the cover content
prompt_content = COVER_PROMPT.format(
title=config.book_name,
author=config.default_author,
genre=config.genre,
alternate_languages=', '.join(config.alternate_languages),
prompt=prompt,
language=config.primary_language
)

# Generar el contenido completo de la portada
cover_content = create_message(
thread_id=thread.id,
content=prompt_content,
assistant=assistant
)

# Guardar el contenido en el archivo markdown
# Save the cover content to markdown
save_to_markdown(book_name, "cover.md", "Cover", cover_content)
console.print("[bold green]✔ Cover generated successfully[/bold green]") # Success message

update_agent_files(book_name, assistant)
return cover_content


# Function to generate the back cover page
def generate_back_cover(book_name, prompt):
"""Generate the back cover page for the book."""
Expand All @@ -94,7 +92,7 @@ def generate_back_cover(book_name, prompt):
# Generate the back cover content
back_cover_content = create_message(
thread_id=thread.id,
content=f"Generate a detailed synopsis for the back cover of the book based on this prompt: {prompt}. Write it in {get_config(book_name).primary_language}.",
content=BACK_COVER_PROMPT.format(prompt=prompt, language=load_book_config(book_name).primary_language),
assistant=assistant
)

Expand All @@ -117,7 +115,7 @@ def generate_epilogue(book_name, prompt):
# Check if the file exists and pass it as an attachment
if os.path.exists(file_path):
console.print(f"[yellow]Existing epilogue found at {file_path}. Attaching for further refinement...[/yellow]") # Progress message
content = f"Use the attached epilogue file as a reference to evolve and improve the content based on this prompt: {prompt}. Write it in {get_config(book_name).primary_language}."
content = EPILOGUE_PROMPT_REFINE.format(prompt=prompt, language=load_book_config(book_name).primary_language)
epilogue_content = create_message(
thread_id=thread.id,
content=content,
Expand All @@ -126,7 +124,7 @@ def generate_epilogue(book_name, prompt):
)
else:
console.print("[yellow]No existing epilogue found. Generating new content...[/yellow]") # Progress message
content = f"Generate the epilogue for the book based on this prompt: {prompt}. Write it in {get_config(book_name).primary_language}."
content = EPILOGUE_PROMPT_NEW.format(prompt=prompt, language=load_book_config(book_name).primary_language)
epilogue_content = create_message(
thread_id=thread.id,
content=content,
Expand Down
37 changes: 24 additions & 13 deletions storycraftr/agent/outline.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import os
from storycraftr.utils.core import load_book_config
from storycraftr.agent.agents import create_or_get_assistant, get_thread, create_message, update_agent_files
from storycraftr.utils.core import get_config, file_has_more_than_three_lines
from storycraftr.utils.core import load_book_config,file_has_more_than_three_lines
from storycraftr.utils.markdown import save_to_markdown
from storycraftr.prompts.outline import (
GENERAL_OUTLINE_PROMPT_NEW,
GENERAL_OUTLINE_PROMPT_REFINE,
CHARACTER_SUMMARY_PROMPT_NEW,
CHARACTER_SUMMARY_PROMPT_REFINE,
PLOT_POINTS_PROMPT_NEW,
PLOT_POINTS_PROMPT_REFINE,
CHAPTER_SYNOPSIS_PROMPT_NEW,
CHAPTER_SYNOPSIS_PROMPT_REFINE
)
from rich.console import Console

console = Console()
Expand All @@ -10,7 +21,7 @@
def generate_general_outline(book_name, prompt):
"""Generate the general outline of the book."""
console.print("[bold blue]Generating general outline...[/bold blue]") # Progress message
language = get_config(book_name).primary_language
language = load_book_config(book_name).primary_language
assistant = create_or_get_assistant(book_name)
thread = get_thread()

Expand All @@ -20,7 +31,7 @@ def generate_general_outline(book_name, prompt):
# Check if the file exists and pass it as an attachment
if os.path.exists(file_path) and file_has_more_than_three_lines(file_path):
console.print(f"[yellow]Existing general outline found at {file_path}. Attaching for further refinement...[/yellow]") # Progress message
content = f"Use the attached general outline file to evolve the content based on this prompt: {prompt}. Write it in {language}."
content = GENERAL_OUTLINE_PROMPT_REFINE.format(prompt=prompt, language=language)
general_outline_content = create_message(
thread_id=thread.id,
content=content,
Expand All @@ -29,7 +40,7 @@ def generate_general_outline(book_name, prompt):
)
else:
console.print("[yellow]No existing general outline found. Generating a new one...[/yellow]") # Progress message
content = f"Create a general outline for a book based on this prompt: {prompt}. Write it in {language}."
content = GENERAL_OUTLINE_PROMPT_NEW.format(prompt=prompt, language=language)
general_outline_content = create_message(
thread_id=thread.id,
content=content,
Expand All @@ -46,7 +57,7 @@ def generate_general_outline(book_name, prompt):
def generate_character_summary(book_name, prompt):
"""Generate the character summary for the book."""
console.print("[bold blue]Generating character summary...[/bold blue]") # Progress message
language = get_config(book_name).primary_language
language = load_book_config(book_name).primary_language
assistant = create_or_get_assistant(book_name)
thread = get_thread()

Expand All @@ -56,7 +67,7 @@ def generate_character_summary(book_name, prompt):
# Check if the file exists and pass it as an attachment
if os.path.exists(file_path) and file_has_more_than_three_lines(file_path):
console.print(f"[yellow]Existing character summary found at {file_path}. Attaching for further refinement...[/yellow]") # Progress message
content = f"Use the attached character summary file to evolve the content based on this prompt: {prompt}. Write it in {language}."
content = CHARACTER_SUMMARY_PROMPT_REFINE.format(prompt=prompt, language=language)
character_summary_content = create_message(
thread_id=thread.id,
content=content,
Expand All @@ -65,7 +76,7 @@ def generate_character_summary(book_name, prompt):
)
else:
console.print("[yellow]No existing character summary found. Generating a new one...[/yellow]") # Progress message
content = f"Generate a character summary for the book based on this prompt: {prompt}. Write it in {language}."
content = CHARACTER_SUMMARY_PROMPT_NEW.format(prompt=prompt, language=language)
character_summary_content = create_message(
thread_id=thread.id,
content=content,
Expand All @@ -82,7 +93,7 @@ def generate_character_summary(book_name, prompt):
def generate_plot_points(book_name, prompt):
"""Generate the main plot points for the book."""
console.print("[bold blue]Generating main plot points...[/bold blue]") # Progress message
language = get_config(book_name).primary_language
language = load_book_config(book_name).primary_language
assistant = create_or_get_assistant(book_name)
thread = get_thread()

Expand All @@ -92,7 +103,7 @@ def generate_plot_points(book_name, prompt):
# Check if the file exists and pass it as an attachment
if os.path.exists(file_path) and file_has_more_than_three_lines(file_path):
console.print(f"[yellow]Existing plot points found at {file_path}. Attaching for further refinement...[/yellow]") # Progress message
content = f"Use the attached plot points file to evolve the content based on this prompt: {prompt}. Write it in {language}."
content = PLOT_POINTS_PROMPT_REFINE.format(prompt=prompt, language=language)
plot_points_content = create_message(
thread_id=thread.id,
content=content,
Expand All @@ -101,7 +112,7 @@ def generate_plot_points(book_name, prompt):
)
else:
console.print("[yellow]No existing plot points found. Generating new ones...[/yellow]") # Progress message
content = f"Generate the main plot points for the book based on this prompt: {prompt}. Write it in {language}."
content = PLOT_POINTS_PROMPT_NEW.format(prompt=prompt, language=language)
plot_points_content = create_message(
thread_id=thread.id,
content=content,
Expand All @@ -118,7 +129,7 @@ def generate_plot_points(book_name, prompt):
def generate_chapter_synopsis(book_name, prompt):
"""Generate the chapter-by-chapter synopsis for the book."""
console.print("[bold blue]Generating chapter-by-chapter synopsis...[/bold blue]") # Progress message
language = get_config(book_name).primary_language
language = load_book_config(book_name).primary_language
assistant = create_or_get_assistant(book_name)
thread = get_thread()

Expand All @@ -128,7 +139,7 @@ def generate_chapter_synopsis(book_name, prompt):
# Check if the file exists and pass it as an attachment
if os.path.exists(file_path) and file_has_more_than_three_lines(file_path):
console.print(f"[yellow]Existing chapter synopsis found at {file_path}. Attaching for further refinement...[/yellow]") # Progress message
content = f"Use the attached chapter-by-chapter synopsis file to evolve the content based on this prompt: {prompt}. Write it in {language}."
content = CHAPTER_SYNOPSIS_PROMPT_REFINE.format(prompt=prompt, language=language)
chapter_synopsis_content = create_message(
thread_id=thread.id,
content=content,
Expand All @@ -137,7 +148,7 @@ def generate_chapter_synopsis(book_name, prompt):
)
else:
console.print("[yellow]No existing chapter synopsis found. Generating a new one...[/yellow]") # Progress message
content = f"Generate a chapter-by-chapter synopsis for the book based on this prompt: {prompt}. Write it in {language}."
content = CHAPTER_SYNOPSIS_PROMPT_NEW.format(prompt=prompt, language=language)
chapter_synopsis_content = create_message(
thread_id=thread.id,
content=content,
Expand Down
Loading

0 comments on commit bd39fb0

Please sign in to comment.