Skip to main content

Python library for interacting with KoboldCPP API

Project description

KoboldAPI

A Python library for interacting with KoboldCPP API, providing robust text and image processing capabilities with built-in chunking and templating support.

Features

  • Text and document processing with Apache Tika integration
  • Smart chunking with regex-based natural language boundaries
  • Image processing with support for various formats including RAW
  • Chat templating system supporting multiple LLM formats
  • Async and streaming generation capabilities
  • Built-in error handling and retry mechanisms

Installation

pip install koboldapi

Quick Start

Basic API Usage

from koboldapi import KoboldAPICore

# Initialize the core API client
api = KoboldAPICore("http://localhost:5001")

# Simple generation
response = api.api_client.generate("What is the capital of France?")
print(response)

# Get model info
model_info = api.get_model_info()
print(f"Model: {model_info['name']}")
print(f"Context Length: {model_info['context_length']}")

Processing Text Documents

import asyncio
from pathlib import Path
from koboldapi import KoboldAPICore, ChunkingProcessor

async def process_document(file_path: str):
    # Initialize API
    core = KoboldAPICore("http://localhost:5001")
    
    # Create processor with 2048 token chunks
    processor = ChunkingProcessor(core.api_client, max_chunk_length=2048)
    
    # Process document - works with any format supported by Apache Tika
    chunks, metadata = processor.chunk_file(Path(file_path))
    
    print(f"Document metadata: {metadata}")
    print(f"Number of chunks: {len(chunks)}")
    
    # Process each chunk
    for i, (chunk, token_count) in enumerate(chunks):
        response = core.api_client.generate(
            prompt=chunk,
            max_length=1024
        )
        print(f"\nChunk {i+1} ({token_count} tokens):")
        print(response)

# Run with asyncio
asyncio.run(process_document("document.pdf"))

Image Processing

from koboldapi import ImageProcessor, InstructTemplate, KoboldAPI

def analyze_image(image_path: str, instruction: str = "Describe this image"):
    # Initialize components
    api = KoboldAPI("http://localhost:5001")
    processor = ImageProcessor(max_dimension=1024)
    template = InstructTemplate("http://localhost:5001")
    
    # Process image
    encoded_image, _ = processor.process_image(image_path)
    if not encoded_image:
        raise ValueError("Failed to process image")
    
    # Create prompt with template
    prompt = template.wrap_prompt(instruction)
    
    # Generate description
    return api.generate(
        prompt=prompt,
        images=[encoded_image],
        temperature=0.1
    )

# Use the function
result = analyze_image("photo.jpg", "What objects do you see in this image?")
print(result)

Advanced Features

Streaming Generation

import asyncio
from koboldapi import KoboldAPI

async def stream_response(prompt: str):
    api = KoboldAPI("http://localhost:5001")
    
    async for token in api.stream_generate(prompt):
        print(token, end='', flush=True)
    print()

# Use with asyncio
asyncio.run(stream_response("Write a short story about a robot"))

Custom Chunking Configuration

from koboldapi import ChunkingProcessor, KoboldAPI

# Initialize with custom chunking parameters
processor = ChunkingProcessor(
    api_client=KoboldAPI("http://localhost:5001"),
    max_chunk_length=4096,
    max_total_chunks=1000
)

# Process with metadata
chunks, metadata = processor.chunk_file("large_document.txt")

Error Handling

The library provides custom exceptions for proper error handling:

from koboldapi import KoboldAPIError

try:
    api = KoboldAPI("http://localhost:5001")
    response = api.generate("Test prompt")
except KoboldAPIError as e:
    print(f"API Error: {e}")

Best Practices

  1. Chunking: Always use the ChunkingProcessor for large documents to ensure proper token management.

  2. Templates: Use the InstructTemplate system to ensure proper formatting for different models.

  3. Error Handling: Implement proper error handling using try/except blocks with KoboldAPIError.

  4. Resource Management: For large files or batch processing, consider using async methods and proper cleanup.

  5. Image Processing: Set appropriate max_dimension and quality parameters based on your model's requirements.

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

koboldapi-0.5.1.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

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

koboldapi-0.5.1-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file koboldapi-0.5.1.tar.gz.

File metadata

  • Download URL: koboldapi-0.5.1.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for koboldapi-0.5.1.tar.gz
Algorithm Hash digest
SHA256 120e49c5c261269d94b674ad7003e8e141e2a210f7463286763f24deef5a5e2d
MD5 2c38aca21fd54612111e6ecd68651e5d
BLAKE2b-256 a9a114d155428fd48cdb5f916c51bc354bd331f6abd04cdce39cef006416e63a

See more details on using hashes here.

File details

Details for the file koboldapi-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: koboldapi-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for koboldapi-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0976c5d6922a0552460877771d95677d820c3c04fa7685a4f7136941096b9f49
MD5 55ac52f4d72adeac3da290c2a2bae076
BLAKE2b-256 6ce59101551d76e5db6d7bb6795fdc3182173f9dc54ecbd3243cdbaa828ab468

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