Skip to main content

A prompt engineering toolkit for structured LLM prompt construction and format conversion

Project description

crocoprompt

A prompt engineering toolkit for structured LLM prompt construction and format conversion.

PyPI version Python 3.11+ License: MIT

Installation

pip install crocoprompt

Quick Start

from 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.

Core Concepts

PromptSection

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 World

SectionPrompt

A 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())

Prompt Strategies

Zero-Shot

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.

Few-Shot

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())

Chain-of-Thought

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())

Cue-based Chain-of-Thought

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())

Chain-of-Knowledge

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())

Output Converters

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.

Custom Section Order

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"]))

Examples

Sentiment Analysis Prompt

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))

Complex Reasoning Task

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))

API Reference

Data Structures

  • PromptSection: A single prompt section with content, variables, prefix, and suffix
  • Example: A named example used in few-shot learning
  • SectionPrompt: A prompt composed of named sections
  • Triplet: A knowledge triplet (subject, predicate, object)

Prompt Classes

  • ZeroShotPrompt: Plain zero-shot
  • ZeroShotRolePrompt: Zero-shot with role context
  • ZeroShotEmotionPrompt: Zero-shot with emotional framing
  • FewShotPrompt: Few-shot with examples
  • ChainOfThoughtPrompt: Chain-of-Thought reasoning
  • CueChainOfThoughtPrompt: CoT with answer cue
  • ChainOfKnowledge: Knowledge-grounded reasoning

Converters

  • MarkdownConverter: Convert to Markdown format
  • XMLConverter: Convert to XML format
  • YAMLConverter: Convert to YAML format

Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues on GitHub.

License

MIT License — see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

crocoprompt-0.1.1rc2.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

crocoprompt-0.1.1rc2-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file crocoprompt-0.1.1rc2.tar.gz.

File metadata

  • Download URL: crocoprompt-0.1.1rc2.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for crocoprompt-0.1.1rc2.tar.gz
Algorithm Hash digest
SHA256 fde74366232432452e620d3d9515f5dbe1e607d9300ef1727eaa4e3298ab826f
MD5 44d4a6f291ee18be27d949ff6eb57fc7
BLAKE2b-256 c0ab0a4b5e3ec29f005687546bf3a5a7e0a71e7bd42c033e79b76e059db05e03

See more details on using hashes here.

Provenance

The following attestation bundles were made for crocoprompt-0.1.1rc2.tar.gz:

Publisher: publish-pypi.yml on postovyi/crocoprompt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file crocoprompt-0.1.1rc2-py3-none-any.whl.

File metadata

  • Download URL: crocoprompt-0.1.1rc2-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for crocoprompt-0.1.1rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 66a0186fc42da254cca47caae426212c205da7ec96dbb904dce48aacda016bdf
MD5 56c2d28ee2fbfb7244293be285b60f97
BLAKE2b-256 c881ccd3e15406e292b6b486078a584c87fe03660cceb074471145c7e6c9f1a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for crocoprompt-0.1.1rc2-py3-none-any.whl:

Publisher: publish-pypi.yml on postovyi/crocoprompt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page