Skip to content

Commit

Permalink
feat(agent): Add update files after each iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
Estrada Irribarra, Rodrigo Andres committed Oct 15, 2024
1 parent 4a5c015 commit eaf97c4
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,4 @@ cython_debug/

La purga de los dioses
.DS_Store
behavior.txt
10 changes: 10 additions & 0 deletions examples/example_usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
storycraftr init "La purga de los dioses" --primary-language "es" --alternate-languages "en" --author "Rodrigo Estrada" --genre "science fiction" --behavior "behavior.txt"

storycraftr outline general-outline "Summarize the overall plot of a dystopian science fiction where advanced technology, resembling magic, has led to the fall of humanity’s elite and the rise of a manipulative villain who seeks to destroy both the ruling class and the workers."

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 chapter_synopsis "Outline each chapter of a dystopian society where an ancient elite class, ruling with advanced biotechnology that mimics magic, manipulates both workers and warriors. The protagonist, Zevid, aims to destroy both factions through manipulation, eventually leading to his own version of 'The Purge.'"
19 changes: 4 additions & 15 deletions storycraftr/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def delete_assistant(name):
break

# Function to create or get an assistant
def create_or_get_assistant(name, book_name):
def create_or_get_assistant(book_name):
name = book_name.split("/")[-1]
console.print(f"[bold blue]Searching for existing assistant '{name}'...[/bold blue]") # Progress message
assistant = None
assistants = client.beta.assistants.list()
Expand Down Expand Up @@ -153,19 +154,7 @@ def get_thread():

# Function to update the assistant's knowledge with new files
def update_agent_files(book_name, assistant):
"""Update the assistant with new Markdown files from the book."""
console.print(f"[bold blue]Updating assistant '{assistant.name}' with new files from '{book_name}'...[/bold blue]") # Progress message
md_files = load_markdown_files(book_name)

file_streams = [open(file_path, "rb") for file_path in md_files]

file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
vector_store_id=assistant.vector_store_ids[0], # Assuming the first vector store is associated
files=file_streams
)

while file_batch.status == "queued" or file_batch.status == "in_progress":
console.print(f"[bold yellow]{file_batch.status}...[/bold yellow]") # Progress message
time.sleep(1)
delete_assistant(book_name)
create_or_get_assistant(book_name)

console.print(f"[bold green]Files updated successfully in assistant '{assistant.name}'.[/bold green]") # Success message
14 changes: 9 additions & 5 deletions storycraftr/chapters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from storycraftr.agents import create_or_get_assistant, get_thread, create_message
from storycraftr.agents import create_or_get_assistant, get_thread, create_message, update_agent_files
from storycraftr.core import get_config
from rich.console import Console

Expand All @@ -19,7 +19,7 @@ def save_to_markdown(book_name, file_name, header, content):
def generate_chapter(book_name, prompt, chapter_number):
"""Generate a new chapter based on a prompt."""
console.print(f"[bold blue]Generating chapter {chapter_number}...[/bold blue]") # Progress message
assistant = create_or_get_assistant(book_name, book_name)
assistant = create_or_get_assistant(book_name)
thread = get_thread()

# Prepare the chapter file path
Expand Down Expand Up @@ -48,6 +48,7 @@ def generate_chapter(book_name, prompt, chapter_number):
# Save the updated chapter content to markdown
save_to_markdown(book_name, chapter_file, f"Chapter {chapter_number}", chapter_content)
console.print(f"[bold green]✔ Chapter {chapter_number} generated successfully[/bold green]") # Success message
update_agent_files(book_name, assistant)
return chapter_content

def generate_cover(book_name, prompt):
Expand All @@ -65,7 +66,7 @@ def generate_cover(book_name, prompt):
alternate_languages = ', '.join(config.alternate_languages)

# Crear o obtener el asistente
assistant = create_or_get_assistant(book_name, 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
Expand All @@ -88,14 +89,15 @@ def generate_cover(book_name, prompt):
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."""
console.print("[bold blue]Generating back cover...[/bold blue]") # Progress message
assistant = create_or_get_assistant(book_name, book_name)
assistant = create_or_get_assistant(book_name)
thread = get_thread()

# Generate the back cover content
Expand All @@ -108,13 +110,14 @@ def generate_back_cover(book_name, prompt):
# Save to markdown
save_to_markdown(book_name, "back_cover.md", "Back Cover", back_cover_content)
console.print("[bold green]✔ Back cover generated successfully[/bold green]") # Success message
update_agent_files(book_name, assistant)
return back_cover_content

# Function to generate the epilogue of the book
def generate_epilogue(book_name, prompt):
"""Generate the epilogue for the book."""
console.print("[bold blue]Generating epilogue...[/bold blue]") # Progress message
assistant = create_or_get_assistant(book_name, book_name)
assistant = create_or_get_assistant(book_name)
thread = get_thread()

# Prepare the epilogue file path
Expand Down Expand Up @@ -142,4 +145,5 @@ def generate_epilogue(book_name, prompt):
# Save the updated epilogue content to markdown
save_to_markdown(book_name, "epilogue.md", "Epilogue", epilogue_content)
console.print("[bold green]✔ Epilogue generated successfully[/bold green]") # Success message
update_agent_files(book_name, assistant)
return epilogue_content
14 changes: 9 additions & 5 deletions storycraftr/outline.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from storycraftr.agents import create_or_get_assistant, get_thread, create_message
from storycraftr.agents import create_or_get_assistant, get_thread, create_message, update_agent_files
from storycraftr.core import get_config, file_has_more_than_three_lines
from rich.console import Console

Expand All @@ -20,7 +20,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
assistant = create_or_get_assistant(book_name, book_name)
assistant = create_or_get_assistant(book_name)
thread = get_thread()

# File path for the general outline
Expand Down Expand Up @@ -48,14 +48,15 @@ def generate_general_outline(book_name, prompt):
# Save to markdown
save_to_markdown(book_name, "general_outline.md", "General Outline", general_outline_content)
console.print("[bold green]✔ General outline generated successfully[/bold green]") # Success message
update_agent_files(book_name, assistant)
return general_outline_content

# Function to generate the character summary of the book
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
assistant = create_or_get_assistant(book_name, book_name)
assistant = create_or_get_assistant(book_name)
thread = get_thread()

# File path for the character summary
Expand Down Expand Up @@ -83,14 +84,15 @@ def generate_character_summary(book_name, prompt):
# Save to markdown
save_to_markdown(book_name, "character_summary.md", "Character Summary", character_summary_content)
console.print("[bold green]✔ Character summary generated successfully[/bold green]") # Success message
update_agent_files(book_name, assistant)
return character_summary_content

# Function to generate the main plot points of the book
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
assistant = create_or_get_assistant(book_name, book_name)
assistant = create_or_get_assistant(book_name)
thread = get_thread()

# File path for the plot points
Expand Down Expand Up @@ -118,14 +120,15 @@ def generate_plot_points(book_name, prompt):
# Save to markdown
save_to_markdown(book_name, "plot_points.md", "Main Plot Points", plot_points_content)
console.print("[bold green]✔ Main plot points generated successfully[/bold green]") # Success message
update_agent_files(book_name, assistant)
return plot_points_content

# Function to generate the chapter-by-chapter synopsis of the book
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
assistant = create_or_get_assistant(book_name, book_name)
assistant = create_or_get_assistant(book_name)
thread = get_thread()

# File path for the chapter synopsis
Expand Down Expand Up @@ -153,4 +156,5 @@ def generate_chapter_synopsis(book_name, prompt):
# Save to markdown
save_to_markdown(book_name, "chapter_synopsis.md", "Chapter Synopsis", chapter_synopsis_content)
console.print("[bold green]✔ Chapter-by-chapter synopsis generated successfully[/bold green]") # Success message
update_agent_files(book_name, assistant)
return chapter_synopsis_content
119 changes: 119 additions & 0 deletions storycraftr/retrieval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
from storycraftr.agents import create_message, get_thread
from rich.console import Console
from rich.progress import Progress

console = Console()

def summarize_content(assistant, original_prompt):
"""
Summarizes the prompt or content to reduce input size, generating a new thread for the summary step.
"""
thread = get_thread() # Generar un nuevo thread
content = f"Summarize the following prompt to make it more concise:\n\nPrompt:\n{original_prompt}"

# Log de inicio
console.print("[cyan]Summarizing the prompt...[/cyan]")

# Enviar el mensaje a través del asistente y obtener la respuesta
summary_response = create_message(
thread_id=thread.id,
content=content,
assistant=assistant
)

if summary_response:
console.print("[green]Summary completed successfully.[/green]")
return summary_response
else:
console.print("[red]Error summarizing prompt.[/red]")
return ""

def optimize_query_with_summary(assistant, summarized_prompt):
"""
Uses the summarized prompt to optimize it for a better response, generating a new thread for optimization.
"""
thread = get_thread() # Generar un nuevo thread
content = f"Using the following summarized prompt, optimize it for the best result:\n\nSummarized Prompt: {summarized_prompt}"

# Log de inicio
console.print("[cyan]Optimizing the summarized prompt...[/cyan]")

# Enviar el mensaje a través del asistente
optimized_response = create_message(
thread_id=thread.id,
content=content,
assistant=assistant
)

if optimized_response:
console.print("[green]Optimization completed successfully.[/green]")
return optimized_response
else:
console.print("[red]Error optimizing the query.[/red]")
return ""

def final_query(assistant, optimized_prompt):
"""
Executes the final query with the optimized prompt, generating a new thread for the final query.
"""
thread = get_thread() # Generar un nuevo thread
content = f"Answer the following query:\n\nQuery: {optimized_prompt}"

# Log de inicio
console.print("[cyan]Executing the final query...[/cyan]")

final_response = create_message(
thread_id=thread.id,
content=content,
assistant=assistant
)

if final_response:
console.print("[green]Final query executed successfully.[/green]")
return final_response
else:
console.print("[red]Error in the final query.[/red]")
return ""

def handle_failed_prompt(assistant, original_prompt):
"""
The complete process to optimize a failed prompt using heuristic steps. Each step generates its own thread.
1. Summarize the original prompt.
2. Optimize the summarized prompt.
3. Execute the final query using the optimized prompt.
"""
with Progress() as progress:
task = progress.add_task("[yellow]Processing failed prompt...", total=3)

# Step 1: Summarize the original prompt
console.print("Step 1: Summarizing the original prompt...")
summarized_prompt = summarize_content(assistant, original_prompt)
progress.update(task, advance=1)

# Step 2: Optimize the summarized prompt
console.print("Step 2: Optimizing the summarized prompt...")
optimized_prompt = optimize_query_with_summary(assistant, summarized_prompt)
progress.update(task, advance=1)

# Step 3: Execute the final query
console.print("Step 3: Executing the final query...")
final_response = final_query(assistant, optimized_prompt)
progress.update(task, advance=1)

return final_response

# Ejemplo de uso
if __name__ == "__main__":
from storycraftr.agents import create_or_get_assistant

assistant = create_or_get_assistant() # Obtener o crear el asistente

original_prompt = "What are the key points of this content related to X?"

result = handle_failed_prompt(assistant, original_prompt)

if result:
console.print(f"[green]Final response received successfully![/green]")
print(result)
else:
console.print(f"[red]Failed to process the prompt.[/red]")
Loading

0 comments on commit eaf97c4

Please sign in to comment.