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.2.tar.gz (23.8 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.2-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file crocoprompt-0.1.2.tar.gz.

File metadata

  • Download URL: crocoprompt-0.1.2.tar.gz
  • Upload date:
  • Size: 23.8 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.2.tar.gz
Algorithm Hash digest
SHA256 cece84ccd21ee7166a8552d1d4396ed2ac55f9d565845a79fd44a50546cba363
MD5 c05f9acf44d3c9c339b2949e7d7a896f
BLAKE2b-256 2de92dac0f3ac7f3a80c603680edcc6434a5a71789a2bead01f125632d0e651e

See more details on using hashes here.

Provenance

The following attestation bundles were made for crocoprompt-0.1.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: crocoprompt-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 23.8 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 897020d8d14aaf9b649af25b7a3cfef54628eee1ac1f4e4daa2cca0c3cc5fcb3
MD5 2438aa64aa0c412910196e08b0a3439c
BLAKE2b-256 83c42e837d8699f01c9d7e654b9e7ebff75167129dec680df6dc514ffd140a7f

See more details on using hashes here.

Provenance

The following attestation bundles were made for crocoprompt-0.1.2-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