Inbound adapters package¶
Kind: reference. This page is generated from the docstrings of
typevet.adapters.inbound. The API index lists the other packages.
typevet.adapters.inbound
¶
Inbound adapters (library entry points).
Examples:
from typevet.adapters.inbound import generate
from typevet.testing import StaticGenerationFake
result = generate(
StaticGenerationFake({"ok": True}),
prompt="hi",
schema={"type": "object", "additionalProperties": False},
model="fake",
)
See Also
- typevet.adapters.inbound.api:
generatehelper - typevet.adapters.inbound.helpers:
run_synchelper - typevet.adapters.inbound.settings:
TYPEVET_LLAMA__*composition root - typevet.adapters.inbound.backend_settings:
TYPEVET_BACKENDselection - typevet.ports.generation: GenerationPort
Attributes:
| Name | Type | Description |
|---|---|---|
generate |
function
|
Build a request and invoke a generation port. |
run_sync |
function
|
Run an async generation coroutine from sync code. |
load_calibration_map |
function
|
Read a calibration map file and check its sha256. |
LlamaSettings |
type
|
llama.cpp connection settings for composition roots. |
load_llama_settings |
function
|
Read |
llama_cpp_adapter |
function
|
Build |
async_llama_cpp_adapter |
function
|
Build the async llama.cpp adapter from settings; owns its client and masks the key in errors. |
VllmSettings |
type
|
vLLM connection and API gateway settings; |
load_backend |
function
|
Read |
load_vllm_settings |
function
|
Read |
vllm_http_client |
function
|
Build the shared vLLM |
generation_adapter |
function
|
Build the adapter |
async_vllm_generation_adapter |
function
|
Build the async vLLM adapter. |
open_judgment |
function
|
Open the judgment session |
Classes¶
VllmSettings
dataclass
¶
VllmSettings(
base_url: str,
model: str,
timeout: float = _DEFAULT_TIMEOUT,
api_key: str | None = None,
max_concurrency: int = 1,
user_agent: str | None = None,
auth_header: str = "Authorization",
auth_scheme: str = "Bearer",
headers: Mapping[str, str] = dict(),
request_id_header: str | None = None,
)
Connection options for a vLLM OpenAI-compatible server.
Attributes:
| Name | Type | Description |
|---|---|---|
base_url |
str
|
Server root without a trailing slash. |
model |
str
|
Served model name. |
timeout |
float
|
HTTP request timeout in seconds. |
api_key |
str | None
|
Key sent in |
max_concurrency |
int
|
Maximum POSTs in flight for one async adapter. |
user_agent |
str | None
|
|
auth_header |
str
|
Header that carries the key. |
auth_scheme |
str
|
Scheme before the key; empty sends the key bare. |
headers |
Mapping[str, str]
|
Extra headers with literal values, sent
on every request. A read-only copy after the checks. Excluded
from |
request_id_header |
str | None
|
Header that gets a fresh UUID4 hex
value on each request, or |
Examples:
from typevet.adapters.inbound.backend_settings import VllmSettings
VllmSettings(base_url="http://127.0.0.1:8000", model="served-model")
LlamaSettings
dataclass
¶
LlamaSettings(
base_url: str = _DEFAULT_BASE_URL,
timeout: float = _DEFAULT_TIMEOUT,
default_model: str | None = None,
multimodal_model: str = _DEFAULT_MULTIMODAL_MODEL,
api_key: str | None = None,
headers: Mapping[str, str] = dict(),
auth_header: str = "Authorization",
auth_scheme: str = "Bearer",
user_agent: str | None = None,
request_id_header: str | None = None,
autoload: bool = True,
)
Connection options for a local llama.cpp OpenAI-compat router.
Attributes:
| Name | Type | Description |
|---|---|---|
base_url |
str
|
Router root without a trailing slash. |
timeout |
float
|
HTTP request timeout in seconds. |
default_model |
str | None
|
Default model id when a call omits one. |
multimodal_model |
str
|
Model id the opt-in image smoke asks for. |
api_key |
str | None
|
Key sent in |
headers |
Mapping[str, str]
|
Extra headers with literal values, sent
on every request. A read-only copy after the checks. Excluded
from |
auth_header |
str
|
Header that carries the key. |
auth_scheme |
str
|
Scheme before the key; empty sends the key bare. |
user_agent |
str | None
|
|
request_id_header |
str | None
|
Header that gets a fresh UUID4 hex
value on each request, or |
autoload |
bool
|
When |
Examples:
from typevet.adapters.inbound.settings import LlamaSettings
LlamaSettings(base_url="http://127.0.0.1:8090", timeout=300.0)
Methods:¶
__post_init__
¶
Copy headers read-only and check the header rules.
request_id_header follows the name rules and must not name the
auth header or an extra header (#411).
Raises:
| Type | Description |
|---|---|
TypeError
|
When |
ValueError
|
When a header field breaks a rule of
|
Functions:¶
generate
¶
generate(
port: GenerationPort,
*,
prompt: str,
schema: Mapping[str, Any],
model: str,
media: tuple[ImageInput, ...] = (),
) -> GenerationResult
Build a request and invoke the generation port.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
port
|
GenerationPort
|
Outbound adapter that implements |
required |
prompt
|
str
|
Natural-language instruction. |
required |
schema
|
Mapping[str, Any]
|
JSON Schema object as a mapping. |
required |
model
|
str
|
Backend model id or alias. |
required |
media
|
tuple[ImageInput, ...]
|
Images the prompt marks, one |
()
|
Returns:
| Type | Description |
|---|---|
GenerationResult
|
Validated generation result from the port. |
async_vllm_generation_adapter
¶
async_vllm_generation_adapter(
environ: Mapping[str, str] | None = None,
*,
transport: AsyncBaseTransport | None = None,
) -> AsyncVllmGenerationAdapter
Build the async vLLM generation adapter from TYPEVET_VLLM__*.
The adapter reads the vLLM settings whatever TYPEVET_BACKEND says. Its
httpx.AsyncClient has the same base URL, timeout, client_headers,
event hooks and proxy handling as vllm_http_client, and TYPEVET_VLLM__MAX_CONCURRENCY
sets its POST limit. That client binds to the first event loop that uses
it, so build one adapter per event loop, for example per asyncio.run.
The adapter records the first running loop that calls generate, and a
call on a different loop raises RuntimeError before any request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environ
|
Mapping[str, str] | None
|
Mapping to read. Defaults to |
None
|
transport
|
AsyncBaseTransport | None
|
Optional async transport, for example
|
None
|
Returns:
| Type | Description |
|---|---|
AsyncVllmGenerationAdapter
|
An adapter that closes its client on |
AsyncVllmGenerationAdapter
|
show the configured key. |
Raises:
| Type | Description |
|---|---|
ValueError
|
When a vLLM variable is invalid. |
generation_adapter
¶
generation_adapter(
environ: Mapping[str, str] | None = None, *, transport: BaseTransport | None = None
) -> LlamaCppGenerationAdapter | VllmGenerationAdapter
Build the generation adapter that TYPEVET_BACKEND selects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environ
|
Mapping[str, str] | None
|
Mapping to read. Defaults to |
None
|
transport
|
BaseTransport | None
|
Optional transport for the vLLM or llama.cpp client. |
None
|
Returns:
| Type | Description |
|---|---|
LlamaCppGenerationAdapter | VllmGenerationAdapter
|
A llama.cpp adapter on the |
LlamaCppGenerationAdapter | VllmGenerationAdapter
|
|
LlamaCppGenerationAdapter | VllmGenerationAdapter
|
|
LlamaCppGenerationAdapter | VllmGenerationAdapter
|
its errors never show the configured key. |
Raises:
| Type | Description |
|---|---|
ValueError
|
When a backend or vLLM variable is invalid, or when
|
load_backend
¶
Read TYPEVET_BACKEND.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environ
|
Mapping[str, str] | None
|
Mapping to read. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
Backend
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When the value is not |
load_vllm_settings
¶
load_vllm_settings(environ: Mapping[str, str] | None = None) -> VllmSettings
Read TYPEVET_VLLM__* variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environ
|
Mapping[str, str] | None
|
Mapping to read. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
VllmSettings
|
Frozen settings. An empty |
VllmSettings
|
|
VllmSettings
|
gives |
VllmSettings
|
|
VllmSettings
|
|
VllmSettings
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When |
open_judgment
¶
open_judgment(
environ: Mapping[str, str] | None = None,
*,
transport: BaseTransport | None = None,
framing: ModelFramingPort | None = None,
) -> Iterator[GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession]
Open the judgment session that TYPEVET_BACKEND selects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environ
|
Mapping[str, str] | None
|
Mapping to read. Defaults to |
None
|
transport
|
BaseTransport | None
|
Optional transport for the vLLM or llama.cpp client. The fake branch ignores it. |
None
|
framing
|
ModelFramingPort | None
|
Optional model framing for the llama.cpp branch. Requires a
served |
None
|
Yields:
| Type | Description |
|---|---|
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
A llama.cpp session from |
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
|
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
exit. The model may be text-only: the session then records |
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
|
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
|
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
With |
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
the probed served family and scores through that framing without |
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
|
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
|
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
never show the configured key or a header value. Or a vLLM session |
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
from |
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
|
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
client closes on exit, and judgment errors never show the configured |
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
key or a header value. For |
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
|
GemmaNativeVisionSession | VllmJudgmentSession | FakeJudgmentSession
|
from |
Raises:
| Type | Description |
|---|---|
ValueError
|
When a backend, vLLM or |
vllm_http_client
¶
vllm_http_client(
settings: VllmSettings, *, transport: BaseTransport | None = None
) -> Client
Build the shared vLLM HTTP client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
VllmSettings
|
vLLM connection settings. |
required |
transport
|
BaseTransport | None
|
Optional transport, for example |
None
|
Returns:
| Type | Description |
|---|---|
Client
|
A client with |
Client
|
gives its headers: the extra |
Client
|
|
Client
|
set, which by default gives |
Client
|
|
Client
|
|
Client
|
else changes, so environment proxy settings apply with or without a |
Client
|
key. The async client from |
Client
|
same headers and hooks. |
load_calibration_map
¶
load_calibration_map(path: str | PathLike[str], *, sha256: str) -> CalibrationMap
Read, verify and validate one calibration map file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
The map file. |
required |
sha256
|
str
|
The expected sha256 hex digest of the file bytes. |
required |
Returns:
| Type | Description |
|---|---|
CalibrationMap
|
The validated map, with |
Raises:
| Type | Description |
|---|---|
CalibrationDigestError
|
|
CalibrationMapError
|
The file is not UTF-8 JSON, or the document fails validation. |
OSError
|
The file cannot be read. |
run_sync
¶
Run an async coroutine synchronously and return its result.
Use this from plain scripts instead of adding *_sync methods on ports.
Pass a coroutine object (for example port.generate(request)), not the
unbound method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coro
|
Coroutine[Any, Any, T]
|
Coroutine to execute (typically from |
required |
Returns:
| Type | Description |
|---|---|
T
|
The coroutine result (for example a |
Raises:
| Type | Description |
|---|---|
TypeError
|
When |
RuntimeError
|
When called from a thread that already has a running loop. |
async_llama_cpp_adapter
¶
async_llama_cpp_adapter(
settings: LlamaSettings, *, transport: AsyncBaseTransport | None = None
) -> AsyncLlamaCppGenerationAdapter
Build an async llama.cpp adapter from composition-root settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
LlamaSettings
|
Values read from the environment or constructed in tests. |
required |
transport
|
AsyncBaseTransport | None
|
Optional async transport for the client, for example
|
None
|
Returns:
| Type | Description |
|---|---|
AsyncLlamaCppGenerationAdapter
|
An adapter on the |
AsyncLlamaCppGenerationAdapter
|
|
AsyncLlamaCppGenerationAdapter
|
extra headers, its errors are masked copies with no cause or context. |
Examples:
llama_cpp_adapter
¶
llama_cpp_adapter(
settings: LlamaSettings, *, transport: BaseTransport | None = None
) -> LlamaCppGenerationAdapter
Build a sync llama.cpp adapter from composition-root settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
LlamaSettings
|
Values read from the environment or constructed in tests. |
required |
transport
|
BaseTransport | None
|
Optional transport for the client, for example
|
None
|
Returns:
| Type | Description |
|---|---|
LlamaCppGenerationAdapter
|
An adapter on the |
LlamaCppGenerationAdapter
|
|
LlamaCppGenerationAdapter
|
extra headers, its errors are masked copies with no cause or context. |
Examples:
load_llama_settings
¶
load_llama_settings(environ: Mapping[str, str] | None = None) -> LlamaSettings
Read TYPEVET_LLAMA__* variables for the composition root.
Legacy single-segment names TYPEVET_LLAMA_URL and TYPEVET_GEMMA_MODEL
remain supported when the nested names are unset.
TYPEVET_LLAMA__MULTIMODAL_MODEL names the model id for the opt-in image
smoke and has no legacy alias. The key and header variables follow the
TYPEVET_VLLM__* rules (#410).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environ
|
Mapping[str, str] | None
|
Mapping to read. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
LlamaSettings
|
Frozen settings with defaults for missing keys. An empty |
LlamaSettings
|
|
LlamaSettings
|
|
LlamaSettings
|
|
LlamaSettings
|
|
LlamaSettings
|
|
LlamaSettings
|
An empty |
LlamaSettings
|
|
LlamaSettings
|
|
LlamaSettings
|
Case and outer spaces do not matter. |
Raises:
| Type | Description |
|---|---|
ValueError
|
When |