### Launch Parallel Batch Processes Source: https://context7.com/thu-keg/storywriter/llms.txt Launches 127 parallel batch processes using subprocess.Popen. Ensures all started processes are waited upon to complete before proceeding. ```python processes = [] for i in range(127): process = subprocess.Popen(["python", "./load_premise.py", str(i)]) processes.append(process) print(f"Batch {i} started") # Wait for all batches to complete for process in processes: process.wait() print("All batches completed") ``` -------------------------------- ### Batch Process Story Premises Source: https://context7.com/thu-keg/storywriter/llms.txt Reads premises from a JSONL file and orchestrates the generation pipeline with checkpointing and error tracking. ```python import json import os import sys import agent_try # Load premises from JSONL file # Each line: {"id": "unique-id", "premise": "Story premise text..."} with open("premises.jsonl", "r", encoding="utf-8") as file: data = [json.loads(line) for line in file] output_dir = f"./output/{sys.argv[1]}" # Pass batch identifier as argument num = 0 for record in data: story_id = record["id"] premise = record["premise"] num += 1 # Check for existing events (resume capability) events_path = f"./output/final_events/final_events_{story_id}.json" if os.path.exists(events_path): with open(events_path, "r", encoding="utf-8") as f: message = json.load(f) else: # Generate new events message = agent_try.event_generate(premise, story_id, output_dir) if message == "None": with open(f"{output_dir}/failed_attempts.txt", "a") as f: f.write(f"event_id: {story_id} error\n") continue # Extract structured events events = agent_try.event_extract(message, story_id, output_dir) # Skip if story already exists if os.path.exists(f"./output/final_story/story{story_id}.json"): continue # Generate full story result = agent_try.story_generate(events, story_id, output_dir) if result == "stop": with open(f"{output_dir}/failed_attempts.txt", "a") as f: f.write(f"story_id: {story_id} error\n") continue print(f"{num}th story completed") ``` -------------------------------- ### Generate Story Event Outlines with event_generate Source: https://context7.com/thu-keg/storywriter/llms.txt Use this function to create structured event outlines from a story premise. It involves an event generator and reviewer. Results are saved to JSON files. ```python import agent_try # Generate event outlines from a story premise premise = """A young archaeologist discovers an ancient map leading to a lost civilization. As she follows the clues, she realizes powerful organizations are also hunting for the same treasure, and the map holds secrets that could change human history.""" story_id = "adventure-001" output_prefix = "./output/batch1" # Generate events - returns list of event messages or "None" on failure events_message = agent_try.event_generate(premise, story_id, output_prefix) # Output saved to: ./output/batch1/final_events/final_events_adventure-001.json # Returns: List of event outlines like: # [ # "Event 1: The Discovery\nSetting: Ancient library in Cairo\nCharacter: Dr. Maya Chen...", # "Event 2: The Chase Begins\nSetting: Streets of Istanbul\nConflict: Rival treasure hunters..." # ] if events_message == "None": print("Event generation failed after 5 attempts") else: print(f"Generated {len(events_message)} event entries") ``` -------------------------------- ### Generate Complete Long-Form Stories with story_generate Source: https://context7.com/thu-keg/storywriter/llms.txt Orchestrates multiple agents to produce a complete long-form story from event outlines. It includes sub-event division, chapter distribution, and writing with critic feedback. ```python import agent_try # Generate complete story from event outlines event_outlines = """Event 1: The Map Discovery Setting: Underground vault beneath the British Museum Character: Dr. Maya Chen, archaeologist Action: Deciphers ancient symbols revealing map coordinates Conflict: Security systems activate, trapping her inside Plot Twist: The map shows a location that shouldn't exist Event 2: The Chase Begins Setting: Streets of Istanbul Character: Maya with new ally Marcus Action: Fleeing from corporate mercenaries Conflict: Must reach contact before enemies intercept Plot Twist: The contact is Maya's estranged father""" story_id = "adventure-001" output_prefix = "./output/batch1" ``` -------------------------------- ### Analyze Story Lengths with NLTK and Pandas Source: https://context7.com/thu-keg/storywriter/llms.txt Analyzes token counts across generated stories in a specified folder. It uses NLTK for tokenization and pandas for statistical aggregation. Stories below a minimum length threshold are flagged. ```python import os import json import pandas as pd from nltk.tokenize import word_tokenize def analyze_story_lengths(folder_path, min_length=2000): """Analyze token counts across generated stories.""" lengths = [] for filename in os.listdir(folder_path): if filename.endswith('.json'): file_path = os.path.join(folder_path, filename) with open(file_path, 'r', encoding='utf-8') as file: data = json.load(file) tokens = word_tokenize(data['text']) token_count = len(tokens) if token_count < min_length: print(f"Short story: {filename} ({token_count} tokens)") else: lengths.append(token_count) return lengths # Analyze generated stories lengths = analyze_story_lengths('./after_pro', min_length=2000) series = pd.Series(lengths) print(f"Mean: {series.mean():.0f} tokens") print(f"Median: {series.median():.0f} tokens") print(f"Std Dev: {series.std():.0f} tokens") print(f"Range: {series.min():.0f} - {series.max():.0f} tokens") ``` -------------------------------- ### Generate Story with AgentTry Source: https://context7.com/thu-keg/storywriter/llms.txt Executes the story generation process using the agent_try module and handles completion status. ```python result = agent_try.story_generate(event_outlines, story_id, output_prefix) # Outputs saved to: # - ./output/batch1/final_outlines/storyadventure-001.json (chapter structure) # - ./output/batch1/final_story/storyadventure-001.json (complete story) if result == "finish": print("Story generation completed successfully") elif result == "stop": print("Story generation failed after 5 attempts") ``` -------------------------------- ### event_generate - Generate Story Event Outlines Source: https://context7.com/thu-keg/storywriter/llms.txt Generates structured event outlines from a story premise using collaborative AI agents. The output is saved to JSON files. ```APIDOC ## POST /api/storywriter/event_generate ### Description Generates structured event outlines from a story premise using two collaborative agents: an event generator and an event reviewer. The event model produces outlines containing settings, characters, actions, conflicts, and plot twists, while the reviewer validates coherence and requests regeneration if needed. Results are saved to JSON files for downstream processing. ### Method POST ### Endpoint /api/storywriter/event_generate ### Parameters #### Request Body - **premise** (string) - Required - The initial story premise. - **story_id** (string) - Required - A unique identifier for the story. - **output_prefix** (string) - Required - The prefix for the output file path. ### Request Example ```json { "premise": "A young archaeologist discovers an ancient map leading to a lost civilization. As she follows the clues, she realizes powerful organizations are also hunting for the same treasure, and the map holds secrets that could change human history.", "story_id": "adventure-001", "output_prefix": "./output/batch1" } ``` ### Response #### Success Response (200) - **events_message** (list) - A list of generated event outlines or "None" on failure. #### Response Example ```json [ "Event 1: The Discovery\nSetting: Ancient library in Cairo\nCharacter: Dr. Maya Chen...", "Event 2: The Chase Begins\nSetting: Streets of Istanbul\nConflict: Rival treasure hunters..." ] ``` ``` -------------------------------- ### story_generate - Generate Complete Long-Form Stories Source: https://context7.com/thu-keg/storywriter/llms.txt Orchestrates multiple specialized agents to produce a complete long-form story from event outlines, including sub-event division, chapter distribution, and writing with critic feedback. ```APIDOC ## POST /api/storywriter/story_generate ### Description Orchestrates multiple specialized agents to produce a complete long-form story from event outlines. It employs a multi-stage pipeline: sub-event division (breaking events into 3 sub-events each), chapter distribution (organizing sub-events across chapters for narrative flow), and story writing with real-time critic feedback to prevent deviation from the outline. ### Method POST ### Endpoint /api/storywriter/story_generate ### Parameters #### Request Body - **event_outlines** (string) - Required - The structured event outlines for the story. - **story_id** (string) - Required - A unique identifier for the story. - **output_prefix** (string) - Required - The prefix for the output file path. ### Request Example ```json { "event_outlines": "Event 1: The Map Discovery\nSetting: Underground vault beneath the British Museum\nCharacter: Dr. Maya Chen, archaeologist\nAction: Deciphers ancient symbols revealing map coordinates\nConflict: Security systems activate, trapping her inside\nPlot Twist: The map shows a location that shouldn't exist\n\nEvent 2: The Chase Begins\nSetting: Streets of Istanbul\nCharacter: Maya with new ally Marcus\nAction: Fleeing from corporate mercenaries\nConflict: Must reach contact before enemies intercept\nPlot Twist: The contact is Maya's estranged father", "story_id": "adventure-001", "output_prefix": "./output/batch1" } ``` ### Response #### Success Response (200) - **story_content** (string) - The generated long-form story content. #### Response Example ```json "Once upon a time..." ``` ``` -------------------------------- ### Execute Parallel Batch Processing Source: https://context7.com/thu-keg/storywriter/llms.txt Spawns subprocesses to run batch generation concurrently. ```python import subprocess ``` -------------------------------- ### Implement MessageRedact for AutoGen Source: https://context7.com/thu-keg/storywriter/llms.txt Compresses conversation history to manage context window limits by condensing messages longer than 150 characters. ```python from autogen.agentchat.contrib.capabilities import transform_messages from typing import Dict, List import copy class MessageRedact: def __init__(self): self.condensed_cache = {} def apply_transform(self, messages: List[Dict]) -> List[Dict]: """Compress older messages in conversation history.""" temp_messages = copy.deepcopy(messages) # Preserve first and last 2 messages, compress middle if len(temp_messages) > 2: for message in temp_messages[1:-2]: message_id = hash(message["content"]) if len(message["content"]) > 150: if message_id in self.condensed_cache: message["content"] = self.condensed_cache[message_id] else: # Call LLM to condense story segment condensed = compress_with_llm(message["content"]) self.condensed_cache[message_id] = condensed message["content"] = condensed return temp_messages def get_logs(self, pre_transform_messages, post_transform_messages): """Report if transformation was applied.""" for i in range(len(pre_transform_messages)): if pre_transform_messages[i]["content"] != post_transform_messages[i]["content"]: return "Message compressed", True return "", False # Apply to story writer agent redact_handler = transform_messages.TransformMessages(transforms=[MessageRedact()]) redact_handler.add_to_agent(story_writer_agent) ``` -------------------------------- ### Extract and Structure Events with event_extract Source: https://context7.com/thu-keg/storywriter/llms.txt Parses raw event text data and extracts individual events into a structured JSON format using regex. Saves processed data for story generation. ```python import agent_try # Extract structured events from raw generation output raw_events = [ """Event 1: The Map Discovery Setting: Underground vault beneath the British Museum Character: Dr. Maya Chen, archaeologist Action: Deciphers ancient symbols revealing map coordinates Conflict: Security systems activate, trapping her inside Plot Twist: The map shows a location that shouldn't exist Event 2: Unexpected Alliance Setting: Safe house in London Character: Marcus Webb, former MI6 agent Action: Offers protection in exchange for partnership Conflict: Maya must trust a stranger with her discovery Plot Twist: Marcus has personal connection to the lost civilization""" ] story_id = "adventure-001" output_prefix = "./output/batch1" # Extract and format events # Saves structured JSON to: ./output/batch1/final_process/events_adventure-001.json event_string = agent_try.event_extract(raw_events, story_id, output_prefix) # Returns formatted string for story generation: # "Event 1: The Map Discovery\nSetting: Underground vault...\n\nEvent 2: Unexpected Alliance..." print(event_string) ``` -------------------------------- ### event_extract - Extract and Structure Events Source: https://context7.com/thu-keg/storywriter/llms.txt Parses raw event text data and extracts individual events into a structured JSON format. The processed data is saved for the story generation phase. ```APIDOC ## POST /api/storywriter/event_extract ### Description Parses raw event text data generated by the outline agent and extracts individual events into a structured JSON format. It uses regex pattern matching to identify events and their components, then saves the processed data for the story generation phase. ### Method POST ### Endpoint /api/storywriter/event_extract ### Parameters #### Request Body - **raw_events** (list of strings) - Required - A list of raw event text strings. - **story_id** (string) - Required - A unique identifier for the story. - **output_prefix** (string) - Required - The prefix for the output file path. ### Request Example ```json { "raw_events": [ "Event 1: The Map Discovery\nSetting: Underground vault beneath the British Museum\nCharacter: Dr. Maya Chen, archaeologist\nAction: Deciphers ancient symbols revealing map coordinates\nConflict: Security systems activate, trapping her inside\nPlot Twist: The map shows a location that shouldn't exist", "Event 2: Unexpected Alliance\nSetting: Safe house in London\nCharacter: Marcus Webb, former MI6 agent\nAction: Offers protection in exchange for partnership\nConflict: Maya must trust a stranger with her discovery\nPlot Twist: Marcus has personal connection to the lost civilization" ], "story_id": "adventure-001", "output_prefix": "./output/batch1" } ``` ### Response #### Success Response (200) - **event_string** (string) - A formatted string of extracted events for story generation. #### Response Example ```json "Event 1: The Map Discovery\nSetting: Underground vault...\n\nEvent 2: Unexpected Alliance..." ``` ``` -------------------------------- ### Count Completed Stories Across Batches Source: https://context7.com/thu-keg/storywriter/llms.txt Counts the total number of completed story files across all batch output folders. It checks for the existence of a 'final_story' directory within each batch output. ```python import os def count_completed_stories(num_batches=128): """Count total completed stories across all batch folders.""" total_files = 0 for i in range(num_batches): final_story_path = os.path.join(f"./output/{i}", 'final_story') if os.path.exists(final_story_path) and os.path.isdir(final_story_path): for root, dirs, files in os.walk(final_story_path): total_files += len(files) return total_files total = count_completed_stories() print(f"Total completed stories: {total}") # Expected output structure: # ./output/0/final_story/story.json # ./output/1/final_story/story.json # ... # ./output/127/final_story/story.json ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.