Skip to content

Add @stdlib/prompt module for LLM prompt templating#440

Open
nbeerbower wants to merge 1 commit into
mainfrom
claude/add-prompt-templating-bYV46
Open

Add @stdlib/prompt module for LLM prompt templating#440
nbeerbower wants to merge 1 commit into
mainfrom
claude/add-prompt-templating-bYV46

Conversation

@nbeerbower

Copy link
Copy Markdown
Collaborator

Summary

Adds a new @stdlib/prompt module to the standard library for building and formatting multi-turn LLM conversations. This module provides utilities for creating messages, applying chat templates, and managing conversation state.

Key Changes

  • New module: stdlib/prompt.hml - Core implementation with 244 lines

    • Message constructors: message(), system(), user(), assistant(), tool()
    • Template formatting: format_chat() and apply_template() using Jinja rendering
    • Conversation builder: conversation() object with chainable methods for incremental message construction
    • Utility functions: templates() and get_template_source()
  • Built-in chat templates (7 formats):

    • chatml - OpenAI/Qwen/Yi format
    • llama3 - Meta Llama 3/3.1 format
    • alpaca - Stanford Alpaca instruction format
    • vicuna - Vicuna/LMSys format
    • zephyr - HuggingFace Zephyr (Mistral-based)
    • gemma - Google Gemma format
    • plain - Simple text format for debugging
  • Documentation: stdlib/docs/prompt.md - Comprehensive guide with API reference, examples, and template specifications (329 lines)

  • Tests: tests/parity/modules/stdlib_prompt.hml - Full test coverage including message construction, template formatting, conversation builder, and utility functions

  • Updated documentation: CLAUDE.md - Incremented stdlib module count from 42 to 43 and added prompt module to the module list

Implementation Details

  • Leverages @stdlib/jinja for template rendering, allowing both built-in and custom Jinja templates
  • Conversation builder uses closures to maintain message state and support method chaining
  • Tool messages support optional name field for function/tool identification
  • All message constructors normalize content to strings for flexibility
  • Template validation with helpful error messages for unknown template names

https://claude.ai/code/session_01DFb8Pffjwa7EoWdiE9xbds

Provides structured utilities for building multi-turn LLM conversations
on top of the existing @stdlib/jinja template engine:

- Message constructors: system(), user(), assistant(), tool(), message()
- Built-in chat templates: chatml, llama3, alpaca, vicuna, zephyr, gemma, plain
- format_chat() for applying named templates to message arrays
- apply_template() for custom Jinja templates with extra context
- conversation() builder with chaining, filtering, and format methods
- templates() and get_template_source() for template introspection

Includes parity test (interpreter + compiler), documentation, and
CLAUDE.md updates (43 stdlib modules).

https://claude.ai/code/session_01DFb8Pffjwa7EoWdiE9xbds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants