Add providers module (base, factory)

This commit is contained in:
2026-02-04 12:31:32 +00:00
parent 41338d6513
commit 8a65a9e724

View File

@@ -0,0 +1,62 @@
from abc import ABC, abstractmethod
from typing import Any, AsyncIterator, Dict, Optional
class ProviderResponse:
def __init__(
self,
content: str,
model: str,
provider: str,
usage: Optional[Dict[str, Any]] = None,
latency_ms: float = 0.0,
metadata: Optional[Dict[str, Any]] = None,
):
self.content = content
self.model = model
self.provider = provider
self.usage = usage or {}
self.latency_ms = latency_ms
self.metadata = metadata or {}
class ProviderBase(ABC):
def __init__(
self,
api_key: Optional[str] = None,
model: str = "gpt-4",
temperature: float = 0.7,
**kwargs,
):
self.api_key = api_key
self.model = model
self.temperature = temperature
self.extra_kwargs = kwargs
@property
@abstractmethod
def name(self) -> str:
pass
@abstractmethod
async def complete(
self,
prompt: str,
system_prompt: Optional[str] = None,
max_tokens: Optional[int] = None,
**kwargs,
) -> ProviderResponse:
pass
@abstractmethod
async def stream_complete(
self,
prompt: str,
system_prompt: Optional[str] = None,
max_tokens: Optional[int] = None,
**kwargs,
) -> AsyncIterator[str]:
pass
def validate_api_key(self) -> bool:
return True