Skip to content

Prompt Templates

ByteBuddy allows you to customize prompt templates for different model roles using the promptTemplates configuration. This feature enables you to tailor AI behavior for specific tasks like editing, applying changes, and autocompletion.

Configuration Overview

Prompt templates are configured within model definitions in your config.yaml file:

yaml
models:
  - name: "gpt-4"
    provider: "openai"
    model: "gpt-4"
    apiKey: "${OPENAI_API_KEY}"
    roles:
      - chat
      - edit
      - apply
      - autocomplete
    promptTemplates:
      edit: |
        Here is the code before editing:
        \${original_code}

        Please edit according to the following instructions: \${instructions}

        Provide only the edited code, without explanation.
      apply: |
        Original code: \${original_code}
        New code: \${new_code}
      autocomplete: |
        Complete the following code:
        \${prefix}

Available Template Types

1. Edit Template (promptTemplates.edit)

Used when editing code with the /edit command or through the edit interface.

Available Variables:

  • \${original_code}: The original code to be edited
  • \${instructions}: The user's edit instructions
  • \${filepath}: The file path being edited
  • \${language}: The programming language of the file

Default Template:

yaml
promptTemplates:
  edit: |
    Here is the code before editing:
    \${original_code}

    Please edit according to the following instructions: \${instructions}

    Provide only the edited code, without explanation.

Custom Example:

yaml
models:
  - name: "gpt-4"
    provider: "openai"
    model: "gpt-4"
    promptTemplates:
      edit: |
        You are a professional software engineer. Edit the following code according to the user's instructions.

        File: \${filepath}
        Language: \${language}

        Original code:
        ```\${language}
        \${original_code}
        ```

        Instructions: \${instructions}

        Requirements:
        1. Maintain code quality and best practices
        2. Add appropriate comments for complex changes
        3. Ensure backward compatibility

        Provide only the edited code.

2. Apply Template (promptTemplates.apply)

Used when applying AI-generated code changes.

Available Variables:

  • \${original_code}: The original code
  • \${new_code}: The new code to apply
  • \${filepath}: The file path

Default Template:

yaml
promptTemplates:
  apply: |
    Original code: \${original_code}
    New code: \${new_code}

Custom Example:

yaml
models:
  - name: "gpt-4"
    provider: "openai"
    model: "gpt-4"
    promptTemplates:
      apply: |
        <code>\${original_code}</code>
        <update>\${new_code}</update>

3. Autocomplete Template (promptTemplates.autocomplete)

Used for code autocompletion suggestions.

Available Variables:

  • \${prefix}: Code prefix before the cursor
  • \${suffix}: Code suffix after the cursor (if available)
  • \${filepath}: The file path
  • \${language}: The programming language

Default Template:

yaml
promptTemplates:
  autocomplete: |
    Complete the following code:
    \${prefix}

Custom Example:

yaml
models:
  - name: "gpt-4"
    provider: "openai"
    model: "gpt-4"
    promptTemplates:
      autocomplete: |
        You are a code completion assistant. Complete the following code snippet.

        File: \${filepath}
        Language: \${language}

        Current code:
        ```\${language}
        \${prefix}
        ```

        Provide only the completion part, without explanation.

4. Chat Template (promptTemplates.chat)

Used for general chat conversations.

Available Variables:

  • \${messages}: Conversation history
  • \${system_message}: System message (if configured)

Custom Example:

yaml
models:
  - name: "gpt-4"
    provider: "openai"
    model: "gpt-4"
    promptTemplates:
      chat: |
        You are ByteBuddy, an AI programming assistant. Help users with their coding tasks.

        Conversation history:
        \${messages}

        Current request: \${current_message}

        Provide helpful, accurate, and concise responses.

Template Syntax

ByteBuddy uses Handlebars syntax for prompt templating, supporting basic variable substitution.

Basic Variables

yaml
promptTemplates:
  edit: |
    Hello, \${name}!

Variable Substitution

Supported variables in templates include:

  • \${original_code}: Original code
  • \${instructions}: Edit instructions
  • \${filepath}: File path
  • \${language}: Programming language
  • \${new_code}: New code
  • \${prefix}: Code prefix
  • \${suffix}: Code suffix
  • \${messages}: Conversation history

Advanced Configuration

Model-Specific Templates

Different models can have different prompt templates:

yaml
models:
  - name: "gpt-4-editor"
    provider: "openai"
    model: "gpt-4"
    apiKey: "${OPENAI_API_KEY}"
    roles:
      - edit
    promptTemplates:
      edit: |
        [GPT-4 specific edit template]
        \${original_code}
        Instructions: \${instructions}

  - name: "claude-3-applier"
    provider: "anthropic"
    model: "claude-3-sonnet"
    apiKey: "${ANTHROPIC_API_KEY}"
    roles:
      - apply
    promptTemplates:
      apply: |
        <human>Apply this change:</human>
        <original>\${original_code}</original>
        <new>\${new_code}</new>

Environment Variables in Templates

You can use environment variables in templates:

yaml
models:
  - name: "gpt-4"
    provider: "openai"
    model: "gpt-4"
    promptTemplates:
      edit: |
        Project: ${PROJECT_NAME}
        Environment: ${ENVIRONMENT}

        Code: \${original_code}
        Instructions: \${instructions}

Use Cases

1. Code Review Template

yaml
models:
  - name: "gpt-4"
    provider: "openai"
    model: "gpt-4"
    promptTemplates:
      edit: |
        You are a senior code reviewer. Analyze the following code:

        File: \${filepath}
        Language: \${language}

        Code to review:
        ```\${language}
        \${original_code}
        ```

        Review criteria:
        1. Code quality and readability
        2. Security vulnerabilities
        3. Performance issues
        4. Best practices compliance
        5. Error handling

        Provide specific, actionable feedback.

2. Documentation Generator

yaml
models:
  - name: "gpt-4"
    provider: "openai"
    model: "gpt-4"
    promptTemplates:
      edit: |
        Generate documentation for this \${language} code:

        ```\${language}
        \${original_code}
        ```

        Include:
        1. Function/class description
        2. Parameters and return values
        3. Usage examples
        4. Edge cases and considerations

        Use Markdown format.

3. Test Generator

yaml
models:
  - name: "gpt-4"
    provider: "openai"
    model: "gpt-4"
    promptTemplates:
      edit: |
        Generate tests for this \${language} code:

        ```\${language}
        \${original_code}
        ```

        Requirements:
        1. Cover all public methods
        2. Include edge cases
        3. Follow test framework conventions
        4. Add appropriate assertions

        Provide only test code.

4. Refactoring Assistant

yaml
models:
  - name: "gpt-4"
    provider: "openai"
    model: "gpt-4"
    promptTemplates:
      edit: |
        Refactor this \${language} code to improve:

        1. Readability
        2. Maintainability
        3. Performance

        Original code:
        ```\${language}
        \${original_code}
        ```

        Refactoring goals: \${instructions}

        Provide the refactored code and explain the changes.

Best Practices

1. Keep Templates Concise

yaml
# ✅ Good
models:
  - name: "gpt-4"
    promptTemplates:
      edit: |
        Edit: \${original_code}
        Instructions: \${instructions}

# ❌ Avoid
models:
  - name: "gpt-4"
    promptTemplates:
      edit: |
        You are an AI assistant... [very long system message]
        The user wants you to... [redundant instructions]
        Please... [multiple requests]
        Original: \${original_code}
        Instructions: \${instructions}

2. Use Appropriate Variables

yaml
# ✅ Good - Use available variables
models:
  - name: "gpt-4"
    promptTemplates:
      edit: |
        File: \${filepath}
        Language: \${language}
        Code: \${original_code}
        Instructions: \${instructions}

# ❌ Avoid - Use non-existent variables
models:
  - name: "gpt-4"
    promptTemplates:
      edit: |
        User: \${username}  # This variable doesn't exist
        Time: \${timestamp} # This variable doesn't exist
        Code: \${original_code}

3. Maintain Consistency

yaml
# ✅ Good - Consistent format
models:
  - name: "gpt-4"
    promptTemplates:
      edit: |
        [Edit]
        File: \${filepath}
        Code: \${original_code}
        Instructions: \${instructions}
      apply: |
        [Apply]
        Original: \${original_code}
        New: \${new_code}

# ❌ Avoid - Inconsistent format
models:
  - name: "gpt-4"
    promptTemplates:
      edit: |
        Edit Mode
        =========
        Code: \${original_code}
        Instructions: \${instructions}
      apply: |
        Please apply this change:
        - Old: \${original_code}
        - New: \${new_code}

4. Test Your Templates

After configuring templates in config.yaml, test template effectiveness through actual usage:

  1. Try editing functionality in chat
  2. Check if AI responses meet expectations
  3. Adjust templates as needed

Troubleshooting

Common Issues

  1. Template Not Working

    • Check if variable names match available variables
    • Verify YAML syntax is correct
    • Ensure templates are assigned to the correct model
  2. Unexpected AI Responses

    • Template might be too verbose or confusing
    • Instructions might be unclear
    • Try simplifying the template
  3. Performance Issues

    • Very long templates increase token usage
    • Consider optimizing template length

Debugging Tips

  1. Check Configuration File Verify the syntax and structure of your config.yaml file.

  2. Start Simple Begin with simple templates and gradually increase complexity.

  3. Check Model Compatibility Ensure templates are suitable for the selected model's prompt format.

Complete Configuration Example

yaml
models:
  - name: "gpt-4-editor"
    provider: "openai"
    model: "gpt-4"
    apiKey: "${OPENAI_API_KEY}"
    roles:
      - edit
      - apply
      - autocomplete
    promptTemplates:
      edit: |
        You are a professional software engineer. Please edit the code according to the following instructions:

        File: \${filepath}
        Language: \${language}

        Original code:
        ```\${language}
        \${original_code}
        ```

        Instructions: \${instructions}

        Requirements:
        1. Maintain code quality
        2. Follow best practices
        3. Maintain existing code style

        Provide only the edited code.

      apply: |
        Apply code changes:
        Original code: \${original_code}
        New code: \${new_code}

      autocomplete: |
        Complete the following code:

        ```\${language}
        \${prefix}
        ```

        File: \${filepath}
        Provide only the completion, without explanation.

  - name: "claude-3-autocomplete"
    provider: "anthropic"
    model: "claude-3-haiku"
    apiKey: "${ANTHROPIC_API_KEY}"
    roles:
      - autocomplete
    promptTemplates:
      autocomplete: |
        Complete the code snippet:

        ```\${language}
        \${prefix}
        ```

        Consider file context: \${filepath}
        Provide only code completion part.

context:
  - provider: "codebase"
  - provider: "diff"

rules:
  - "Always include appropriate error handling"
  - "Follow existing code style"

Next Steps

Custom prompt templates enable you to tailor ByteBuddy's behavior to your specific workflows and requirements, improving the quality and relevance of AI assistance.