Skip to main content

Prompt Templates

Prompt templates have become key artifacts for researchers and practitioners working with AI. There is, however, no standardized way of sharing prompt templates. Prompts and prompt templates are shared on the Hugging Face Hub in .txt files, in HF datasets, as strings in model cards, or on GitHub as python strings embedded in scripts, in JSON and YAML files, or in Jinja2 files.

Objectives and non-objectives of this library

Objectives

  • Provide functionality for working with prompt templates locally and sharing them on the Hugging Face Hub.
  • Propose a prompt template standard through .yaml and .json files that enables modular development of complex LLM systems and is interoperable with other libraries

Non-Objective

  • Compete with full-featured prompting libraries like LangChain, ell, etc. The objective is, instead, a simple solution for working with prompt templates locally or on the HF Hub, which is interoperable with other libraries and which the community can build upon.

Documentation

A discussion of the standard prompt format, usage examples, the API reference etc. are available in the docs.

Quick start

Let's use this closed_system_prompts repo of official prompts from OpenAI and Anthropic. These prompt templates have either been leaked or were shared by these LLM providers, but were originally in a non-machine-readable, non-standardized format.

1. Install the library:

pip install prompt-templates

2. List available prompts in a HF Hub repository.

from prompt_templates import list_prompt_templates
files = list_prompt_templates("MoritzLaurer/closed_system_prompts")
print(files)
# ['claude-3-5-artifacts-leak-210624.yaml', 'claude-3-5-sonnet-text-090924.yaml', 'claude-3-5-sonnet-text-image-090924.yaml', 'openai-metaprompt-audio.yaml', 'openai-metaprompt-text.yaml']

3. Download and inspect a prompt template

from prompt_templates import ChatPromptTemplate
prompt_template = ChatPromptTemplate.load_from_hub(
    repo_id="MoritzLaurer/closed_system_prompts",
    filename="claude-3-5-artifacts-leak-210624.yaml"
)
# Inspect template
print(prompt_template.template)
#[{'role': 'system',
#  'content': '<artifacts_info>\nThe assistant can create and reference artifacts ...'},
# {'role': 'user', 'content': '{{user_message}}'}]
# Check required template variables
print(prompt_template.template_variables)
#['current_date', 'user_message']
print(prompt_template.metadata)
#{'source': 'https://gist.github.com/dedlim/6bf6d81f77c19e20cd40594aa09e3ecd'}

4. Populate the template with variables

By default, the populated prompt is returned in the OpenAI messages format, which is compatible with most open-source LLM clients.

messages = prompt_template.populate(
    user_message="Create a tic-tac-toe game for me in Python",
    current_date="Wednesday, 11 December 2024"
)
print(messages)  # doctest: +SKIP
#[{'role': 'system', 'content': '<artifacts_info>\nThe assistant can create and reference artifacts during conversations. Artifacts are ...'}, {'role': 'user', 'content': 'Create a tic-tac-toe game for me in Python'}]

5. Use the populated template with any LLM client

#!pip install openai
from openai import OpenAI
import os
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages
)
print(response.choices[0].message.content[:100], "...")  # doctest: +SKIP
#Here's a simple text-based Tic-Tac-Toe game in Python. This code allows two players to take turns pl ...
from huggingface_hub import InferenceClient
client = InferenceClient(api_key=os.environ.get("HF_TOKEN"))
response = client.chat.completions.create(
    model="meta-llama/Llama-3.3-70B-Instruct", 
    messages=messages,
    max_tokens=500
)
print(response.choices[0].message.content[:100], "...")  # doctest: +SKIP
#<antThinking>Creating a tic-tac-toe game in Python is a good candidate for an artifact. It's a self- ...

If you use an LLM client that expects a format different to the OpenAI messages standard, you can easily reformat the prompt for this client. For example with Anthropic:

#! pip install anthropic
from anthropic import Anthropic
from prompt_templates import format_for_client

messages_anthropic = format_for_client(messages, client="anthropic")

client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
response = client.messages.create(
    model="claude-3-sonnet-20240229",
    system=messages_anthropic["system"],
    messages=messages_anthropic["messages"],
    max_tokens=1000
)
print(response.content[0].text[:100], "...")  # doctest: +SKIP
#Sure, I can create a tic-tac-toe game for you in Python. Here's a simple implementation: ...

Or with the Google Gen AI SDK for Gemini 2.0

#!pip install google-genai
from google import genai
from google.genai import types
from prompt_templates import format_for_client

messages_google = format_for_client(messages, client="google")

client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))

response = client.models.generate_content(
    model='gemini-2.0-flash-exp',
    contents=messages_google["contents"],
    config=types.GenerateContentConfig(
        system_instruction=messages_google["system_instruction"],
    )
)
print(response.text[:100], "...")  # doctest: +SKIP

6. Create your own prompt templates

from prompt_templates import ChatPromptTemplate
messages_template = [
    {"role": "system", "content": "You are a coding assistant who explains concepts clearly and provides short examples."},
    {"role": "user", "content": "Explain what {{concept}} is in {{programming_language}}."}
]
template_variables = ["concept", "programming_language"]
metadata = {
    "name": "Code Teacher",
    "description": "A simple chat prompt for explaining programming concepts with examples",
    "tags": ["programming", "education"],
    "version": "0.0.1",
    "author": "Guido van Bossum"
}
prompt_template = ChatPromptTemplate(
    template=messages_template,
    template_variables=template_variables,
    metadata=metadata,
)

print(prompt_template)
#ChatPromptTemplate(template=[{'role': 'system', 'content': 'You are a coding a..., template_variables=['concept', 'programming_language'], metadata={'name': 'Code Teacher', 'description': 'A simple ..., client_parameters={}, custom_data={}, populator='jinja2', jinja2_security_level='standard')

7. Store or share your prompt templates

You can then store your prompt template locally or share it on the HF Hub.

# save locally
prompt_template.save_to_local("./tests/test_data/example_prompts/code_teacher_test.yaml")
# or save it on the HF Hub
prompt_template.save_to_hub(repo_id="MoritzLaurer/example_prompts_test", filename="code_teacher_test.yaml", create_repo=True)  # doctest: +SKIP
#CommitInfo(commit_url='https://huggingface.co/MoritzLaurer/example_prompts_test/commit/4cefd2c94f684f9bf419382f96b36692cd175e84', commit_message='Upload prompt template code_teacher_test.yaml', commit_description='', oid='4cefd2c94f684f9bf419382f96b36692cd175e84', pr_url=None, repo_url=RepoUrl('https://huggingface.co/MoritzLaurer/example_prompts_test', endpoint='https://huggingface.co', repo_type='dataset', repo_id='MoritzLaurer/example_prompts_test'), pr_revision=None, pr_num=None)

Metadata

Release files for prompt-templates 0.0.16

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prompt-templates 0.0.16
File Size Uploaded Provenance Actions
prompt_templates-0.0.16.tar.gz 27.5 kB Without provenance Details for prompt_templates-0.0.16.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for prompt-templates 0.0.16
File Interpreter ABI Platform Actions
prompt_templates-0.0.16-py3-none-any.whl Python 3 none any Details for prompt_templates-0.0.16-py3-none-any.whl

Total release size: 54.1 kB

Release files / prompt_templates-0.0.16.tar.gz

Download URL prompt_templates-0.0.16.tar.gz
Size 27.5 kB
Tags Source
SHA-256 checksum
How to use checksums
2f4ed5d96ce8396f2e45fcd11075c469df36ed712d11f07497d206f15f197d29
BLAKE2b-256 checksum
How to use checksums
922c8ba46f73c9abe766f40cd0bf3dfa96de63a6e0677c03e076611d7dd3cc74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.1 CPython/3.12.8 Linux/6.8.0-1020-azure

Release files / prompt_templates-0.0.16-py3-none-any.whl

Download URL prompt_templates-0.0.16-py3-none-any.whl
Size 26.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
311d5f4401246e2c21b264f4bd8399951494e836c737fc6ee56ec8e2954e8a86
BLAKE2b-256 checksum
How to use checksums
9050e37556eadd768081a351cdd14aad178505168b0cda9ed82a395d9e9d0c7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.1 CPython/3.12.8 Linux/6.8.0-1020-azure

Security for prompt-templates 0.0.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page