KoboldAPI
A Python library for interacting with KoboldCPP APIs, providing high-level abstractions for text processing, image handling, and generation tasks.
Features
-
Text Processing
- Intelligent text chunking
- Streaming generation capabilities
- Token counting and management
-
Image Processing
- Support for multiple image formats (JPEG, PNG, GIF, TIFF, WEBP, HEIF, RAW)
- Automatic image resizing and optimization
- RAW image processing with thumbnail extraction
- Base64 encoding for API transmission
-
Template Management
- Flexible template system for different LLM models
- Custom template directory support
- Built-in default templates
- Jinja2 templating integration
-
API Integration
- Robust error handling
- Connection management
- Streaming support
- Comprehensive API endpoint coverage
Installation
pip install koboldapi
Quick Start
Text Processing Example
from koboldapi import KoboldAPICore, ChunkingProcessor
# Initialize the API client
core = KoboldAPICore(api_url="http://localhost:5001")
# Create a chunking processor
chunker = ChunkingProcessor(core.api_client, max_chunk_length=2048)
# Process text
chunks, metadata = chunker.chunk_file("document.txt")
for chunk, token_count in chunks:
response = core.wrap_and_generate(
instruction="Summarize this text:",
content=chunk
)
print(response)
Image Processing Example
from koboldapi import KoboldAPICore, ImageProcessor
# Initialize processors
core = KoboldAPICore(api_url="http://localhost:5001")
processor = ImageProcessor(max_dimension=1024)
# Process image
encoded_image, img_path = processor.process_image("image.jpg")
response = core.wrap_and_generate(
instruction="Describe this image:",
images=[encoded_image]
)
print(response)
Streaming Generation Example
from koboldapi import KoboldAPICore
import asyncio
async def stream_example():
core = KoboldAPICore(api_url="http://localhost:5001")
# Stream tokens as they're generated
async for token in core.api_client.stream_generate(
prompt="Write a story about a robot:",
max_length=200
):
print(token, end='', flush=True)
# Or collect all tokens into final result
result = await core.api_client.generate_sync(
prompt="Write a story about a robot:",
max_length=200
)
print(result)
if __name__ == "__main__":
asyncio.run(stream_example())
Detailed Documentation
KoboldAPICore
The main interface for interacting with KoboldCPP APIs.
core = KoboldAPICore(
api_url="http://localhost:5001",
api_password=None, # Optional API password
generation_params={ # Optional generation parameters
'temp': 0.7,
'top_k': 40,
'top_p': 0.9
},
templates_directory="path/to/templates" # Optional custom templates
)
Text Processing
The ChunkingProcessor class handles text segmentation and processing:
chunker = ChunkingProcessor(
api_client, # KoboldAPI instance
max_chunk_length=2048, # Maximum tokens per chunk
max_total_chunks=1000 # Maximum number of chunks
)
# Process a file
chunks, metadata = chunker.chunk_file("document.txt")
# Process raw text
chunks = chunker.chunk_text("Your text content here")
Image Processing
The ImageProcessor class handles image preparation and optimization:
processor = ImageProcessor(
max_dimension=1024, # Maximum image dimension
patch_sizes=[8, 14, 16, 32], # Patch size options
max_file_size=50 * 1024 * 1024 # Maximum file size in bytes
)
# Process an image
encoded_image, path = processor.process_image("image.jpg")
Template Management
Custom templates can be provided in JSON format:
{
"template_name": {
"name": ["model_name_pattern"],
"system_start": "\nSystem: ",
"system_end": "\n",
"user_start": "User: ",
"user_end": "\n",
"assistant_start": "Assistant: "
}
}
Command Line Tools
The package includes example scripts for common tasks:
Text Processing Script
python text-example.py input.txt \
--task translate \
--api-url http://localhost:5001 \
--language French \
--max-chunk-size 8192
Available tasks:
- translate: Translate text to specified language
- summary: Generate text summary
- correct: Fix grammar and spelling
- distill: Create concise version
Image Processing Script
python image-example.py image.jpg \
--api-url http://localhost:5001 \
--instruction "Describe the image in detail."
Error Handling
The library provides custom exceptions for error handling:
try:
result = core.wrap_and_generate(instruction="Your instruction")
except KoboldAPIError as e:
print(f"API Error: {e}")
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the GPLv3 License - see the LICENSE file for details.
Metadata
Release files for koboldapi 0.5.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| koboldapi-0.5.4.tar.gz | 30.6 kB | Without provenance | Details for koboldapi-0.5.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| koboldapi-0.5.4-py3-none-any.whl | Python 3 | none | any | Details for koboldapi-0.5.4-py3-none-any.whl |
Total release size: 61.7 kB
Release files / koboldapi-0.5.4.tar.gz
| Download URL | koboldapi-0.5.4.tar.gz |
|---|---|
| Size | 30.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
da275dfc1ad2ae7aa89177f5c9e8ea80fb4f6050f2a021eeb57519c137ed972d
|
|
BLAKE2b-256 checksum How to use checksums |
62d96042b6ecc1642ed1afa234fde41858c00af9be6d3cd112e0674cc7403ece
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.16
|
Release files / koboldapi-0.5.4-py3-none-any.whl
| Download URL | koboldapi-0.5.4-py3-none-any.whl |
|---|---|
| Size | 31.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2da77d284a1fc5799c2fdb1074381b17413e563c3ef1a705c7aa342afb1c38a0
|
|
BLAKE2b-256 checksum How to use checksums |
0ffb054f080bc5bb80bf53c36193a7e2492966926a8ec02f5bbee08d9123b3e9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.16
|
Security for koboldapi 0.5.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.