mechaharness.inference.base

Inference Strategy: pluggable backends for raw model calls.

Client code depends on InferenceStrategy, not on a specific provider. Concrete strategies adapt OpenAI, Anthropic, LM Studio, vLLM, etc.

class mechaharness.inference.base.InferenceStrategy[source]

Bases: Completer

Strategy interface for talking to an inference engine.

async aclose() → None[source]

Release network resources. Override when the strategy holds a client.

capability_profile() → CapabilityProfile[source]

Declared skills used for cost pricing of this backend.

abstractmethod async complete(request: CompletionRequest) → CompletionResponse[source]

Run a single non-streaming completion.

describe() → dict[str, Any][source]

Machine-readable metadata for CLI/API introspection.

name: str = 'base'
async stream(request: CompletionRequest) → AsyncIterator[str][source]

Optional token stream. Default falls back to a single complete().

OpenAI-compatible provider

OpenAI-compatible inference strategy.

Covers OpenAI, Azure OpenAI (compat endpoints), Groq, Together, Fireworks, LM Studio, vLLM, Ollama (OpenAI mode), and most local OpenAI-shaped servers.

HTTP JSON is modeled in mechaharness.inference.openai_wire. Portable domain types stay in mechaharness.core.types; this strategy maps between them.

class mechaharness.inference.openai_compat.OpenAICompatStrategy(settings: mechaharness.config.Settings, client: AsyncHttpClient, request_policy: OpenAIRequestPolicy | None = None, *, timeout: float | None = None)[source]

Bases: InferenceStrategy

HTTP client for OpenAI Chat Completions and compatible local endpoints.

Hosts inject an AsyncHttpClient and own its lifetime. Settings must contain the resolved endpoint and credentials; apply_backend_defaults resolves OPENAI_API_KEY for the cloud backend. See the OpenAI setup guide for explicit live-test opt-in and local endpoint configuration.

Example (in-memory HTTP response; no API credentials or network needed):
>>> import asyncio
>>> from pyiv.http import MemoryAsyncHttpClient
>>> client = MemoryAsyncHttpClient()
>>> client.add("POST", "http://example.invalid/v1/chat/completions", json={
...     "id": "demo", "object": "chat.completion", "created": 0,
...     "model": "demo", "choices": [{"index": 0,
...         "message": {"role": "assistant", "content": "ok"},
...         "finish_reason": "stop"}],
... })
>>> settings = Settings(model="demo", base_url="http://example.invalid/v1",
...                     api_key=None)
>>> strategy = OpenAICompatStrategy(settings, client, timeout=5)
>>> request = CompletionRequest(model="demo", messages=[
...     ChatMessage(role=Role.USER, content="hi")])
>>> response = asyncio.run(strategy.complete(request))
>>> response.message.content
'ok'
__init__(settings: mechaharness.config.Settings, client: AsyncHttpClient, request_policy: OpenAIRequestPolicy | None = None, *, timeout: float | None = None) → None[source]
async aclose() → None[source]

Release network resources. Override when the strategy holds a client.

async complete(request: CompletionRequest) → CompletionResponse[source]

Run a single non-streaming completion.

describe() → dict[str, Any][source]

Machine-readable metadata for CLI/API introspection.

name: str = 'openai_compat'
prepare_request(request: CompletionRequest, *, stream: bool | None = None) → PreparedOpenAIRequest[source]

Normalize once for transport and fingerprint the same full JSON body.

async stream(request: CompletionRequest) → AsyncIterator[str][source]

Optional token stream. Default falls back to a single complete().

mechaharness.inference.openai_compat.domain_request_to_wire(request: CompletionRequest, *, default_model: str, stream: bool | None = None) → OpenAIChatCompletionRequest[source]
mechaharness.inference.openai_compat.wire_response_to_domain(wire: OpenAIChatCompletionResponse) → CompletionResponse[source]

See Local OpenAI-compat backend (openai_local) for cloud credentials, Luna request parameters, local endpoints, and explicit live-test opt-in.

Request normalization policy

Provider-owned OpenAI request normalization and diagnostic body fingerprints.

Fingerprints describe the exact normalized JSON body, not a provider cache key. Headers, endpoint routing, tenant scope, and cache hit counters are excluded.

class mechaharness.inference.openai_request.OpenAIRequestCapabilities(completion_token_field: 'str' = 'max_tokens', allowed_extra_fields: 'frozenset[str] | None' = None, tool_reasoning_effort: 'str | None' = None)[source]

Bases: object

__init__(completion_token_field: str = 'max_tokens', allowed_extra_fields: frozenset[str] | None = None, tool_reasoning_effort: str | None = None) → None
allowed_extra_fields: frozenset[str] | None = None
completion_token_field: str = 'max_tokens'
tool_reasoning_effort: str | None = None
class mechaharness.inference.openai_request.OpenAIRequestPolicy[source]

Bases: object

Injectable provider policy; hosts override capabilities for custom/proxy endpoints.

capabilities_for(endpoint: str, model: str) → OpenAIRequestCapabilities[source]
prepare(body: dict[str, Any], extras: dict[str, Any], *, endpoint: str) → PreparedOpenAIRequest[source]
class mechaharness.inference.openai_request.PreparedOpenAIRequest(body: dict[str, Any], omitted_fields: tuple[str, ...])[source]

Bases: object

Transport body and omitted field names; diagnostics never contain raw values.

__init__(body: dict[str, Any], omitted_fields: tuple[str, ...]) → None
body: dict[str, Any]
diagnostics() → dict[str, Any][source]
omitted_fields: tuple[str, ...]