AI-Powered Story Creation System using LangChain Agents

Introduction DuoDraft is an innovative story creation system that leverages the power of LangChain and Large Language Models (LLMs) to create a collaborative writing environment between two AI agents: a creative writer and a critical editor. This project demonstrates how we can use modern AI technologies to create a dynamic, iterative writing process that mimics real-world author-editor relationships. Project Inspiration The inspiration for DuoDraft came from observing the traditional writing process, where authors work closely with editors to refine their work. This back-and-forth collaboration often leads to better stories, but not everyone has access to a professional editor. We wanted to create a system that could simulate this collaborative process using AI agents. The key insights that drove the project: Writing is inherently iterative Different perspectives improve story quality The tension between creativity and criticism can lead to better outcomes AI agents can take on specialized roles with different "personalities" Technical Architecture LangChain Integration We chose LangChain as our primary framework because it provides: Powerful agent abstractions Easy integration with OpenAI's models Async support for better performance Flexible prompt templates Built-in conversation management Application Structure The project is organized into several key components: duodraft/ ├── story_agents.py # Core agent logic and story creation ├── app.py # Gradio web interface └── docs/ └── blog.md # Project documentation Key Components StoryPromptTemplate Handles prompt formatting for both agents Maintains consistent context across iterations Structures the communication between agents StoryAgent Base class for both creator and editor agents Manages role-specific behaviors and responses Handles async communication with the LLM DuoDraft Orchestrates the story creation process Manages iterations between agents Tracks story progress and feedback Gradio Interface Provides a user-friendly web interface Offers real-time progress updates Supports different editor styles and genres The Story Creation Process How It Works Initial Story Creation User selects genre and provides optional prompt Creator agent generates first draft System maintains creative temperature settings Editorial Review Editor agent analyzes the story Provides specific feedback and suggestions Can either approve or request revisions Iterative Refinement Creator agent revises based on feedback Process continues for up to 3 iterations Each iteration improves story quality Agent Personalities We implemented three distinct editor styles: Strict New Yorker Editor Highly critical approach Focuses on literary quality Lower temperature setting (0.2) Supportive Writing Coach Encouraging feedback style Balanced approach to criticism Medium temperature setting (0.5) Drama Mode More theatrical feedback Entertaining interaction style Higher temperature setting (0.8) Technical Implementation Details Async Support The project uses async/await throughout to ensure responsive performance: async def create_story(self, genre: str, prompt: str = None) -> Dict[str, str]: # Async story creation logic Progress Tracking We implemented real-time progress updates using Gradio's progress API: progress_callback=lambda value, desc: progress(value, desc) Error Handling The system includes robust error handling for: API failures Invalid responses Timeout scenarios Edge cases in story generation Future Enhancements Multiple Creator Agents Different writing styles Genre specialists Character vs. plot focus Enhanced Feedback Structural analysis Style suggestions Character development tips User Customization Custom prompts Style preferences Iteration control Export Options Multiple formats (PDF, DOCX) Version history Feedback summaries Conclusion DuoDraft demonstrates the potential of AI agents in creative writing. By simulating the author-editor relationship, we've created a tool that can help writers improve their work through iterative feedback and refinement. The project showcases how LangChain and modern LLMs can be used to create practical applications that enhance creative processes. Getting Started github repo: https://github.com/r123singh/duodraft To run DuoDraft locally: Clone the repository Install dependencies: pip install -r requirements.txt Set up your OpenAI API key Run the application: python app.py Visit the Gradio interface at http://localhost:7860 to start creating stories!

Apr 9, 2025 - 05:52
 0
AI-Powered Story Creation System using LangChain Agents

Introduction

DuoDraft is an innovative story creation system that leverages the power of LangChain and Large Language Models (LLMs) to create a collaborative writing environment between two AI agents: a creative writer and a critical editor. This project demonstrates how we can use modern AI technologies to create a dynamic, iterative writing process that mimics real-world author-editor relationships.

configure

Project Inspiration

The inspiration for DuoDraft came from observing the traditional writing process, where authors work closely with editors to refine their work. This back-and-forth collaboration often leads to better stories, but not everyone has access to a professional editor. We wanted to create a system that could simulate this collaborative process using AI agents.

The key insights that drove the project:

  • Writing is inherently iterative
  • Different perspectives improve story quality
  • The tension between creativity and criticism can lead to better outcomes
  • AI agents can take on specialized roles with different "personalities"

Technical Architecture

LangChain Integration

We chose LangChain as our primary framework because it provides:

  1. Powerful agent abstractions
  2. Easy integration with OpenAI's models
  3. Async support for better performance
  4. Flexible prompt templates
  5. Built-in conversation management

Application Structure

The project is organized into several key components:

duodraft/
├── story_agents.py     # Core agent logic and story creation
├── app.py             # Gradio web interface
└── docs/
    └── blog.md        # Project documentation

Key Components

  1. StoryPromptTemplate

    • Handles prompt formatting for both agents
    • Maintains consistent context across iterations
    • Structures the communication between agents
  2. StoryAgent

    • Base class for both creator and editor agents
    • Manages role-specific behaviors and responses
    • Handles async communication with the LLM
  3. DuoDraft

    • Orchestrates the story creation process
    • Manages iterations between agents
    • Tracks story progress and feedback
  4. Gradio Interface

    • Provides a user-friendly web interface
    • Offers real-time progress updates
    • Supports different editor styles and genres

The Story Creation Process

How It Works

  1. Initial Story Creation

    • User selects genre and provides optional prompt
    • Creator agent generates first draft
    • System maintains creative temperature settings
  2. Editorial Review

    • Editor agent analyzes the story
    • Provides specific feedback and suggestions
    • Can either approve or request revisions
  3. Iterative Refinement

    • Creator agent revises based on feedback
    • Process continues for up to 3 iterations
    • Each iteration improves story quality

Agent Personalities

We implemented three distinct editor styles:

  1. Strict New Yorker Editor

    • Highly critical approach
    • Focuses on literary quality
    • Lower temperature setting (0.2)
  2. Supportive Writing Coach

    • Encouraging feedback style
    • Balanced approach to criticism
    • Medium temperature setting (0.5)
  3. Drama Mode

    • More theatrical feedback
    • Entertaining interaction style
    • Higher temperature setting (0.8)

Technical Implementation Details

Async Support

The project uses async/await throughout to ensure responsive performance:

async def create_story(self, genre: str, prompt: str = None) -> Dict[str, str]:
    # Async story creation logic

Progress Tracking

We implemented real-time progress updates using Gradio's progress API:

progress_callback=lambda value, desc: progress(value, desc)

Error Handling

The system includes robust error handling for:

  • API failures
  • Invalid responses
  • Timeout scenarios
  • Edge cases in story generation

Future Enhancements

  1. Multiple Creator Agents

    • Different writing styles
    • Genre specialists
    • Character vs. plot focus
  2. Enhanced Feedback

    • Structural analysis
    • Style suggestions
    • Character development tips
  3. User Customization

    • Custom prompts
    • Style preferences
    • Iteration control
  4. Export Options

    • Multiple formats (PDF, DOCX)
    • Version history
    • Feedback summaries

Conclusion

DuoDraft demonstrates the potential of AI agents in creative writing. By simulating the author-editor relationship, we've created a tool that can help writers improve their work through iterative feedback and refinement. The project showcases how LangChain and modern LLMs can be used to create practical applications that enhance creative processes.

Getting Started

github repo: https://github.com/r123singh/duodraft
To run DuoDraft locally:

  1. Clone the repository
  2. Install dependencies:
   pip install -r requirements.txt
  1. Set up your OpenAI API key
  2. Run the application:
   python app.py

Visit the Gradio interface at http://localhost:7860 to start creating stories!

Story