Skip to main content

Efficient utilities for working with Large Language Models

Project description

RLHF-Utils

PyPI version Python Versions License: MIT

A streamlined Python toolkit for building and working with Large Language Models (LLMs), designed for researchers and engineers.

🚀 Installation

pip install rlhf-utils

✨ Features

  • Online Server Module: Efficient utilities for LLM API integrations
    • ⚡ Parallel OpenAI API calls with multithreading
    • 📊 Built-in progress tracking for batch requests
    • 🛡️ Error handling and retry logic
    • 🔄 Complete API response access

🔍 Usage Examples

Parallel OpenAI API Calls

Process multiple prompts simultaneously with optimal resource utilization:

from rlhf_utils.online_server import multithread_openai_chat_completions_call
from openai import OpenAI

# Initialize OpenAI client
client = OpenAI(api_key="your-api-key")

# Define messages for multiple API calls
messages = [
    [{"role": "user", "content": "Explain quantum computing"}],
    [{"role": "user", "content": "Write a short poem about AI"}],
    [{"role": "user", "content": "Summarize the history of the internet"}]
]

# Make parallel API calls (returns complete response objects)
responses = multithread_openai_chat_completions_call(
    client=client,
    messages=messages,
    model_name="gpt-3.5-turbo",
    max_workers=3
)

# Access response data
for i, response in enumerate(responses):
    if response is None:
        print(f"Request {i} failed")
        continue
        
    # Get the generated content
    content = response.choices[0].message.content
    print(f"Response {i+1}:\n{content}\n")
    
    # Access metadata like token usage
    print(f"Total tokens: {response.usage.total_tokens}")

Working with Response Objects

The function returns complete OpenAI ChatCompletion objects with all API response data:

# Access different parts of the response
response = responses[0]  # First response

# Message content
content = response.choices[0].message.content

# Token usage
completion_tokens = response.usage.completion_tokens
prompt_tokens = response.usage.prompt_tokens
total_tokens = response.usage.total_tokens

# Model information
model = response.model

# Other metadata
finish_reason = response.choices[0].finish_reason

Performance Benchmarking

The parallel implementation offers significant speedups:

  • Processing 10 prompts sequentially: ~20 seconds
  • With multithread_openai_chat_completions_call: ~3 seconds

🛠️ For Developers

Clone the repository to contribute:

git clone https://github.com/yourusername/rlhf-utils.git
cd rlhf-utils
pip install -e .

Run tests:

python -m unittest discover tests

📝 License

MIT

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

rlhf_utils-0.1.1.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

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

rlhf_utils-0.1.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file rlhf_utils-0.1.1.tar.gz.

File metadata

  • Download URL: rlhf_utils-0.1.1.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rlhf_utils-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bfe0c3738a39f0080148b1e338be0e4a3b858a13bc33274f9dd73c72f1accf9c
MD5 6c374bb07a60502ee978629fe20efa9c
BLAKE2b-256 a7741720c298ba399b4beb09d52d133391bb27b06497e76147b5d61c6ac81566

See more details on using hashes here.

File details

Details for the file rlhf_utils-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: rlhf_utils-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rlhf_utils-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 00f3e028435a864f96d8853824340c834fd8bc6561fb0ea52e837584db6ae6b1
MD5 1b7c594e0ee36283a742d923fece232c
BLAKE2b-256 05e3b1473f976ce3736f9722ebb1cd5d0cd9b3204072291865b2d4f8352089e5

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