Skip to main content

This package provides convenient access to the GenAI Models' REST API from any Python 3.7+ application.

Project description

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.

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

kloud_services-1.0.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

kloud_services-1.0.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file kloud_services-1.0.0.tar.gz.

File metadata

  • Download URL: kloud_services-1.0.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for kloud_services-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d385097fd600121330395195d07d6fcba4995eab69b36d67238313b06c196881
MD5 3cb0e1e72ede6dd8acd0a711fd760005
BLAKE2b-256 e3ffa325a56e8a2eca648c368d1080edfe5affc809ab3f3f6e14926b877e6214

See more details on using hashes here.

File details

Details for the file kloud_services-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: kloud_services-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for kloud_services-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9e29bb3de99e14359bddf5640229f048a4d8ea1371a3fa430701c9201a3a42b2
MD5 bd1aeb3a111f782a8b8f2cd8fb98f701
BLAKE2b-256 eb40d7f14113806baf6dbf52f65d3c1de990d0169961124e28ffbcea3800f22a

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