Skip to main content

LLM Utils

A Python library providing utility functions for working with Large Language Models (LLMs).

Overview

LLM Utils is a collection of utility functions and classes designed to simplify working with language models, particularly for chat applications, message formatting, and model interactions. It provides tools for transforming message formats, managing chat sessions, grouping messages by length, and interacting with various LLM providers.

Features

  • Message Formatting: Transform between different chat message formats (ChatML, ShareGPT)
  • Chat Sessions: Manage conversation history and interactions with LLMs
  • Message Grouping: Efficiently batch messages based on length for optimal processing
  • Model Integration: Interface with various LLM backends including OpenAI models
  • Visualization: Display chat messages and conversations as HTML in Jupyter notebooks
  • Command Line Tools: VLLM serving and load balancing utilities

Installation

# Install from PyPI
pip install speedy_llm_utils

# Install from source
pip install -e .

# Or using Poetry
poetry install

Note: While the package is named speedy_llm_utils on PyPI, you still import it as llm_utils in your code:

import llm_utils

Dependencies

  • Python ≥ 3.9
  • numpy
  • pandas
  • google-generativeai
  • transformers
  • dspy
  • langchain
  • langchain-core
  • langchain-openai
  • json-repair

Usage Examples

Transforming Message Formats

from llm_utils import transform_messages, transform_messages_to_chatml

# Transform messages between formats
chatml_messages = transform_messages_to_chatml(messages)

Working with Chat Sessions

from llm_utils import OAI_LM

# Initialize LLM instance
lm = OAI_LM(model_name="gpt-3.5-turbo")

# Create a chat session
session = lm.create_chat_session(
    system_prompt="You are a helpful assistant."
)

# Generate a response
response = session.send_message("Hello, how can you help me today?")
print(response)

Message Grouping

from llm_utils import split_indices_by_length, group_messages_by_len

# Group messages by length for efficient processing
batches = split_indices_by_length(
    lengths=[len(msg) for msg in messages],
    batch_size_by_mean_length=1000,
    random_seed=42,
    verbose=True,
    shuffle=True
)

Visualizing Conversations

from llm_utils import display_chat_messages_as_html, display_conversations

# Display conversations in a Jupyter notebook
display_conversations(conversations)

Command Line Tools

LLM Utils provides command-line utilities:

  • svllm: Start a VLLM server
  • svllm-lb: Run a VLLM load balancer
# Start a VLLM server
svllm --model <model_name> --port <port>

# Run a load balancer
svllm-lb --servers <server1,server2> --port <port>

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

[Insert your license information here]

Author

  • Anh Vo

Metadata

Release files for speedy-llm-utils 1.1.10

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

Source distribution (sdist)

Source distribution for speedy-llm-utils 1.1.10
File Size Uploaded
speedy_llm_utils-1.1.10.tar.gz 21.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for speedy-llm-utils 1.1.10
File Interpreter ABI Platform
speedy_llm_utils-1.1.10-py3-none-any.whl Python 3 none any Details

Total release size: 45.9 kB

Release files / speedy_llm_utils-1.1.10.tar.gz

Download URL speedy_llm_utils-1.1.10.tar.gz
Size 21.7 kB
Tags Source
SHA-256 checksum
How to use checksums
566900b46be18291279afe05cf374505eded9d114f84ea981460c4830ebab2be
BLAKE2b-256 checksum
How to use checksums
cc244a269f375b01ac78c638a7f83edb442ae998a2d0252fd20f628a48893ca7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / speedy_llm_utils-1.1.10-py3-none-any.whl

Download URL speedy_llm_utils-1.1.10-py3-none-any.whl
Size 24.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4132ec67760611a939594b24d9471435ffb56dc68a9ea274251b38de5eef1d0f
BLAKE2b-256 checksum
How to use checksums
c7b5febd8ba3e06b21ec2ee348aa34bc84cbb471a0bf5cc558239762a319df29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release history Release notifications | RSS feed

This release

1.1.10 This release

2 release files

1.1.9

2 release files

1.1.8

2 release files

1.1.7

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

1 release file

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