A Python wrapper for OpenAI-like API with custom access key management
Project description
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.
Project details
Release history Release notifications | RSS feed
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 ks_openai-1.0.0.tar.gz.
File metadata
- Download URL: ks_openai-1.0.0.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
420b49aeac497c88a4250d9fa36d772c15bd776ac4462408b54fa7b9ee6664b2
|
|
| MD5 |
365df5c862997e32984801d5dc829fe8
|
|
| BLAKE2b-256 |
1502deb348bc8a905b4b59e7933cca80c728f570b71a0bc9e45be610ae76ccce
|
File details
Details for the file ks_openai-1.0.0-py3-none-any.whl.
File metadata
- Download URL: ks_openai-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b7003ffc5caffc500e7751d40d6e7d4ec529cd922b58e2ab6528527df269af2
|
|
| MD5 |
62f0955b94b6450dba136aefa3fe6498
|
|
| BLAKE2b-256 |
7fb632a6ecbdef7c580333416f6f80e3020132efca6db7158171ffcb1b311572
|