Add providers module (base, factory)
This commit is contained in:
62
src/promptforge/providers/base.py
Normal file
62
src/promptforge/providers/base.py
Normal 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
|
||||
Reference in New Issue
Block a user