Examples

Comprehensive examples demonstrating Kerb’s capabilities across all modules.

By Module

Generation

Text generation examples with multiple providers.

Context Management

Context window and token management examples.

Parsing

Output parsing and validation examples.

Preprocessing

Text preprocessing and cleaning examples.

Evaluation

Model evaluation and benchmarking examples.

Prompt Engineering

Prompt templates and optimization examples.

Retrieval

RAG and semantic search examples.

Fine-Tuning

Model fine-tuning and dataset preparation examples.

See Fine-Tuning Documentation for detailed guides:

Quick Start Examples

Basic Generation

from kerb.generation import generate, ModelName, LLMProvider

response = generate(
    "Explain quantum computing",
    model=ModelName.GPT_4O_MINI,
    provider=LLMProvider.OPENAI
)
print(response.content)

RAG Pipeline

from kerb.document import load_document
from kerb.chunk import chunk_text
from kerb.embedding import embed, embed_batch
from kerb.retrieval import semantic_search, Document
from kerb.generation import generate, ModelName

# Load and process
doc = load_document("paper.pdf")
chunks = chunk_text(doc.content, chunk_size=512)
embeddings = embed_batch(chunks)

# Search
query_embedding = embed("What are the findings?")
documents = [Document(content=c) for c in chunks]
results = semantic_search(query_embedding, documents, embeddings, top_k=5)

# Generate
context = "\n".join([r.document.content for r in results])
answer = generate(f"Based on: {context}\n\nQuestion: What are the findings?")

Response Caching

from kerb.cache import create_memory_cache, generate_prompt_key
from kerb.generation import generate, ModelName

cache = create_memory_cache(max_size=1000)

def cached_generate(prompt, model=ModelName.GPT_4O_MINI):
    key = generate_prompt_key(prompt, model=model.value)
    if cached := cache.get(key):
        return cached['response']
    response = generate(prompt, model=model)
    cache.set(key, {'response': response})
    return response

Running Examples

All examples are self-contained and can be run directly:

# Set your API keys
export OPENAI_API_KEY="your-key"
export ANTHROPIC_API_KEY="your-key"

# Run any example
python docs/examples/generation/01_basic_generation.py

# Or use the run script
python scripts/run_examples.py --module generation

Additional Resources