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
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)
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file k_llms-0.0.51.tar.gz.
File metadata
- Download URL: k_llms-0.0.51.tar.gz
- Upload date:
- Size: 32.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3a10907d6750b0abcc40501218376464b628232d1c1b23e495fa05e219aae74
|
|
| MD5 |
d13bcc0aff0eaedab1e714c8e07b0ada
|
|
| BLAKE2b-256 |
37c47f149748749ce73af7eb1b993de0620de6b41414554f044fcf92e0a0d6a7
|
File details
Details for the file k_llms-0.0.51-py3-none-any.whl.
File metadata
- Download URL: k_llms-0.0.51-py3-none-any.whl
- Upload date:
- Size: 33.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1a1cac7f06941699d6b10191a757c014e2492d8d6fc261430656b4ab11fa6ee
|
|
| MD5 |
506e9b1b088b79c34e369fb1a19c1810
|
|
| BLAKE2b-256 |
2bd551669f483a41728374eaa7e2df30af28b1d942c24cafac744756034f0c61
|