A prompt engineering toolkit for structured LLM prompt construction and format conversion.
pip install crocopromptfrom crocoprompt import ZeroShotPrompt, MarkdownConverter
from crocoprompt.construct.base import PromptSection
prompt = ZeroShotPrompt(
instructions=PromptSection(content="Summarise the following article in three bullet points.")
)
print(MarkdownConverter.convert(prompt))
# # Instructions
# Summarise the following article in three bullet points.The fundamental building block of all prompts. Represents a single section with optional variable substitution and wrapping:
from crocoprompt.construct.base import PromptSection
section = PromptSection(
content="Hello {name}",
variables={"name": "World"},
prefix="Greeting:",
)
print(section.render()) # Greeting:\nHello WorldA prompt composed of named PromptSection objects, compiled by joining sections with double newlines:
from crocoprompt.construct.base import PromptSection, SectionPrompt
prompt = SectionPrompt(
role=PromptSection(content="You are a helpful assistant."),
task=PromptSection(content="Translate to Spanish."),
)
print(prompt.compile())Plain instructions without examples.
| Class | Description |
|---|---|
ZeroShotPrompt |
Plain instructions only |
ZeroShotRolePrompt |
Instructions + role prefix |
ZeroShotEmotionPrompt |
Instructions + emotion suffix |
from crocoprompt import ZeroShotRolePrompt
from crocoprompt.construct.base import PromptSection
prompt = ZeroShotRolePrompt(
instructions=PromptSection(content="Translate to French."),
role="Expert Translator",
)
print(prompt.compile())
# Role: Expert Translator
#
# Translate to French.Instructions with labelled examples to demonstrate the expected pattern:
from crocoprompt import FewShotPrompt
from crocoprompt.construct.base import PromptSection, Example
prompt = FewShotPrompt(
instructions=PromptSection(content="Classify the sentiment."),
examples=[
Example(name="pos", content="Input: I love it! Output: Positive"),
Example(name="neg", content="Input: Terrible. Output: Negative"),
],
)
print(prompt.compile())Explicit step-by-step reasoning section to encourage better outputs:
from crocoprompt import ChainOfThoughtPrompt
from crocoprompt.construct.base import PromptSection
prompt = ChainOfThoughtPrompt(
instructions=PromptSection(content="What is 7 * 8?"),
thinking=PromptSection(content="Let's think step by step."),
)
print(prompt.compile())Chain-of-Thought augmented with a partial answer scaffold:
from crocoprompt import CueChainOfThoughtPrompt
from crocoprompt.construct.base import PromptSection
prompt = CueChainOfThoughtPrompt(
instructions=PromptSection(content="Solve this."),
thinking=PromptSection(content="Let's think step by step."),
cue=PromptSection(content="First, I notice that"),
)
print(prompt.compile())Instructions grounded by structured knowledge triplets:
from crocoprompt import ChainOfKnowledge, Triplet
from crocoprompt.construct.base import PromptSection
prompt = ChainOfKnowledge(
instructions=PromptSection(content="Answer the question."),
knowledge_triplets=[
Triplet(items=["Paris", "is capital of", "France"]),
Triplet(items=["France", "is in", "Europe"]),
],
explanation=PromptSection(content="Use the above facts."),
)
print(prompt.compile())Convert compiled prompts to different formats for various platforms.
| Converter | Output Format |
|---|---|
MarkdownConverter |
Markdown with headers (# Section) |
XMLConverter |
XML tags (<section>) |
YAMLConverter |
YAML block scalars (`section: |
from crocoprompt import (
SectionPrompt,
MarkdownConverter,
XMLConverter,
YAMLConverter,
)
from crocoprompt.construct.base import PromptSection
prompt = SectionPrompt(
role=PromptSection(content="You are a helpful assistant."),
task=PromptSection(content="Translate the text to Spanish."),
)
# Markdown output
print(MarkdownConverter.convert(prompt))
# # Role
# You are a helpful assistant.
#
# # Task
# Translate the text to Spanish.
# XML output
print(XMLConverter.convert(prompt))
# <role>
# You are a helpful assistant.
# </role>
#
# <task>
# Translate the text to Spanish.
# </task>
# YAML output
print(YAMLConverter.convert(prompt))
# role: |
# You are a helpful assistant.
#
# task: |
# Translate the text to Spanish.Control the order of sections in the output:
prompt = SectionPrompt(
a=PromptSection(content="A"),
b=PromptSection(content="B"),
)
# Default insertion order
print(prompt.compile()) # A\n\nB
# Custom order
print(prompt.compile(order=["b", "a"])) # B\n\nA
# Works with converters too
print(XMLConverter.convert(prompt, order=["b", "a"]))from crocoprompt import FewShotPrompt, MarkdownConverter
from crocoprompt.construct.base import PromptSection, Example
sentiment_prompt = FewShotPrompt(
instructions=PromptSection(
content="Classify the sentiment of the following text.",
),
examples=[
Example(
name="positive",
content="Input: I absolutely love this product!\nOutput: Positive",
),
Example(
name="negative",
content="Input: This is the worst experience ever.\nOutput: Negative",
),
Example(
name="neutral",
content="Input: The weather is cloudy today.\nOutput: Neutral",
),
],
)
print(MarkdownConverter.convert(sentiment_prompt))from crocoprompt import (
ChainOfKnowledge,
CueChainOfThoughtPrompt,
XMLConverter,
)
from crocoprompt.construct.base import PromptSection, Triplet
reasoning_prompt = ChainOfKnowledge(
instructions=PromptSection(
content="Based on the facts provided, answer: Is Paris the capital of France?",
),
knowledge_triplets=[
Triplet(items=["Paris", "is capital of", "France"]),
Triplet(items=["France", "is in", "Europe"]),
],
explanation=PromptSection(
content="Use the knowledge above to construct your answer.",
),
)
print(XMLConverter.convert(reasoning_prompt))PromptSection: A single prompt section with content, variables, prefix, and suffixExample: A named example used in few-shot learningSectionPrompt: A prompt composed of named sectionsTriplet: A knowledge triplet (subject, predicate, object)
ZeroShotPrompt: Plain zero-shotZeroShotRolePrompt: Zero-shot with role contextZeroShotEmotionPrompt: Zero-shot with emotional framingFewShotPrompt: Few-shot with examplesChainOfThoughtPrompt: Chain-of-Thought reasoningCueChainOfThoughtPrompt: CoT with answer cueChainOfKnowledge: Knowledge-grounded reasoning
MarkdownConverter: Convert to Markdown formatXMLConverter: Convert to XML formatYAMLConverter: Convert to YAML format
Contributions are welcome! Please feel free to submit pull requests or open issues on GitHub.
MIT License — see LICENSE for details.