Skip to main content

LLM integration layer for kore-mind. Runtime-agnostic cognitive bridge.

Project description

kore-bridge

LLM integration layer for kore-mind. Runtime-agnostic cognitive bridge.

Middleware cognitivo entre tu app y el LLM. Cache, routing, rate limiting, A/B testing — todo antes de gastar tokens.

Part of kore-stack — the complete cognitive middleware for LLMs. pip install kore-stack for the full stack, or install individually:

Install

pip install kore-bridge                # core (zero deps beyond kore-mind)
pip install kore-bridge[openai]        # + OpenAI
pip install kore-bridge[anthropic]     # + Anthropic
pip install kore-bridge[sc]            # + Selector Complexity routing
pip install kore-bridge[all]           # everything

Quick start (Ollama — 100% local, zero API keys)

ollama pull llama3.2
from kore_mind import Mind
from kore_bridge import Bridge, OllamaProvider

mind = Mind("agent.db")
llm = OllamaProvider(model="llama3.2")  # local, free, private
bridge = Bridge(mind=mind, llm=llm)

# Think with context (auto-remembers)
response = bridge.think("Help me with my proof", user="carlos")

# Observe something
bridge.observe("User prefers concise answers")

# Reflect: LLM generates emergent identity from memories
identity = bridge.reflect()
print(identity.summary)

v0.3.2 — kore-mind v0.4.0 embeddings

Now requires kore-mind>=0.4.0 which adds persistent SQLite cache, float16 quantization, async embeddings, streaming batch, and dimension validation. See kore-mind changelog.

v0.3.1 — kore-mind optimized embeddings

Optimized Ollama embeddings: connection reuse, LRU cache, batch embedding, and fallback warnings.

v0.2 Features

Smart Cache

If the LLM already answered something similar, don't spend tokens.

bridge = Bridge(mind=mind, llm=llm, cache_ttl=3600.0)

r1 = bridge.think("What is P vs NP?")   # calls LLM
r2 = bridge.think("What is P vs NP?")   # cache hit, no LLM call

# Bypass cache when needed
r3 = bridge.think("What is P vs NP?", use_cache=False)  # forces LLM

Cache is per-user: different users get different cached responses.

Rate Limiting

If the user asked the same thing 3 times, respond from memory.

bridge = Bridge(
    mind=mind, llm=llm,
    rate_limit=3,       # max 3 identical queries per window
    rate_window=3600.0, # 1 hour window
)

# 4th identical query → served from cache/memory, no LLM call

Per-user Filtering

Each user gets their own context. The user parameter in think() filters memories by source.

bridge.think("Help with Python", user="alice")  # alice's memories
bridge.think("Help with Rust", user="bob")       # bob's memories

LLM Routing

Simple queries to local Ollama (free), complex ones to GPT-4 (powerful).

from kore_bridge import RouterProvider, OllamaProvider
from kore_bridge.providers import OpenAIProvider

router = RouterProvider(
    providers={
        "fast": OllamaProvider(model="llama3.2"),
        "quality": OpenAIProvider(model="gpt-4o"),
    },
    # Optional: custom routing logic
    route_fn=lambda msgs: "fast" if len(msgs[-1]["content"]) < 100 else "quality",
)

bridge = Bridge(mind=mind, llm=router)
bridge.think("Hi")                    # → Ollama (fast)
bridge.think("Explain quantum...")    # → GPT-4 (quality)
print(router.last_route)             # "quality"

Summarize (used by reflect()) always routes to "quality".

SC Routing (Selector Complexity)

Routing based on formal proof complexity theory. Not heuristics — mathematics.

pip install kore-bridge[sc]
from kore_bridge import SCRouterProvider, OllamaProvider
from kore_bridge.providers import OpenAIProvider
from sc_router import ToolCatalog, Tool

catalog = ToolCatalog()
catalog.register(Tool(
    name="calculator",
    description="Arithmetic calculations",
    input_types={"expression"},
    output_types={"number"},
    capability_tags={"math", "calculate"},
))

router = SCRouterProvider(
    providers={
        "fast": OllamaProvider(model="llama3.2"),
        "quality": OpenAIProvider(model="gpt-4o"),
    },
    catalog=catalog,
)

bridge = Bridge(mind=mind, llm=router)
bridge.think("What is 2+2?")              # SC(0) → Ollama
bridge.think("Analyze and cross-reference market data")  # SC(2+) → GPT-4

print(router.last_sc_level)       # 0, 1, 2, or 3
print(router.last_classification) # full classification evidence

A/B Testing

Compare two providers with the same query and context.

from kore_bridge import Experiment

exp = Experiment(
    mind,
    variant_a=OllamaProvider(model="llama3.2"),
    variant_b=OllamaProvider(model="mistral"),
)

result = exp.run("Explain recursion")
print(result.variant_a)      # llama3.2's response
print(result.variant_b)      # mistral's response
print(result.faster)         # "a" or "b"
print(f"A: {result.time_a_ms:.0f}ms, B: {result.time_b_ms:.0f}ms")

# Batch comparison
results = exp.run_batch(["Q1", "Q2", "Q3"])

By default, remember=False — experiments don't contaminate memory.

Observability

Full tracing when enable_traces=True on the Mind.

mind = Mind("agent.db", enable_traces=True)
bridge = Bridge(mind=mind, llm=llm)

bridge.think("Hello", user="carlos")

traces = mind.traces(operation="bridge.think")
for t in traces:
    print(f"{t.operation} | {t.duration_ms:.0f}ms | cache_hit={t.metadata['cache_hit']}")

Providers

# Ollama (local, recommended for OSS)
from kore_bridge import OllamaProvider
llm = OllamaProvider(model="llama3.2")

# OpenAI
from kore_bridge.providers import OpenAIProvider
llm = OpenAIProvider(model="gpt-4o-mini")

# Anthropic
from kore_bridge.providers import AnthropicProvider
llm = AnthropicProvider(model="claude-sonnet-4-5-20250929")

# Any callable
from kore_bridge import CallableLLM
llm = CallableLLM(lambda msgs: my_custom_api(msgs))

# Router (multiple providers)
from kore_bridge import RouterProvider
llm = RouterProvider(providers={"fast": ollama, "quality": openai})

Backward compatibility

All new parameters have defaults that preserve v0.1 behavior:

# This works exactly the same as v0.1
bridge = Bridge(mind=mind, llm=llm)
bridge.think("Hello")

Part of kore-stack

Package What it does
kore-mind Memory, identity, traces, cache storage
kore-bridge (this) LLM integration, cache logic, rate limiting, A/B testing, SC routing
sc-router Query routing by Selector Complexity theory
kore-stack All of the above, one install: pip install kore-stack

Demo

python examples/demo_llm.py              # uses llama3.2
python examples/demo_llm.py mistral      # uses mistral

License

MIT

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

kore_bridge-0.3.2.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

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

kore_bridge-0.3.2-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file kore_bridge-0.3.2.tar.gz.

File metadata

  • Download URL: kore_bridge-0.3.2.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for kore_bridge-0.3.2.tar.gz
Algorithm Hash digest
SHA256 81a5961a6da1b63a7735a16df8ef4cf2e182e2e139ed5ac9af4ba04441fdaffc
MD5 0c09c52257bdb35ee15c039ffb680e1c
BLAKE2b-256 3f06b2e6cf72849208c6a235245352e9619805b02f9e1c827d45dfeb0ae3a72f

See more details on using hashes here.

File details

Details for the file kore_bridge-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: kore_bridge-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for kore_bridge-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bb7885dda5f831a3200e260130631fd8e15db30bd660602d734f6b9bcbe44032
MD5 fe14322a92f63d24fd596044fdb55504
BLAKE2b-256 54db5274a90be4fb1ec99012d02250c00e40e534d1f32f471b61136a309fb478

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