Skip to main content

llm-strings

A lightweight library for message handling with instruction fine-tuned language models.

This module provides two classes: LMString and LMRecordStrings.

  • LMString is a subclass of Python's built-in string class that overides the format method to provide more informative feedback about missing formatting arguments.

  • LMRecordStrings stores multiple string records and formats them according to the input text conversations. You can still format messages after wrapped in a collection, and you have a guarantee over the roles sequence: system (optional), user, assistant, user, assistant,...

Usage

LMString Class

Initialize the LMString

template = LMString("Hello, {name}. Today is {day}.")
# Output: Template String initialized successfully. Required args: {'day', 'name'}

In this example, we create an LMString that expects two arguments: name and day.

Format the LMString

template.format(name="Alice")
# Output: Template String has been partially formatted successfully. Required args now: ['day']

If only a subset of the required arguments are provided, format will return an LMString that only expects the remaining ones.

template.format(name="Alice", day="Monday")
#Output: "Hello, Alice. Today is Monday."

When all required arguments are provided, format will return an ordinary string.

Compose LMString with another string

template @ "How are you?"
# Output: "Hello, {name}. Today is {day}. How are you?"

The @ operator can be used to concatenate an LMString with another string. The resulting LMString will expect all arguments that the original LMString expected.

LMRecordStrings Class

Initialize the LMRecordStrings

record = LMRecordStrings("Hello, {name}.")

In this example, we create an LMRecordStrings that expects one argument: name.

Compose LMRecordStrings with another string or LMRecordStrings

record @ "Today is {day}."
record @ LMRecordStrings("How are you?")

The @ operator can be used to concatenate an LMRecordStrings with another string or LMRecordStrings. The resulting LMRecordStrings will expect all arguments that the original LMRecordStrings expected.

Format the LMRecordStrings

record.format(0, name="Alice")
# Output: 0. Record formatted succesfully. Formatted args: {'name'}. Required args now: set()

The format method formats a specific record in the LMRecordStrings. The first argument is the index of the record to format, and the remaining arguments are the formatting arguments.

record.vformat(name="Alice", day="Monday")
# Output: Records formatted succesfully!

The vformat method formats all records in the LMRecordStrings that expect the provided arguments.

Print the LMRecordStrings

print(record)

The __str__ method returns a string representation of the LMRecordStrings that can be printed to the console. The string representation includes a border and separates each record with a line.

record = LMRecordStrings("Hello, {name}.") @ "Today is {day}." @ LMRecordStrings("How are you?")
record = record.format(0, name="Alice").vformat(day="Monday")
print(record)

This would output:

┌────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ 0. System: ***  Hello, Alice. ***                                                                         │
│------------------------------------------------------------------------------------------------------------│
│ 1. User: Today is Monday.                                                                                 │
│------------------------------------------------------------------------------------------------------------│
│ 2. Assistant: How are you?                                                                                │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

More examples

Optional[System],(User,Asisstant)*n invariant:

# Initializing a conversation with an opening message
conversation = LMRecordStrings() @ "Hello, {name}. How can I assist you today?"

# Adding more records to the conversation
conversation @ "I need assistance with {topic}." @ "Certainly, what specific aspect of {topic} are you interested in?"

# Formatting the first record with the user's name
conversation = conversation.format(0, name="Alice")

# Formatting the second record (user's message) with the topic
conversation = conversation.format(1, topic="Python programming")

# Formatting the third record (assistant's message) with the same topic
conversation = conversation.format(2, topic="Python programming")

# Adding a user reply to the assistant's question
conversation @ "{aspect} of Python programming sounds challenging."

# Formatting the fourth record (user's message) with the specific aspect
conversation = conversation.format(3, aspect="asynchronous code execution")

# Let's add the assistant's final message
conversation @ "I can help you understand {concept} with some examples and explanations."

# Formatting the fifth record (assistant's message) with the specific concept
conversation = conversation.format(4, concept="asynchronous code execution")

# Print the entire conversation
print(conversation)

This will produce an output representing a back-and-forth conversation between the user and the assistant, with roles correctly alternating and messages formatted based on the arguments provided:

┌────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ 0. System: ***  Hello, Alice. How can I assist you today? ***                                             │
│------------------------------------------------------------------------------------------------------------│
│ 1. User: I need assistance with Python programming.                                                       │
│------------------------------------------------------------------------------------------------------------│
│ 2. Assistant: Certainly, what specific aspect of Python programming are you interested in?                │
│------------------------------------------------------------------------------------------------------------│
│ 3. User: Asynchronous code execution of Python programming sounds challenging.                            │
│------------------------------------------------------------------------------------------------------------│
│ 4. Assistant: I can help you understand asynchronous code execution with some examples and explanations.  │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

V-Formatting

# Initializing a conversation with an opening message
conversation = LMRecordStrings() @ "Hello, {name}. How can I assist you today?"

# Adding more records to the conversation
conversation @ "I need assistance with {topic}." @ "Certainly, what specific aspect of {topic} are you interested in?"

# Adding a user reply to the assistant's question
conversation @ "{aspect} of {topic} sounds challenging."

# Let's add the assistant's final message
conversation @ "I can help you understand {concept} with some examples and explanations."

# Now, we use vformat to apply the formatting arguments to all records
formatted_conversation = conversation.vformat(
    name="Alice",
    topic="Python programming",
    aspect="asynchronous code execution",
    concept="asynchronous code execution"
)

# Print the fully formatted conversation
print(formatted_conversation)

This will produce an output similar to the one before, but this time we are formatting all the records in one go at the end, using the vformat method:

┌────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ 0. System: ***  Hello, Alice. How can I assist you today? ***                                             │
│------------------------------------------------------------------------------------------------------------│
│ 1. User: I need assistance with Python programming.                                                       │
│------------------------------------------------------------------------------------------------------------│
│ 2. Assistant: Certainly, what specific aspect of Python programming are you interested in?                │
│------------------------------------------------------------------------------------------------------------│
│ 3. User: Asynchronous code execution of Python programming sounds challenging.                            │
│------------------------------------------------------------------------------------------------------------│
│ 4. Assistant: I can help you understand asynchronous code execution with some examples and explanations.  │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

The vformat method is particularly useful when you have multiple records that require similar arguments, and you want to apply the formatting in a single step. It streamlines the process and makes the code cleaner and more concise, especially when dealing with lengthy conversations.

Metadata

Release files for llm-strings 0.1.3

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

Source distribution (sdist)

Source distribution for llm-strings 0.1.3
File Size Uploaded Provenance
llm_strings-0.1.3.tar.gz 9.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-strings 0.1.3
File Interpreter ABI Platform
llm_strings-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 16.5 kB

Release files / llm_strings-0.1.3.tar.gz

Download URL llm_strings-0.1.3.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7a5cf4f3ab6198b69dc709cd81c4f1833f810714359b90f7a9720783c35956ef
BLAKE2b-256 checksum
How to use checksums
7cb3faa16e01eb5884e43e30bf15a923fdb7be96fe4b9fbe73cb4cea3a10ec74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.18

Release files / llm_strings-0.1.3-py3-none-any.whl

Download URL llm_strings-0.1.3-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
599a51c6cd8349827bd5d2c0b56de12c7ae3bee3175d42942eb28d257c0a1e50
BLAKE2b-256 checksum
How to use checksums
83b2b39422ce1780e574bdd5bb2a8469e89fc5cb7e3c17cc13c92030660a70f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.18

Security for llm-strings 0.1.3

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.

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files No provenance

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