Package: llm-strings
llm-stringsA lightweight library for message handling with instruction fine-tuned language models.This module provides two classes:LMStringandLMRecordStrings.LMStringis a subclass of Python's built-in string class that overides theformatmethod to provide more informative feedback about missing formatting arguments.LMRecordStringsstores 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,...UsageLMString ClassInitialize theLMStringtemplate=LMString("Hello,{name}. Today is{day}.")# Output: Template String initialized successfully. Required args: {'day', 'name'}In this example, we create anLMStringthat expects two arguments:nameandday.Format theLMStringtemplate.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,formatwill return anLMStringthat only expects the remaining ones.template.format(name="Alice",day="Monday")#Output: "Hello, Alice. Today is Monday."When all required arguments are provided,formatwill return an ordinary string.ComposeLMStringwith another stringtemplate@"How are you?"# Output: "Hello, {name}. Today is {day}. How are you?"The@operator can be used to concatenate anLMStringwith another string. The resultingLMStringwill expect all arguments that the originalLMStringexpected.LMRecordStrings ClassInitialize theLMRecordStringsrecord=LMRecordStrings("Hello,{name}.")In this example, we create anLMRecordStringsthat expects one argument:name.ComposeLMRecordStringswith another string orLMRecordStringsrecord@"Today is{day}."record@LMRecordStrings("How are you?")The@operator can be used to concatenate anLMRecordStringswith another string orLMRecordStrings. The resultingLMRecordStringswill expect all arguments that the originalLMRecordStringsexpected.Format theLMRecordStringsrecord.format(0,name="Alice")# Output: 0. Record formatted succesfully. Formatted args: {'name'}. Required args now: set()Theformatmethod formats a specific record in theLMRecordStrings. 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!Thevformatmethod formats all records in theLMRecordStringsthat expect the provided arguments.Print theLMRecordStringsprint(record)The__str__method returns a string representation of theLMRecordStringsthat 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 examplesOptional[System],(User,Asisstant)*n invariant:# Initializing a conversation with an opening messageconversation=LMRecordStrings()@"Hello,{name}. How can I assist you today?"# Adding more records to the conversationconversation@"I need assistance with{topic}."@"Certainly, what specific aspect of{topic}are you interested in?"# Formatting the first record with the user's nameconversation=conversation.format(0,name="Alice")# Formatting the second record (user's message) with the topicconversation=conversation.format(1,topic="Python programming")# Formatting the third record (assistant's message) with the same topicconversation=conversation.format(2,topic="Python programming")# Adding a user reply to the assistant's questionconversation@"{aspect}of Python programming sounds challenging."# Formatting the fourth record (user's message) with the specific aspectconversation=conversation.format(3,aspect="asynchronous code execution")# Let's add the assistant's final messageconversation@"I can help you understand{concept}with some examples and explanations."# Formatting the fifth record (assistant's message) with the specific conceptconversation=conversation.format(4,concept="asynchronous code execution")# Print the entire conversationprint(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 messageconversation=LMRecordStrings()@"Hello,{name}. How can I assist you today?"# Adding more records to the conversationconversation@"I need assistance with{topic}."@"Certainly, what specific aspect of{topic}are you interested in?"# Adding a user reply to the assistant's questionconversation@"{aspect}of{topic}sounds challenging."# Let's add the assistant's final messageconversation@"I can help you understand{concept}with some examples and explanations."# Now, we use vformat to apply the formatting arguments to all recordsformatted_conversation=conversation.vformat(name="Alice",topic="Python programming",aspect="asynchronous code execution",concept="asynchronous code execution")# Print the fully formatted conversationprint(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 thevformatmethod:┌────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ 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.  │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────┘Thevformatmethod 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.