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. |
LlamaSettings |
type
|
llama.cpp connection settings for composition roots. |
load_llama_settings |
function
|
Read |
llama_cpp_adapter |
function
|
Build |
VllmSettings |
type
|
vLLM connection 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. |
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,
)
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
|
Bearer key, or |
max_concurrency |
int
|
Maximum POSTs in flight for one async adapter. |
user_agent |
str | None
|
|
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,
)
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. |
Examples:
from typevet.adapters.inbound.settings import LlamaSettings
LlamaSettings(base_url="http://127.0.0.1:8090", timeout=300.0)
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, headers 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 client. The llama.cpp branch ignores it. |
None
|
Returns:
| Type | Description |
|---|---|
LlamaCppGenerationAdapter | VllmGenerationAdapter
|
A llama.cpp adapter from |
LlamaCppGenerationAdapter | VllmGenerationAdapter
|
the |
LlamaCppGenerationAdapter | VllmGenerationAdapter
|
and its errors never show the configured key. |
Raises:
| Type | Description |
|---|---|
ValueError
|
When a backend or vLLM variable is invalid. |
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
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When |
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
|
client also sends |
Client
|
is set, the client sends it as |
Client
|
so environment proxy settings apply with or without a key. The async |
Client
|
client from |
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. |
llama_cpp_adapter
¶
llama_cpp_adapter(settings: LlamaSettings) -> 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 |
Returns:
| Type | Description |
|---|---|
LlamaCppGenerationAdapter
|
An adapter that does not read |
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.
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. |
Raises:
| Type | Description |
|---|---|
ValueError
|
When |