Define the LLM adapter interface in backend/engine/adapters/base.py with async methods complete(), list_models(), and test_connection(). The AdapterResponse dataclass holds response text, token counts, latency, model name, and raw metadata. Includes 11 tests covering instantiation guards, concrete subclass behavior, and dataclass semantics.
51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
"""Base adapter interface for LLM endpoints."""
|
|
|
|
from abc import ABC, abstractmethod
|
|
from dataclasses import dataclass, field
|
|
from typing import Any
|
|
|
|
|
|
@dataclass
|
|
class AdapterResponse:
|
|
"""Response from an LLM adapter call."""
|
|
|
|
text: str
|
|
tokens_in: int
|
|
tokens_out: int
|
|
latency_ms: float
|
|
model: str = ""
|
|
raw: dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
class BaseAdapter(ABC):
|
|
"""Abstract base class for LLM endpoint adapters.
|
|
|
|
All adapters must implement complete(), list_models(), and test_connection().
|
|
"""
|
|
|
|
@abstractmethod
|
|
async def complete(
|
|
self, prompt: str, model: str, params: dict[str, Any]
|
|
) -> AdapterResponse:
|
|
"""Send a completion request to the LLM endpoint.
|
|
|
|
Args:
|
|
prompt: The user prompt text.
|
|
model: Model identifier (e.g. "gpt-4", "llama3").
|
|
params: Additional parameters (temperature, max_tokens, etc.).
|
|
|
|
Returns:
|
|
AdapterResponse with the generated text and usage metadata.
|
|
"""
|
|
|
|
@abstractmethod
|
|
async def list_models(self) -> list[str]:
|
|
"""Return available model identifiers from the endpoint."""
|
|
|
|
@abstractmethod
|
|
async def test_connection(self) -> bool:
|
|
"""Test whether the endpoint is reachable and authenticated.
|
|
|
|
Returns:
|
|
True if connection is healthy, False otherwise.
|
|
"""
|