Skip to main content

Utility functions for LLM applications

Project description

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

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

speedy_llm_utils-1.1.7.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

speedy_llm_utils-1.1.7-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file speedy_llm_utils-1.1.7.tar.gz.

File metadata

  • Download URL: speedy_llm_utils-1.1.7.tar.gz
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for speedy_llm_utils-1.1.7.tar.gz
Algorithm Hash digest
SHA256 7b0d8842d732bb2b91094d413a93205364e8159d0aee685d9e94fed99b1eb4bc
MD5 3839ea0e26c7f0c6b35a0d519810687b
BLAKE2b-256 de5757075c0a28da1599f3c8f5bb2a7717879c1043b1c0da63072ca801e08bd2

See more details on using hashes here.

File details

Details for the file speedy_llm_utils-1.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for speedy_llm_utils-1.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 88a4eea7b30620c4bfa48dee195d5bbf8ba3803ef300c27933b3297e918e184d
MD5 f0730acb8817a16ad8335ad2d6837522
BLAKE2b-256 f08a98a1f707ff0cc3b9b6e8bdd24ded3e8f9cfda70689de349d966d1f224b8a

See more details on using hashes here.

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