Spaces:
Sleeping
Sleeping
File size: 2,023 Bytes
c2b7a7b 6dfbf93 c2b7a7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | """Base interfaces for HTTP-based LLM providers."""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any, Dict, Iterable, List, Literal, Optional, Protocol, TypedDict
MessageRole = Literal["user", "assistant", "system"]
class ChatMessage(TypedDict, total=False):
"""Simple chat message structure."""
role: MessageRole
content: str
created_at: float
@dataclass
class ProviderConfig:
"""Configuration shared across providers."""
api_key: str
model: str
timeout_sec: int = 60
base_url: Optional[str] = None
max_tokens: Optional[int] = None
@dataclass
class ToolCall:
"""Represents a function/tool call from the LLM."""
name: str
arguments: Dict[str, Any]
id: Optional[str] = None
@dataclass
class LLMResponse:
"""LLM response payload returned to the caller."""
text: str
prompt_tokens: int
response_tokens: int
tool_calls: List[ToolCall] = field(default_factory=list)
finish_reason: Optional[str] = None
@property
def total_tokens(self) -> int:
return self.prompt_tokens + self.response_tokens
@property
def has_tool_calls(self) -> bool:
"""Check if the response contains tool calls."""
return len(self.tool_calls) > 0
class BaseLLM(Protocol):
"""Provider protocol."""
config: ProviderConfig
def __init__(self, config: ProviderConfig) -> None:
...
@classmethod
def describe(cls) -> dict:
"""Metadata used by callers/UI."""
...
def generate(self, messages: Iterable[ChatMessage]) -> LLMResponse:
"""Generate a completion from the conversation history."""
...
def format_messages(self, messages: Iterable[ChatMessage]) -> List[ChatMessage]:
"""Allow providers to tweak the message list before sending."""
return list(messages)
__all__ = [
"BaseLLM",
"ChatMessage",
"LLMResponse",
"MessageRole",
"ProviderConfig",
"ToolCall",
]
|