Utils API Reference

lmitf.utils.print_conversation(msgs)[source]

Print the conversation in a readable format.

Parameters:

msg: list

The conversation messages to print.

lmitf.utils.buf_image(image: Image) bytes[source]
lmitf.utils.open_image(b64_str: str) Image[source]
lmitf.utils.encode_image(image: Image) str[source]
lmitf.utils.res_to_image(response: dict) Image[source]

Overview

The utils module provides utility functions for common tasks when working with LMITF, including conversation formatting, data processing, and helper functions.

Function Reference

Icon Legend

  • 🤖 Assistant: Messages from the AI assistant

  • 👤 User: Messages from the user

  • ⚙️ System: System messages and prompts

Integration with BaseLLM

The utils module integrates seamlessly with LMITF classes:

from lmitf import BaseLLM
from lmitf.utils import print_conversation

llm = BaseLLM()

# Build a conversation
messages = [
    {"role": "system", "content": "You are a coding assistant."},
    {"role": "user", "content": "How do I create a Python list?"}
]

# Get response
response = llm.call(messages)

# Add response to conversation
messages.append({"role": "assistant", "content": response})

# Display the full conversation nicely
print_conversation(messages)

Alternative Import

The print_conversation function is also available as print_turn in the main module:

from lmitf import print_turn

# These are equivalent
print_turn(messages)
print_conversation(messages)

Custom Formatting

For custom conversation formatting, you can extend the function:

from wasabi import msg

def custom_print_conversation(msgs, show_timestamps=False):
    """Custom conversation printer with optional timestamps."""
    import datetime
    
    for i, turn in enumerate(msgs):
        icon = '🤖' if turn['role'] == 'assistant' else (
            '⚙️' if turn['role'] == 'system' else '👤'
        )
        
        header = f"{icon} Turn {i+1}"
        if show_timestamps:
            header += f" ({datetime.datetime.now().strftime('%H:%M:%S')})"
            
        msg.divider(header)
        print(turn['content'])
        print()  # Extra spacing

# Usage
custom_print_conversation(messages, show_timestamps=True)

Best Practices

  1. Debug Conversations: Use print_conversation() to debug multi-turn conversations

  2. Log Interactions: Capture conversation flows for analysis

  3. User Interface: Display conversations in CLI applications

  4. Development: Monitor conversation state during development

Future Utilities

The utils module may be extended with additional helper functions:

  • Message validation

  • Token counting utilities

  • Conversation saving/loading

  • Format conversion helpers