ks_openai
A Python package for interacting with the KS OpenAI-like API.
Installation
pip install ks_openai
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 responsetop_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 ks-openai 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ks_openai-1.0.0.tar.gz | 4.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ks_openai-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.9 kB
Release files / ks_openai-1.0.0.tar.gz
| Download URL | ks_openai-1.0.0.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
420b49aeac497c88a4250d9fa36d772c15bd776ac4462408b54fa7b9ee6664b2
|
|
BLAKE2b-256 checksum How to use checksums |
1502deb348bc8a905b4b59e7933cca80c728f570b71a0bc9e45be610ae76ccce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Release files / ks_openai-1.0.0-py3-none-any.whl
| Download URL | ks_openai-1.0.0-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7b7003ffc5caffc500e7751d40d6e7d4ec529cd922b58e2ab6528527df269af2
|
|
BLAKE2b-256 checksum How to use checksums |
7fb632a6ecbdef7c580333416f6f80e3020132efca6db7158171ffcb1b311572
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|