Skip to main content

ks_openai

A Python package for interacting with the KS OpenAI-like API.

Installation

pip install kloud-services

Configuration

Set your API key as an environment variable:

import os
os.environ["OPENAI_API_KEY"] = "your-api-key-here"

Basic Usage

from ks_openai import generate_response

# Create a list of messages
messages = [
    {"role": "user", "content": "What is Python?"}
]

# Generate a response
response = generate_response(
    model="gpt-4o-2024-05-13",
    messages=messages
)

# Print the response
print(response.choices[0].message.content)

Advanced Usage

from ks_openai import generate_response

# Example with all parameters
response = generate_response(
    model="gpt-4o-2024-05-13",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is Python?"}
    ],
    temperature=0.7,           # Controls randomness (0.0 to 1.0)
    max_tokens=150,           # Maximum length of response
    top_p=0.9,               # Nucleus sampling parameter
    frequency_penalty=0.5,    # Reduces repetition of similar words
    presence_penalty=0.5,     # Encourages new topics
    stop=["\n", "END"]       # Stop sequences
)

API Reference

generate_response()

Main function to generate responses from the API.

Parameters:

  • model (str, required): The model identifier (e.g., "gpt-4o-2024-05-13")
  • messages (List[dict], required): List of message objects with 'role' and 'content'
  • temperature (float, optional): Sampling temperature (0.0 to 1.0)
  • max_tokens (int, optional): Maximum number of tokens in response
  • top_p (float, optional): Nucleus sampling parameter (0.0 to 1.0)
  • frequency_penalty (float, optional): Penalty for frequent tokens (0.0 to 2.0)
  • presence_penalty (float, optional): Penalty for new tokens (0.0 to 2.0)
  • stop (List[str], optional): List of stopping sequences

Returns:

OpenAIResponse object with the following structure:

class OpenAIResponse:
    id: str                   # Response identifier
    choices: List[Choice]     # List of response choices
    created: int             # Timestamp
    model: str               # Model used
    usage: Usage             # Token usage statistics

Response Structure

Message

class Message:
    role: str        # Role of the message (e.g., "user", "assistant")
    content: str     # Content of the message

Content Filter Results

class ContentFilterSeverity:
    filtered: bool   # Whether content was filtered
    severity: str    # Severity level of content

class ContentFilterResults:
    hate: ContentFilterSeverity
    self_harm: ContentFilterSeverity
    sexual: ContentFilterSeverity
    violence: ContentFilterSeverity

Error Handling

try:
    response = generate_response(
        model="gpt-4o-2024-05-13",
        messages=[{"role": "user", "content": "Hello"}]
    )
except Exception as e:
    print(f"Error: {str(e)}")

Examples

Simple Question-Answer

messages = [{"role": "user", "content": "What is Python?"}]
response = generate_response(model="gpt-4o-2024-05-13", messages=messages)
print(response.choices[0].message.content)

Conversation with Context

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "What is Python?"},
    {"role": "assistant", "content": "Python is a programming language."},
    {"role": "user", "content": "What can I build with it?"}
]
response = generate_response(model="gpt-4o-2024-05-13", messages=messages)
print(response.choices[0].message.content)

Requirements

  • Python 3.7+
  • requests
  • pydantic

License

This project is licensed under the MIT License.

Metadata

Release files for kloud-services 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kloud-services 1.0.0
File Size Uploaded
kloud_services-1.0.0.tar.gz 7.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kloud-services 1.0.0
File Interpreter ABI Platform
kloud_services-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.8 kB

Release files / kloud_services-1.0.0.tar.gz

Download URL kloud_services-1.0.0.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d385097fd600121330395195d07d6fcba4995eab69b36d67238313b06c196881
BLAKE2b-256 checksum
How to use checksums
e3ffa325a56e8a2eca648c368d1080edfe5affc809ab3f3f6e14926b877e6214
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / kloud_services-1.0.0-py3-none-any.whl

Download URL kloud_services-1.0.0-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9e29bb3de99e14359bddf5640229f048a4d8ea1371a3fa430701c9201a3a42b2
BLAKE2b-256 checksum
How to use checksums
eb40d7f14113806baf6dbf52f65d3c1de990d0169961124e28ffbcea3800f22a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page