Skip to main content

Retab official python library

Project description

k-LLMS

Built with 🩷 at retab

k-llms is a wrapper around the OpenAI client that adds consensus functionality through the n parameter.

Features

  • Drop-in replacement for OpenAI client
  • Uses the n parameter to generate multiple completions efficiently
  • Automatic result consolidation using majority voting
  • Likelihood computations
  • Support for both sync and async operations
  • Compatible with all OpenAI chat completion parameters
  • Support for structured outputs with parse()

Installation

# The wrapper uses the official OpenAI client
pip install openai
pip install k-llms

Usage

Basic Usage

from k_llms import KLLMs
from openai import OpenAI

# Initialize the client (uses OPENAI_API_KEY env var by default)
kllms_client = KLLMs()

openai_client = OpenAI()

# Make a single request (normal OpenAI behavior)
response = openai_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}]
)

# Make multiple requests with consensus
consensus_response = kllms_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "What is 2+2?"}],
    n=3  # Generates 3 completions and consolidates
)

Structured Outputs with Parse

from pydantic import BaseModel

class UserInfo(BaseModel):
    name: str
    age: int

# Single parse request
result = openai_client.chat.completions.parse(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "John is 30 years old"}],
    response_format=UserInfo
)

# Multiple parse requests with consensus
result = kllms_client.chat.completions.parse(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "John is 30 years old"}],
    response_format=UserInfo,
    n=3
)

# Access consolidated result
consensus_user = result.choices[0].message.parsed  # Consolidated UserInfo object
original_users = [choice.message.parsed for choice in result.choices[1:]]  # Original results

Async Usage

from k_llms import AsyncKLLMs
from openai import AsyncOpenAI
import asyncio

async def main():
    kllms_client = AsyncKLLMs()
    openai_client = AsyncOpenAI()
    
    response = await kllms_client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Hello!"}],
        n=3
    )
    print(response.choices[0].message.content)

asyncio.run(main())

How Consensus Works

When n > 1:

  1. For chat completions: Uses OpenAI's native n parameter to generate multiple completions in a single API call
  2. For responses API: Makes parallel requests (as the Responses API doesn't support the n parameter)
  3. For both completions.create() and parse(): Results are consolidated using majority voting
    • For simple values: Most common value wins
    • For JSON/dict responses: Field-by-field majority voting
    • For lists: Element-by-element consolidation
  4. All responses return a choices array where:
    • choices[0]: Consolidated/consensus result
    • choices[1...n]: Individual original results from each API call

API Compatibility

The wrapper maintains full compatibility with the OpenAI client API. All parameters supported by the official client work seamlessly, including:

  • temperature, top_p, max_tokens
  • response_format, tools, tool_choice
  • stream (automatically disabled - all responses are non-streaming)
  • All other OpenAI parameters

Limitations

  • Streaming is not supported (all requests return KLLMsChatCompletion objects)

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

k_llms-0.0.54.tar.gz (29.4 kB view details)

Uploaded Source

Built Distribution

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

k_llms-0.0.54-py3-none-any.whl (37.1 kB view details)

Uploaded Python 3

File details

Details for the file k_llms-0.0.54.tar.gz.

File metadata

  • Download URL: k_llms-0.0.54.tar.gz
  • Upload date:
  • Size: 29.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.4

File hashes

Hashes for k_llms-0.0.54.tar.gz
Algorithm Hash digest
SHA256 9ba9bba2907cbf2c2e8de6a4a0ae4bf598fdf1331773342d3f035a5e21ebba7d
MD5 8a2511a04038fb4b90e9150f019c3933
BLAKE2b-256 3b42118c3543f358691026a4f7af186f5a0433fe7721eba212fd60ae599d1f86

See more details on using hashes here.

File details

Details for the file k_llms-0.0.54-py3-none-any.whl.

File metadata

  • Download URL: k_llms-0.0.54-py3-none-any.whl
  • Upload date:
  • Size: 37.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.4

File hashes

Hashes for k_llms-0.0.54-py3-none-any.whl
Algorithm Hash digest
SHA256 d9afe380f4dd6d78ea74a1ce318effa1f0cc56968e3c316f2a89aeddec05b01f
MD5 8a28f05bc8a5bf9a64a7f493afa73e38
BLAKE2b-256 4f655506daf0739461128332f1faad01ba883e4cd1b0f62ac339eddbab04be4d

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