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
nparameter 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:
- For chat completions: Uses OpenAI's native
nparameter to generate multiple completions in a single API call - For responses API: Makes parallel requests (as the Responses API doesn't support the
nparameter) - For both
completions.create()andparse(): 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
- All responses return a choices array where:
choices[0]: Consolidated/consensus resultchoices[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_tokensresponse_format,tools,tool_choicestream(automatically disabled - all responses are non-streaming)- All other OpenAI parameters
Limitations
- Streaming is not supported (all requests return
KLLMsChatCompletionobjects)
Metadata
Release files for k-llms 0.0.55
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| k_llms-0.0.55.tar.gz | 38.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| k_llms-0.0.55-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 68.9 kB
Release files / k_llms-0.0.55.tar.gz
| Download URL | k_llms-0.0.55.tar.gz |
|---|---|
| Size | 38.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
779021e56b0405a741c7a6bc69754a183db726f83c9dbc9e319b432aa8ed20d0
|
|
BLAKE2b-256 checksum How to use checksums |
0b6b5d7d55649756f9ec71180caf56c5d93838f2663b9ea1f011f0ce4c4f6100
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.4
|
Release files / k_llms-0.0.55-py3-none-any.whl
| Download URL | k_llms-0.0.55-py3-none-any.whl |
|---|---|
| Size | 30.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
75b02e0c7899c4b249d30429c4b5f89bc3ed9aabb4be88825da5e5b510ab53fa
|
|
BLAKE2b-256 checksum How to use checksums |
20ad5f1548c28a7a639d2238ac589f2ab534c03238ae9adbbdc8990eea9fead2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.4
|